29 #ifndef CDPL_BASE_PROPERTYCONTAINER_HPP
30 #define CDPL_BASE_PROPERTYCONTAINER_HPP
33 #include <unordered_map>
76 typedef std::unordered_map<LookupKey, Any, LookupKey::HashFunc> PropertyMap;
104 template <
typename T>
105 void setProperty(
const LookupKey& key,
T&& val);
118 template <
typename T>
119 const T& getProperty(
const LookupKey& key)
const;
134 template <
typename T>
135 const T& getPropertyOrDefault(
const LookupKey& key,
const T& def_val)
const;
149 inline const Any& getProperty(
const LookupKey& key,
bool throw_ =
false)
const;
156 inline bool isPropertySet(
const LookupKey& key)
const;
250 inline bool isEmptyAny(
const Any& val)
const;
252 template <
typename T>
253 bool isEmptyAny(
const T& val)
const;
255 PropertyMap properties;
263 template <
typename T>
266 return getProperty(key,
true).template getData<T>();
269 template <
typename T>
272 const Any& val = getProperty(key,
false);
274 return (val.
isEmpty() ? def_val : val.template getData<T>());
279 static const Any NOT_FOUND;
283 if (it != properties.end())
292 template <
typename T>
295 if (isEmptyAny(val)) {
296 properties.erase(key);
300 properties[key] = std::forward<T>(val);
305 return (properties.find(key) != properties.end());
308 bool CDPL::Base::PropertyContainer::isEmptyAny(
const Any& val)
const
313 template <
typename T>
314 bool CDPL::Base::PropertyContainer::isEmptyAny(
const T&)
const
Definition of class CDPL::Base::Any.
Definition of the preprocessor macro CDPL_BASE_API.
#define CDPL_BASE_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
Definition of exception classes.
Definition of class CDPL::Base::LookupKey.
Safe, type checked container for arbitrary data of variable type.
Definition: Any.hpp:60
bool isEmpty() const noexcept
Tells whether the Any instance stores any data.
Definition: Any.hpp:183
Thrown to indicate that some requested data item could not be found.
Definition: Base/Exceptions.hpp:171
Unique lookup key for control-parameter and property values.
Definition: LookupKey.hpp:54
const std::string & getName() const
Returns the name of the LookupKey instance.
Base class providing methods for the storage and lookup of object properties.
Definition: PropertyContainer.hpp:74
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.
Definition: PropertyContainer.hpp:264
ConstPropertyIterator getPropertiesBegin() const
Returns a constant iterator pointing to the beginning of the property entries.
PropertyContainer()
Constructs an empty PropertyContainer instance.
Definition: PropertyContainer.hpp:226
bool removeProperty(const LookupKey &key)
Removes the entry for the property specified by key.
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,...
Definition: PropertyContainer.hpp:270
const PropertyContainer & getProperties() const
Returns a const reference to itself.
Definition: PropertyContainer.hpp:319
PropertyMap::const_iterator ConstPropertyIterator
A constant iterator used to iterate over the property entries.
Definition: PropertyContainer.hpp:87
virtual ~PropertyContainer()
Virtual destructor.
PropertyContainer & operator=(const PropertyContainer &cntnr)
Assignment operator.
void addProperties(const PropertyContainer &cntnr)
Adds the property entries of the PropertyContainer instance cntnr.
PropertyContainer(const PropertyContainer &cntnr)
Constructs a copy of the PropertyContainer instance cntnr.
std::size_t getNumProperties() const
Returns the number of property entries.
void clearProperties()
Erases all container entries.
ConstPropertyIterator end() const
Returns a constant iterator pointing to the end of the property entries.
void swap(PropertyContainer &cntnr)
Exchanges the property entries of this container with the ones of cntnr.
bool isPropertySet(const LookupKey &key) const
Tells whether or not a value has been assigned to the property specified by key.
Definition: PropertyContainer.hpp:303
ConstPropertyIterator getPropertiesEnd() const
Returns a constant iterator pointing to the end of the property entries.
PropertyMap::value_type PropertyEntry
A Base::LookupKey / Base::Any pair that stores the property value for a given property key.
Definition: PropertyContainer.hpp:82
ConstPropertyIterator begin() const
Returns a constant iterator pointing to the beginning of the property entries.
void copyProperties(const PropertyContainer &cntnr)
Replaces the current property entries by a copy of the entries in cntnr.
void setProperty(const LookupKey &key, T &&val)
Sets the value of the property specified by key to val.
Definition: PropertyContainer.hpp:293
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
The namespace of the Chemical Data Processing Library.