libwreport  3.10
var.h
1 #ifndef WREPORT_VAR_H
2 #define WREPORT_VAR_H
3 
4 #include <wreport/error.h>
5 #include <wreport/varinfo.h>
6 #include <cstdio>
7 #include <string>
8 #include <memory>
9 
10 struct lua_State;
11 
12 namespace wreport {
13 
23 class Var
24 {
25 protected:
28 
30  bool m_isset;
31 
43  union {
44  int32_t i;
45  char* c;
46  } m_value;
47 
50 
52  void allocate();
53 
55  void copy_value(const Var& var);
57  void move_value(Var& var);
58  void assign_i_checked(int32_t val);
59  void assign_d_checked(double val);
60  void assign_b_checked(uint8_t* val, unsigned size);
61  void assign_c_checked(const char* val, unsigned size);
62 
63 public:
65  Var(Varinfo info);
66 
68  Var(Varinfo info, int val);
69 
71  Var(Varinfo info, double val);
72 
74  Var(Varinfo info, const char* val);
75 
77  Var(Varinfo info, const std::string& val);
78 
89  Var(Varinfo info, const Var& var);
90 
92  Var(const Var& var);
93 
100  Var(Var&& var);
101 
102  ~Var();
103 
105  Var& operator=(const Var& var);
106 
113  Var& operator=(Var&& var);
114 
115  bool operator==(const Var& var) const;
116  bool operator!=(const Var& var) const { return !operator==(var); }
117 
122  bool value_equals(const Var& var) const;
123 
125  Varcode code() const throw () { return m_info->code; }
126 
128  Varinfo info() const throw () { return m_info; }
129 
131  bool isset() const throw () { return m_isset; }
132 
133 
135  int enqi() const;
136 
138  double enqd() const;
139 
141  const char* enqc() const;
142 
144  std::string enqs() const;
145 
147  template<typename T>
148  T enq() const
149  {
150  throw error_unimplemented("getting value of unsupported type");
151  }
152 
157  template<typename T>
158  T enq(T default_value) const
159  {
160  if (!isset()) return default_value;
161  return enq<T>();
162  }
163 
165  void seti(int val);
166 
168  void setd(double val);
169 
171  void setc(const char* val);
172 
174  void sets(const std::string& val);
175 
177  void setf(const char* val);
178 
185  void setc_truncate(const char* val);
186 
191  void setval(const Var& src);
192 
197  void setattrs(const Var& src);
198 
204  void set(int val) { seti(val); }
205  void set(double val) { setd(val); }
206  void set(const char* val) { setc(val); }
207  void set(const std::string& val) { setc(val.c_str()); }
208  void set(const Var& var) { setval(var); setattrs(var); }
210 
212  void unset();
213 
215  void clear_attrs();
216 
226  const Var* enqa(Varcode code) const;
227 
236  void seta(const Var& attr);
237 
246  void seta(Var&& attr);
247 
256  void seta(std::unique_ptr<Var>&& attr);
257 
259  void unseta(Varcode code);
260 
269  const Var* next_attr() const;
270 
277  std::string format(const char* ifundef="") const;
278 
280  void format(FILE* out, const char* ifundef="") const;
281 
288  void print(FILE* out) const;
289 
296  void print(std::ostream& out) const;
297 
304  void print_without_attrs(FILE* out, const char* end="\n") const;
305 
312  void print_without_attrs(std::ostream& out) const;
313 
325  unsigned diff(const Var& var) const;
326 
327 
331  void lua_push(struct lua_State* L);
332 
338  static Var* lua_check(struct lua_State* L, int idx);
339 };
340 
341 template<> inline int Var::enq() const { return enqi(); }
342 template<> inline float Var::enq() const { return (float)enqd(); }
343 template<> inline double Var::enq() const { return enqd(); }
344 template<> inline const char* Var::enq() const { return enqc(); }
345 template<> inline std::string Var::enq() const { return enqs(); }
346 
347 }
348 #endif
double enqd() const
Get the value as a double.
bool m_isset
True if the variable is set, false otherwise.
Definition: var.h:30
void set(const char *val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type) ...
Definition: var.h:206
void set(const std::string &val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type) ...
Definition: var.h:207
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.
void set(int val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type) ...
Definition: var.h:204
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:23
void setc_truncate(const char *val)
Set the value from a string value, truncating it if it is too long.
void copy_value(const Var &var)
Copy the value from var. var is assumed to have the same varinfo as us.
int enqi() const
Get the value as an integer.
void unset()
Unset the value.
void set(const Var &var)
Shortcuts (use with care, as the semanthics are slightly different depending on the type) ...
Definition: var.h:208
T enq(T default_value) const
Return the variable value, or the given default value if the variable is not set. ...
Definition: var.h:158
Reports that a feature is still not implemented.
Definition: error.h:229
Varcode code
Variable code, as in WMO BUFR/CREX table B.
Definition: varinfo.h:138
void set(double val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type) ...
Definition: var.h:205
wreport exceptions.
void allocate()
Make sure that m_value is allocated. It does nothing if it already is.
bool value_equals(const Var &var) const
Test if the values are the same, regardless of variable codes or attributes.
Varinfo info() const
Get informations about the variable.
Definition: var.h:128
void seta(const Var &attr)
Set an attribute of the variable.
const char * enqc() const
Get the value as a string.
void print(FILE *out) const
Print the variable to an output stream.
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition: varinfo.h:57
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.
bool isset() const
Definition: var.h:131
Information about a variable.
Definition: varinfo.h:135
static Var * lua_check(struct lua_State *L, int idx)
Check that the element at idx is a Var.
unsigned diff(const Var &var) const
Compare two Var and return the number of differences.
T enq() const
Templated version of enq.
Definition: var.h:148
Varcode code() const
Retrieve the Varcode for a variable.
Definition: var.h:125
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 unseta(Varcode code)
Remove the attribute with the given code.
Var * m_attrs
Attribute list (ordered by Varcode)
Definition: var.h:49
Var & operator=(const Var &var)
Assignment.
const Var * next_attr() const
Get the next attribute in the attribute list.
void move_value(Var &var)
Move the value from var. var is assumed to have the same varinfo as us. var is left unset...
std::string enqs() const
Get the value as a std::string.
std::string format(const char *ifundef="") const
Create a formatted string representation of the variable value.
void print_without_attrs(FILE *out, const char *end="\n") const
Print the variable to an output stream, without its attributes.
Varinfo m_info
Metadata about the variable.
Definition: var.h:27
Var(Varinfo info)
Create a new Var, with undefined value.
const Var * enqa(Varcode code) const
Query variable attributes.