libwreport 3.38
var.h
1#ifndef WREPORT_VAR_H
2#define WREPORT_VAR_H
3
4#include <wreport/error.h>
5#include <wreport/fwd.h>
6#include <wreport/varinfo.h>
7#include <cstdio>
8#include <string>
9#include <memory>
10
11struct lua_State;
12
13namespace wreport {
14
24class Var
25{
26protected:
29
31 bool m_isset;
32
44 union {
45 int32_t i;
46 char* c;
48
51
53 void allocate();
54
56 void copy_value(const Var& var);
58 void move_value(Var& var);
59 void assign_i_checked(int32_t val);
60 void assign_d_checked(double val);
61 void assign_b_checked(const uint8_t* val, unsigned size);
62 [[deprecated("Use the version with const uint8_t*")]] void assign_b_checked(uint8_t* val, unsigned size);
63 void assign_c_checked(const char* val, unsigned size);
64
65public:
68
75 Var(Varinfo info, int val);
76
78 Var(Varinfo info, double val);
79
86 Var(Varinfo info, const char* val);
87
93 Var(Varinfo info, const std::string& val);
94
105 Var(Varinfo info, const Var& var);
106
108 Var(const Var& var);
109
116 Var(Var&& var);
117
118 ~Var();
119
121 Var& operator=(const Var& var);
122
130
131 bool operator==(const Var& var) const;
132 bool operator!=(const Var& var) const { return !operator==(var); }
133
138 bool value_equals(const Var& var) const;
139
141 Varcode code() const throw () { return m_info->code; }
142
144 Varinfo info() const throw () { return m_info; }
145
147 bool isset() const throw () { return m_isset; }
148
149
157 int enqi() const;
158
160 double enqd() const;
161
169 const char* enqc() const;
170
178 std::string enqs() const;
179
181 template<typename T>
182 T enq() const
183 {
184 throw error_unimplemented("getting value of unsupported type");
185 }
186
191 template<typename T>
192 T enq(T default_value) const
193 {
194 if (!isset()) 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) { setval(var); setattrs(var); }
267
269 void unset();
270
273
283 const Var* enqa(Varcode code) const;
284
293 void seta(const Var& attr);
294
303 void seta(Var&& attr);
304
313 void seta(std::unique_ptr<Var>&& attr);
314
317
326 const Var* next_attr() const;
327
337 std::string format(const char* ifundef="") const;
338
340 void format(FILE* out, const char* ifundef="") const;
341
348 void print(FILE* out) const;
349
356 void print(std::ostream& out) const;
357
364 void print_without_attrs(FILE* out, const char* end="\n") const;
365
372 void print_without_attrs(std::ostream& out) const;
373
385 unsigned diff(const Var& var) const;
386
387
391 void lua_push(struct lua_State* L);
392
397 void lua_push(struct lua_State* L) const;
398
404 static Var* lua_check(struct lua_State* L, int idx);
405
411 static const Var* lua_const_check(struct lua_State* L, int idx);
412};
413
414template<> inline int Var::enq() const { return enqi(); }
415template<> inline float Var::enq() const { return static_cast<float>(enqd()); }
416template<> inline double Var::enq() const { return enqd(); }
417template<> inline const char* Var::enq() const { return enqc(); }
418template<> inline std::string Var::enq() const { return enqs(); }
419
420}
421#endif
A physical variable.
Definition var.h:25
unsigned diff(const Var &var) const
Compare two Var and return the number of differences.
void set(const std::string &val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type)
Definition var.h:264
void setc_truncate(const char *val)
Set the value from a string value, truncating it if it is too long.
void setd(double val)
Set the value from a double value.
double enqd() const
Get the value as a double.
void unseta(Varcode code)
Remove the attribute with the given code.
Varinfo m_info
Metadata about the variable.
Definition var.h:28
void seta(std::unique_ptr< Var > &&attr)
Set an attribute of the variable.
void lua_push(struct lua_State *L)
Push the variable as an object in the lua stack.
void seti(int val)
Set the value from an integer value.
T enq() const
Templated version of enq.
Definition var.h:182
void print_without_attrs(FILE *out, const char *end="\n") const
Print the variable to an output stream, without its attributes.
bool value_equals(const Var &var) const
Test if the values are the same, regardless of variable codes or attributes.
std::string format(const char *ifundef="") const
Create a formatted string representation of the variable value.
int enqi() const
Get the value as an integer.
Var & operator=(Var &&var)
Move assignment.
Var * m_attrs
Attribute list (ordered by Varcode)
Definition var.h:50
const char * enqc() const
Get the value as a string.
void print(std::ostream &out) const
Print the variable to an output stream.
void sets(const std::string &val)
Set the value from a string or opaque binary value.
Varinfo info() const
Get informations about the variable.
Definition var.h:144
bool isset() const
Definition var.h:147
Var(const Var &var)
Copy constructor.
Var(Var &&var)
Move constructor.
void seta(const Var &attr)
Set an attribute of the variable.
void clear_attrs()
Remove all attributes.
void setc(const char *val)
Set the value from a string or opaque binary value.
void move_value(Var &var)
Move the value from var. var is assumed to have the same varinfo as us. var is left unset.
void unset()
Unset the value.
void set(const char *val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type)
Definition var.h:263
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.
static Var * lua_check(struct lua_State *L, int idx)
Check that the element at idx is a Var.
Var(Varinfo info, double val)
Create a new Var, with double value.
void allocate()
Make sure that m_value is allocated. It does nothing if it already is.
bool m_isset
True if the variable is set, false otherwise.
Definition var.h:31
void set(int val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type)
Definition var.h:261
void print_without_attrs(std::ostream &out) const
Print the variable to an output stream, without its attributes.
Var(Varinfo info, const char *val)
Create a new Var, with character value.
Var(Varinfo info, const std::string &val)
Create a new Var, with character value.
Var(Varinfo info, const Var &var)
Create a new Var with the value from another one.
void set(double val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type)
Definition var.h:262
void seta(Var &&attr)
Set an attribute of the variable.
T enq(T default_value) const
Return the variable value, or the given default value if the variable is not set.
Definition var.h:192
Var & operator=(const Var &var)
Assignment.
void format(FILE *out, const char *ifundef="") const
Write the formatted value of this variable to an output stream.
static const Var * lua_const_check(struct lua_State *L, int idx)
Check that the element at idx is a Var.
void lua_push(struct lua_State *L) const
Push the variable as an object in the lua stack, with only read-only methods.
void copy_value(const Var &var)
Copy the value from var. var is assumed to have the same varinfo as us.
Varcode code() const
Retrieve the Varcode for a variable.
Definition var.h:141
const Var * enqa(Varcode code) const
Query variable attributes.
Var(Varinfo info, int val)
Create a new Var, with integer value.
void setf(const char *val)
Set from a value formatted with the format() method.
union wreport::Var::@0 m_value
Value of the variable.
void print(FILE *out) const
Print the variable to an output stream.
void set(const Var &var)
Shortcuts (use with care, as the semanthics are slightly different depending on the type)
Definition var.h:265
const Var * next_attr() const
Get the next attribute in the attribute list.
Var(Varinfo info)
Create a new Var, with undefined value.
std::string enqs() const
Get the value as a std::string.
Reports that a feature is still not implemented.
Definition error.h:241
wreport exceptions.
String functions.
Definition benchmark.h:13
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition fwd.h:12
Information about a variable.
Definition varinfo.h:139
Varcode code
Variable code, as in WMO BUFR/CREX table B.
Definition varinfo.h:141
Implement fast access to information about WMO variables.