Chemical Data Processing Library C++ API - Version 1.4.0
PropertyContainer.hpp
Go to the documentation of this file.
1 /*
2  * PropertyContainer.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_BASE_PROPERTYCONTAINER_HPP
30 #define CDPL_BASE_PROPERTYCONTAINER_HPP
31 
32 #include <utility>
33 #include <unordered_map>
34 
35 #include "CDPL/Base/APIPrefix.hpp"
36 #include "CDPL/Base/LookupKey.hpp"
37 #include "CDPL/Base/Any.hpp"
38 #include "CDPL/Base/Exceptions.hpp"
39 
40 
41 namespace CDPL
42 {
43 
44  namespace Base
45  {
46 
74  {
75 
76  typedef std::unordered_map<LookupKey, Any, LookupKey::HashFunc> PropertyMap;
77 
78  public:
82  typedef PropertyMap::value_type PropertyEntry;
83 
87  typedef PropertyMap::const_iterator ConstPropertyIterator;
88 
93  std::size_t getNumProperties() const;
94 
104  template <typename T>
105  void setProperty(const LookupKey& key, T&& val);
106 
118  template <typename T>
119  const T& getProperty(const LookupKey& key) const;
120 
134  template <typename T>
135  const T& getPropertyOrDefault(const LookupKey& key, const T& def_val) const;
136 
149  inline const Any& getProperty(const LookupKey& key, bool throw_ = false) const;
150 
156  inline bool isPropertySet(const LookupKey& key) const;
157 
163 
169 
175 
181 
187  bool removeProperty(const LookupKey& key);
188 
193 
202  void addProperties(const PropertyContainer& cntnr);
203 
208  void copyProperties(const PropertyContainer& cntnr);
209 
214  void swap(PropertyContainer& cntnr);
215 
220  inline const PropertyContainer& getProperties() const;
221 
222  protected:
227 
233 
238 
248 
249  private:
250  inline bool isEmptyAny(const Any& val) const;
251 
252  template <typename T>
253  bool isEmptyAny(const T& val) const;
254 
255  PropertyMap properties;
256  };
257  } // namespace Base
258 } // namespace CDPL
259 
260 
261 // Implementation of template members
262 
263 template <typename T>
265 {
266  return getProperty(key, true).template getData<T>();
267 }
268 
269 template <typename T>
270 const T& CDPL::Base::PropertyContainer::getPropertyOrDefault(const LookupKey& key, const T& def_val) const
271 {
272  const Any& val = getProperty(key, false);
273 
274  return (val.isEmpty() ? def_val : val.template getData<T>());
275 }
276 
278 {
279  static const Any NOT_FOUND;
280 
281  ConstPropertyIterator it = properties.find(key);
282 
283  if (it != properties.end())
284  return it->second;
285 
286  if (throw_ex)
287  throw ItemNotFound("PropertyContainer: property " + key.getName() + " not found");
288 
289  return NOT_FOUND;
290 }
291 
292 template <typename T>
294 {
295  if (isEmptyAny(val)) {
296  properties.erase(key);
297  return;
298  }
299 
300  properties[key] = std::forward<T>(val);
301 }
302 
304 {
305  return (properties.find(key) != properties.end());
306 }
307 
308 bool CDPL::Base::PropertyContainer::isEmptyAny(const Any& val) const
309 {
310  return val.isEmpty();
311 }
312 
313 template <typename T>
314 bool CDPL::Base::PropertyContainer::isEmptyAny(const T&) const
315 {
316  return false;
317 }
318 
320 {
321  return *this;
322 }
323 
324 #endif // CDPL_BASE_PROPERTYCONTAINER_HPP
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.