Chemical Data Processing Library C++ API - Version 1.2.0
Public Types | Public Member Functions | Protected Member Functions | List of all members
CDPL::Base::PropertyContainer Class Reference

A class providing methods for the storage and lookup of object properties. More...

#include <PropertyContainer.hpp>

+ Inheritance diagram for CDPL::Base::PropertyContainer:

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 AnygetProperty (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 PropertyContainergetProperties () 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...
 
PropertyContaineroperator= (const PropertyContainer &cntnr)
 Assignment operator. More...
 

Detailed Description

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.

Member Typedef Documentation

◆ PropertyEntry

typedef PropertyMap::value_type CDPL::Base::PropertyContainer::PropertyEntry

A Base::LookupKey / Base::Any pair that stores the property value for a given property key.

◆ ConstPropertyIterator

typedef PropertyMap::const_iterator CDPL::Base::PropertyContainer::ConstPropertyIterator

A constant iterator used to iterate over the property entries.

Constructor & Destructor Documentation

◆ PropertyContainer() [1/2]

CDPL::Base::PropertyContainer::PropertyContainer ( )
inlineprotected

Constructs an empty PropertyContainer instance.

◆ PropertyContainer() [2/2]

CDPL::Base::PropertyContainer::PropertyContainer ( const PropertyContainer cntnr)
protected

Constructs a copy of the PropertyContainer instance cntnr.

Parameters
cntnrThe PropertyContainer instance to copy.

◆ ~PropertyContainer()

virtual CDPL::Base::PropertyContainer::~PropertyContainer ( )
protectedvirtual

Virtual destructor.

Member Function Documentation

◆ getNumProperties()

std::size_t CDPL::Base::PropertyContainer::getNumProperties ( ) const

Returns the number of property entries.

Returns
The number of property entries.

◆ setProperty()

template<typename T >
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).

Parameters
keyThe key of the property value to assign or remove.
valThe value of the property.

◆ getProperty() [1/2]

template<typename T >
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.

Parameters
keyThe key of the property value to return.
Returns
The stored value of the property as a const reference to an object of type T.
Exceptions
Base::ItemNotFoundif 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.

◆ getPropertyOrDefault()

template<typename 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.

Parameters
keyThe key of the property for which to return the stored (or specified default) value.
def_valThe default value that shall be returned if an entry for the specified property does not exist.
Returns
The stored property value or the default specified by def_val.
Exceptions
Base::BadCastif the stored property value cannot be casted to the target type T.

◆ getProperty() [2/2]

const CDPL::Base::Any & CDPL::Base::PropertyContainer::getProperty ( const LookupKey key,
bool  throw_ = false 
) const
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.

Parameters
keyThe 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.
Returns
The stored property value or and empty Base::Any object.
Exceptions
Base::ItemNotFoundif an entry for the requested property value does not exist and throw_ is true.

◆ isPropertySet()

bool CDPL::Base::PropertyContainer::isPropertySet ( const LookupKey key) const
inline

Tells whether or not a value has been assigned to the property specified by key.

Parameters
keyThe key of the property.
Returns
true if a value has been assigned to the specified property, and false otherwise.

◆ getPropertiesBegin()

ConstPropertyIterator CDPL::Base::PropertyContainer::getPropertiesBegin ( ) const

Returns a constant iterator pointing to the beginning of the property entries.

Returns
A constant iterator pointing to the beginning of the property entries.

◆ getPropertiesEnd()

ConstPropertyIterator CDPL::Base::PropertyContainer::getPropertiesEnd ( ) const

Returns a constant iterator pointing to the end of the property entries.

Returns
A constant iterator pointing to the end of the property entries.

◆ begin()

ConstPropertyIterator CDPL::Base::PropertyContainer::begin ( ) const

Returns a constant iterator pointing to the beginning of the property entries.

Returns
A constant iterator pointing to the beginning of the property entries.

◆ end()

ConstPropertyIterator CDPL::Base::PropertyContainer::end ( ) const

Returns a constant iterator pointing to the end of the property entries.

Returns
A constant iterator pointing to the end of the property entries.

◆ removeProperty()

bool CDPL::Base::PropertyContainer::removeProperty ( const LookupKey key)

Clears the value of the property specified by key.

Parameters
keyThe key of the property value to erase.
Returns
true if an entry for key could be found and was erased, and false otherwise.

◆ clearProperties()

void CDPL::Base::PropertyContainer::clearProperties ( )

Clears all property values.

◆ addProperties()

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.

Parameters
cntnrThe PropertyContainer instance containing the property value entries to add.

◆ copyProperties()

void CDPL::Base::PropertyContainer::copyProperties ( const PropertyContainer cntnr)

Replaces the current set of properties by a copy of the entries in cntnr.

Parameters
cntnrThe PropertyContainer instance containing the property value entries to add.

◆ swap()

void CDPL::Base::PropertyContainer::swap ( PropertyContainer cntnr)

Exchanges the properties of this container with the properties of the container cntnr.

Parameters
cntnrThe container to exchange the properties with.

◆ getProperties()

const CDPL::Base::PropertyContainer & CDPL::Base::PropertyContainer::getProperties ( ) const
inline

Returns a const reference to itself.

Returns
A const reference to itself.

◆ operator=()

PropertyContainer& CDPL::Base::PropertyContainer::operator= ( const PropertyContainer cntnr)
protected

Assignment operator.

Internally calls copyProperties() to perform the actual work.

Parameters
cntnrThe PropertyContainer instance to copy.
Returns
A reference to itself.

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