var.h
#ifndef WREPORT_VAR_H
#define WREPORT_VAR_H
#include <wreport/error.h>
#include <wreport/fwd.h>
#include <wreport/varinfo.h>
#include <cstdio>
#include <string>
#include <memory>
struct lua_State;
namespace wreport {
class Var
{
protected:
Varinfo m_info;
bool m_isset;
union {
int32_t i;
char* c;
} m_value;
Var* m_attrs;
void allocate();
void copy_value(const Var& var);
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:
Var(Varinfo info);
Var(Varinfo info, int val);
Var(Varinfo info, double val);
Var(Varinfo info, const char* val);
Var(Varinfo info, const std::string& val);
Var(Varinfo info, const Var& var);
Var(const Var& var);
Var(Var&& var);
~Var();
Var& operator=(const Var& var);
Var& operator=(Var&& var);
bool operator==(const Var& var) const;
bool operator!=(const Var& var) const { return !operator==(var); }
bool value_equals(const Var& var) const;
Varcode code() const throw () { return m_info->code; }
Varinfo info() const throw () { return m_info; }
bool isset() const throw () { return m_isset; }
int enqi() const;
double enqd() const;
const char* enqc() const;
std::string enqs() const;
template<typename T>
T enq() const
{
throw error_unimplemented("getting value of unsupported type");
}
template<typename T>
T enq(T default_value) const
{
if (!isset()) return default_value;
return enq<T>();
}
void seti(int val);
void setd(double val);
void setc(const char* val);
void sets(const std::string& val);
void setf(const char* val);
void setc_truncate(const char* val);
void setval(const Var& src);
void setattrs(const Var& src);
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); }
void unset();
void clear_attrs();
const Var* enqa(Varcode code) const;
void seta(const Var& attr);
void seta(Var&& attr);
void seta(std::unique_ptr<Var>&& attr);
void unseta(Varcode code);
const Var* next_attr() const;
std::string format(const char* ifundef="") const;
void format(FILE* out, const char* ifundef="") const;
void print(FILE* out) const;
void print(std::ostream& out) const;
void print_without_attrs(FILE* out, const char* end="\n") const;
void print_without_attrs(std::ostream& out) const;
unsigned diff(const Var& var) const;
void lua_push(struct lua_State* L);
void lua_push(struct lua_State* L) const;
static Var* lua_check(struct lua_State* L, int idx);
static const Var* lua_const_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
wreport::Var::diff
unsigned diff(const Var &var) const
Compare two Var and return the number of differences.
wreport::Var::set
void set(const std::string &val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type).
Definition var.h:263
wreport::Var::setc_truncate
void setc_truncate(const char *val)
Set the value from a string value, truncating it if it is too long.
wreport::Var::setd
void setd(double val)
Set the value from a double value.
wreport::Var::enqd
double enqd() const
Get the value as a double.
wreport::Var::unseta
void unseta(Varcode code)
Remove the attribute with the given code.
wreport::Var::m_info
Varinfo m_info
Metadata about the variable.
Definition var.h:28
wreport::Var::seta
void seta(std::unique_ptr< Var > &&attr)
Set an attribute of the variable.
wreport::Var::lua_push
void lua_push(struct lua_State *L)
Push the variable as an object in the lua stack.
wreport::Var::seti
void seti(int val)
Set the value from an integer value.
wreport::Var::enq
T enq() const
Templated version of enq.
Definition var.h:181
wreport::Var::print_without_attrs
void print_without_attrs(FILE *out, const char *end="\n") const
Print the variable to an output stream, without its attributes.
wreport::Var::value_equals
bool value_equals(const Var &var) const
Test if the values are the same, regardless of variable codes or attributes.
wreport::Var::format
std::string format(const char *ifundef="") const
Create a formatted string representation of the variable value.
wreport::Var::enqi
int enqi() const
Get the value as an integer.
wreport::Var::operator=
Var & operator=(Var &&var)
Move assignment.
wreport::Var::m_attrs
Var * m_attrs
Attribute list (ordered by Varcode).
Definition var.h:50
wreport::Var::enqc
const char * enqc() const
Get the value as a string.
wreport::Var::m_value
union wreport::Var::@025340023251102023200317330123174016247226104271 m_value
Value of the variable.
wreport::Var::print
void print(std::ostream &out) const
Print the variable to an output stream.
wreport::Var::sets
void sets(const std::string &val)
Set the value from a string or opaque binary value.
wreport::Var::info
Varinfo info() const
Get informations about the variable.
Definition var.h:143
wreport::Var::isset
bool isset() const
Definition var.h:146
wreport::Var::Var
Var(const Var &var)
Copy constructor.
wreport::Var::Var
Var(Var &&var)
Move constructor.
wreport::Var::seta
void seta(const Var &attr)
Set an attribute of the variable.
wreport::Var::clear_attrs
void clear_attrs()
Remove all attributes.
wreport::Var::setc
void setc(const char *val)
Set the value from a string or opaque binary value.
wreport::Var::move_value
void move_value(Var &var)
Move the value from var. var is assumed to have the same varinfo as us. var is left unset.
wreport::Var::unset
void unset()
Unset the value.
wreport::Var::set
void set(const char *val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type).
Definition var.h:262
wreport::Var::setval
void setval(const Var &src)
Set the value from another variable, performing conversions if needed.
wreport::Var::setattrs
void setattrs(const Var &src)
Replace all attributes in this variable with all the attributes from src.
wreport::Var::lua_check
static Var * lua_check(struct lua_State *L, int idx)
Check that the element at idx is a Var.
wreport::Var::Var
Var(Varinfo info, double val)
Create a new Var, with double value.
wreport::Var::allocate
void allocate()
Make sure that m_value is allocated. It does nothing if it already is.
wreport::Var::m_isset
bool m_isset
True if the variable is set, false otherwise.
Definition var.h:31
wreport::Var::set
void set(int val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type).
Definition var.h:260
wreport::Var::print_without_attrs
void print_without_attrs(std::ostream &out) const
Print the variable to an output stream, without its attributes.
wreport::Var::Var
Var(Varinfo info, const char *val)
Create a new Var, with character value.
wreport::Var::Var
Var(Varinfo info, const std::string &val)
Create a new Var, with character value.
wreport::Var::Var
Var(Varinfo info, const Var &var)
Create a new Var with the value from another one.
wreport::Var::set
void set(double val)
Shortcuts (use with care, as the semanthics are slightly different depending on the type).
Definition var.h:261
wreport::Var::seta
void seta(Var &&attr)
Set an attribute of the variable.
wreport::Var::enq
T enq(T default_value) const
Return the variable value, or the given default value if the variable is not set.
Definition var.h:191
wreport::Var::operator=
Var & operator=(const Var &var)
Assignment.
wreport::Var::format
void format(FILE *out, const char *ifundef="") const
Write the formatted value of this variable to an output stream.
wreport::Var::lua_const_check
static const Var * lua_const_check(struct lua_State *L, int idx)
Check that the element at idx is a Var.
wreport::Var::lua_push
void lua_push(struct lua_State *L) const
Push the variable as an object in the lua stack, with only read-only methods.
wreport::Var::copy_value
void copy_value(const Var &var)
Copy the value from var. var is assumed to have the same varinfo as us.
wreport::Var::code
Varcode code() const
Retrieve the Varcode for a variable.
Definition var.h:140
wreport::Var::enqa
const Var * enqa(Varcode code) const
Query variable attributes.
wreport::Var::Var
Var(Varinfo info, int val)
Create a new Var, with integer value.
wreport::Var::setf
void setf(const char *val)
Set from a value formatted with the format() method.
wreport::Var::print
void print(FILE *out) const
Print the variable to an output stream.
wreport::Var::set
void set(const Var &var)
Shortcuts (use with care, as the semanthics are slightly different depending on the type).
Definition var.h:264
wreport::Var::next_attr
const Var * next_attr() const
Get the next attribute in the attribute list.
wreport::Var::Var
Var(Varinfo info)
Create a new Var, with undefined value.
wreport::Var::enqs
std::string enqs() const
Get the value as a std::string.
wreport::error_unimplemented
Reports that a feature is still not implemented.
Definition error.h:241
error.h
wreport exceptions.
wreport
String functions.
Definition benchmark.h:13
wreport::Varinfo
const _Varinfo * Varinfo
Varinfo reference.
Definition fwd.h:11
wreport::Varcode
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition fwd.h:12
varinfo.h
Implement fast access to information about WMO variables.