libwreport  2.4
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 
105  Varcode code() const throw ();
106 
108  Varinfo info() const throw ();
109 
111  const char* value() const throw ();
112 
114  bool isset() const throw ();
115 
117  int enqi() const;
118 
120  double enqd() const;
121 
123  const char* enqc() const;
124 
126  template<typename T>
127  T enq() const
128  {
129  throw error_unimplemented("getting value of unsupported type");
130  }
131 
136  template<typename T>
137  T enq(T default_value) const
138  {
139  if (!isset()) return default_value;
140  return enq<T>();
141  }
142 
144  void seti(int val);
145 
147  void setd(double val);
148 
150  void setc(const char* val);
151 
158  void set_binary(const unsigned char* val);
159 
166  void setc_truncate(const char* val);
167 
169  void set_from_formatted(const char* val);
170 
176  void set(int val) { seti(val); }
177  void set(double val) { setd(val); }
178  void set(const char* val) { setc(val); }
179  void set(const std::string& val) { setc(val.c_str()); }
180  void set(const Var& var) { copy_val(var); }
182 
184  void unset();
185 
187  void clear_attrs();
188 
198  const Var* enqa(Varcode code) const;
199 
204  const Var* enqa_by_associated_field_significance(unsigned significance) const;
205 
214  void seta(const Var& attr);
215 
224  void seta(std::auto_ptr<Var> attr);
225 
227  void unseta(Varcode code);
228 
237  const Var* next_attr() const;
238 
245  void copy_val(const Var& src);
246 
253  void copy_val_only(const Var& src);
254 
261  void copy_attrs(const Var& src);
262 
270  void copy_attrs_if_defined(const Var& src);
271 
278  std::string format(const char* ifundef = "(undef)") const;
279 
286  void print(FILE* out) const;
287 
294  void print(std::ostream& out) const;
295 
302  void print_without_attrs(FILE* out) const;
303 
310  void print_without_attrs(std::ostream& out) const;
311 
323  unsigned diff(const Var& var) const;
324 
325 
329  void lua_push(struct lua_State* L);
330 
336  static Var* lua_check(struct lua_State* L, int idx);
337 };
338 
339 template<> inline int Var::enq() const { return enqi(); }
340 template<> inline float Var::enq() const { return (float)enqd(); }
341 template<> inline double Var::enq() const { return enqd(); }
342 template<> inline const char* Var::enq() const { return enqc(); }
343 template<> inline std::string Var::enq() const { return enqc(); }
344 
345 
346 }
347 
348 #endif
349 /* vim:set ts=4 sw=4: */