Chemical Data Processing Library C++ API - Version 1.2.0
|
A class providing methods for the storage and lookup of object properties. More...
#include <PropertyContainer.hpp>
Public Types | |
typedef PropertyMap::value_type | PropertyEntry |
A Base::LookupKey / Base::Any pair that stores the property value for a given property key. More... | |
typedef PropertyMap::const_iterator | ConstPropertyIterator |
A constant iterator used to iterate over the property entries. More... | |
Public Member Functions | |
std::size_t | getNumProperties () const |
Returns the number of property entries. More... | |
template<typename T > | |
void | setProperty (const LookupKey &key, T &&val) |
Sets the value of the property specified by key to val. More... | |
template<typename T > | |
const T & | getProperty (const LookupKey &key) const |
Returns the value of the property specified by key as a const reference to an object of type T. More... | |
template<typename T > | |
const T & | getPropertyOrDefault (const LookupKey &key, const T &def_val) const |
Returns the value of the property specified by key as a const reference to an object of type T, or the default value def_val if a stored value does not exist. More... | |
const Any & | getProperty (const LookupKey &key, bool throw_=false) const |
Returns the value of the property specified by key. More... | |
bool | isPropertySet (const LookupKey &key) const |
Tells whether or not a value has been assigned to the property specified by key. More... | |
ConstPropertyIterator | getPropertiesBegin () const |
Returns a constant iterator pointing to the beginning of the property entries. More... | |
ConstPropertyIterator | getPropertiesEnd () const |
Returns a constant iterator pointing to the end of the property entries. More... | |
ConstPropertyIterator | begin () const |
Returns a constant iterator pointing to the beginning of the property entries. More... | |
ConstPropertyIterator | end () const |
Returns a constant iterator pointing to the end of the property entries. More... | |
bool | removeProperty (const LookupKey &key) |
Clears the value of the property specified by key. More... | |
void | clearProperties () |
Clears all property values. More... | |
void | addProperties (const PropertyContainer &cntnr) |
Adds the property value entries in the PropertyContainer instance cntnr. More... | |
void | copyProperties (const PropertyContainer &cntnr) |
Replaces the current set of properties by a copy of the entries in cntnr. More... | |
void | swap (PropertyContainer &cntnr) |
Exchanges the properties of this container with the properties of the container cntnr. More... | |
const PropertyContainer & | getProperties () const |
Returns a const reference to itself. More... | |
Protected Member Functions | |
PropertyContainer () | |
Constructs an empty PropertyContainer instance. More... | |
PropertyContainer (const PropertyContainer &cntnr) | |
Constructs a copy of the PropertyContainer instance cntnr. More... | |
virtual | ~PropertyContainer () |
Virtual destructor. More... | |
PropertyContainer & | operator= (const PropertyContainer &cntnr) |
Assignment operator. More... | |
A class providing methods for the storage and lookup of object properties.
The purpose of PropertyContainer
is to provide a common facility for the storage and lookup of dynamic object properties to subclasses and their clients.
PropertyContainer
stores the properties in a map that associates unique property keys of type Base::LookupKey with corresponding property values of type Base::Any. Iterators pointing to the beginning and end of the property key/value pairs (see PropertyContainer::PropertyEntry) can be retrieved by the methods getPropertiesBegin() and getPropertiesEnd(), respectively. The number of currently stored property value entries is accessible via the method getNumProperties().
For the explicit assignment of property values the method setProperty() is provided which expects the key of the property as its first and the value to assign as the second argument. Whether the value of a particular property has been set can be tested by the method isPropertySet(). For the erasure of property values the methods removeProperty() and clearProperties() are provided. The first method clears the value of a single property while the latter method removes all assigned property values.
To access the value of a property, two types of getProperty() methods are available that both expect the key of the property as the first argument. The templated versions return the stored property value (or the specified default if not available) as a reference to an object of the specified template argument type. The non-template method returns the requested property value indirectly as a reference to the Base::Any instance storing the actual value. If the requested property value does not exist, an additional argument decides whether to throw an exception or to return an empty Base::Any instance.
typedef PropertyMap::value_type CDPL::Base::PropertyContainer::PropertyEntry |
A Base::LookupKey / Base::Any pair that stores the property value for a given property key.
typedef PropertyMap::const_iterator CDPL::Base::PropertyContainer::ConstPropertyIterator |
A constant iterator used to iterate over the property entries.
|
inlineprotected |
Constructs an empty PropertyContainer
instance.
|
protected |
Constructs a copy of the PropertyContainer
instance cntnr.
cntnr | The PropertyContainer instance to copy. |
|
protectedvirtual |
Virtual destructor.
std::size_t CDPL::Base::PropertyContainer::getNumProperties | ( | ) | const |
Returns the number of property entries.
void CDPL::Base::PropertyContainer::setProperty | ( | const LookupKey & | key, |
T && | val | ||
) |
Sets the value of the property specified by key to val.
If val is of type Base::Any and empty, i.e. the method Base::Any::isEmpty() returns true
, and a property entry for key exists, the entry gets erased (equivalent to removeProperty() with key as argument).
key | The key of the property value to assign or remove. |
val | The value of the property. |
const T & CDPL::Base::PropertyContainer::getProperty | ( | const LookupKey & | key | ) | const |
Returns the value of the property specified by key as a const
reference to an object of type T.
If an entry for the specified property exists, the stored value will be returned. Otherwise a Base::ItemNotFound exception will be thrown.
key | The key of the property value to return. |
const
reference to an object of type T. Base::ItemNotFound | if an entry for the requested property value does not exist, and Base::BadCast if the stored property value cannot be casted to the target type T. |
const T & CDPL::Base::PropertyContainer::getPropertyOrDefault | ( | const LookupKey & | key, |
const T & | def_val | ||
) | const |
Returns the value of the property specified by key as a const
reference to an object of type T, or the default value def_val if a stored value does not exist.
If a value has been assigned to the specified property, the stored value will be returned. Otherwise the default value specified by def_val gets returned.
key | The key of the property for which to return the stored (or specified default) value. |
def_val | The default value that shall be returned if an entry for the specified property does not exist. |
Base::BadCast | if the stored property value cannot be casted to the target type T. |
|
inline |
Returns the value of the property specified by key.
If an entry for the specified property exists, the stored value will be returned. Otherwise an empty Base::Any object gets returned if _throw is false
, and a Base::ItemNotFound exception will be thrown if _throw is true
.
key | The key of the property value to return. |
throw_ | Specifies whether to throw a Base::ItemNotFound exception or to return an empty Base::Any object if the requested property value does not exist. |
Base::ItemNotFound | if an entry for the requested property value does not exist and throw_ is true . |
Tells whether or not a value has been assigned to the property specified by key.
key | The key of the property. |
true
if a value has been assigned to the specified property, and false
otherwise. ConstPropertyIterator CDPL::Base::PropertyContainer::getPropertiesBegin | ( | ) | const |
Returns a constant iterator pointing to the beginning of the property entries.
ConstPropertyIterator CDPL::Base::PropertyContainer::getPropertiesEnd | ( | ) | const |
Returns a constant iterator pointing to the end of the property entries.
ConstPropertyIterator CDPL::Base::PropertyContainer::begin | ( | ) | const |
Returns a constant iterator pointing to the beginning of the property entries.
ConstPropertyIterator CDPL::Base::PropertyContainer::end | ( | ) | const |
Returns a constant iterator pointing to the end of the property entries.
Clears the value of the property specified by key.
key | The key of the property value to erase. |
true
if an entry for key could be found and was erased, and false
otherwise. void CDPL::Base::PropertyContainer::clearProperties | ( | ) |
Clears all property values.
void CDPL::Base::PropertyContainer::addProperties | ( | const PropertyContainer & | cntnr | ) |
Adds the property value entries in the PropertyContainer
instance cntnr.
Any property values which have no corresponding assigned value in cntnr are left unchanged. Otherwise the value of the local property gets overwritten by the value stored in cntnr.
cntnr | The PropertyContainer instance containing the property value entries to add. |
void CDPL::Base::PropertyContainer::copyProperties | ( | const PropertyContainer & | cntnr | ) |
Replaces the current set of properties by a copy of the entries in cntnr.
cntnr | The PropertyContainer instance containing the property value entries to add. |
void CDPL::Base::PropertyContainer::swap | ( | PropertyContainer & | cntnr | ) |
Exchanges the properties of this container with the properties of the container cntnr.
cntnr | The container to exchange the properties with. |
|
inline |
Returns a const
reference to itself.
const
reference to itself.
|
protected |
Assignment operator.
Internally calls copyProperties() to perform the actual work.
cntnr | The PropertyContainer instance to copy. |