cValue Class Reference¶

API: cValue Class Reference
API
cValue Class Reference

Public Types

enum  Type

Public Member Functions

Constructors
void operator= (const cValue &other)
Setter functions. Note that overloaded assignment operators also exist.
void set (bool b)
void set (intval_t l, const char *unit=nullptr)
void set (int l, const char *unit=nullptr)
void set (double d, const char *unit=nullptr)
void setPreservingUnit (intval_t l)
void setPreservingUnit (double d)
void setUnit (const char *unit)
void convertTo (const char *unit)
void convertToDouble ()
void set (const char *s)
void set (const std::string &s)
void set (const opp_string &s)
void set (any_ptr ptr)
void set (cObject *obj)
void set (const void *)=delete
Getter functions. Note that overloaded conversion operators also exist.
bool boolValue () const
intval_t intValue () const
intval_t intValueRaw () const
intval_t intValueInUnit (const char *targetUnit) const
double doubleValue () const
double doubleValueRaw () const
double doubleValueInUnit (const char *targetUnit) const
const char * getUnit () const
const char * stringValue () const
const std::string & stdstringValue () const
any_ptr pointerValue () const
bool isNullptr () const
bool containsObject () const
bool containsXML () const
cObjectobjectValue () const
cXMLElementxmlValue () const
Overloaded assignment and conversion operators.
cValueoperator= (bool b)
cValueoperator= (char c)
cValueoperator= (unsigned char c)
cValueoperator= (short i)
cValueoperator= (unsigned short i)
cValueoperator= (int i)
cValueoperator= (unsigned int i)
cValueoperator= (long l)
cValueoperator= (unsigned long l)
cValueoperator= (long long l)
cValueoperator= (unsigned long long l)
cValueoperator= (double d)
cValueoperator= (long double d)
cValueoperator= (const char *s)
cValueoperator= (const std::string &s)
cValueoperator= (any_ptr ptr)
cValueoperator= (cObject *obj)
 operator bool () const
 operator char () const
 operator unsigned char () const
 operator int () const
 operator unsigned int () const
 operator short () const
 operator unsigned short () const
 operator long () const
 operator unsigned long () const
 operator long long () const
 operator unsigned long long () const
 operator double () const
 operator long double () const
 operator const char * () const
 operator std::string () const
 operator any_ptr () const
 operator cObject * () const
 operator cXMLElement * () const

Type, unit conversion and misc.

static const char * getTypeName (Type t)
static double convertUnit (double d, const char *unit, const char *targetUnit)
static double parseQuantity (const char *str, const char *expectedUnit=nullptr)
static double parseQuantity (const char *str, std::string &outActualUnit)
static const char * getBestUnit (double d, const char *unit)
static std::string formatQuantity (double d, const char *unit, int maxSignificantDigits=6, const char *groupSeparator="'")
static std::string formatQuantityInBestUnit (double d, const char *unit, int maxSignificantDigits=6, const char *groupSeparator="'")
Type getType () const
const char * getTypeName () const
bool isNumeric () const
bool isSet () const
std::string str () const
bool operator== (const cValue &other)

Description

A variant-like value class used during evaluating NED expressions.

See notes below.

Pointer values

With type==POINTER, cValue only stored the pointer, and does nothing extra on top of that. If the pointer points to an object, the object's ownership is unaffected, and the object is never deleted or cloned by cValue.

See also
cDynamicExpression, cNedFunction, Define_NED_Function()

Member Enumeration Documentation

◆ Type

enum Type

Type of the value stored in a cValue object.

Member Function Documentation

◆ operator=() [1/18]

void operator= ( const cValue & other)

Assignment

◆ getType()

Type getType ( ) const
inline

Returns the value type.

◆ getTypeName() [1/2]

const char * getTypeName ( ) const
inline

Returns the value type as string.

References getTypeName().

Referenced by getTypeName().

◆ getTypeName() [2/2]

const char * getTypeName ( Type t)
static

Returns the given type as a string.

◆ isNumeric()

bool isNumeric ( ) const
inline

Returns true if the stored value is of a numeric type.

◆ isSet()

bool isSet ( ) const
inline

Returns true if the value is not empty, i.e. type is not UNDEF.

◆ str()

std::string str ( ) const

Returns the value in text form.

Referenced by parseQuantity(), and parseQuantity().

◆ operator==()

bool operator== ( const cValue & other)

Comparison. Note that two cValues are equal if they are of the same type (i.e. there is no implicit int <--> double conversion); if they contain object pointers, they are equal if they point to the same object.

◆ convertUnit()

double convertUnit ( double d,
const char * unit,
const char * targetUnit )
static

Convert the given number into the target unit (e.g. milliwatt to watt). Throws an exception if conversion is not possible (unknown/unrelated units).

See also
convertTo(), doubleValueInUnit(), setUnit()

◆ parseQuantity() [1/2]

double parseQuantity ( const char * str,
const char * expectedUnit = nullptr )
static

Invokes parseQuantity(), and converts the result into the given unit. If conversion is not possible (unrelated or unknown units), and error is thrown.

References str().

◆ parseQuantity() [2/2]

double parseQuantity ( const char * str,
std::string & outActualUnit )
static

Converts a quantity given as string to a double, and returns it, together with the unit it was given in. If there are several numbers and units (see syntax), everything is converted into the last unit.

Syntax: <number> | (<number> <unit>)+

If there is a syntax error, or if unit mismatch is found (i.e. distance is given instead of time), the method throws an exception.

References str().

◆ getBestUnit()

const char * getBestUnit ( double d,
const char * unit )
static

Returns the best unit for human consumption for the given quantity.

◆ formatQuantity()

std::string formatQuantity ( double d,
const char * unit,
int maxSignificantDigits = 6,
const char * groupSeparator = "'" )
static

Formats the given quantity as a human-readable string. The output uses automatically selected regular or scientific notation, automatic unit conversion to the most readable unit, and digit grouping (e.g. thousands separator). The optional maxSignificantDigits argument limits the number of significant digits shown; values that are rounded are prefixed with a tilde (~). The default (15) shows the full double precision without approximation. The groupSeparator argument controls the thousands separator character(s); use "" to disable grouping.

◆ formatQuantityInBestUnit()

std::string formatQuantityInBestUnit ( double d,
const char * unit,
int maxSignificantDigits = 6,
const char * groupSeparator = "'" )
static

Formats the given quantity as a human-readable string, after converting it to the best unit for human consumption. Equivalent to calling getBestUnit(), convertUnit(), and formatQuantity() in sequence. The groupSeparator argument controls the thousands separator character(s); use "" to disable grouping.

◆ set() [1/10]

◆ set() [2/10]

void set ( intval_t l,
const char * unit = nullptr )
inline

Sets the value to the given integer value and measurement unit (optional). The unit string pointer is expected to stay valid during the entire duration of the simulation (see related class comment).

◆ set() [3/10]

void set ( int l,
const char * unit = nullptr )
inline

Sets the value to the given integer value and measurement unit (optional). The unit string pointer is expected to stay valid during the entire duration of the simulation (see related class comment). This is a convenience method that delegates to the intval_t version.

References set().

Referenced by set().

◆ set() [4/10]

void set ( double d,
const char * unit = nullptr )
inline

Sets the value to the given double value and measurement unit (optional). The unit string pointer is expected to stay valid during the entire duration of the simulation (see related class comment).

◆ setPreservingUnit() [1/2]

void setPreservingUnit ( intval_t l)
inline

Sets the value to the given integer value, preserving the current measurement unit. The object must already have the INT type.

◆ setPreservingUnit() [2/2]

void setPreservingUnit ( double d)
inline

Sets the value to the given double value, preserving the current measurement unit. The object must already have the DOUBLE type.

◆ setUnit()

void setUnit ( const char * unit)

Sets the measurement unit to the given value, leaving the numeric part of the quantity unchanged. The object must already have the DOUBLE type. The unit string pointer is expected to stay valid during the entire duration of the simulation (see related class comment).

◆ convertTo()

void convertTo ( const char * unit)

Permanently converts this value to the given unit. The value must already have the type DOUBLE. If the current unit cannot be converted to the given one, an error will be thrown. The unit string pointer is expected to stay valid during the entire simulation (see related class comment).

See also
doubleValueInUnit()

◆ convertToDouble()

void convertToDouble ( )

If this value is of type INT, converts it into DOUBLE; has no effect if it is already a DOUBLE; and throws an error otherwise.

◆ set() [5/10]

void set ( const char * s)
inline

Sets the value to the given string value. The string itself will be copied. nullptr is also accepted and treated as an empty string.

◆ set() [6/10]

void set ( const std::string & s)
inline

Sets the value to the given string value.

◆ set() [7/10]

void set ( const opp_string & s)
inline

Sets the value to the given string value.

References opp_string::c_str().

◆ set() [8/10]

void set ( any_ptr ptr)
inline

Sets the value to the given pointer. The pointer is treated by as an opaque value: If it points to an object, the object's ownership is unaffected, and cValue will never delete or clone the object.

◆ set() [9/10]

void set ( cObject * obj)
inline

Sets the value to the given object, via set(any_ptr). Note that cValue solely stores the object's pointer, and does nothing extra. The object's ownership is unaffected, and cValue will never delete or clone the object.

References set().

Referenced by set().

◆ set() [10/10]

void set ( const void * )
delete

Deleted function to prevent non-cObject pointers from silently being converted to bool.

◆ boolValue()

bool boolValue ( ) const
inline

Returns the value as a boolean. The type must be BOOL.

Referenced by operator bool().

◆ intValue()

intval_t intValue ( ) const

Returns a dimensionless value as an integer. The type must be INT. This method cannot be used for values that have a unit (it will throw an exception); for those, use either intValueInUnit() or intValueRaw()+getUnit().

Referenced by operator char(), operator int(), operator long(), operator long long(), operator short(), operator unsigned char(), operator unsigned int(), operator unsigned long(), operator unsigned long long(), and operator unsigned short().

◆ intValueRaw()

intval_t intValueRaw ( ) const

Returns value as an integer. The type must be INT. This method should be used together with getUnit() to be able to make sense of the returned value.

◆ intValueInUnit()

intval_t intValueInUnit ( const char * targetUnit) const

Returns the numeric value as an integer converted to the given unit. If the current unit cannot be converted to the given one, an error will be thrown. The type must be INT.

◆ doubleValue()

double doubleValue ( ) const

Returns a dimensionless value as a double. The type must be DOUBLE or INT. This method cannot be used for values that have a unit (it will throw an exception); for those, use either doubleValueInUnit() or doubleValueRaw()+getUnit().

Referenced by operator double(), and operator long double().

◆ doubleValueRaw()

double doubleValueRaw ( ) const

Returns the value as a double. The type must be DOUBLE or INT. This method should be used together with getUnit() to be able to make sense of the returned value.

◆ doubleValueInUnit()

double doubleValueInUnit ( const char * targetUnit) const

Returns the numeric value as a double converted to the given unit. If the current unit cannot be converted to the given one, an error will be thrown. The type must be DOUBLE or INT.

◆ getUnit()

const char * getUnit ( ) const
inline

Returns the unit ("s", "mW", "Hz", "bps", etc), or nullptr if there was no unit was specified. Unit is only valid for the DOUBLE and INT types.

◆ stringValue()

const char * stringValue ( ) const
inline

Returns value as const char *. The type must be STRING.

Referenced by operator const char *().

◆ stdstringValue()

const std::string & stdstringValue ( ) const
inline

Returns value as std::string. The type must be STRING.

Referenced by operator std::string().

◆ pointerValue()

any_ptr pointerValue ( ) const
inline

Returns value as any_ptr. The type must be POINTER.

Referenced by operator any_ptr().

◆ isNullptr()

bool isNullptr ( ) const
inline

Returns true if the value is nullptr.

◆ containsObject()

bool containsObject ( ) const

Returns true if the value contains a (non-null) pointer to a cObject.

◆ containsXML()

bool containsXML ( ) const

Returns true if the value contains a (non-null) pointer to a cXMLElement.

◆ objectValue()

cObject * objectValue ( ) const

Returns value as pointer to cObject. The type must be POINTER, and the pointer must point to an instance of cObject or be nullptr.

Referenced by operator cObject *().

◆ xmlValue()

cXMLElement * xmlValue ( ) const

Returns value as pointer to cXMLElement. The type must be POINTER, and the pointer must point to an instance of cXMLElement or be nullptr.

Referenced by operator cXMLElement *().

◆ operator=() [2/18]

cValue & operator= ( bool b)
inline

Equivalent to set(bool).

References set().

◆ operator=() [3/18]

cValue & operator= ( char c)
inline

Converts the argument to long, and calls set(intval_t).

References set().

◆ operator=() [4/18]

cValue & operator= ( unsigned char c)
inline

Converts the argument to long, and calls set(intval_t).

References set().

◆ operator=() [5/18]

cValue & operator= ( short i)
inline

Converts the argument to long, and calls set(intval_t).

References set().

◆ operator=() [6/18]

cValue & operator= ( unsigned short i)
inline

Converts the argument to long, and calls set(intval_t).

References set().

◆ operator=() [7/18]

cValue & operator= ( int i)
inline

Converts the argument to long, and calls set(intval_t).

References set().

◆ operator=() [8/18]

cValue & operator= ( unsigned int i)
inline

Converts the argument to long, and calls set(intval_t).

References set().

◆ operator=() [9/18]

cValue & operator= ( long l)
inline

Equivalent to set(intval_t).

References set().

◆ operator=() [10/18]

cValue & operator= ( unsigned long l)
inline

Converts the argument to long, and calls set(intval_t).

References omnetpp::checked_int_cast(), and set().

◆ operator=() [11/18]

cValue & operator= ( long long l)
inline

Equivalent to set(intval_t).

References omnetpp::checked_int_cast(), and set().

◆ operator=() [12/18]

cValue & operator= ( unsigned long long l)
inline

Converts the argument to long, and calls set(intval_t).

References omnetpp::checked_int_cast(), and set().

◆ operator=() [13/18]

cValue & operator= ( double d)
inline

Equivalent to set(double).

References set().

◆ operator=() [14/18]

cValue & operator= ( long double d)
inline

Converts the argument to double, and calls set(double).

References set().

◆ operator=() [15/18]

cValue & operator= ( const char * s)
inline

Equivalent to set(const char *).

References set().

◆ operator=() [16/18]

cValue & operator= ( const std::string & s)
inline

Equivalent to set(const std::string&).

References set().

◆ operator=() [17/18]

cValue & operator= ( any_ptr ptr)
inline

Equivalent to set(any_ptr).

References set().

◆ operator=() [18/18]

cValue & operator= ( cObject * obj)
inline

Equivalent to set(cObject *).

References set().

◆ operator bool()

operator bool ( ) const
inline

Equivalent to boolValue().

References boolValue().

◆ operator char()

operator char ( ) const
inline

Calls intValue() and converts the result to char. An exception is thrown if the conversion would result in a data loss,

References omnetpp::checked_int_cast(), and intValue().

◆ operator unsigned char()

operator unsigned char ( ) const
inline

Calls intValue() and converts the result to unsigned char. An exception is thrown if the conversion would result in a data loss,

References omnetpp::checked_int_cast(), and intValue().

◆ operator int()

operator int ( ) const
inline

Calls intValue() and converts the result to int. An exception is thrown if the conversion would result in a data loss,

References omnetpp::checked_int_cast(), and intValue().

◆ operator unsigned int()

operator unsigned int ( ) const
inline

Calls intValue() and converts the result to unsigned int. An exception is thrown if the conversion would result in a data loss,

References omnetpp::checked_int_cast(), and intValue().

◆ operator short()

operator short ( ) const
inline

Calls intValue() and converts the result to short. An exception is thrown if the conversion would result in a data loss,

References omnetpp::checked_int_cast(), and intValue().

◆ operator unsigned short()

operator unsigned short ( ) const
inline

Calls intValue() and converts the result to unsigned short. An exception is thrown if the conversion would result in a data loss,

References omnetpp::checked_int_cast(), and intValue().

◆ operator long()

operator long ( ) const
inline

Calls intValue() and converts the result to long. An exception is thrown if the conversion would result in a data loss,

References omnetpp::checked_int_cast(), and intValue().

◆ operator unsigned long()

operator unsigned long ( ) const
inline

Calls intValue() and converts the result to unsigned long. An exception is thrown if the conversion would result in a data loss,

References omnetpp::checked_int_cast(), and intValue().

◆ operator long long()

operator long long ( ) const
inline

Calls intValue() and converts the result to long long. An exception is thrown if the conversion would result in a data loss,

References omnetpp::checked_int_cast(), and intValue().

◆ operator unsigned long long()

operator unsigned long long ( ) const
inline

Calls intValue() and converts the result to unsigned long long. An exception is thrown if the conversion would result in a data loss,

References omnetpp::checked_int_cast(), and intValue().

◆ operator double()

operator double ( ) const
inline

Equivalent to doubleValue().

References doubleValue().

◆ operator long double()

operator long double ( ) const
inline

Calls doubleValue() and converts the result to long double. Note that this is a potentially lossy operation.

References doubleValue().

◆ operator const char *()

operator const char * ( ) const
inline

Equivalent to stringValue().

References stringValue().

◆ operator std::string()

operator std::string ( ) const
inline

Equivalent to stdstringValue().

References stdstringValue().

◆ operator any_ptr()

operator any_ptr ( ) const
inline

Equivalent to pointerValue().

References pointerValue().

◆ operator cObject *()

operator cObject * ( ) const
inline

Equivalent to objectValue().

References objectValue().

◆ operator cXMLElement *()

operator cXMLElement * ( ) const
inline

Equivalent to xmlValue().

Note: The lifetime of the returned object tree is limited; see xmlValue() for details.

References xmlValue().