StateVariable Class Reference

Back to the index.

Public Types | Public Member Functions | Static Public Member Functions | List of all members
StateVariable Class Reference

StateVariables make up the persistent state of Component objects. More...

#include <StateVariable.h>

Inheritance diagram for StateVariable:
UnitTestable

Public Types

enum  Type {
  String = 0, Bool, Double, UInt8,
  UInt16, UInt32, UInt64, SInt8,
  SInt16, SInt32, SInt64, Custom
}
 An enumeration of the possible types of a StateVariable. More...
 

Public Member Functions

 StateVariable (const string &name, string *ptrToString)
 Constructor for a String StateVariable. More...
 
 StateVariable (const string &name, bool *ptrToVar)
 Constructor for a Bool StateVariable. More...
 
 StateVariable (const string &name, double *ptrToVar)
 Constructor for a Double StateVariable. More...
 
 StateVariable (const string &name, uint8_t *ptrToVar)
 Constructor for a UInt8 StateVariable. More...
 
 StateVariable (const string &name, uint16_t *ptrToVar)
 Constructor for a UInt16 StateVariable. More...
 
 StateVariable (const string &name, uint32_t *ptrToVar)
 Constructor for a UInt32 StateVariable. More...
 
 StateVariable (const string &name, uint64_t *ptrToVar)
 Constructor for a UInt64 StateVariable. More...
 
 StateVariable (const string &name, int8_t *ptrToVar)
 Constructor for a SInt8 StateVariable. More...
 
 StateVariable (const string &name, int16_t *ptrToVar)
 Constructor for a SInt16 StateVariable. More...
 
 StateVariable (const string &name, int32_t *ptrToVar)
 Constructor for a SInt32 StateVariable. More...
 
 StateVariable (const string &name, int64_t *ptrToVar)
 Constructor for a SInt64 StateVariable. More...
 
 StateVariable (const string &name, CustomStateVariableHandler *ptrToHandler)
 Constructor for a custom StateVariable. More...
 
const string & GetName () const
 Gets the name of the variable. More...
 
enum Type GetType () const
 Gets the type of the variable. More...
 
void SerializeValue (ostream &ss) const
 Serializes the variable value into a string. More...
 
void Serialize (ostream &ss, SerializationContext &context) const
 Serializes the variable into a string. More...
 
bool CopyValueFrom (const StateVariable &otherVariable)
 Copy the value from another variable into this variable. More...
 
string ToString () const
 Returns the variable as a readable string. More...
 
uint64_t ToInteger () const
 Returns the variable as an unsignedinteger value. More...
 
double ToDouble () const
 Returns the variable as a double value. More...
 
bool SetValue (const string &expression)
 Set the variable's value, using a string expression. More...
 
bool SetValue (uint64_t value)
 Set the variable's value to an integer. More...
 

Static Public Member Functions

static void RunUnitTests (int &nSucceeded, int &nFailures)
 
- Static Public Member Functions inherited from UnitTestable
static void RunUnitTests (int &nSucceeded, int &nFailures)
 Runs unit test cases. More...
 

Detailed Description

StateVariables make up the persistent state of Component objects.

A StateVariable has a name, and a value.

Definition at line 67 of file StateVariable.h.

Member Enumeration Documentation

◆ Type

An enumeration of the possible types of a StateVariable.

Enumerator
String 
Bool 
Double 
UInt8 
UInt16 
UInt32 
UInt64 
SInt8 
SInt16 
SInt32 
SInt64 
Custom 

Definition at line 74 of file StateVariable.h.

Constructor & Destructor Documentation

◆ StateVariable() [1/12]

StateVariable::StateVariable ( const string &  name,
string *  ptrToString 
)

Constructor for a String StateVariable.

Parameters
nameThe variable's name.
ptrToStringA pointer to the string which this variable refers to.

Definition at line 36 of file StateVariable.cc.

◆ StateVariable() [2/12]

StateVariable::StateVariable ( const string &  name,
bool *  ptrToVar 
)

Constructor for a Bool StateVariable.

Parameters
nameThe variable's name.
ptrToVarA pointer to the variable.

Definition at line 44 of file StateVariable.cc.

◆ StateVariable() [3/12]

StateVariable::StateVariable ( const string &  name,
double *  ptrToVar 
)

Constructor for a Double StateVariable.

Parameters
nameThe variable's name.
ptrToVarA pointer to the variable.

Definition at line 52 of file StateVariable.cc.

◆ StateVariable() [4/12]

StateVariable::StateVariable ( const string &  name,
uint8_t *  ptrToVar 
)

Constructor for a UInt8 StateVariable.

Parameters
nameThe variable's name.
ptrToVarA pointer to the variable.

Definition at line 60 of file StateVariable.cc.

◆ StateVariable() [5/12]

StateVariable::StateVariable ( const string &  name,
uint16_t *  ptrToVar 
)

Constructor for a UInt16 StateVariable.

Parameters
nameThe variable's name.
ptrToVarA pointer to the variable.

Definition at line 68 of file StateVariable.cc.

◆ StateVariable() [6/12]

StateVariable::StateVariable ( const string &  name,
uint32_t *  ptrToVar 
)

Constructor for a UInt32 StateVariable.

Parameters
nameThe variable's name.
ptrToVarA pointer to the variable.

Definition at line 76 of file StateVariable.cc.

◆ StateVariable() [7/12]

StateVariable::StateVariable ( const string &  name,
uint64_t *  ptrToVar 
)

Constructor for a UInt64 StateVariable.

Parameters
nameThe variable's name.
ptrToVarA pointer to the variable.

Definition at line 84 of file StateVariable.cc.

◆ StateVariable() [8/12]

StateVariable::StateVariable ( const string &  name,
int8_t *  ptrToVar 
)

Constructor for a SInt8 StateVariable.

Parameters
nameThe variable's name.
ptrToVarA pointer to the variable.

Definition at line 92 of file StateVariable.cc.

◆ StateVariable() [9/12]

StateVariable::StateVariable ( const string &  name,
int16_t *  ptrToVar 
)

Constructor for a SInt16 StateVariable.

Parameters
nameThe variable's name.
ptrToVarA pointer to the variable.

Definition at line 100 of file StateVariable.cc.

◆ StateVariable() [10/12]

StateVariable::StateVariable ( const string &  name,
int32_t *  ptrToVar 
)

Constructor for a SInt32 StateVariable.

Parameters
nameThe variable's name.
ptrToVarA pointer to the variable.

Definition at line 108 of file StateVariable.cc.

◆ StateVariable() [11/12]

StateVariable::StateVariable ( const string &  name,
int64_t *  ptrToVar 
)

Constructor for a SInt64 StateVariable.

Parameters
nameThe variable's name.
ptrToVarA pointer to the variable.

Definition at line 116 of file StateVariable.cc.

◆ StateVariable() [12/12]

StateVariable::StateVariable ( const string &  name,
CustomStateVariableHandler ptrToHandler 
)

Constructor for a custom StateVariable.

Parameters
nameThe variable's name.
ptrToHandlerA pointer to the custom handler.

Definition at line 124 of file StateVariable.cc.

Member Function Documentation

◆ CopyValueFrom()

bool StateVariable::CopyValueFrom ( const StateVariable otherVariable)

Copy the value from another variable into this variable.

Parameters
otherVariableThe variable to copy from.
Returns
True if the value was copied, false if copying was not possible (i.e. the types differed).

Definition at line 177 of file StateVariable.cc.

References Bool, Custom, Double, pbool, pdouble, phandler, psint16, psint32, psint64, psint8, pstr, puint16, puint32, puint64, puint8, SInt16, SInt32, SInt64, SInt8, String, UInt16, UInt32, UInt64, and UInt8.

Referenced by SetValue().

◆ GetName()

const string & StateVariable::GetName ( ) const

Gets the name of the variable.

Returns
The name of the variable.

Definition at line 138 of file StateVariable.cc.

References Bool, Custom, Double, SInt16, SInt32, SInt64, SInt8, String, UInt16, UInt32, UInt64, and UInt8.

Referenced by RootComponent::CheckVariableWrite(), Component::CheckVariableWrite(), and SetValue().

◆ GetType()

enum StateVariable::Type StateVariable::GetType ( ) const

Gets the type of the variable.

Returns
The type of the variable.

Definition at line 132 of file StateVariable.cc.

Referenced by Component::Clone(), and SetValue().

◆ RunUnitTests()

static void StateVariable::RunUnitTests ( int &  nSucceeded,
int &  nFailures 
)
static

◆ Serialize()

void StateVariable::Serialize ( ostream &  ss,
SerializationContext context 
) const

Serializes the variable into a string.

Parameters
ssA stream where the variable (type, name, and value) will be appended.
contextSerialization context (tab indentation, etc).

Definition at line 383 of file StateVariable.cc.

References SerializeValue(), and SerializationContext::Tabs().

Referenced by SetValue().

◆ SerializeValue()

void StateVariable::SerializeValue ( ostream &  ss) const

Serializes the variable value into a string.

Strings are serialized as quoted strings; most other types without quoting.

Parameters
ssA stream where the variable value will be appended.

Definition at line 362 of file StateVariable.cc.

References Custom, EscapedString::Generate(), String, and ToString().

Referenced by Serialize(), and Component::SetVariableValue().

◆ SetValue() [1/2]

bool StateVariable::SetValue ( const string &  expression)

Set the variable's value, using a string expression.

Note that the expression may be a single value (e.g. 42), or something more complex (e.g. 123 + cpu0.pc * 4).

When setting string values, the expression should be a C-style escaped string, e.g.: "This is a string with a \" inside it."

Parameters
expressionThe new value.
Returns
True if the value was set, false if e.g. there was a parse error.

Definition at line 412 of file StateVariable.cc.

References Bool, Custom, EscapedString::Decode(), Double, StringHelper::ParseNumber(), SInt16, SInt32, SInt64, SInt8, String, UInt16, UInt32, UInt64, and UInt8.

Referenced by RootComponent::CheckVariableWrite(), DummyComponent::GetAttribute(), GXemul::GetStep(), SetValue(), and Component::SetVariableValue().

◆ SetValue() [2/2]

bool StateVariable::SetValue ( uint64_t  value)

Set the variable's value to an integer.

Parameters
valueThe new value.
Returns
True if the value was set, false if e.g. there was a type error.

Definition at line 565 of file StateVariable.cc.

References UnitTest::Assert(), Bool, CopyValueFrom(), Custom, Double, EscapedString::Generate(), GetName(), GetType(), Serialize(), SetValue(), SInt16, SInt32, SInt64, SInt8, String, ToString(), UInt16, UInt32, UInt64, UInt8, UNITTEST, and UNITTESTS.

◆ ToDouble()

double StateVariable::ToDouble ( ) const

Returns the variable as a double value.

Returns
A double, representing the variable's value.

Definition at line 321 of file StateVariable.cc.

References Bool, Custom, Double, SInt16, SInt32, SInt64, SInt8, String, UInt16, UInt32, UInt64, and UInt8.

◆ ToInteger()

uint64_t StateVariable::ToInteger ( ) const

◆ ToString()

string StateVariable::ToString ( ) const

Member Data Documentation

◆ pbool

bool* StateVariable::pbool

Definition at line 317 of file StateVariable.h.

Referenced by CopyValueFrom().

◆ pdouble

double* StateVariable::pdouble

Definition at line 318 of file StateVariable.h.

Referenced by CopyValueFrom().

◆ phandler

CustomStateVariableHandler* StateVariable::phandler

Definition at line 327 of file StateVariable.h.

Referenced by CopyValueFrom().

◆ psint16

int16_t* StateVariable::psint16

Definition at line 324 of file StateVariable.h.

Referenced by CopyValueFrom().

◆ psint32

int32_t* StateVariable::psint32

Definition at line 325 of file StateVariable.h.

Referenced by CopyValueFrom().

◆ psint64

int64_t* StateVariable::psint64

Definition at line 326 of file StateVariable.h.

Referenced by CopyValueFrom().

◆ psint8

int8_t* StateVariable::psint8

Definition at line 323 of file StateVariable.h.

Referenced by CopyValueFrom().

◆ pstr

string* StateVariable::pstr

Definition at line 316 of file StateVariable.h.

Referenced by CopyValueFrom().

◆ puint16

uint16_t* StateVariable::puint16

Definition at line 320 of file StateVariable.h.

Referenced by CopyValueFrom().

◆ puint32

uint32_t* StateVariable::puint32

Definition at line 321 of file StateVariable.h.

Referenced by CopyValueFrom().

◆ puint64

uint64_t* StateVariable::puint64

Definition at line 322 of file StateVariable.h.

Referenced by CopyValueFrom().

◆ puint8

uint8_t* StateVariable::puint8

Definition at line 319 of file StateVariable.h.

Referenced by CopyValueFrom().


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

Generated on Fri Dec 7 2018 19:52:23 for GXemul by doxygen 1.8.13