Chemical Data Processing Library C++ API - Version 1.1.1
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 
75  {
76 
77  typedef std::unordered_map<LookupKey, Any, LookupKey::HashFunc> PropertyMap;
78 
79  public:
83  typedef PropertyMap::value_type PropertyEntry;
84 
88  typedef PropertyMap::const_iterator ConstPropertyIterator;
89 
94  std::size_t getNumProperties() const;
95 
105  template <typename T>
106  void setProperty(const LookupKey& key, T&& val);
107 
119  template <typename T>
120  const T& getProperty(const LookupKey& key) const;
121 
135  template <typename T>
136  const T& getPropertyOrDefault(const LookupKey& key, const T& def_val) const;
137 
150  inline const Any& getProperty(const LookupKey& key, bool throw_ = false) const;
151 
157  inline bool isPropertySet(const LookupKey& key) const;
158 
164 
170 
176 
182 
188  bool removeProperty(const LookupKey& key);
189 
194 
203  void addProperties(const PropertyContainer& cntnr);
204 
209  void copyProperties(const PropertyContainer& cntnr);
210 
215  void swap(PropertyContainer& cntnr);
216 
221  inline const PropertyContainer& getProperties() const;
222 
223  protected:
228 
234 
239 
249 
250  private:
251  inline bool isEmptyAny(const Any& val) const;
252 
253  template <typename T>
254  bool isEmptyAny(const T& val) const;
255 
256  PropertyMap properties;
257  };
258  } // namespace Base
259 } // namespace CDPL
260 
261 
262 // Implementation of template members
263 
264 template <typename T>
266 {
267  return getProperty(key, true).template getData<T>();
268 }
269 
270 template <typename T>
271 const T& CDPL::Base::PropertyContainer::getPropertyOrDefault(const LookupKey& key, const T& def_val) const
272 {
273  const Any& val = getProperty(key, false);
274 
275  return (val.isEmpty() ? def_val : val.template getData<T>());
276 }
277 
279 {
280  static const Any NOT_FOUND;
281 
282  ConstPropertyIterator it = properties.find(key);
283 
284  if (it != properties.end())
285  return it->second;
286 
287  if (throw_ex)
288  throw ItemNotFound("PropertyContainer: property " + key.getName() + " not found");
289 
290  return NOT_FOUND;
291 }
292 
293 template <typename T>
295 {
296  if (isEmptyAny(val)) {
297  properties.erase(key);
298  return;
299  }
300 
301  properties[key] = std::forward<T>(val);
302 }
303 
305 {
306  return (properties.find(key) != properties.end());
307 }
308 
309 bool CDPL::Base::PropertyContainer::isEmptyAny(const Any& val) const
310 {
311  return val.isEmpty();
312 }
313 
314 template <typename T>
315 bool CDPL::Base::PropertyContainer::isEmptyAny(const T&) const
316 {
317  return false;
318 }
319 
321 {
322  return *this;
323 }
324 
325 #endif // CDPL_BASE_PROPERTYCONTAINER_HPP
CDPL::Base::PropertyContainer::swap
void swap(PropertyContainer &cntnr)
Exchanges the properties of this container with the properties of the container cntnr.
CDPL::Base::Any::isEmpty
bool isEmpty() const noexcept
Tells whether the Any instance stores any data.
Definition: Any.hpp:182
CDPL::Base::PropertyContainer::getPropertiesBegin
ConstPropertyIterator getPropertiesBegin() const
Returns a constant iterator pointing to the beginning of the property entries.
CDPL::Base::PropertyContainer::ConstPropertyIterator
PropertyMap::const_iterator ConstPropertyIterator
A constant iterator used to iterate over the property entries.
Definition: PropertyContainer.hpp:88
CDPL::Base::PropertyContainer::copyProperties
void copyProperties(const PropertyContainer &cntnr)
Replaces the current set of properties by a copy of the entries in cntnr.
CDPL_BASE_API
#define CDPL_BASE_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
CDPL::Base::PropertyContainer::~PropertyContainer
virtual ~PropertyContainer()
Virtual destructor.
CDPL::Base::PropertyContainer::getNumProperties
std::size_t getNumProperties() const
Returns the number of property entries.
CDPL::Base::ItemNotFound
Thrown to indicate that some requested data item could not be found.
Definition: Base/Exceptions.hpp:171
CDPL::Base::PropertyContainer::clearProperties
void clearProperties()
Clears all property values.
CDPL::Base::LookupKey::getName
const std::string & getName() const
Returns the name of the LookupKey instance.
CDPL::Base::LookupKey
An unique lookup key for control-parameter and property values.
Definition: LookupKey.hpp:54
APIPrefix.hpp
Definition of the preprocessor macro CDPL_BASE_API.
CDPL::Base::PropertyContainer::addProperties
void addProperties(const PropertyContainer &cntnr)
Adds the property value entries in the PropertyContainer instance cntnr.
LookupKey.hpp
Definition of the class CDPL::Base::LookupKey.
CDPL::Base::PropertyContainer::PropertyEntry
PropertyMap::value_type PropertyEntry
A Base::LookupKey / Base::Any pair that stores the property value for a given property key.
Definition: PropertyContainer.hpp:83
CDPL::Base::Any
A safe, type checked container for arbitrary data of variable type.
Definition: Any.hpp:59
CDPL::Base::PropertyContainer::begin
ConstPropertyIterator begin() const
Returns a constant iterator pointing to the beginning of the property entries.
CDPL::Base::PropertyContainer::isPropertySet
bool isPropertySet(const LookupKey &key) const
Tells whether or not a value has been assigned to the property specified by key.
Definition: PropertyContainer.hpp:304
CDPL::Base::PropertyContainer::getProperties
const PropertyContainer & getProperties() const
Returns a const reference to itself.
Definition: PropertyContainer.hpp:320
CDPL::Base::PropertyContainer::setProperty
void setProperty(const LookupKey &key, T &&val)
Sets the value of the property specified by key to val.
Definition: PropertyContainer.hpp:294
CDPL::Chem::AtomType::T
const unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
Exceptions.hpp
Definition of exception classes.
CDPL::Base::PropertyContainer
A class providing methods for the storage and lookup of object properties.
Definition: PropertyContainer.hpp:75
CDPL::Base::PropertyContainer::PropertyContainer
PropertyContainer()
Constructs an empty PropertyContainer instance.
Definition: PropertyContainer.hpp:227
CDPL::Base::PropertyContainer::end
ConstPropertyIterator end() const
Returns a constant iterator pointing to the end of the property entries.
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Base::PropertyContainer::removeProperty
bool removeProperty(const LookupKey &key)
Clears the value of the property specified by key.
CDPL::Base::PropertyContainer::operator=
PropertyContainer & operator=(const PropertyContainer &cntnr)
Assignment operator.
CDPL::Base::PropertyContainer::getPropertyOrDefault
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:271
Any.hpp
Definition of the class CDPL::Base::Any.
CDPL::Base::PropertyContainer::PropertyContainer
PropertyContainer(const PropertyContainer &cntnr)
Constructs a copy of the PropertyContainer instance cntnr.
CDPL::Base::PropertyContainer::getProperty
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:265
CDPL::Base::PropertyContainer::getPropertiesEnd
ConstPropertyIterator getPropertiesEnd() const
Returns a constant iterator pointing to the end of the property entries.