libwreport 2.1
|
00001 /* 00002 * wreport/var - Store a value and its informations 00003 * 00004 * Copyright (C) 2005--2011 ARPA-SIM <urpsim@smr.arpa.emr.it> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 * 00019 * Author: Enrico Zini <enrico@enricozini.com> 00020 */ 00021 00022 #ifndef WREPORT_VAR_H 00023 #define WREPORT_VAR_H 00024 00031 #include <wreport/error.h> 00032 #include <wreport/varinfo.h> 00033 #include <cstdio> 00034 #include <string> 00035 #include <memory> 00036 00037 struct lua_State; 00038 00039 namespace wreport { 00040 00050 class Var 00051 { 00052 protected: 00054 Varinfo m_info; 00055 00057 char* m_value; 00058 00060 Var* m_attrs; 00061 00062 public: 00064 Var(Varinfo info); 00065 00067 Var(Varinfo info, int val); 00068 00070 Var(Varinfo info, double val); 00071 00073 Var(Varinfo info, const char* val); 00074 00076 Var(const Var& var); 00077 00079 Var(const Var& var, bool with_attrs); 00080 00091 Var(Varinfo info, const Var& var); 00092 00093 ~Var(); 00094 00096 Var& operator=(const Var& var); 00097 00099 bool operator==(const Var& var) const; 00100 00102 bool operator!=(const Var& var) const { return !operator==(var); } 00103 00105 Varcode code() const throw (); 00106 00108 Varinfo info() const throw (); 00109 00111 const char* value() const throw (); 00112 00114 bool isset() const throw (); 00115 00117 int enqi() const; 00118 00120 double enqd() const; 00121 00123 const char* enqc() const; 00124 00126 template<typename T> 00127 T enq() const 00128 { 00129 throw error_unimplemented("getting value of unsupported type"); 00130 } 00131 00136 template<typename T> 00137 T enq(T default_value) const 00138 { 00139 if (!isset()) return default_value; 00140 return enq<T>(); 00141 } 00142 00144 void seti(int val); 00145 00147 void setd(double val); 00148 00150 void setc(const char* val); 00151 00158 void set_binary(const unsigned char* val); 00159 00166 void setc_truncate(const char* val); 00167 00169 void set_from_formatted(const char* val); 00170 00176 void set(int val) { seti(val); } 00177 void set(double val) { setd(val); } 00178 void set(const char* val) { setc(val); } 00179 void set(const std::string& val) { setc(val.c_str()); } 00180 void set(const Var& var) { copy_val(var); } 00182 00184 void unset(); 00185 00187 void clear_attrs(); 00188 00198 const Var* enqa(Varcode code) const; 00199 00204 const Var* enqa_by_associated_field_significance(unsigned significance) const; 00205 00214 void seta(const Var& attr); 00215 00224 void seta(std::auto_ptr<Var> attr); 00225 00227 void unseta(Varcode code); 00228 00237 const Var* next_attr() const; 00238 00245 void copy_val(const Var& src); 00246 00253 void copy_val_only(const Var& src); 00254 00261 void copy_attrs(const Var& src); 00262 00270 void copy_attrs_if_defined(const Var& src); 00271 00278 std::string format(const char* ifundef = "(undef)") const; 00279 00286 void print(FILE* out) const; 00287 00294 void print(std::ostream& out) const; 00295 00302 void print_without_attrs(FILE* out) const; 00303 00310 void print_without_attrs(std::ostream& out) const; 00311 00323 unsigned diff(const Var& var) const; 00324 00325 00329 void lua_push(struct lua_State* L); 00330 00336 static Var* lua_check(struct lua_State* L, int idx); 00337 }; 00338 00339 template<> inline int Var::enq() const { return enqi(); } 00340 template<> inline float Var::enq() const { return (float)enqd(); } 00341 template<> inline double Var::enq() const { return enqd(); } 00342 template<> inline const char* Var::enq() const { return enqc(); } 00343 template<> inline std::string Var::enq() const { return enqc(); } 00344 00345 00346 } 00347 00348 #endif 00349 /* vim:set ts=4 sw=4: */