Chemical Data Processing Library C++ API - Version 1.1.1
ControlParameterContainer.hpp
Go to the documentation of this file.
1 /*
2  * ControlParameterContainer.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_CONTROLPARAMETERCONTAINER_HPP
30 #define CDPL_BASE_CONTROLPARAMETERCONTAINER_HPP
31 
32 #include <vector>
33 #include <utility>
34 #include <cstddef>
35 #include <unordered_map>
36 #include <functional>
37 
38 #include "CDPL/Base/APIPrefix.hpp"
39 #include "CDPL/Base/LookupKey.hpp"
40 #include "CDPL/Base/Any.hpp"
41 
42 
43 namespace CDPL
44 {
45 
46  namespace Base
47  {
48 
93  {
94 
95  typedef std::unordered_map<LookupKey, Any, LookupKey::HashFunc> ParameterMap;
96 
97  public:
102  typedef ParameterMap::value_type ParameterEntry;
103 
107  typedef ParameterMap::const_iterator ConstParameterIterator;
108 
116  typedef std::function<void(const LookupKey&, const Any&)> ParameterChangedCallbackFunction;
117 
125  typedef std::function<void(const LookupKey&)> ParameterRemovedCallbackFunction;
126 
136  typedef std::function<void()> ParentChangedCallbackFunction;
137 
142  std::size_t getNumParameters() const;
143 
157  template <typename T>
158  void setParameter(const LookupKey& key, T&& val);
159 
180  const Any& getParameter(const LookupKey& key, bool throw_ = false, bool local = false) const;
181 
198  template <typename T>
199  const T& getParameter(const LookupKey& key, bool local = false) const;
200 
218  template <typename T>
219  const T& getParameterOrDefault(const LookupKey& key, const T& def_val, bool local = false) const;
220 
232  bool removeParameter(const LookupKey& key);
233 
242 
255  bool isParameterSet(const LookupKey& key, bool local = false) const;
256 
269 
279 
285 
291 
297 
303 
319 
326 
334 
343 
351 
360 
368 
376  void unregisterParentChangedCallback(std::size_t id);
377 
382  inline const ControlParameterContainer& getParameters() const;
383 
384  protected:
389  parent(0) {}
390 
398  parameters(cntnr.parameters), parent(0) {}
399 
407 
417 
418  private:
419  void parameterRemoved(const LookupKey&) const;
420  void parameterChanged(const LookupKey&, const Any&) const;
421 
422  void parentParameterRemoved(const LookupKey&) const;
423  void parentParameterChanged(const LookupKey&, const Any&) const;
424 
425  void parentChanged() const;
426 
427  inline bool isEmptyAny(const Any& val) const;
428 
429  template <typename T>
430  bool isEmptyAny(const T& val) const;
431 
432  typedef std::vector<ControlParameterContainer*> ChildContainer;
433  typedef std::pair<std::size_t, ParameterChangedCallbackFunction> ParamChangedCallbackContainerEntry;
434  typedef std::vector<ParamChangedCallbackContainerEntry> ParamChangedCallbackContainer;
435  typedef std::pair<std::size_t, ParameterRemovedCallbackFunction> ParamRemovedCallbackContainerEntry;
436  typedef std::vector<ParamRemovedCallbackContainerEntry> ParamRemovedCallbackContainer;
437  typedef std::pair<std::size_t, ParentChangedCallbackFunction> ParentChangedCallbackContainerEntry;
438  typedef std::vector<ParentChangedCallbackContainerEntry> ParentChangedCallbackContainer;
439 
440  ParameterMap parameters;
441  const ControlParameterContainer* parent;
442  mutable ChildContainer children;
443  ParamChangedCallbackContainer paramChangedCallbacks;
444  ParamRemovedCallbackContainer paramRemovedCallbacks;
445  ParentChangedCallbackContainer parentChangedCallbacks;
446  };
447  } // namespace Base
448 } // namespace CDPL
449 
450 
451 // Implementation of template members
452 
453 template <typename T>
455 {
456  return getParameter(key, true, local).template getData<T>();
457 }
458 
459 template <typename T>
460 const T& CDPL::Base::ControlParameterContainer::getParameterOrDefault(const LookupKey& key, const T& def_val, bool local) const
461 {
462  const Any& val = getParameter(key, false, local);
463 
464  return (val.isEmpty() ? def_val : val.template getData<T>());
465 }
466 
467 template <typename T>
469 {
470  if (isEmptyAny(val)) {
471  removeParameter(key);
472  return;
473  }
474 
475  const Any& any_val = (parameters[key] = std::forward<T>(val));
476 
477  parameterChanged(key, any_val);
478 }
479 
480 bool CDPL::Base::ControlParameterContainer::isEmptyAny(const Any& val) const
481 {
482  return val.isEmpty();
483 }
484 
485 template <typename T>
486 bool CDPL::Base::ControlParameterContainer::isEmptyAny(const T&) const
487 {
488  return false;
489 }
490 
492 {
493  return *this;
494 }
495 
496 #endif // CDPL_BASE_CONTROLPARAMETERCONTAINER_HPP
CDPL::Base::ControlParameterContainer::ControlParameterContainer
ControlParameterContainer(const ControlParameterContainer &cntnr)
Constructs a copy of the ControlParameterContainer instance cntnr.
Definition: ControlParameterContainer.hpp:397
CDPL::Base::ControlParameterContainer
A class providing methods for the storage and lookup of control-parameter values.
Definition: ControlParameterContainer.hpp:93
CDPL::Base::Any::isEmpty
bool isEmpty() const noexcept
Tells whether the Any instance stores any data.
Definition: Any.hpp:182
CDPL::Base::ControlParameterContainer::getParametersEnd
ConstParameterIterator getParametersEnd() const
Returns a constant iterator pointing to the end of the entries.
CDPL::Base::ControlParameterContainer::ParentChangedCallbackFunction
std::function< void()> ParentChangedCallbackFunction
A functor class that wraps callback target functions which get invoked when the parent container has ...
Definition: ControlParameterContainer.hpp:136
CDPL::Base::ControlParameterContainer::getParametersBegin
ConstParameterIterator getParametersBegin() const
Returns a constant iterator pointing to the beginning of the entries.
CDPL::Base::ControlParameterContainer::ConstParameterIterator
ParameterMap::const_iterator ConstParameterIterator
A constant iterator used to iterate over the control-parameter entries.
Definition: ControlParameterContainer.hpp:107
CDPL_BASE_API
#define CDPL_BASE_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
CDPL::Base::ControlParameterContainer::setParameter
void setParameter(const LookupKey &key, T &&val)
Sets the value of the control-parameter specified by key to val.
Definition: ControlParameterContainer.hpp:468
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::ControlParameterContainer::ControlParameterContainer
ControlParameterContainer()
Constructs an empty ControlParameterContainer instance.
Definition: ControlParameterContainer.hpp:388
CDPL::Base::ControlParameterContainer::unregisterParentChangedCallback
void unregisterParentChangedCallback(std::size_t id)
Unregisters the callback specified by id.
LookupKey.hpp
Definition of the class CDPL::Base::LookupKey.
CDPL::Base::Any
A safe, type checked container for arbitrary data of variable type.
Definition: Any.hpp:59
CDPL::Base::ControlParameterContainer::removeParameter
bool removeParameter(const LookupKey &key)
Removes the entry for the control-parameter specified by key.
CDPL::Base::ControlParameterContainer::getParameterOrDefault
const T & getParameterOrDefault(const LookupKey &key, const T &def_val, bool local=false) const
Returns the value of the control-parameter specified by key as a const reference to an object of type...
Definition: ControlParameterContainer.hpp:460
CDPL::Base::ControlParameterContainer::~ControlParameterContainer
virtual ~ControlParameterContainer()
Destructor.
CDPL::Base::ControlParameterContainer::end
ConstParameterIterator end() const
Returns a constant iterator pointing to the end of the entries.
CDPL::Base::ControlParameterContainer::begin
ConstParameterIterator begin() const
Returns a constant iterator pointing to the beginning of the entries.
CDPL::Base::ControlParameterContainer::registerParentChangedCallback
std::size_t registerParentChangedCallback(const ParentChangedCallbackFunction &func)
Registers a callback target function that gets invoked when the parent container has been changed or ...
CDPL::Base::ControlParameterContainer::registerParameterChangedCallback
std::size_t registerParameterChangedCallback(const ParameterChangedCallbackFunction &func)
Registers a callback target function that gets invoked when the value of a control-parameter has chan...
CDPL::Base::ControlParameterContainer::isParameterSet
bool isParameterSet(const LookupKey &key, bool local=false) const
Tells whether or not a value has been assigned to the control-parameter specified by key.
CDPL::Base::ControlParameterContainer::ParameterRemovedCallbackFunction
std::function< void(const LookupKey &)> ParameterRemovedCallbackFunction
A functor class that wraps callback target functions which get invoked when a control-parameter entry...
Definition: ControlParameterContainer.hpp:125
CDPL::Base::ControlParameterContainer::registerParameterRemovedCallback
std::size_t registerParameterRemovedCallback(const ParameterRemovedCallbackFunction &func)
Registers a callback target function that gets invoked when a control-parameter entry has been remove...
CDPL::Chem::AtomType::T
const unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
CDPL::Base::ControlParameterContainer::unregisterParameterChangedCallback
void unregisterParameterChangedCallback(std::size_t id)
Unregisters the callback specified by id.
CDPL::Base::ControlParameterContainer::setParent
void setParent(const ControlParameterContainer *cntnr)
Sets or removes the parent control-parameter container used to resolve requests for missing entries.
CDPL::Base::ControlParameterContainer::getNumParameters
std::size_t getNumParameters() const
Returns the number of container entries.
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Base::ControlParameterContainer::getParent
const ControlParameterContainer * getParent() const
Returns a pointer to the parent control-parameter container.
CDPL::Base::ControlParameterContainer::unregisterParameterRemovedCallback
void unregisterParameterRemovedCallback(std::size_t id)
Unregisters the callback specified by id.
CDPL::Base::ControlParameterContainer::copyParameters
void copyParameters(const ControlParameterContainer &cntnr)
Replaces the current set of properties by a copy of the entries in cntnr.
CDPL::Base::ControlParameterContainer::getParameter
const Any & getParameter(const LookupKey &key, bool throw_=false, bool local=false) const
Returns the value of the control-parameter specified by key.
CDPL::Base::ControlParameterContainer::ParameterEntry
ParameterMap::value_type ParameterEntry
A Base::LookupKey / Base::Any pair used to store the control-parameter values and associated keys.
Definition: ControlParameterContainer.hpp:102
CDPL::Base::ControlParameterContainer::operator=
ControlParameterContainer & operator=(const ControlParameterContainer &cntnr)
Assignment operator.
CDPL::Base::ControlParameterContainer::clearParameters
void clearParameters()
Erases all container entries.
CDPL::Base::ControlParameterContainer::getParameters
const ControlParameterContainer & getParameters() const
Returns a const reference to itself.
Definition: ControlParameterContainer.hpp:491
Any.hpp
Definition of the class CDPL::Base::Any.
CDPL::Base::ControlParameterContainer::ParameterChangedCallbackFunction
std::function< void(const LookupKey &, const Any &)> ParameterChangedCallbackFunction
A functor class that wraps callback target functions which get invoked when the value of a control-pa...
Definition: ControlParameterContainer.hpp:116
CDPL::Base::ControlParameterContainer::addParameters
void addParameters(const ControlParameterContainer &cntnr)
Adds the control-parameter value entries in the ControlParameterContainer instance cntnr.