libwreport  3.29
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 
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;
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(uint8_t* val, unsigned size);
62  void assign_c_checked(const char* val, unsigned size);
63 
64 public:
67 
69  Var(Varinfo info, int val);
70 
72  Var(Varinfo info, double val);
73 
75  Var(Varinfo info, const char* val);
76 
78  Var(Varinfo info, const std::string& val);
79 
90  Var(Varinfo info, const Var& var);
91 
93  Var(const Var& var);
94 
101  Var(Var&& var);
102 
103  ~Var();
104 
106  Var& operator=(const Var& var);
107 
114  Var& operator=(Var&& var);
115 
116  bool operator==(const Var& var) const;
117  bool operator!=(const Var& var) const { return !operator==(var); }
118 
123  bool value_equals(const Var& var) const;
124 
126  Varcode code() const throw () { return m_info->code; }
127 
129  Varinfo info() const throw () { return m_info; }
130 
132  bool isset() const throw () { return m_isset; }
133 
134 
136  int enqi() const;
137 
139  double enqd() const;
140 
142  const char* enqc() const;
143 
145  std::string enqs() const;
146 
148  template<typename T>
149  T enq() const
150  {
151  throw error_unimplemented("getting value of unsupported type");
152  }
153 
158  template<typename T>
159  T enq(T default_value) const
160  {
161  if (!isset()) return default_value;
162  return enq<T>();
163  }
164 
166  void seti(int val);
167 
169  void setd(double val);
170 
172  void setc(const char* val);
173 
175  void sets(const std::string& val);
176 
178  void setf(const char* val);
179 
186  void setc_truncate(const char* val);
187 
192  void setval(const Var& src);
193 
198  void setattrs(const Var& src);
199 
205  void set(int val) { seti(val); }
206  void set(double val) { setd(val); }
207  void set(const char* val) { setc(val); }
208  void set(const std::string& val) { setc(val.c_str()); }
209  void set(const Var& var) { setval(var); setattrs(var); }
211 
213  void unset();
214 
216  void clear_attrs();
217 
227  const Var* enqa(Varcode code) const;
228 
237  void seta(const Var& attr);
238 
247  void seta(Var&& attr);
248 
257  void seta(std::unique_ptr<Var>&& attr);
258 
261 
270  const Var* next_attr() const;
271 
278  std::string format(const char* ifundef="") const;
279 
281  void format(FILE* out, const char* ifundef="") const;
282 
289  void print(FILE* out) const;
290 
297  void print(std::ostream& out) const;
298 
305  void print_without_attrs(FILE* out, const char* end="\n") const;
306 
313  void print_without_attrs(std::ostream& out) const;
314 
326  unsigned diff(const Var& var) const;
327 
328 
332  void lua_push(struct lua_State* L);
333 
338  void lua_push(struct lua_State* L) const;
339 
345  static Var* lua_check(struct lua_State* L, int idx);
346 
352  static const Var* lua_const_check(struct lua_State* L, int idx);
353 };
354 
355 template<> inline int Var::enq() const { return enqi(); }
356 template<> inline float Var::enq() const { return (float)enqd(); }
357 template<> inline double Var::enq() const { return enqd(); }
358 template<> inline const char* Var::enq() const { return enqc(); }
359 template<> inline std::string Var::enq() const { return enqs(); }
360 
361 }
362 #endif
wreport::Var::lua_check
static Var * lua_check(struct lua_State *L, int idx)
Check that the element at idx is a Var.
wreport::Var::set
void set(const std::string &val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type)
Definition: var.h:208
wreport::Var::enqd
double enqd() const
Get the value as a double.
wreport::Var::set
void set(int val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type)
Definition: var.h:205
wreport::Var::m_attrs
Var * m_attrs
Attribute list (ordered by Varcode)
Definition: var.h:50
wreport::Var::enqc
const char * enqc() const
Get the value as a string.
wreport::Var::seta
void seta(Var &&attr)
Set an attribute of the variable.
wreport::Var::enqa
const Var * enqa(Varcode code) const
Query variable attributes.
wreport::Var::enqs
std::string enqs() const
Get the value as a std::string.
wreport::Var::m_info
Varinfo m_info
Metadata about the variable.
Definition: var.h:28
wreport::Var::print
void print(std::ostream &out) const
Print the variable to an output stream.
wreport::Var::Var
Var(Varinfo info)
Create a new Var, with undefined value.
wreport::Var::copy_value
void copy_value(const Var &var)
Copy the value from var. var is assumed to have the same varinfo as us.
wreport::Var::lua_push
void lua_push(struct lua_State *L)
Push the variable as an object in the lua stack.
wreport::Var::lua_const_check
static const Var * lua_const_check(struct lua_State *L, int idx)
Check that the element at idx is a Var.
wreport::Var::print_without_attrs
void print_without_attrs(FILE *out, const char *end="\n") const
Print the variable to an output stream, without its attributes.
wreport::Var::move_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.
wreport::Var::diff
unsigned diff(const Var &var) const
Compare two Var and return the number of differences.
wreport::Var::next_attr
const Var * next_attr() const
Get the next attribute in the attribute list.
wreport::Var::format
std::string format(const char *ifundef="") const
Create a formatted string representation of the variable value.
wreport::Var::allocate
void allocate()
Make sure that m_value is allocated. It does nothing if it already is.
wreport::Var::unset
void unset()
Unset the value.
wreport::Var::set
void set(const Var &var)
Shortcuts (use with care, as the semanthics are slightly different depending on the type)
Definition: var.h:209
wreport::Var::Var
Var(Varinfo info, double val)
Create a new Var, with double value.
wreport::_Varinfo
Information about a variable.
Definition: varinfo.h:137
wreport::Var::clear_attrs
void clear_attrs()
Remove all attributes.
wreport::Var::unseta
void unseta(Varcode code)
Remove the attribute with the given code.
wreport::Var::m_value
union wreport::Var::@0 m_value
Value of the variable.
wreport::Varcode
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition: fwd.h:12
wreport::Var::operator=
Var & operator=(Var &&var)
Move assignment.
wreport::Var::Var
Var(const Var &var)
Copy constructor.
wreport::error_unimplemented
Reports that a feature is still not implemented.
Definition: error.h:241
wreport::Var::Var
Var(Varinfo info, int val)
Create a new Var, with integer value.
wreport::Var::enq
T enq() const
Templated version of enq.
Definition: var.h:149
wreport::Var::Var
Var(Var &&var)
Move constructor.
wreport::Var::seta
void seta(const Var &attr)
Set an attribute of the variable.
wreport::Var::sets
void sets(const std::string &val)
Set the value from a string or opaque binary value.
error.h
wreport exceptions.
wreport::Var::set
void set(const char *val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type)
Definition: var.h:207
wreport::Var::setc
void setc(const char *val)
Set the value from a string or opaque binary value.
wreport::Var::print_without_attrs
void print_without_attrs(std::ostream &out) const
Print the variable to an output stream, without its attributes.
wreport::Var::set
void set(double val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type)
Definition: var.h:206
wreport::Var::operator=
Var & operator=(const Var &var)
Assignment.
wreport::_Varinfo::code
Varcode code
Variable code, as in WMO BUFR/CREX table B.
Definition: varinfo.h:139
wreport::Var::m_isset
bool m_isset
True if the variable is set, false otherwise.
Definition: var.h:31
wreport::Var
A physical variable.
Definition: var.h:25
wreport::Var::setd
void setd(double val)
Set the value from a double value.
wreport::Var::enq
T enq(T default_value) const
Return the variable value, or the given default value if the variable is not set.
Definition: var.h:159
wreport::Var::setattrs
void setattrs(const Var &src)
Replace all attributes in this variable with all the attributes from src.
wreport::Var::seti
void seti(int val)
Set the value from an integer value.
wreport::Var::format
void format(FILE *out, const char *ifundef="") const
Write the formatted value of this variable to an output stream.
wreport::Var::Var
Var(Varinfo info, const Var &var)
Create a new Var with the value from another one.
wreport::Var::setc_truncate
void setc_truncate(const char *val)
Set the value from a string value, truncating it if it is too long.
wreport::Var::seta
void seta(std::unique_ptr< Var > &&attr)
Set an attribute of the variable.
wreport::Var::value_equals
bool value_equals(const Var &var) const
Test if the values are the same, regardless of variable codes or attributes.
wreport::Var::Var
Var(Varinfo info, const char *val)
Create a new Var, with character value.
wreport::Var::info
Varinfo info() const
Get informations about the variable.
Definition: var.h:129
wreport::Var::print
void print(FILE *out) const
Print the variable to an output stream.
wreport::Var::setval
void setval(const Var &src)
Set the value from another variable, performing conversions if needed.
wreport::Var::enqi
int enqi() const
Get the value as an integer.
wreport::Var::lua_push
void lua_push(struct lua_State *L) const
Push the variable as an object in the lua stack, with only read-only methods.
wreport::Var::setf
void setf(const char *val)
Set from a value formatted with the format() method.
varinfo.h
Implement fast access to information about WMO variables.
wreport::Var::code
Varcode code() const
Retrieve the Varcode for a variable.
Definition: var.h:126
wreport::Var::isset
bool isset() const
Definition: var.h:132
wreport
String functions.
Definition: benchmark.h:13
wreport::Var::Var
Var(Varinfo info, const std::string &val)
Create a new Var, with character value.