The Sparta Modeling Framework
Loading...
Searching...
No Matches
sparta::KeyValue Class Reference

The type-to-type name map. Not really necessary, but useful. More...

#include <KeyValue.hpp>

Classes

class  TypeNameVisitor
 Visitor class for looking up type name. More...
 

Public Member Functions

template<class U >
 KeyValue (const std::string &key, U val, const std::string &desc="")
 Construct a Key/Value pair.
 
std::string getKey () const
 Get the key.
 
void setDesc (const std::string &desc)
 Set the description.
 
std::string getDesc () const
 Get the Description.
 
template<class T >
const T getValue () const
 Get the value of this key/value.
 
template<class T >
void operator= (const T &rhp)
 Assign a new value to this object.
 
template<class T >
 operator T () const
 Cast operator for the key/value.
 
const std::string & getTypeName () const
 Gets the compiler-independent readable type string of the value currently held.
 

Static Public Member Functions

template<typename T >
static bool hasTypeNameFor ()
 Determines if there is a known compiler-independent typename for type T.
 
template<typename T >
static const std::string & lookupTypeName ()
 Determines if there is a known compiler-independent typename for type T.
 

Static Public Attributes

static TypeToTypeNameMap GBL_type_name_map
 Map of internal typeid names to C++ names.
 

Protected Member Functions

template<class T >
T & getValueRef_ ()
 Gets a reference to the value currently held by this object if the correct template type is specified.
 
template<class T >
const T & getValueRef_ () const
 

Detailed Description

The type-to-type name map. Not really necessary, but useful.

The variant list that the KeyValue class will support. To add a new type, just add it to the end of this list.

All elements in this list must:

  • Support default construction
  • Support assignment operator (=)
  • Support comparison operator (==) as a const qualified method. For vector, this implies that the value_type supports operator== as a const method as well.
Warning
If adding a new type that is not a copyable POD type, a std::string, or a vector of POD types (or std::strings), code will likely break. Never add [const]char* to this list; it creates

This class hold a key/value pair

Definition at line 75 of file KeyValue.hpp.

Constructor & Destructor Documentation

◆ KeyValue()

template<class U >
sparta::KeyValue::KeyValue ( const std::string &  key,
val,
const std::string &  desc = "" 
)
inline

Construct a Key/Value pair.

Parameters
keyThe key
valThe ValueVar (can be anything of any of the above supported types)

Example usage:

KeyValue val1("unsigned integer", 10u);
KeyValue val2("float", 10.1);
KeyValue val3("string", "hello");
KeyValue val4("signed ", -10);
KeyValue val5("unsigned long", 10ull);
The type-to-type name map. Not really necessary, but useful.
Definition KeyValue.hpp:76

Definition at line 104 of file KeyValue.hpp.

Member Function Documentation

◆ getDesc()

std::string sparta::KeyValue::getDesc ( ) const
inline

Get the Description.

Returns
The Description

Definition at line 141 of file KeyValue.hpp.

◆ getKey()

std::string sparta::KeyValue::getKey ( ) const
inline

Get the key.

Returns
The key

Definition at line 125 of file KeyValue.hpp.

◆ getTypeName()

const std::string & sparta::KeyValue::getTypeName ( ) const
inline

Gets the compiler-independent readable type string of the value currently held.

Returns
compiler-independent type name.
Exceptions
SpartaExceptionif held variant type is not a known type. This indicates a serious error.
Note
This is not necessarily what would be returned from typeid() or Variant::type().

Definition at line 261 of file KeyValue.hpp.

◆ getValue()

template<class T >
const T sparta::KeyValue::getValue ( ) const
inline

Get the value of this key/value.

Returns
The value of this key/value pair
Exceptions
boost::bad_getif the type requested does not match the type of the key

A painful way to get to the value of the key/value. Example usage:

KeyValue val1("unsigned integer", 10u);
unsigned int val = val1.getValue<unsigned int>();

Definition at line 158 of file KeyValue.hpp.

◆ getValueRef_() [1/2]

template<class T >
sparta::KeyValue::getValueRef_ ( )
inlineprotected

Gets a reference to the value currently held by this object if the correct template type is specified.

Template Parameters
Ttype to get current value as. This nmust be the type described by getTypeName or an exception will likely be thrown
Warning
Getting the reference to the held value is dangerous. This reference is invalidated if the held type (type of value_) changes.
discard this reference as quickly as possible.
Returns
reference to the value currenty held in this variant.
Exceptions
boost::bad_getif T does not match the current held type.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 279 of file KeyValue.hpp.

◆ getValueRef_() [2/2]

template<class T >
const T & sparta::KeyValue::getValueRef_ ( ) const
inlineprotected

Definition at line 294 of file KeyValue.hpp.

◆ hasTypeNameFor()

template<typename T >
static bool sparta::KeyValue::hasTypeNameFor ( )
inlinestatic

Determines if there is a known compiler-independent typename for type T.

Template Parameters
TType for which a name will be looked up
Returns
true if a name is found for type T

Definition at line 216 of file KeyValue.hpp.

◆ lookupTypeName()

template<typename T >
static const std::string & sparta::KeyValue::lookupTypeName ( )
inlinestatic

Determines if there is a known compiler-independent typename for type T.

Template Parameters
TType for which a name will be looked up
Returns
Name associated with type T.
Exceptions
SpartaExceptionif no name was found associated with type T

Definition at line 231 of file KeyValue.hpp.

◆ operator T()

template<class T >
sparta::KeyValue::operator T ( ) const
inline

Cast operator for the key/value.

Returns
The value of this key/value pair, if the types match
Exceptions
boost::bad_getif the type requested does not match the type of the key

A fast way to get to the value of the key/value. Example usage:

KeyValue val1("unsigned integer", 10u);
unsigned int val = val1;

Definition at line 206 of file KeyValue.hpp.

◆ operator=()

template<class T >
void sparta::KeyValue::operator= ( const T &  rhp)
inline

Assign a new value to this object.

Returns
Exceptions
SpartaExceptionif value specified cannot be assigned to the internal variant.
Note
This method is NOT restricted to the current held value type. Other types can be specified

Definition at line 189 of file KeyValue.hpp.

◆ setDesc()

void sparta::KeyValue::setDesc ( const std::string &  desc)
inline

Set the description.

Parameters
descThe description of this key/value pair

Definition at line 133 of file KeyValue.hpp.

Member Data Documentation

◆ GBL_type_name_map

TypeToTypeNameMap sparta::KeyValue::GBL_type_name_map
static

Map of internal typeid names to C++ names.

Used for looking up human-readable C++ names based on variant or template types.

Note
This is used by Parameters for a type list

Definition at line 87 of file KeyValue.hpp.


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