/usr/include/wreport/var.h is in libwreport-dev 2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | /*
* wreport/var - Store a value and its informations
*
* Copyright (C) 2005--2011 ARPA-SIM <urpsim@smr.arpa.emr.it>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Enrico Zini <enrico@enricozini.com>
*/
#ifndef WREPORT_VAR_H
#define WREPORT_VAR_H
/** @file
* @ingroup core
* Implement wreport::Var, an encapsulation of a measured variable.
*/
#include <wreport/error.h>
#include <wreport/varinfo.h>
#include <cstdio>
#include <string>
#include <memory>
struct lua_State;
namespace wreport {
/**
* Holds a wreport variable
*
* A wreport::Var contains:
* \li a wreport::Varcode identifying what is measured. See @ref vartable.h
* \li a measured value, that can be an integer, double or string depending on
* the wreport::Varcode
* \li zero or more attributes, in turn represented by wreport::Var structures
*/
class Var
{
protected:
/// Metadata about the variable
Varinfo m_info;
/// Value of the variable
char* m_value;
/// Attribute list (ordered by Varcode)
Var* m_attrs;
public:
/// Create a new Var, with undefined value
Var(Varinfo info);
/// Create a new Var, with integer value
Var(Varinfo info, int val);
/// Create a new Var, with double value
Var(Varinfo info, double val);
/// Create a new Var, with character value
Var(Varinfo info, const char* val);
/// Copy constructor
Var(const Var& var);
/// Copy constructor
Var(const Var& var, bool with_attrs);
/**
* Create a new Var with the value from another one
*
* Conversions are applied if necessary
*
* @param info
* The wreport::Varinfo describing the variable to create
* @param var
* The variable with the value to use
*/
Var(Varinfo info, const Var& var);
~Var();
/// Assignment
Var& operator=(const Var& var);
/// Equality
bool operator==(const Var& var) const;
/// Equality
bool operator!=(const Var& var) const { return !operator==(var); }
/// Retrieve the Varcode for a variable
Varcode code() const throw ();
/// Get informations about the variable
Varinfo info() const throw ();
/// Retrieve the internal string representation of the value for a variable.
const char* value() const throw ();
/// @returns true if the variable is defined, else false
bool isset() const throw ();
/// Get the value as an integer
int enqi() const;
/// Get the value as a double
double enqd() const;
/// Get the value as a string
const char* enqc() const;
/// Templated version of enq
template<typename T>
T enq() const
{
throw error_unimplemented("getting value of unsupported type");
}
/**
* Return the variable value, or the given default value if the variable is
* not set
*/
template<typename T>
T enq(T default_value) const
{
if (!isset()) return default_value;
return enq<T>();
}
/// Set the value from an integer value
void seti(int val);
/// Set the value from a double value
void setd(double val);
/// Set the value from a string value
void setc(const char* val);
/**
* Set the raw, binary value from a string value.
*
* This is similar to setc(), but it always copies as many bytes as the
* variable is long, including null bytes.
*/
void set_binary(const unsigned char* val);
/**
* Set the value from a string value, truncating \a val if it is too long
*
* If a value is truncated, the last character is set to '>' to mark the
* truncation.
*/
void setc_truncate(const char* val);
/// Set from a value formatted with the format() method
void set_from_formatted(const char* val);
/**
* Shortcuts (use with care, as the semanthics are slightly different
* depending on the type)
* @{
*/
void set(int val) { seti(val); }
void set(double val) { setd(val); }
void set(const char* val) { setc(val); }
void set(const std::string& val) { setc(val.c_str()); }
void set(const Var& var) { copy_val(var); }
/// @}
/// Unset the value
void unset();
/// Remove all attributes
void clear_attrs();
/**
* Query variable attributes
*
* @param code
* The wreport::Varcode of the attribute requested. See @ref vartable.h
* @returns attr
* A pointer to the attribute if it exists, else NULL. The pointer points to
* the internal representation and must not be deallocated by the caller.
*/
const Var* enqa(Varcode code) const;
/**
* Query variable attribute according to significance given in CODE TABLE
* 031021
*/
const Var* enqa_by_associated_field_significance(unsigned significance) const;
/**
* Set an attribute of the variable. An existing attribute with the same
* wreport::Varcode will be replaced.
*
* @param attr
* The attribute to add. It will be copied inside var, and memory management
* will still be in charge of the caller.
*/
void seta(const Var& attr);
/**
* Set an attribute of the variable. An existing attribute with the same
* wreport::Varcode will be replaced.
*
* @param attr
* The attribute to add. It will be used directly, and var will take care of
* its memory management.
*/
void seta(std::auto_ptr<Var> attr);
/// Remove the attribute with the given code
void unseta(Varcode code);
/**
* Get the next attribute in the attribute list
*
* Example attribute iteration:
*
* for (const Var* a = var.next_attr(); a != NULL; a = a->next_attr())
* // Do something with a
*/
const Var* next_attr() const;
/**
* Set the value from another variable, performing conversions if
* needed.
*
* The attributes of \a src will also be copied
*/
void copy_val(const Var& src);
/**
* Set the value from another variable, performing conversions if
* needed.
*
* The attributes of \a src will NOT be copied
*/
void copy_val_only(const Var& src);
/**
* Copy all the attributes from another variable
*
* @param src
* The variable with the attributes to copy.
*/
void copy_attrs(const Var& src);
/**
* Copy all the attributes from another variable, unless they are set to an
* undefined value
*
* @param src
* The variable with the attributes to copy.
*/
void copy_attrs_if_defined(const Var& src);
/**
* Create a formatted string representation of the variable value
*
* @param ifundef
* String to use if the variable is undefiend
*/
std::string format(const char* ifundef = "(undef)") const;
/**
* Print the variable to an output stream
*
* @param out
* The output stream to use for printing
*/
void print(FILE* out) const;
/**
* Print the variable to an output stream
*
* @param out
* The output stream to use for printing
*/
void print(std::ostream& out) const;
/**
* Print the variable to an output stream, without its attributes
*
* @param out
* The output stream to use for printing
*/
void print_without_attrs(FILE* out) const;
/**
* Print the variable to an output stream, without its attributes
*
* @param out
* The output stream to use for printing
*/
void print_without_attrs(std::ostream& out) const;
/**
* Compare two Var and return the number of differences.
*
* Details of the differences found will be formatted using the notes
* system (@see notes.h).
*
* @param var
* The variable to compare with this one
* @returns
* The number of differences found and reported
*/
unsigned diff(const Var& var) const;
/**
* Push the variable as an object in the lua stack
*/
void lua_push(struct lua_State* L);
/**
* Check that the element at \a idx is a Var
*
* @return the Var element, or NULL if the check failed
*/
static Var* lua_check(struct lua_State* L, int idx);
};
template<> inline int Var::enq() const { return enqi(); }
template<> inline float Var::enq() const { return (float)enqd(); }
template<> inline double Var::enq() const { return enqd(); }
template<> inline const char* Var::enq() const { return enqc(); }
template<> inline std::string Var::enq() const { return enqc(); }
}
#endif
/* vim:set ts=4 sw=4: */
|