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: */