source: rrlib_rtti/tParameterDefinition.h

tip
Last change on this file was 105:3daccc24cd47, checked in by Max Reichardt <mreichardt@…>, 2 years ago

Fixes small bugs

File size: 8.0 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
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    rrlib/rtti/tParameterDefinition.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2017-02-22
27 *
28 * \brief   Contains tParameterDefinition
29 *
30 * \b tParameterDefinition
31 *
32 * Generic parameter definition with name, data type, and possibly default value - fully serializable.
33 * Such generic parameters are required in generic parts of some RRLibs and Finroc.
34 * Related to generic objects, these parameter definitions fit reasonably well to rrlib_rtti - but could also be separated.
35 *
36 */
37//----------------------------------------------------------------------
38#ifndef __rrlib__rtti__tParameterDefinition_h__
39#define __rrlib__rtti__tParameterDefinition_h__
40
41//----------------------------------------------------------------------
42// External includes (system with <>, local with "")
43//----------------------------------------------------------------------
44#include "rrlib/util/tIteratorRange.h"
45
46//----------------------------------------------------------------------
47// Internal includes with ""
48//----------------------------------------------------------------------
49#include "rrlib/rtti/tGenericObject.h"
50
51//----------------------------------------------------------------------
52// Namespace declaration
53//----------------------------------------------------------------------
54namespace rrlib
55{
56namespace rtti
57{
58
59//----------------------------------------------------------------------
60// Forward declarations / typedefs / enums
61//----------------------------------------------------------------------
62
63//----------------------------------------------------------------------
64// Class declaration
65//----------------------------------------------------------------------
66//! Generic parameter definition
67/*!
68 * Generic parameter definition with name, data type, and possibly default value - fully serializable.
69 * Such generic parameters are required in generic parts of some RRLibs and Finroc.
70 * Related to generic objects, these parameter definitions fit reasonably well to rrlib_rtti - but could also be separated.
71 */
72class tParameterDefinition
73{
74
75//----------------------------------------------------------------------
76// Public methods and typedefs
77//----------------------------------------------------------------------
78public:
79
80  tParameterDefinition() : is_static(false)
81  {}
82
83  tParameterDefinition(const tParameterDefinition& other);
84
85  /*!
86   * \param name Name of parameter
87   * \param data_type Data type of parameter
88   * \param is_static Whether parameter is static (or constant) and may not be changed at application runtime
89   * \param copy_name Whether 'name' should be copied - or whether it can safely be referenced (can be false for all string literals)
90   */
91  tParameterDefinition(const char* name, const tType& data_type, bool is_static, bool copy_name = false) :
92    name(name, copy_name),
93    owned_default(),
94    type_and_default(nullptr, data_type),
95    is_static(is_static)
96  {}
97
98  /*!
99   * \param name Name of parameter
100   * \param default_value Default value of parameter
101   * \param is_static Whether parameter is static (or constant) and may not be changed at application runtime
102   * \param copy_name Whether 'name' should be copied - or whether it can safely be referenced (can be false for all string literals)
103   * \param copy_default_value Whether 'default_value' should be copied - or whether it can safely be referenced
104   */
105  tParameterDefinition(const char* name, const tTypedConstPointer& default_value, bool is_static, bool copy_name = false, bool copy_default_value = false) :
106    name(name, copy_name),
107    owned_default(copy_default_value ? default_value.GetType().CreateGenericObject() : nullptr),
108    type_and_default(copy_default_value ? owned_default->GetRawDataPointer() : default_value.GetRawDataPointer(), default_value.GetType()),
109    is_static(is_static)
110  {
111    if (owned_default)
112    {
113      owned_default->DeepCopyFrom(default_value);
114    }
115  }
116
117  /*!
118   * \param name Name of parameter
119   * \param default_value Default value of parameter (pointer to avoid ambiguities with other constructors)
120   * \param is_static Whether parameter is static (or constant) and may not be changed at application runtime
121   * \param copy_name Whether 'name' should be copied - or whether it can safely be referenced (can be false for all string literals)
122   * \param copy_default_value Whether 'default_value' should be copied - or whether it can safely be referenced
123   */
124  template <typename T>
125  tParameterDefinition(const char* name, const T* default_value, bool is_static, bool copy_name = false, bool copy_default_value = false) :
126    tParameterDefinition(name, tTypedConstPointer(default_value), copy_name, copy_default_value)
127  {
128  }
129
130  tParameterDefinition& operator=(const tParameterDefinition& other);
131
132  /*!
133   * \return Default value of parameter. May contain nullptr if no default value has been set.
134   */
135  const tTypedConstPointer& GetDefaultValue() const
136  {
137    return type_and_default;
138  }
139
140  /*!
141   * \return Name of parameter
142   */
143  const char* GetName() const
144  {
145    return name.Get();
146  }
147
148  /*!
149   * \return Data type of parameter
150   */
151  const tType& GetType() const
152  {
153    return type_and_default.GetType();
154  }
155
156  /*!
157   * \return False if this is an empty object with no parameter definition (e.g. created by default constructor)
158   */
159  bool IsDefined() const
160  {
161    return name.Get() != nullptr;
162  }
163
164  /*!
165   * \return Whether parameter is static (or constant) and may not be changed at application runtime
166   */
167  bool IsStatic() const
168  {
169    return is_static;
170  }
171
172  operator bool() const
173  {
174    return IsDefined();
175  }
176
177//----------------------------------------------------------------------
178// Private fields and methods
179//----------------------------------------------------------------------
180private:
181
182  /*! Name of parameter */
183  rrlib::util::tManagedConstCharPointer name;
184
185  /*! If default value was copied: Contains copy */
186  std::unique_ptr<tGenericObject> owned_default;
187
188  /*! Type and default value of parameter (nullptr if no default value is set) */
189  tTypedConstPointer type_and_default;
190
191  /*! Whether parameter is static (or constant) and may not be changed at application runtime */
192  bool is_static;
193};
194
195typedef rrlib::util::tIteratorRange<tParameterDefinition*> tParameterDefinitionRange;
196typedef rrlib::util::tIteratorRange<const tParameterDefinition*> tConstParameterDefinitionRange;
197
198serialization::tOutputStream& operator << (serialization::tOutputStream& stream, const tParameterDefinition& definition);
199serialization::tInputStream& operator >> (serialization::tInputStream& stream, tParameterDefinition& definition);
200serialization::tOutputStream& operator << (serialization::tOutputStream& stream, const tConstParameterDefinitionRange& definition_range);
201inline serialization::tOutputStream& operator << (serialization::tOutputStream& stream, const tParameterDefinitionRange& definition_range)
202{
203  stream << tConstParameterDefinitionRange(definition_range.Begin(), definition_range.End());
204  return stream;
205}
206
207//----------------------------------------------------------------------
208// End of namespace declaration
209//----------------------------------------------------------------------
210}
211}
212
213
214#endif
Note: See TracBrowser for help on using the repository browser.