libwreport  2.9
var.h
Go to the documentation of this file.
1 /*
2  * wreport/var - Store a value and its informations
3  *
4  * Copyright (C) 2005--2011 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef WREPORT_VAR_H
23 #define WREPORT_VAR_H
24 
31 #include <wreport/error.h>
32 #include <wreport/varinfo.h>
33 #include <cstdio>
34 #include <string>
35 #include <memory>
36 
37 struct lua_State;
38 
39 namespace wreport {
40 
50 class Var
51 {
52 protected:
55 
57  char* m_value;
58 
61 
62 public:
64  Var(Varinfo info);
65 
67  Var(Varinfo info, int val);
68 
70  Var(Varinfo info, double val);
71 
73  Var(Varinfo info, const char* val);
74 
76  Var(const Var& var);
77 
79  Var(const Var& var, bool with_attrs);
80 
91  Var(Varinfo info, const Var& var);
92 
93  ~Var();
94 
96  Var& operator=(const Var& var);
97 
99  bool operator==(const Var& var) const;
100 
102  bool operator!=(const Var& var) const { return !operator==(var); }
103 
108  bool value_equals(const Var& var) const;
109 
111  Varcode code() const throw ();
112 
114  Varinfo info() const throw ();
115 
117  const char* value() const throw ();
118 
120  bool isset() const throw ();
121 
123  int enqi() const;
124 
126  double enqd() const;
127 
129  const char* enqc() const;
130 
132  template<typename T>
133  T enq() const
134  {
135  throw error_unimplemented("getting value of unsupported type");
136  }
137 
142  template<typename T>
143  T enq(T default_value) const
144  {
145  if (!isset()) return default_value;
146  return enq<T>();
147  }
148 
150  void seti(int val);
151 
153  void setd(double val);
154 
156  void setc(const char* val);
157 
164  void set_binary(const unsigned char* val);
165 
172  void setc_truncate(const char* val);
173 
175  void set_from_formatted(const char* val);
176 
182  void set(int val) { seti(val); }
183  void set(double val) { setd(val); }
184  void set(const char* val) { setc(val); }
185  void set(const std::string& val) { setc(val.c_str()); }
186  void set(const Var& var) { copy_val(var); }
188 
190  void unset();
191 
193  void clear_attrs();
194 
204  const Var* enqa(Varcode code) const;
205 
210  const Var* enqa_by_associated_field_significance(unsigned significance) const;
211 
220  void seta(const Var& attr);
221 
230  void seta(std::auto_ptr<Var> attr);
231 
233  void unseta(Varcode code);
234 
243  const Var* next_attr() const;
244 
251  void copy_val(const Var& src);
252 
259  void copy_val_only(const Var& src);
260 
267  void copy_attrs(const Var& src);
268 
276  void copy_attrs_if_defined(const Var& src);
277 
284  std::string format(const char* ifundef = "(undef)") const;
285 
292  void print(FILE* out) const;
293 
300  void print(std::ostream& out) const;
301 
308  void print_without_attrs(FILE* out) const;
309 
316  void print_without_attrs(std::ostream& out) const;
317 
329  unsigned diff(const Var& var) const;
330 
331 
335  void lua_push(struct lua_State* L);
336 
342  static Var* lua_check(struct lua_State* L, int idx);
343 };
344 
345 template<> inline int Var::enq() const { return enqi(); }
346 template<> inline float Var::enq() const { return (float)enqd(); }
347 template<> inline double Var::enq() const { return enqd(); }
348 template<> inline const char* Var::enq() const { return enqc(); }
349 template<> inline std::string Var::enq() const { return enqc(); }
350 
351 
352 }
353 
354 #endif
355 /* vim:set ts=4 sw=4: */
double enqd() const
Get the value as a double.
void copy_val_only(const Var &src)
Set the value from another variable, performing conversions if needed.
void set_from_formatted(const char *val)
Set from a value formatted with the format() method.
void set(const char *val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type) ...
Definition: var.h:184
void copy_attrs_if_defined(const Var &src)
Copy all the attributes from another variable, unless they are set to an undefined value...
void set(const std::string &val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type) ...
Definition: var.h:185
void set_binary(const unsigned char *val)
Set the raw, binary value from a string value.
std::string format(const char *ifundef="(undef)") const
Create a formatted string representation of the variable value.
void set(int val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type) ...
Definition: var.h:182
void lua_push(struct lua_State *L)
Push the variable as an object in the lua stack.
void copy_attrs(const Var &src)
Copy all the attributes from another variable.
Holds a wreport variable.
Definition: var.h:50
void setc_truncate(const char *val)
Set the value from a string value, truncating val if it is too long.
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:186
T enq(T default_value) const
Return the variable value, or the given default value if the variable is not set. ...
Definition: var.h:143
Reports that a feature is still not implemented.
Definition: error.h:293
void set(double val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type) ...
Definition: var.h:183
wreport exceptions
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.
void seta(const Var &attr)
Set an attribute of the variable.
bool operator!=(const Var &var) const
Equality.
Definition: var.h:102
const char * enqc() const
Get the value as a string.
void print(FILE *out) const
Print the variable to an output stream.
void print_without_attrs(FILE *out) const
Print the variable to an output stream, without its attributes.
void setc(const char *val)
Set the value from a string value.
void seti(int val)
Set the value from an integer value.
bool isset() const
const Var * enqa_by_associated_field_significance(unsigned significance) const
Query variable attribute according to significance given in CODE TABLE 031021.
short unsigned int Varcode
Holds the WMO variable code of a variable.
Definition: varinfo.h:78
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:133
Varcode code() const
Retrieve the Varcode for a variable.
Implement fast access to information about WMO variables.
void clear_attrs()
Remove all attributes.
Smart pointer to handle/use varinfos.
Definition: varinfo.h:336
const char * value() const
Retrieve the internal string representation of the value for a variable.
void setd(double val)
Set the value from a double value.
void unseta(Varcode code)
Remove the attribute with the given code.
Var * m_attrs
Attribute list (ordered by Varcode)
Definition: var.h:60
Var & operator=(const Var &var)
Assignment.
const Var * next_attr() const
Get the next attribute in the attribute list.
bool operator==(const Var &var) const
Equality.
Varinfo m_info
Metadata about the variable.
Definition: var.h:54
char * m_value
Value of the variable.
Definition: var.h:57
Var(Varinfo info)
Create a new Var, with undefined value.
const Var * enqa(Varcode code) const
Query variable attributes.
void copy_val(const Var &src)
Set the value from another variable, performing conversions if needed.