libwreport  3.29
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
wreport::Var Class Reference

A physical variable. More...

#include <var.h>

Public Member Functions

 Var (Varinfo info)
 Create a new Var, with undefined value.
 
 Var (Varinfo info, int val)
 Create a new Var, with integer value.
 
 Var (Varinfo info, double val)
 Create a new Var, with double 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 character value.
 
 Var (Varinfo info, const Var &var)
 Create a new Var with the value from another one. More...
 
 Var (const Var &var)
 Copy constructor.
 
 Var (Var &&var)
 Move constructor. More...
 
Varoperator= (const Var &var)
 Assignment.
 
Varoperator= (Var &&var)
 Move assignment. More...
 
bool operator== (const Var &var) const
 
bool operator!= (const Var &var) const
 
bool value_equals (const Var &var) const
 Test if the values are the same, regardless of variable codes or attributes.
 
Varcode code () const throw ()
 Retrieve the Varcode for a variable.
 
Varinfo info () const throw ()
 Get informations about the variable.
 
bool isset () const throw ()
 
int enqi () const
 Get the value as an integer.
 
double enqd () const
 Get the value as a double.
 
const char * enqc () const
 Get the value as a string.
 
std::string enqs () const
 Get the value as a std::string.
 
template<typename T >
enq () const
 Templated version of enq.
 
template<typename T >
enq (T default_value) const
 Return the variable value, or the given default value if the variable is not set.
 
void seti (int val)
 Set the value from an integer value.
 
void setd (double val)
 Set the value from a double value.
 
void setc (const char *val)
 Set the value from a string or opaque binary value.
 
void sets (const std::string &val)
 Set the value from a string or opaque binary value.
 
void setf (const char *val)
 Set from a value formatted with the format() method.
 
void setc_truncate (const char *val)
 Set the value from a string value, truncating it if it is too long. More...
 
void setval (const Var &src)
 Set the value from another variable, performing conversions if needed. More...
 
void setattrs (const Var &src)
 Replace all attributes in this variable with all the attributes from src.
 
void unset ()
 Unset the value.
 
void clear_attrs ()
 Remove all attributes.
 
const Varenqa (Varcode code) const
 Query variable attributes. More...
 
void seta (const Var &attr)
 Set an attribute of the variable. More...
 
void seta (Var &&attr)
 Set an attribute of the variable. More...
 
void seta (std::unique_ptr< Var > &&attr)
 Set an attribute of the variable. More...
 
void unseta (Varcode code)
 Remove the attribute with the given code.
 
const Varnext_attr () const
 Get the next attribute in the attribute list. More...
 
std::string format (const char *ifundef="") const
 Create a formatted string representation of the variable value. More...
 
void format (FILE *out, const char *ifundef="") const
 Write the formatted value of this variable to an output stream.
 
void print (FILE *out) const
 Print the variable to an output stream. More...
 
void print (std::ostream &out) const
 Print the variable to an output stream. More...
 
void print_without_attrs (FILE *out, const char *end="\n") const
 Print the variable to an output stream, without its attributes. More...
 
void print_without_attrs (std::ostream &out) const
 Print the variable to an output stream, without its attributes. More...
 
unsigned diff (const Var &var) const
 Compare two Var and return the number of differences. More...
 
void lua_push (struct lua_State *L)
 Push the variable as an object in the lua stack.
 
void lua_push (struct lua_State *L) const
 Push the variable as an object in the lua stack, with only read-only methods.
 
template<>
int enq () const
 
void set (int val)
 Shortcuts (use with care, as the semanthics are slightly different depending on the type)
 
void set (double val)
 Shortcuts (use with care, as the semanthics are slightly different depending on the type)
 
void set (const char *val)
 Shortcuts (use with care, as the semanthics are slightly different depending on the type)
 
void set (const std::string &val)
 Shortcuts (use with care, as the semanthics are slightly different depending on the type)
 
void set (const Var &var)
 Shortcuts (use with care, as the semanthics are slightly different depending on the type)
 

Static Public Member Functions

static Varlua_check (struct lua_State *L, int idx)
 Check that the element at idx is a Var. More...
 
static const Varlua_const_check (struct lua_State *L, int idx)
 Check that the element at idx is a Var. More...
 

Protected Member Functions

void allocate ()
 Make sure that m_value is allocated. It does nothing if it already is.
 
void copy_value (const Var &var)
 Copy the value from var. var is assumed to have the same varinfo as us.
 
void move_value (Var &var)
 Move the value from var. var is assumed to have the same varinfo as us. var is left unset.
 
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)
 

Protected Attributes

Varinfo m_info
 Metadata about the variable.
 
bool m_isset
 True if the variable is set, false otherwise.
 
union {
   int32_t   i
 
   char *   c
 
m_value
 Value of the variable. More...
 
Varm_attrs
 Attribute list (ordered by Varcode)
 

Detailed Description

A physical variable.

A wreport::Var contains:

Constructor & Destructor Documentation

◆ Var() [1/2]

wreport::Var::Var ( Varinfo  info,
const Var var 
)

Create a new Var with the value from another one.

Conversions are applied if necessary, attributes are not copied.

Parameters
infoThe wreport::Varinfo describing the variable to create
varThe variable with the value to use

◆ Var() [2/2]

wreport::Var::Var ( Var &&  var)

Move constructor.

After movement, var will still a valid variable, but it will be unset and without attributes.

Member Function Documentation

◆ diff()

unsigned wreport::Var::diff ( const Var var) const

Compare two Var and return the number of differences.

Details of the differences found will be formatted using the notes system (

See also
notes.h).
Parameters
varThe variable to compare with this one
Returns
The number of differences found and reported

◆ enqa()

const Var* wreport::Var::enqa ( Varcode  code) const

Query variable attributes.

Parameters
codeThe wreport::Varcode of the attribute requested. See 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.

◆ format()

std::string wreport::Var::format ( const char *  ifundef = "") const

Create a formatted string representation of the variable value.

Parameters
ifundefString to use if the variable is undefiend

◆ isset()

bool wreport::Var::isset ( ) const
throw (
)
inline
Returns
true if the variable is defined, else false

References m_isset.

Referenced by enq().

◆ lua_check()

static Var* wreport::Var::lua_check ( struct lua_State *  L,
int  idx 
)
static

Check that the element at idx is a Var.

Returns
the Var element, or NULL if the check failed

◆ lua_const_check()

static const Var* wreport::Var::lua_const_check ( struct lua_State *  L,
int  idx 
)
static

Check that the element at idx is a Var.

Returns
the Var element, or NULL if the check failed

◆ next_attr()

const Var* wreport::Var::next_attr ( ) const

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

◆ operator=()

Var& wreport::Var::operator= ( Var &&  var)

Move assignment.

After movement, var will still a valid variable, but it will be unset and without attributes.

◆ print() [1/2]

void wreport::Var::print ( FILE *  out) const

Print the variable to an output stream.

Parameters
outThe output stream to use for printing

◆ print() [2/2]

void wreport::Var::print ( std::ostream &  out) const

Print the variable to an output stream.

Parameters
outThe output stream to use for printing

◆ print_without_attrs() [1/2]

void wreport::Var::print_without_attrs ( FILE *  out,
const char *  end = "\n" 
) const

Print the variable to an output stream, without its attributes.

Parameters
outThe output stream to use for printing

◆ print_without_attrs() [2/2]

void wreport::Var::print_without_attrs ( std::ostream &  out) const

Print the variable to an output stream, without its attributes.

Parameters
outThe output stream to use for printing

◆ seta() [1/3]

void wreport::Var::seta ( const Var attr)

Set an attribute of the variable.

An existing attribute with the same wreport::Varcode will be replaced.

Parameters
attrThe attribute to add. It will be copied inside var, and memory management will still be in charge of the caller.

◆ seta() [2/3]

void wreport::Var::seta ( std::unique_ptr< Var > &&  attr)

Set an attribute of the variable.

An existing attribute with the same wreport::Varcode will be replaced.

Parameters
attrThe attribute to add. It will be used directly, and var will take care of its memory management.

◆ seta() [3/3]

void wreport::Var::seta ( Var &&  attr)

Set an attribute of the variable.

An existing attribute with the same wreport::Varcode will be replaced.

Parameters
attrThe attribute to add. Its value will be moved inside the destination attribute, and attr will be unset.

◆ setc_truncate()

void wreport::Var::setc_truncate ( 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.

◆ setval()

void wreport::Var::setval ( const Var src)

Set the value from another variable, performing conversions if needed.

The attributes of src will be ignored.

Referenced by set().

Field Documentation

◆ m_value

union { ... } wreport::Var::m_value

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.


The documentation for this class was generated from the following file: