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.
| Var (const Var &var)
| Copy constructor.
| Var (Var &&var)
| Move constructor.
Var & | operator= (const Var &var)
| Assignment.
Var & | operator= (Var &&var)
| Move assignment.
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>
T | enq () const
| Templated version of enq.
template<typename T>
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.
void | setval (const Var &src)
| Set the value from another variable, performing conversions if needed.
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 Var * | enqa (Varcode code) const
| Query variable attributes.
void | seta (const Var &attr)
| Set an attribute of the variable.
void | seta (Var &&attr)
| Set an attribute of the variable.
void | seta (std::unique_ptr< Var > &&attr)
| Set an attribute of the variable.
void | unseta (Varcode code)
| Remove the attribute with the given code.
const Var * | next_attr () const
| Get the next attribute in the attribute list.
std::string | format (const char *ifundef="") const
| Create a formatted string representation of the variable value.
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.
void | print (std::ostream &out) const
| Print the variable to an output stream.
void | print_without_attrs (FILE *out, const char *end="\n") const
| Print the variable to an output stream, without its attributes.
void | print_without_attrs (std::ostream &out) const
| Print the variable to an output stream, without its attributes.
unsigned | diff (const Var &var) const
| Compare two Var and return the number of differences.
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
template<>
float | enq () const
template<>
double | enq () const
template<>
const char * | enq () const
template<>
std::string | 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 Var * | lua_check (struct lua_State *L, int idx)
| Check that the element at idx is a Var.
static const Var * | lua_const_check (struct lua_State *L, int idx)
| Check that the element at idx is a Var.

## 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.
Var * | m_attrs
| Attribute list (ordered by Varcode).

## Detailed Description

A physical variable.

A wreport::Var contains:

- a wreport::Varinfo describing the variable
- a value, that can be integer, floating point, string or opaque binary data as specified by the Varinfo
- zero or more attributes, represented by other wreport::Var objects

## Constructor & Destructor Documentation

## Var() [1/5]

wreport::Var::Var | ( | Varinfo | info,
|  | int | val )

Create a new Var, with integer value.

The value is taken as the unscaled raw version. See :func:seti for details.

References info().

## Var() [2/5]

wreport::Var::Var | ( | Varinfo | info,
|  | const char * | val )

Create a new Var, with character value.

The value is taken as the unscaled raw version. See :func:setc for details.

References info().

## Var() [3/5]

wreport::Var::Var | ( | Varinfo | info,
|  | const std::string & | val )

Create a new Var, with character value.

The value is taken as the unscaled raw version. See :func:setc for details.

References info().

## Var() [4/5]

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

  info | The wreport::Varinfo describing the variable to create
  var | The variable with the value to use

References info(), and Var().

## Var() [5/5]

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

Move constructor.

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

References Var().

## 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

  var | The variable to compare with this one

Returns
  The number of differences found and reported

References Var().

## enqa()

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

Query variable attributes.

Parameters

  code | The 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.

References code(), and Var().

## enqc()

const char * wreport::Var::enqc | ( |  | ) | const

Get the value as a string.

If the variable is a scaled decimal value, it is returned unscaled. This can be used to access the exact underlying representation without hitting limits from the floating point representation

## enqi()

int wreport::Var::enqi | ( |  | ) | const

Get the value as an integer.

If the variable is a scaled decimal value, it is returned unscaled. This can be used to access the exact underlying representation without hitting limits from the floating point representation

## enqs()

std::string wreport::Var::enqs | ( |  | ) | const

Get the value as a std::string.

If the variable is a scaled decimal value, it is returned unscaled. This can be used to access the exact underlying representation without hitting limits from the floating point representation

## format()

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

Create a formatted string representation of the variable value.

If the variable is a scaled decimal value, it is formatted as a scaled decimal value with the right amount of digits.

Parameters

  ifundef | String 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()

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

References Var().

## lua_const_check()

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

References Var().

## 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

References Var().

## 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.

References Var().

## print() [1/2]

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

Print the variable to an output stream.

Parameters

  out | The 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

  out | The 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

  out | The 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

  out | The 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

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

References Var().

## 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

  attr | The 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

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

References Var().

## setc()

void wreport::Var::setc | ( | const char * | val | ) |

Set the value from a string or opaque binary value.

If the variable is a scaled decimal value, it sets its unscaled version, making it possible, together with :func:enqc, to work with the exact underlying representation of values.

Referenced by set(), and set().

## 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.

## setf()

void wreport::Var::setf | ( | const char * | val | ) |

Set from a value formatted with the format() method.

This will assume the input value is represented as scaled to its actual physical value

## seti()

void wreport::Var::seti | ( | int | val | ) |

Set the value from an integer value.

If the variable is a scaled decimal value, it sets its unscaled version, making it possible, together with :func:enqi, to work with the exact underlying representation of values.

Referenced by set().

## sets()

void wreport::Var::sets | ( | const std::string & | val | ) |

Set the value from a string or opaque binary value.

If the variable is a scaled decimal value, it sets its unscaled version, making it possible, together with :func:enqs, to work with the exact underlying representation of values.

## 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.

References Var().

Referenced by set().

## Field Documentation

## [union]

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:

- var.h