parameters.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2011, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef HECTOR_POSE_ESTIMATION_PARAMETERS_H
30 #define HECTOR_POSE_ESTIMATION_PARAMETERS_H
31 
32 #include <list>
33 #include <string>
34 #include <boost/shared_ptr.hpp>
35 #include <boost/function.hpp>
36 #include <boost/type_traits.hpp>
37 
38 namespace hector_pose_estimation {
39 
40  class Parameter;
41  template <typename T> class ParameterT;
44  typedef boost::function<void(ParameterPtr)> ParameterRegisterFunc;
45 
46  class Parameter {
47  public:
48  std::string key;
49  Parameter(const std::string& key) : key(key), parameter_(this) {}
50  Parameter(Parameter& other) : key(other.key), parameter_(&other) {}
51  virtual ~Parameter() {}
52 
53  virtual ParameterPtr clone() { return parameter_ ? parameter_->clone() : ParameterPtr(); }
54  virtual const char *type() const { return parameter_ ? parameter_->type() : 0; }
55 
56  virtual bool empty() const { return !parameter_; }
57  virtual bool isAlias() const { return false; }
58 
59  template <typename T> bool hasType() const {
60  return dynamic_cast<const ParameterT<T> *>(parameter_) != 0;
61  }
62 
63  template <typename T> const T& as() const {
64  const ParameterT<T>& p = dynamic_cast<const ParameterT<T> &>(*parameter_);
65  return p.value();
66  }
67 
68  template <typename T> T& as() {
69  ParameterT<T>& p = dynamic_cast<ParameterT<T> &>(*parameter_);
70  return p.value();
71  }
72 
73  operator void*() const { return reinterpret_cast<void *>(!empty()); }
74 
75  template <typename T> Parameter& operator =(const T& value) {
76  ParameterT<T>& p = dynamic_cast<ParameterT<T>&>(*parameter_);
77  p.set(value);
78  return *this;
79  }
80 
81  protected:
83  };
84 
85  template <typename T>
86  class ParameterT : public Parameter {
87  public:
88  typedef typename boost::remove_reference<typename boost::remove_const<T>::type>::type param_type;
89 
90  ParameterT(const std::string& key, param_type &value) : Parameter(key), value_(value) {}
91  ParameterT(Parameter& other) : Parameter(other), value_(other.as<T>()) {}
92  virtual ~ParameterT() {}
93 
94  ParameterPtr clone() { return ParameterPtr(new ParameterT<T>(*this)); }
95  const char *type() const { return typeid(param_type).name(); }
96 
97  operator param_type&() const { return value_; }
98  param_type& value() { return value_; }
99  const param_type& value() const { return value_; }
100  void set(const param_type& value) { value_ = value; }
101 
102  protected:
103  param_type& value_;
104  };
105 
106  class Alias : public Parameter {
107  public:
108  Alias() : Parameter(std::string()) { parameter_ = 0; }
109  Alias(const ParameterPtr& other) : Parameter(other->key) { *this = parameter_; }
110  Alias(const ParameterPtr& other, const std::string& key) : Parameter(key) { *this = parameter_; }
111  virtual ~Alias() {}
112 
113  virtual bool isAlias() const { return true; }
114 
115  using Parameter::operator =;
116  Alias& operator =(const ParameterPtr& other) {
117  parameter_ = other.get();
118  if (key.empty()) key = other->key;
119  return *this;
120  }
121  };
122 
123  template <typename T>
124  class AliasT : public Alias {
125  public:
126  typedef typename boost::remove_reference<typename boost::remove_const<T>::type>::type param_type;
127 
128  AliasT() {}
129  AliasT(const ParameterPtr& other) : Alias(other) {}
130  AliasT(const ParameterPtr& other, const std::string& key) : Alias(other, key) {}
131  virtual ~AliasT() {}
132 
133  operator param_type&() const { return dynamic_cast<ParameterT<T> &>(*parameter_).value(); }
134  param_type& value() { return dynamic_cast<ParameterT<T> &>(*parameter_).value(); }
135  const param_type& value() const { return dynamic_cast<const ParameterT<T> &>(*parameter_).value(); }
136  void set(const param_type& value) { dynamic_cast<ParameterT<T> &>(*parameter_).set(value); }
137 
138  using Alias::operator =;
139  };
140 
141  class ParameterList : public std::list<ParameterPtr> {
142  public:
143  using std::list<ParameterPtr>::iterator;
144  using std::list<ParameterPtr>::const_iterator;
145 
147 
148  template <typename T> ParameterList& add(const std::string& key, T& value, const T& default_value) {
149  value = default_value;
150  return add(key, value);
151  }
152 
153  template <typename T> ParameterList& add(const std::string& key, T* value) {
154  return add(key, *value);
155  }
156 
157  template <typename T> ParameterList& add(const std::string& key, T& value);
158 
159  ParameterList& add(ParameterPtr const& parameter);
160  ParameterList& add(ParameterList const& other);
161  ParameterList& add(Parameter& alias, const std::string& key = std::string());
162  ParameterList& addAlias(const std::string& key, Alias& alias) { return add(alias, key); }
163 
164  ParameterList& copy(const std::string& prefix, ParameterList const& parameters);
165  ParameterList& copy(ParameterList const& parameters);
166 
167  ParameterPtr const& get(const std::string& key) const;
168  template <typename T> T& getAs(const std::string& key) const {
169  return get(key)->as<T>();
170  }
171 
172  using std::list<ParameterPtr>::erase;
173  iterator erase(const std::string& key);
174 
175  void initialize(ParameterRegisterFunc func) const;
176  };
177 
178  template <typename T> inline ParameterList& ParameterList::add(const std::string& key, T& value) {
179  return add(ParameterPtr(new ParameterT<T>(key, value)));
180  }
181 
182  static inline ParameterList operator+(ParameterList const& list1, ParameterList const& list2)
183  {
184  ParameterList result;
185  return result.add(list1).add(list2);
186  }
187 
189  virtual void operator()(ParameterPtr) {}
190  };
191 
192 } // namespace hector_pose_estimation
193 
194 #endif // HECTOR_POSE_ESTIMATION_PARAMETERS_H
virtual bool isAlias() const
Definition: parameters.h:57
virtual const char * type() const
Definition: parameters.h:54
AliasT(const ParameterPtr &other)
Definition: parameters.h:129
virtual bool empty() const
Definition: parameters.h:56
ROSCONSOLE_DECL void initialize()
T & getAs(const std::string &key) const
Definition: parameters.h:168
Alias(const ParameterPtr &other, const std::string &key)
Definition: parameters.h:110
void set(const param_type &value)
Definition: parameters.h:100
boost::shared_ptr< const Parameter > ParameterConstPtr
Definition: parameters.h:43
const param_type & value() const
Definition: parameters.h:135
ParameterList & addAlias(const std::string &key, Alias &alias)
Definition: parameters.h:162
boost::remove_reference< typename boost::remove_const< T >::type >::type param_type
Definition: parameters.h:126
bool add(const actionlib::TwoIntsGoal &req, actionlib::TwoIntsResult &res)
ParameterList & add(const std::string &key, T &value, const T &default_value)
Definition: parameters.h:148
virtual void operator()(ParameterPtr)
Definition: parameters.h:189
ParameterT(const std::string &key, param_type &value)
Definition: parameters.h:90
Alias(const ParameterPtr &other)
Definition: parameters.h:109
const char * type() const
Definition: parameters.h:95
virtual bool isAlias() const
Definition: parameters.h:113
Parameter & operator=(const T &value)
Definition: parameters.h:75
static ParameterList operator+(ParameterList const &list1, ParameterList const &list2)
Definition: parameters.h:182
Parameter(const std::string &key)
Definition: parameters.h:49
AliasT(const ParameterPtr &other, const std::string &key)
Definition: parameters.h:130
ParameterList & add(const std::string &key, T *value)
Definition: parameters.h:153
const param_type & value() const
Definition: parameters.h:99
boost::function< void(ParameterPtr)> ParameterRegisterFunc
Definition: parameters.h:44
boost::remove_reference< typename boost::remove_const< T >::type >::type param_type
Definition: parameters.h:88
virtual ParameterPtr clone()
Definition: parameters.h:53
boost::shared_ptr< Parameter > ParameterPtr
Definition: parameters.h:41


hector_pose_estimation_core
Author(s): Johannes Meyer
autogenerated on Thu Feb 18 2021 03:29:30