/usr/include/wreport/var.h is in libwreport-dev 3.6-1build2.
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 | #ifndef WREPORT_VAR_H
#define WREPORT_VAR_H
#include <wreport/error.h>
#include <wreport/varinfo.h>
#include <cstdio>
#include <string>
#include <memory>
struct lua_State;
namespace wreport {
/**
* A physical variable
*
* A wreport::Var contains:
* \li a wreport::Varinfo describing the variable
* \li a value, that can be integer, floating point, string or opaque binary
* data as specified by the Varinfo
* \li zero or more attributes, represented by other wreport::Var objects
*/
class Var
{
protected:
/// Metadata about the variable
Varinfo m_info;
/// True if the variable is set, false otherwise
bool m_isset;
/**
* Value of the variable
*
* For numeric values, it is the value encoded to an integer decimal string
* according to m_info.
*
* For string values, it is the 0-terminated string.
*
* For binary values, it is a raw buffer where the first m_info->bit_len
* bits are the binary value, and the rest is set to 0.
*/
union {
int32_t i;
char* c;
} m_value;
/// Attribute list (ordered by Varcode)
Var* m_attrs;
/// Make sure that m_value is allocated. It does nothing if it already is.
void allocate();
/// Copy the value from var. var is assumed to have the same varinfo as us.
void copy_value(const Var& var);
/// Move the value from var. var is assumed to have the same varinfo as us. var is left unset.
void move_value(Var& var);
void assign_i_checked(int32_t val);
void assign_d_checked(double val);
void assign_b_checked(uint8_t* val, unsigned size);
void assign_c_checked(const char* val, unsigned size);
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);
/// Create a new Var, with character value
Var(Varinfo info, const std::string& val);
/**
* Create a new Var with the value from another one.
*
* Conversions are applied if necessary, attributes are not copied.
*
* @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);
/// Copy constructor
Var(const Var& var);
/**
* Move constructor.
*
* After movement, \a var will still a valid variable, but it will be unset
* and without attributes.
*/
Var(Var&& var);
~Var();
/// Assignment
Var& operator=(const Var& var);
/**
* Move assignment
*
* After movement, \a var will still a valid variable, but it will be unset
* and without attributes.
*/
Var& operator=(Var&& var);
bool operator==(const Var& var) const;
bool operator!=(const Var& var) const { return !operator==(var); }
/**
* Test if the values are the same, regardless of variable codes or
* attributes
*/
bool value_equals(const Var& var) const;
/// Retrieve the Varcode for a variable
Varcode code() const throw () { return m_info->code; }
/// Get informations about the variable
Varinfo info() const throw () { return m_info; }
/// @returns true if the variable is defined, else false
bool isset() const throw () { return m_isset; }
/// 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;
/// Get the value as a std::string
std::string enqs() 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 or opaque binary value
void setc(const char* val);
/// Set the value from a string or opaque binary value
void sets(const std::string& val);
/// Set from a value formatted with the format() method
void setf(const char* val);
/**
* Set the value from a string value, truncating it 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 the value from another variable, performing conversions if
* needed. The attributes of \a src will be ignored.
*/
void setval(const Var& src);
/**
* Replace all attributes in this variable with all the attributes from \a
* src
*/
void setattrs(const Var& src);
/**
* 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) { setval(var); setattrs(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;
/**
* 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. Its value will be moved inside the destination
* attribute, and attr will be unset.
*/
void seta(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::unique_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;
/**
* 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="") const;
/// Write the formatted value of this variable to an output stream
void format(FILE* out, const char* ifundef="") 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 char* end="\n") 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 enqs(); }
}
#endif
|