OMNeT++ Simulation Library
5.6.1
|
#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 delegates 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 also be regarded as internal data structures, and they should not be directly accessed or manipulated 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 (intpar_t l) |
_OPPDEPRECATED cPar & | setLongValue (intpar_t l) |
cPar & | setDoubleValue (double d) |
cPar & | setStringValue (const char *s) |
cPar & | setStringValue (const std::string &s) |
cPar & | setXMLValue (cXMLElement *node) |
cPar & | setExpression (cExpression *e, cComponent *evalcontext=nullptr) |
void | setEvaluationContext (cComponent *ctx) |
Getter functions. Note that overloaded conversion operators also exist. | |
bool | boolValue () const |
intpar_t | intValue () const |
_OPPDEPRECATED intpar_t | longValue () 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 |
cXMLElement * | xmlValue () const |
cExpression * | getExpression () const |
cComponent * | getEvaluationContext () const |
Miscellaneous utility functions. | |
void | convertToConst () |
void | parse (const char *text) |
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= (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 cXMLElement * () const | |
Public Member Functions inherited from cObject | |
cObject () | |
cObject (const cObject &other) | |
virtual | ~cObject () |
virtual const char * | getClassName () const |
bool | isName (const char *s) const |
virtual const char * | getFullName () const |
virtual std::string | getFullPath () const |
const cObject * | getThisPtr () const |
virtual _OPPDEPRECATED std::string | info () const |
virtual _OPPDEPRECATED std::string | detailedInfo () const |
virtual cObject * | dup () const |
virtual void | parsimPack (cCommBuffer *buffer) const |
virtual void | parsimUnpack (cCommBuffer *buffer) |
virtual bool | isOwnedObject () const |
cObject * | findObject (const char *name, bool deep=true) |
void | copyNotSupported () const |
Type, flags. | |
Type | getType () const |
bool | isNumeric () const |
bool | isVolatile () 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 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.
Compatibility method, delegates to setIntValue().
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.
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.
intpar_t intValue | ( | ) | const |
Returns value as intpar_t. The cPar type must be INT. Note: Implicit conversion from DOUBLE is intentionally missing.
|
inline |
Compatibility method, delegates to intValue().
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.
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).
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.
void convertToConst | ( | ) |
For non-const values, replaces the stored expression with its evaluation.
void parse | ( | const char * | text | ) |
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().
|
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,
References omnetpp::checked_int_cast().
|
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().
|
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().
|
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().
|
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().
|
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().
|
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().
|
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().
|
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().
|
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().
|
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 xmlValue(). NOTE: The lifetime of the returned object tree is limited; see xmlValue() for details.