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 |
---|
8 | // modify it under the terms of the GNU General Public License |
---|
9 | // as published by the Free Software Foundation; either version 2 |
---|
10 | // of the License, or (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 |
---|
18 | // along with this program; if not, write to the Free Software |
---|
19 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | // |
---|
21 | //---------------------------------------------------------------------- |
---|
22 | /*!\file plugins/data_ports/tPortCreationInfo.h |
---|
23 | * |
---|
24 | * \author Max Reichardt |
---|
25 | * |
---|
26 | * \date 2012-10-24 |
---|
27 | * |
---|
28 | * \brief Contains tPortCreationInfo |
---|
29 | * |
---|
30 | * \b tPortCreationInfo |
---|
31 | * |
---|
32 | * This class contains various information for the creation of ports. |
---|
33 | * |
---|
34 | */ |
---|
35 | //---------------------------------------------------------------------- |
---|
36 | #ifndef __plugins__data_ports__tPortCreationInfo_h__ |
---|
37 | #define __plugins__data_ports__tPortCreationInfo_h__ |
---|
38 | |
---|
39 | //---------------------------------------------------------------------- |
---|
40 | // External includes (system with <>, local with "") |
---|
41 | //---------------------------------------------------------------------- |
---|
42 | #include "core/port/tPortWrapperBase.h" |
---|
43 | |
---|
44 | //---------------------------------------------------------------------- |
---|
45 | // Internal includes with "" |
---|
46 | //---------------------------------------------------------------------- |
---|
47 | #include "plugins/data_ports/common/tAbstractDataPortCreationInfo.h" |
---|
48 | |
---|
49 | //---------------------------------------------------------------------- |
---|
50 | // Namespace declaration |
---|
51 | //---------------------------------------------------------------------- |
---|
52 | namespace finroc |
---|
53 | { |
---|
54 | namespace data_ports |
---|
55 | { |
---|
56 | |
---|
57 | //---------------------------------------------------------------------- |
---|
58 | // Forward declarations / typedefs / enums |
---|
59 | //---------------------------------------------------------------------- |
---|
60 | |
---|
61 | //---------------------------------------------------------------------- |
---|
62 | // Class declaration |
---|
63 | //---------------------------------------------------------------------- |
---|
64 | //! Bundle of port creation parameters |
---|
65 | /*! |
---|
66 | * This class bundles various parameters for the creation of data ports. |
---|
67 | * |
---|
68 | * Instead of providing suitable constructors for all types of sensible |
---|
69 | * combinations of the numerous (often optional) construction parameters, |
---|
70 | * there is only one constructor taking a single argument of this class. |
---|
71 | */ |
---|
72 | template <typename T> |
---|
73 | class tPortCreationInfo : public common::tAbstractDataPortCreationInfo |
---|
74 | { |
---|
75 | enum { cBOUNDABLE = tIsBoundable<T>::value }; |
---|
76 | |
---|
77 | //---------------------------------------------------------------------- |
---|
78 | // Public methods and typedefs |
---|
79 | //---------------------------------------------------------------------- |
---|
80 | public: |
---|
81 | |
---|
82 | /*! Base class */ |
---|
83 | typedef common::tAbstractDataPortCreationInfo tBase; |
---|
84 | |
---|
85 | /*! |
---|
86 | * \return Bounds for port |
---|
87 | */ |
---|
88 | template < bool AVAILABLE = cBOUNDABLE > |
---|
89 | typename std::enable_if<AVAILABLE, tBounds<T>>::type GetBounds() const |
---|
90 | { |
---|
91 | T t = T(); |
---|
92 | tBounds<T> result(t, t); |
---|
93 | if (!BoundsSet()) |
---|
94 | { |
---|
95 | FINROC_LOG_PRINT_STATIC(DEBUG_WARNING, "Bounds were not set"); |
---|
96 | return result; |
---|
97 | } |
---|
98 | rrlib::serialization::tInputStream is(bounds); |
---|
99 | is >> result; |
---|
100 | return result; |
---|
101 | } |
---|
102 | |
---|
103 | /*! |
---|
104 | * \return Default value |
---|
105 | */ |
---|
106 | T GetDefault() const |
---|
107 | { |
---|
108 | T t = T(); |
---|
109 | GetDefault(t); |
---|
110 | return t; |
---|
111 | } |
---|
112 | |
---|
113 | /*! |
---|
114 | * \param buffer Buffer to store result in |
---|
115 | */ |
---|
116 | void GetDefault(T& buffer) const |
---|
117 | { |
---|
118 | if (!DefaultValueSet()) |
---|
119 | { |
---|
120 | FINROC_LOG_PRINT_STATIC(DEBUG_WARNING, "Default value was not set"); |
---|
121 | return; |
---|
122 | } |
---|
123 | rrlib::serialization::tInputStream is(default_value); |
---|
124 | is >> buffer; |
---|
125 | } |
---|
126 | |
---|
127 | /*! Various Set methods for different port properties */ |
---|
128 | template < bool DISABLE = tIsString<T>::value > |
---|
129 | void Set(const typename std::enable_if < !DISABLE, T >::type& default_value) |
---|
130 | { |
---|
131 | SetDefault(default_value); |
---|
132 | } |
---|
133 | |
---|
134 | template < bool AVAILABLE = cBOUNDABLE > |
---|
135 | void Set(const typename std::enable_if<AVAILABLE, tBounds<T>>::type& bounds) |
---|
136 | { |
---|
137 | rrlib::serialization::tOutputStream os(this->bounds); |
---|
138 | os << bounds; |
---|
139 | } |
---|
140 | |
---|
141 | void Set(const tPortCreationInfo& other) |
---|
142 | { |
---|
143 | *this = other; |
---|
144 | } |
---|
145 | |
---|
146 | void Set(const std::string& string) |
---|
147 | { |
---|
148 | SetString(string); |
---|
149 | } |
---|
150 | |
---|
151 | // we replicate this here, since Set() for default values catches pointers if T is bool |
---|
152 | void Set(core::tFrameworkElement* parent) |
---|
153 | { |
---|
154 | this->parent = parent; |
---|
155 | } |
---|
156 | void Set(const char* string) |
---|
157 | { |
---|
158 | SetString(string); |
---|
159 | } |
---|
160 | |
---|
161 | //---------------------------------------------------------------------- |
---|
162 | // Private fields and methods |
---|
163 | //---------------------------------------------------------------------- |
---|
164 | private: |
---|
165 | |
---|
166 | template < bool STRING = tIsString<T>::value > |
---|
167 | void SetString(const typename std::enable_if < !STRING, tString >::type& s) |
---|
168 | { |
---|
169 | common::tAbstractDataPortCreationInfo::SetString(s); |
---|
170 | } |
---|
171 | |
---|
172 | template < bool STRING = tIsString<T>::value > |
---|
173 | void SetString(const typename std::enable_if<STRING, tString>::type& s) |
---|
174 | { |
---|
175 | if (!name_set) |
---|
176 | { |
---|
177 | name = s; |
---|
178 | name_set = true; |
---|
179 | } |
---|
180 | else if (!DefaultValueSet()) |
---|
181 | { |
---|
182 | SetDefault(s); |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | config_entry = s; |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | void SetDefault(const T& default_val) |
---|
191 | { |
---|
192 | if (DefaultValueSet()) |
---|
193 | { |
---|
194 | FINROC_LOG_PRINT_STATIC(DEBUG_WARNING, "Default value already set"); |
---|
195 | } |
---|
196 | rrlib::serialization::tOutputStream os(default_value); |
---|
197 | os << default_val; |
---|
198 | } |
---|
199 | }; |
---|
200 | |
---|
201 | //---------------------------------------------------------------------- |
---|
202 | // End of namespace declaration |
---|
203 | //---------------------------------------------------------------------- |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | #endif |
---|