libwreport  3.40
var.h
1 #ifndef WREPORT_VAR_H
2 #define WREPORT_VAR_H
3 
4 #include <cstdio>
5 #include <memory>
6 #include <string>
7 #include <wreport/error.h>
8 #include <wreport/fwd.h>
9 #include <wreport/varinfo.h>
10 
11 struct lua_State;
12 
13 namespace wreport {
14 
24 class Var
25 {
26 protected:
29 
31  bool m_isset;
32 
44  union {
45  int32_t i;
46  char* c;
47  } m_value;
48 
51 
53  void allocate();
54 
56  void copy_value(const Var& var);
59  void move_value(Var& var);
60  void assign_i_checked(int32_t val);
61  void assign_d_checked(double val);
62  void assign_b_checked(const uint8_t* val, unsigned size);
63  [[deprecated("Use the version with const uint8_t*")]] void
64  assign_b_checked(uint8_t* val, unsigned size);
65  void assign_c_checked(const char* val, unsigned size);
66 
67 public:
69  Var(Varinfo info);
70 
77  Var(Varinfo info, int val);
78 
80  Var(Varinfo info, double val);
81 
88  Var(Varinfo info, const char* val);
89 
95  Var(Varinfo info, const std::string& val);
96 
107  Var(Varinfo info, const Var& var);
108 
110  Var(const Var& var);
111 
118  Var(Var&& var);
119 
120  ~Var();
121 
123  Var& operator=(const Var& var);
124 
131  Var& operator=(Var&& var);
132 
133  bool operator==(const Var& var) const;
134  bool operator!=(const Var& var) const { return !operator==(var); }
135 
140  bool value_equals(const Var& var) const;
141 
143  Varcode code() const throw() { return m_info->code; }
144 
146  Varinfo info() const throw() { return m_info; }
147 
149  bool isset() const throw() { return m_isset; }
150 
158  int enqi() const;
159 
161  double enqd() const;
162 
170  const char* enqc() const;
171 
179  std::string enqs() const;
180 
182  template <typename T> T enq() const
183  {
184  throw error_unimplemented("getting value of unsupported type");
185  }
186 
191  template <typename T> T enq(T default_value) const
192  {
193  if (!isset())
194  return default_value;
195  return enq<T>();
196  }
197 
205  void seti(int val);
206 
208  void setd(double val);
209 
217  void setc(const char* val);
218 
226  void sets(const std::string& val);
227 
234  void setf(const char* val);
235 
242  void setc_truncate(const char* val);
243 
248  void setval(const Var& src);
249 
254  void setattrs(const Var& src);
255 
261  void set(int val) { seti(val); }
262  void set(double val) { setd(val); }
263  void set(const char* val) { setc(val); }
264  void set(const std::string& val) { setc(val.c_str()); }
265  void set(const Var& var)
266  {
267  setval(var);
268  setattrs(var);
269  }
271 
273  void unset();
274 
276  void clear_attrs();
277 
287  const Var* enqa(Varcode code) const;
288 
297  void seta(const Var& attr);
298 
307  void seta(Var&& attr);
308 
317  void seta(std::unique_ptr<Var>&& attr);
318 
320  void unseta(Varcode code);
321 
330  const Var* next_attr() const;
331 
341  std::string format(const char* ifundef = "") const;
342 
344  void format(FILE* out, const char* ifundef = "") const;
345 
352  void print(FILE* out) const;
353 
360  void print(std::ostream& out) const;
361 
368  void print_without_attrs(FILE* out, const char* end = "\n") const;
369 
376  void print_without_attrs(std::ostream& out) const;
377 
389  unsigned diff(const Var& var) const;
390 
394  void lua_push(struct lua_State* L);
395 
400  void lua_push(struct lua_State* L) const;
401 
407  static Var* lua_check(struct lua_State* L, int idx);
408 
414  static const Var* lua_const_check(struct lua_State* L, int idx);
415 };
416 
417 template <> inline int Var::enq() const { return enqi(); }
418 template <> inline float Var::enq() const { return static_cast<float>(enqd()); }
419 template <> inline double Var::enq() const { return enqd(); }
420 template <> inline const char* Var::enq() const { return enqc(); }
421 template <> inline std::string Var::enq() const { return enqs(); }
422 
423 } // namespace wreport
424 #endif
void print_without_attrs(FILE *out, const char *end="\) const
Print the variable to an output stream, without its attributes.
bool m_isset
True if the variable is set, false otherwise.
Definition: var.h:31
unsigned diff(const Var &var) const
Compare two Var and return the number of differences.
std::string enqs() const
Get the value as a std::string.
bool isset() const
Definition: var.h:149
void setval(const Var &src)
Set the value from another variable, performing conversions if needed.
void setattrs(const Var &src)
Replace all attributes in this variable with all the attributes from src.
T enq() const
Templated version of enq.
Definition: var.h:182
void sets(const std::string &val)
Set the value from a string or opaque binary value.
union wreport::Var::@0 m_value
Value of the variable.
void lua_push(struct lua_State *L)
Push the variable as an object in the lua stack.
A physical variable.
Definition: var.h:24
void setc_truncate(const char *val)
Set the value from a string value, truncating it if it is too long.
const Var * next_attr() const
Get the next attribute in the attribute list.
void copy_value(const Var &var)
Copy the value from var. var is assumed to have the same varinfo as us.
Reports that a feature is still not implemented.
Definition: error.h:241
void unset()
Unset the value.
Varcode code
Variable code, as in WMO BUFR/CREX table B.
Definition: varinfo.h:142
wreport exceptions.
void allocate()
Make sure that m_value is allocated. It does nothing if it already is.
Varcode code() const
Retrieve the Varcode for a variable.
Definition: var.h:143
bool value_equals(const Var &var) const
Test if the values are the same, regardless of variable codes or attributes.
void seta(const Var &attr)
Set an attribute of the variable.
Varinfo info() const
Get informations about the variable.
Definition: var.h:146
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition: fwd.h:12
void setc(const char *val)
Set the value from a string or opaque binary value.
void seti(int val)
Set the value from an integer value.
Information about a variable.
Definition: varinfo.h:139
const Var * enqa(Varcode code) const
Query variable attributes.
T enq(T default_value) const
Return the variable value, or the given default value if the variable is not set. ...
Definition: var.h:191
static Var * lua_check(struct lua_State *L, int idx)
Check that the element at idx is a Var.
const char * enqc() const
Get the value as a string.
String functions.
Definition: benchmark.h:13
Implement fast access to information about WMO variables.
void clear_attrs()
Remove all attributes.
void setd(double val)
Set the value from a double value.
void setf(const char *val)
Set from a value formatted with the format() method.
void print(FILE *out) const
Print the variable to an output stream.
int enqi() const
Get the value as an integer.
void unseta(Varcode code)
Remove the attribute with the given code.
Var * m_attrs
Attribute list (ordered by Varcode)
Definition: var.h:50
Var & operator=(const Var &var)
Assignment.
static const Var * lua_const_check(struct lua_State *L, int idx)
Check that the element at idx is a Var.
void move_value(Var &var)
Move the value from var.
std::string format(const char *ifundef="") const
Create a formatted string representation of the variable value.
double enqd() const
Get the value as a double.
Varinfo m_info
Metadata about the variable.
Definition: var.h:28
Var(Varinfo info)
Create a new Var, with undefined value.