source: finroc_plugins_parameters/tParameter.h @ 78:df42a6d9c51d

17.03
Last change on this file since 78:df42a6d9c51d was 78:df42a6d9c51d, checked in by Max Reichardt <mreichardt@…>, 4 years ago

Merged with 14.08 from Robot Makers

File size: 9.4 KB
Line 
1//
2// You received this file as part of Finroc
3// A framework for intelligent robot control
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    plugins/parameters/tParameter.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2012-11-28
27 *
28 * \brief   Contains tParameter
29 *
30 * \b tParameter
31 *
32 * Parameter that can be changed at application runtime.
33 * To deal with issues of concurrency, it is based on tPort.
34 * Parameter values can be set in code, loaded from configuration
35 * files or specified via the command line if set up accordingly.
36 *
37 */
38//----------------------------------------------------------------------
39#ifndef __plugins__parameters__tParameter_h__
40#define __plugins__parameters__tParameter_h__
41
42//----------------------------------------------------------------------
43// External includes (system with <>, local with "")
44//----------------------------------------------------------------------
45
46//----------------------------------------------------------------------
47// Internal includes with ""
48//----------------------------------------------------------------------
49#include "plugins/parameters/internal/tParameterCreationInfo.h"
50#include "plugins/parameters/internal/tParameterImplementation.h"
51#include "plugins/parameters/internal/tParameterInfo.h"
52
53//----------------------------------------------------------------------
54// Namespace declaration
55//----------------------------------------------------------------------
56namespace finroc
57{
58namespace parameters
59{
60
61//----------------------------------------------------------------------
62// Forward declarations / typedefs / enums
63//----------------------------------------------------------------------
64
65//----------------------------------------------------------------------
66// Class declaration
67//----------------------------------------------------------------------
68//! Parameter
69/*!
70 * Parameter that can be changed at application runtime.
71 * To deal with issues of concurrency, it is based on tPort.
72 * Parameter values can be set in code, loaded from configuration
73 * files or specified via the command line if set up accordingly.
74 */
75template <typename T>
76class tParameter
77{
78  typedef internal::tParameterImplementation < T, std::is_arithmetic<T>::value || std::is_same<bool, T>::value > tImplementation;
79
80  typedef data_ports::api::tPortImplementation<T, data_ports::api::tPortImplementationTypeTrait<T>::type> tPortImplementation;
81
82//----------------------------------------------------------------------
83// Public methods and typedefs
84//----------------------------------------------------------------------
85public:
86
87  /*! Should methods passing buffers by-value be available? */
88  enum { cPASS_BY_VALUE = data_ports::tIsCheaplyCopiedType<T>::value };
89
90  /*! Type T */
91  typedef T tDataType;
92
93  /*! Bundles all possible constructor parameters of tParameter */
94  typedef internal::tParameterCreationInfo<T> tConstructorParameters;
95
96  /*! Creates no wrapped parameter */
97  tParameter() : implementation()
98  {}
99
100  /*!
101   * Constructor takes variadic argument list... just any properties you want to assign to parameter.
102   *
103   * The first string is interpreted as parameter name, the second possibly as config entry.
104   * A framework element pointer is interpreted as parent.
105   * tFrameworkElement::tFlags arguments are interpreted as flags.
106   * tBounds<T> are parameter's bounds.
107   * const T& is interpreted as port's default value.
108   * tParameterCreationInfo<T> argument is copied.
109   *
110   * This becomes a little tricky when parameter has numeric or string type.
111   * There we have these rules:
112   *
113   * string type: The second string argument is interpreted as default_value. The third as config entry.
114   * numeric type: The first numeric argument is interpreted as default_value.
115   */
116  template<typename ... ARGS>
117  tParameter(const ARGS&... args) : implementation()
118  {
119    core::tPortWrapperBase::tConstructorArguments<data_ports::tPortCreationInfo<T>> creation_info(args...);
120    if (!(creation_info.flags.Raw() & core::tFrameworkElementFlags(core::tFrameworkElementFlag::DELETED).Raw())) // do not create parameter, if deleted flag is set
121    {
122      implementation = tImplementation(creation_info);
123      implementation.GetWrapped()->AddAnnotation(*(new internal::tParameterInfo()));
124      SetConfigEntry(creation_info.config_entry);
125    }
126  }
127
128  /*!
129   * \param listener Listener to add (see tInputPort.h)
130   */
131  template <typename TListener>
132  void AddListener(TListener& listener)
133  {
134    implementation.AddPortListener(listener);
135  }
136  template <typename TListener>
137  void AddListenerSimple(TListener& listener)
138  {
139    implementation.AddPortListenerSimple(listener);
140  }
141
142  /*!
143   * Gets parameter's current value.
144   * (only available for 'cheaply copied' types)
145   *
146   * \param v unused dummy parameter for std::enable_if technique
147   * \return Parameter's current value by-value.
148   */
149  template <bool AVAILABLE = cPASS_BY_VALUE>
150  inline T Get(typename std::enable_if<AVAILABLE, void>::type* v = NULL) const
151  {
152    return implementation.Get();
153  }
154
155  /*!
156   * Gets parameter's current value
157   *
158   * (Using this Get()-variant is efficient when using 'cheaply copied' types,
159   * but can be extremely costly with large data types)
160   *
161   * \param result Buffer to (deep) copy parameter's current value to
162   */
163  inline void Get(T& result) const
164  {
165    return implementation.Get(result);
166  }
167
168  /*!
169   * (throws a std::runtime_error if parameter is not bounded)
170   *
171   * \return Bounds as they are currently set
172   */
173  template <bool AVAILABLE = data_ports::IsBoundable<T>::value>
174  inline typename std::enable_if<AVAILABLE, data_ports::tBounds<T>>::type GetBounds() const
175  {
176    return implementation.GetBounds();
177  }
178
179  /*!
180   * \return Place in configuration file this parameter is configured from (nodes are separated with '/')
181   */
182  std::string GetConfigEntry()
183  {
184    internal::tParameterInfo* parameter_info = implementation.GetWrapped()->template GetAnnotation<internal::tParameterInfo>();
185    return parameter_info->GetConfigEntry();
186  }
187
188  /*!
189   * \return Name of wrapped framework element (see tFrameworkElement::GetName())
190   */
191  inline const tString& GetName() const
192  {
193    return implementation.GetName();
194  }
195
196  /*!
197   * Gets Port's current value in buffer
198   *
199   * \return Buffer with port's current value with read lock.
200   */
201  inline data_ports::tPortDataPointer<const T> GetPointer() const
202  {
203    return implementation.GetPointer();
204  }
205
206  /*!
207   * \return Wrapped port. For rare case that someone really needs to access ports.
208   */
209  inline typename tImplementation::tPortBackend* GetWrapped() const
210  {
211    return implementation.GetWrapped();
212  }
213
214  /*!
215   * \return Has parameter changed since last changed-flag-reset?
216   */
217  inline bool HasChanged() const
218  {
219    return implementation.HasChanged();
220  }
221
222  /*!
223   * Initialize this parameter.
224   * This must be called prior to using parameter.
225   *
226   * For parameters created in e.g. component constructor, this is done automatically.
227   * For parameters created dynamically, this usually needs to be called.
228   */
229  inline void Init()
230  {
231    implementation.Init();
232  }
233
234  /*!
235   * Deletes port wrapped by this class.
236   * After calling this method, this object no longer wraps any object.
237   * Unless a new object is assigned, only GetWrapped() may called.
238   */
239  void ManagedDelete()
240  {
241    implementation.ManagedDelete();
242  }
243
244  /*!
245   * Reset changed flag.
246   */
247  inline void ResetChanged()
248  {
249    implementation.ResetChanged();
250  }
251
252  /*!
253   * \param new_value New value of parameter
254   */
255  void Set(const T& new_value)
256  {
257    tPortImplementation::BrowserPublish(*implementation.GetWrapped(), new_value, rrlib::time::cNO_TIME);
258  }
259
260  /*!
261   * \param config_entry New place in configuration file this parameter is configured from (nodes are separated with '/')
262   */
263  void SetConfigEntry(const std::string& config_entry)
264  {
265    internal::tParameterInfo* parameter_info = implementation.GetWrapped()->template GetAnnotation<internal::tParameterInfo>();
266    parameter_info->SetConfigEntry(config_entry);
267  }
268
269//----------------------------------------------------------------------
270// Private fields and methods
271//----------------------------------------------------------------------
272private:
273
274  /*! Parameter implementation */
275  tImplementation implementation;
276};
277
278extern template class tParameter<int>;
279extern template class tParameter<long long int>;
280extern template class tParameter<float>;
281extern template class tParameter<double>;
282extern template class tParameter<std::string>;
283extern template class tParameter<bool>;
284
285//----------------------------------------------------------------------
286// End of namespace declaration
287//----------------------------------------------------------------------
288}
289}
290
291
292#endif
Note: See TracBrowser for help on using the repository browser.