OMNeT++ Simulation Library
6.0.3
|
#include <cpar.h>
Represents a module or channel parameter.
When a module or channel is created, parameter objects are added automatically, based on the NED declaration of the module/channel. It is not possible to create further parameters (or to remove parameters) at runtime. This is enforced by the cPar constructor being private.
Parameters get their initial values automatically, from the NED declarations and the configuration. It is possible to change the parameter value during runtime (see various setter methods and operator='s), but not the type of the parameter (see getType()). The type correspond to NED types (bool, double, int, string, xml), and cannot be changed at runtime.
The module or channel object can get notified when a parameter is changed; one has to override cComponent::handleParameterChange() for that.
Note: In earlier versions of OMNeT++, cPar could be used as a general value storage object, and attached to cMessages as well. From the 4.0 version, simulation models should use cMsgPar for that.
Implementation note: from the 4.0 version, almost all methods of cPar delegate to an internal cParImpl object, which actually stores the value, and generally does the real job. This was done to allow sharing parameter objects which have the same name, same value, etc. among module/channel instances. This significantly reduces memory consumption of most simulation models. Because cPar is just a thin wrapper around cParImpl, cPar is not meant for subclassing, and none if its methods are virtual. cParImpl and subclasses should be regarded as internal data structures, and should not be directly accessed from model code.
Public Member Functions | |
Redefined cObject methods | |
void | operator= (const cPar &other) |
virtual const char * | getName () const override |
virtual std::string | str () const override |
virtual cObject * | getOwner () const override |
virtual void | forEachChild (cVisitor *v) override |
Setter functions. Note that overloaded assignment operators also exist. | |
cPar & | setBoolValue (bool b) |
cPar & | setIntValue (intval_t l) |
cPar & | setDoubleValue (double d) |
cPar & | setStringValue (const char *s) |
cPar & | setStringValue (const std::string &s) |
cPar & | setObjectValue (cObject *object) |
cPar & | setXMLValue (cXMLElement *node) |
cPar & | setValue (const cValue &value) |
cPar & | setExpression (cExpression *e, cComponent *evalcontext=nullptr) |
void | setEvaluationContext (cComponent *ctx) |
Getter functions. Note that overloaded conversion operators also exist. | |
bool | boolValue () const |
intval_t | intValue () const |
double | doubleValue () const |
double | doubleValueInUnit (const char *targetUnit) const |
const char * | getUnit () const |
const char * | stringValue () const |
std::string | stdstringValue () const |
bool | isEmptyString () const |
cObject * | objectValue () const |
cXMLElement * | xmlValue () const |
cValue | getValue () const |
cExpression * | getExpression () const |
cComponent * | getEvaluationContext () const |
Miscellaneous utility functions. | |
void | convertToConst () |
void | parse (const char *text, const char *baseDirectory=nullptr, FileLine loc=FileLine()) |
Overloaded assignment and conversion operators. | |
cPar & | operator= (bool b) |
cPar & | operator= (char c) |
cPar & | operator= (unsigned char c) |
cPar & | operator= (int i) |
cPar & | operator= (unsigned int i) |
cPar & | operator= (short i) |
cPar & | operator= (unsigned short i) |
cPar & | operator= (long i) |
cPar & | operator= (unsigned long i) |
cPar & | operator= (long long i) |
cPar & | operator= (unsigned long long i) |
cPar & | operator= (double d) |
cPar & | operator= (long double d) |
cPar & | operator= (const char *s) |
cPar & | operator= (const std::string &s) |
cPar & | operator= (cObject *object) |
cPar & | operator= (cXMLElement *node) |
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 cObject * () const | |
operator cXMLElement * () const | |
Public Member Functions inherited from cObject | |
cObject () | |
cObject (const cObject &other)=default | |
virtual | ~cObject () |
virtual const char * | getClassName () const |
bool | isName (const char *s) const |
virtual const char * | getFullName () const |
virtual std::string | getFullPath () const |
virtual std::string | getClassAndFullName () const |
virtual std::string | getClassAndFullPath () const |
const cObject * | getThisPtr () const |
virtual std::ostream & | printOn (std::ostream &os) const |
virtual cObject * | dup () const |
virtual void | parsimPack (cCommBuffer *buffer) const |
virtual void | parsimUnpack (cCommBuffer *buffer) |
virtual bool | isOwnedObject () const |
virtual bool | isSoftOwner () const |
cObject * | findObject (const char *name, bool deep=true) |
virtual cClassDescriptor * | getDescriptor () const |
void | copyNotSupported () const |
Type, flags. | |
Type | getType () const |
bool | isNumeric () const |
bool | isVolatile () const |
bool | isMutable () const |
bool | isExpression () const |
bool | isShared () const |
bool | isSet () const |
bool | containsValue () const |
cProperties * | getProperties () const |
static const char * | getTypeName (Type t) |
Additional Inherited Members | |
Protected Member Functions inherited from cObject | |
virtual void | take (cOwnedObject *obj) |
virtual void | drop (cOwnedObject *obj) |
void | dropAndDelete (cOwnedObject *obj) |
void operator= | ( | const cPar & | other | ) |
Assignment operator.
|
overridevirtual |
Returns the parameter name.
Reimplemented from cObject.
|
overridevirtual |
Returns the value in text form.
Reimplemented from cObject.
|
overridevirtual |
Returns the component (module/channel) this parameter belongs to. Note: return type is cObject only for technical reasons, it can be safely cast to cComponent.
Reimplemented from cObject.
|
overridevirtual |
Type getType | ( | ) | const |
Returns the parameter type
|
static |
Returns the given type as a string.
bool isNumeric | ( | ) | const |
Returns true if the stored value is of a numeric type.
bool isVolatile | ( | ) | const |
Returns true if this parameter is marked in the NED file as "volatile". This flag affects the operation of setExpression().
bool isMutable | ( | ) | const |
Returns true if this parameter is marked in the NED file as mutable. A non-mutable parameter cannot have its value changed.
bool isExpression | ( | ) | const |
Returns false if the stored value is a constant, and true if it is an expression. (It is not examined whether the expression yields a constant value.)
bool isShared | ( | ) | const |
Returns true if the parameter value expression is shared among several modules to save memory. This flag is purely informational, and whether a parameter is shared or not does not affect operation at all.
bool isSet | ( | ) | const |
Returns true if the parameter is assigned a value, and false otherwise. Parameters of an already initialized module or channel are guaranteed to assigned, so this method will return true for them.
bool containsValue | ( | ) | const |
Returns true if the parameter is set (see isSet()) or contains a default value, and false otherwise. Parameters of an already initialized module or channel are guaranteed to be assigned, so this method will return true for them.
cProperties* getProperties | ( | ) | const |
Return the properties for this parameter. Properties cannot be changed at runtime.
cPar& setBoolValue | ( | bool | b | ) |
Sets the value to the given bool value.
cPar& setDoubleValue | ( | double | d | ) |
Sets the value to the given double value.
cPar& setStringValue | ( | const char * | s | ) |
Sets the value to the given string value. The cPar will make its own copy of the string. nullptr is also accepted and treated as an empty string.
|
inline |
Sets the value to the given string value.
References cPar::setStringValue().
Referenced by cPar::setStringValue().
cPar& setXMLValue | ( | cXMLElement * | node | ) |
Sets the value to the given cXMLElement.
Sets the parameter's value. This method invokes the value setter method (setBoolValue(), setIntValue(), etc.) that corresponds to the parameter's type with the value in the cValue object. If the value's type doesn't match the parameter's type, an error is raised.
cPar& setExpression | ( | cExpression * | e, |
cComponent * | evalcontext = nullptr |
||
) |
Sets the value to the given expression. This object will assume the responsibility to delete the expression object.
The evalcontext parameter determines the module or channel in the context of which the expression will be evaluated. If evalcontext is nullptr, the owner of this parameter will be used.
Note: if the parameter is marked as non-volatile (isVolatile()==false), one should not set an expression as value. This is not enforced by cPar though.
|
inline |
If the parameter contains an expression (see isExpression()), this method sets the evaluation context for the expression.
bool boolValue | ( | ) | const |
Returns value as a boolean. The cPar type must be BOOL.
intval_t intValue | ( | ) | const |
Returns value as intval_t. The cPar type must be INT. Note: Implicit conversion from DOUBLE is intentionally missing.
double doubleValue | ( | ) | const |
Returns value as double. The cPar type must be DOUBLE. Note: Implicit conversion from INT is intentionally missing.
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.
const char* getUnit | ( | ) | const |
Returns the parameter's unit ("s", "mW", "Hz", "bps", etc) as declared with the @unit property of the parameter in NED, or nullptr if no unit was specified. Unit is only valid for INT and DOUBLE types.
const char* stringValue | ( | ) | const |
Returns value as const char *. The cPar type must be STRING. This method may only be invoked when the parameter's value is a string constant and not the result of expression evaluation, otherwise an error is thrown. This practically means this method cannot be used on parameters declared as "volatile string" in NED; they can only be accessed using stdstringValue().
std::string stdstringValue | ( | ) | const |
Returns value as string. The cPar type must be STRING.
|
inline |
Convenience method: returns true of the parameter contains an empty string. The cPar type must be STRING.
cObject* objectValue | ( | ) | const |
Returns the value as object. The cPar type must be OBJECT. The ownership of the object remains with the cPar. It is not possible for the user to take ownership of the object, but creating a copy via dup() is an option.
Lifetime of the returned objects:
The returned object pointer is stored within the parameter. A setObjectValue() call will delete the previous object (given that the parameter owns the object).
Note: If the object parameter refers to an object which it doesn't own, it never deletes the object. If an object doesn't know its owner (it is a cObject but not a cOwnedObject), it is considered owned.
If the parameter is declared volatile (see isVolatile()), the returned object pointer (the pointer of the object which was produced by evaluating the expression) is stored. Upon the next objectValue() call, the previously returned object is deleted (given that the parameter owns the object), and replaced with the new object.
Note that parameters may be shared under the hood. This means that a previously returned object may also be deleted if another module's similar volatile parameter is evaluated using objectValue().
Therefore, the lifetime of returned objects should be considered very limited. In simple terms, the object should either be used only locally and then its pointer forgotten, or be cloned using dup() for long-term use.
cXMLElement* xmlValue | ( | ) | const |
Returns value as pointer to cXMLElement. The cPar type must be XML.
The lifetime of the returned object tree is undefined, but it is valid at least until the end of the current simulation event or initialize() call. Modules are expected to process their XML configurations at once (within one event or within initialize()), and not hang on to pointers returned from this method. The reason for the limited lifetime is that this method may return pointers to objects stored in an internal XML document cache, and the simulation kernel reserves the right to discard cached XML documents at any time to free up memory, and re-load them on demand (i.e. when xmlValue() is called again).
cValue getValue | ( | ) | const |
Generic access to the parameter's value. This method delegates to the value accessor method (boolValue(), intValue(), etc.) that corresponds to the parameter's type, and returns the result.
cExpression* getExpression | ( | ) | const |
Returns pointer to the expression stored by the object, or nullptr.
|
inline |
If the parameter contains an expression, this method returns the module or channel in the context of which the expression will be evaluated. (The context affects the resolution of parameter references, and NED operators like index
or sizeof()
.) If the parameter does not contain an expression, the return value is undefined.
|
inline |
For non-const values, replaces the stored expression with its evaluation.
Converts the value from string, and stores the result. If the text cannot be parsed, an exception is thrown, which can be caught as std::runtime_error& if necessary.
Note: this method understands expressions too, but does NOT handle the special values "default" and "ask".
|
inline |
Equivalent to setBoolValue().
|
inline |
Delegates to setIntValue().
|
inline |
Delegates to setIntValue().
|
inline |
Delegates to setIntValue().
|
inline |
Delegates to setIntValue().
|
inline |
Delegates to setIntValue().
|
inline |
Delegates to setIntValue().
|
inline |
Equivalent to setIntValue().
|
inline |
Delegates to setIntValue().
|
inline |
Delegates to setIntValue().
|
inline |
Delegates to setIntValue().
|
inline |
Equivalent to setDoubleValue().
|
inline |
Converts the argument to double, and calls setDoubleValue().
|
inline |
Equivalent to setStringValue().
|
inline |
Equivalent to setStringValue().
Equivalent to setObjectValue().
|
inline |
Equivalent to setXMLValue().
|
inline |
Equivalent to boolValue().
|
inline |
Calls intValue() and converts the result to char. An exception is thrown if the conversion would result in a data loss,
|
inline |
Calls intValue() and converts the result to unsigned char. An exception is thrown if the conversion would result in a data loss,
|
inline |
Calls intValue() and converts the result to int. An exception is thrown if the conversion would result in a data loss,
|
inline |
Calls intValue() and converts the result to unsigned int. An exception is thrown if the conversion would result in a data loss,
|
inline |
Calls intValue() and converts the result to short. An exception is thrown if the conversion would result in a data loss,
|
inline |
Calls intValue() and converts the result to unsigned short. An exception is thrown if the conversion would result in a data loss,
|
inline |
Calls intValue() and converts the result to long. An exception is thrown if the conversion would result in a data loss,
|
inline |
Calls intValue() and converts the result to unsigned long. An exception is thrown if the conversion would result in a data loss,
|
inline |
Calls intValue() and converts the result to long long. An exception is thrown if the conversion would result in a data loss,
|
inline |
Calls intValue() and converts the result to unsigned long long. An exception is thrown if the conversion would result in a data loss,
|
inline |
Equivalent to doubleValue().
|
inline |
Calls doubleValue() and converts the result to long double.
|
inline |
Equivalent to stringValue().
|
inline |
Equivalent to stdstringValue().
|
inline |
Equivalent to objectValue().
|
inline |
Equivalent to xmlValue(). NOTE: The lifetime of the returned object tree is limited; see xmlValue() for details.