Chemical Data Processing Library C++ API - Version 1.4.0
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 
92  {
93 
94  typedef std::unordered_map<LookupKey, Any, LookupKey::HashFunc> ParameterMap;
95 
96  public:
101  typedef ParameterMap::value_type ParameterEntry;
102 
106  typedef ParameterMap::const_iterator ConstParameterIterator;
107 
115  typedef std::function<void(const LookupKey&, const Any&)> ParameterChangedCallbackFunction;
116 
124  typedef std::function<void(const LookupKey&)> ParameterRemovedCallbackFunction;
125 
135  typedef std::function<void()> ParentChangedCallbackFunction;
136 
141  std::size_t getNumParameters() const;
142 
156  template <typename T>
157  void setParameter(const LookupKey& key, T&& val);
158 
179  const Any& getParameter(const LookupKey& key, bool throw_ = false, bool local = false) const;
180 
197  template <typename T>
198  const T& getParameter(const LookupKey& key, bool local = false) const;
199 
217  template <typename T>
218  const T& getParameterOrDefault(const LookupKey& key, const T& def_val, bool local = false) const;
219 
231  bool removeParameter(const LookupKey& key);
232 
241 
254  bool isParameterSet(const LookupKey& key, bool local = false) const;
255 
268 
278 
284 
290 
296 
302 
318 
325 
333 
342 
350 
359 
367 
375  void unregisterParentChangedCallback(std::size_t id);
376 
381  inline const ControlParameterContainer& getParameters() const;
382 
383  protected:
388  parent(0) {}
389 
397  parameters(cntnr.parameters), parent(0) {}
398 
406 
416 
417  private:
418  void parameterRemoved(const LookupKey&) const;
419  void parameterChanged(const LookupKey&, const Any&) const;
420 
421  void parentParameterRemoved(const LookupKey&) const;
422  void parentParameterChanged(const LookupKey&, const Any&) const;
423 
424  void parentChanged() const;
425 
426  inline bool isEmptyAny(const Any& val) const;
427 
428  template <typename T>
429  bool isEmptyAny(const T& val) const;
430 
431  typedef std::vector<ControlParameterContainer*> ChildContainer;
432  typedef std::pair<std::size_t, ParameterChangedCallbackFunction> ParamChangedCallbackContainerEntry;
433  typedef std::vector<ParamChangedCallbackContainerEntry> ParamChangedCallbackContainer;
434  typedef std::pair<std::size_t, ParameterRemovedCallbackFunction> ParamRemovedCallbackContainerEntry;
435  typedef std::vector<ParamRemovedCallbackContainerEntry> ParamRemovedCallbackContainer;
436  typedef std::pair<std::size_t, ParentChangedCallbackFunction> ParentChangedCallbackContainerEntry;
437  typedef std::vector<ParentChangedCallbackContainerEntry> ParentChangedCallbackContainer;
438 
439  ParameterMap parameters;
440  const ControlParameterContainer* parent;
441  mutable ChildContainer children;
442  ParamChangedCallbackContainer paramChangedCallbacks;
443  ParamRemovedCallbackContainer paramRemovedCallbacks;
444  ParentChangedCallbackContainer parentChangedCallbacks;
445  };
446  } // namespace Base
447 } // namespace CDPL
448 
449 
450 // Implementation of template members
451 
452 template <typename T>
454 {
455  return getParameter(key, true, local).template getData<T>();
456 }
457 
458 template <typename T>
459 const T& CDPL::Base::ControlParameterContainer::getParameterOrDefault(const LookupKey& key, const T& def_val, bool local) const
460 {
461  const Any& val = getParameter(key, false, local);
462 
463  return (val.isEmpty() ? def_val : val.template getData<T>());
464 }
465 
466 template <typename T>
468 {
469  if (isEmptyAny(val)) {
470  removeParameter(key);
471  return;
472  }
473 
474  const Any& any_val = (parameters[key] = std::forward<T>(val));
475 
476  parameterChanged(key, any_val);
477 }
478 
479 bool CDPL::Base::ControlParameterContainer::isEmptyAny(const Any& val) const
480 {
481  return val.isEmpty();
482 }
483 
484 template <typename T>
485 bool CDPL::Base::ControlParameterContainer::isEmptyAny(const T&) const
486 {
487  return false;
488 }
489 
491 {
492  return *this;
493 }
494 
495 #endif // CDPL_BASE_CONTROLPARAMETERCONTAINER_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 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
Base class providing methods for the storage and lookup of control-parameter values.
Definition: ControlParameterContainer.hpp:92
std::size_t getNumParameters() const
Returns the number of container entries.
ControlParameterContainer(const ControlParameterContainer &cntnr)
Constructs a copy of the ControlParameterContainer instance cntnr.
Definition: ControlParameterContainer.hpp:396
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.
ConstParameterIterator getParametersBegin() const
Returns a constant iterator pointing to the beginning of the entries.
void setParameter(const LookupKey &key, T &&val)
Sets the value of the control-parameter specified by key to val.
Definition: ControlParameterContainer.hpp:467
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:115
void copyParameters(const ControlParameterContainer &cntnr)
Replaces the current control-parameter entries by a copy of the entries in cntnr.
void clearParameters()
Erases all container entries.
void unregisterParameterRemovedCallback(std::size_t id)
Unregisters the callback specified by id.
const Any & getParameter(const LookupKey &key, bool throw_=false, bool local=false) const
Returns the value of the control-parameter specified by key.
std::function< void()> ParentChangedCallbackFunction
A functor class that wraps callback target functions which get invoked when the parent container has ...
Definition: ControlParameterContainer.hpp:135
ConstParameterIterator end() const
Returns a constant iterator pointing to the end of the entries.
bool removeParameter(const LookupKey &key)
Removes the entry for the control-parameter specified by key.
void unregisterParameterChangedCallback(std::size_t id)
Unregisters the callback specified by id.
void unregisterParentChangedCallback(std::size_t id)
Unregisters the callback specified by id.
ConstParameterIterator getParametersEnd() const
Returns a constant iterator pointing to the end of the entries.
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:459
std::size_t registerParameterChangedCallback(const ParameterChangedCallbackFunction &func)
Registers a callback target function that gets invoked when the value of a control-parameter has chan...
void addParameters(const ControlParameterContainer &cntnr)
Adds the control-parameter value entries of the ControlParameterContainer instance cntnr.
ConstParameterIterator begin() const
Returns a constant iterator pointing to the beginning of the entries.
std::size_t registerParameterRemovedCallback(const ParameterRemovedCallbackFunction &func)
Registers a callback target function that gets invoked when a control-parameter entry has been remove...
void setParent(const ControlParameterContainer *cntnr)
Sets or removes the parent control-parameter container used to resolve requests for missing entries.
ParameterMap::value_type ParameterEntry
A Base::LookupKey / Base::Any pair used to store the control-parameter values and associated keys.
Definition: ControlParameterContainer.hpp:101
ParameterMap::const_iterator ConstParameterIterator
A constant iterator used to iterate over the control-parameter entries.
Definition: ControlParameterContainer.hpp:106
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:124
ControlParameterContainer()
Constructs an empty ControlParameterContainer instance.
Definition: ControlParameterContainer.hpp:387
const ControlParameterContainer & getParameters() const
Returns a const reference to itself.
Definition: ControlParameterContainer.hpp:490
const ControlParameterContainer * getParent() const
Returns a pointer to the parent control-parameter container.
virtual ~ControlParameterContainer()
Virtual destructor.
ControlParameterContainer & operator=(const ControlParameterContainer &cntnr)
Assignment operator.
std::size_t registerParentChangedCallback(const ParentChangedCallbackFunction &func)
Registers a callback target function that gets invoked when the parent container has been changed or ...
Unique lookup key for control-parameter and property values.
Definition: LookupKey.hpp:54
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
The namespace of the Chemical Data Processing Library.