MIRA
Accessor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
50 #ifndef _MIRA_ACCESSOR_H_
51 #define _MIRA_ACCESSOR_H_
52 
55 
56 namespace mira {
57 
59 
60 template<typename T> class AccessorGetterPartInternalRedirect;
61 
62 template <typename T, typename SerializerTag>
63 class IsTransparentSerializable<AccessorGetterPartInternalRedirect<T>,SerializerTag> : public std::true_type {};
64 
71 template<typename T>
73 {
74 public:
75  typedef T value_type;
76 
77  static const bool isDirectGetter = true;
78 
79 public:
80  AccessorGetterPart(const T& value) : internal(value) {}
81 
82 public:
83  const value_type& get() const {
84  return internal.get();
85  }
86 
87 public:
88  template<typename Reflector>
89  void reflectRead(Reflector& r) {
90  r.delegate(internal);
91  }
92 
93 private:
95 };
96 
105 template<typename T>
106 class AccessorGetterPartInternalRedirect
107 {
108 public:
109  typedef T value_type;
110 
111  static const bool isDirectGetter = true;
112 
113 public:
114  AccessorGetterPartInternalRedirect(const T& value) : getter((T*)&value) {}
115 
116 public:
117  const value_type& get() const {
118  return *getter;
119  }
120 
121 public:
122  template<typename Reflector>
123  void reflect(Reflector& r) {
124  r.delegate(*getter);
125  }
126 
127 private:
128  T* getter; // for reading
129 };
130 
131 //specialization for getters
132 template <typename T>
134 {
135 public:
136  typedef T value_type;
137 
138  static const bool isDirectGetter = false;
139 
140 public:
141  AccessorGetterPart(const Getter<T>& iGetter) : getter(iGetter) {}
142 
143 public:
144  value_type get() const {
145  return getter.get();
146  }
147 
148 public:
149  template<typename Reflector>
150  void reflectRead(Reflector& r) {
151  r.delegate(getter);
152  }
153 
154 private:
155  Getter<T> getter; // for reading
156 };
157 
159 
165 template<typename T>
167  static_assert(sizeof(T)==0, "Not implemented for direct access. "
168  "There was no need yet");
169 };
170 
171 // specialization for setters
172 template<typename T>
174 {
175 public:
176  typedef T value_type;
177 
178 public:
179  AccessorSetterPart(const Setter<T>& iSetter) : setter(iSetter) {}
180 
181 public:
182  void set(const value_type& value) {
183  setter.set(value);
184  }
185 
186 public:
187  template<typename Reflector>
188  void reflectWrite(Reflector& r) {
189  r.delegate(setter);
190  }
191 
192 private:
193  Setter<T> setter; // for writing
194 };
195 
197 template <typename T>
198 class NullSetter {};
199 
200 // specialization for "Null"-Setter, that does nothing
201 template<typename T>
203 {
204 public:
205  typedef T value_type;
206 
207 public:
210 
211 public:
212  void set(const value_type& value) {}
213 
214 public:
215  template<typename Reflector>
216  void reflectWrite(Reflector& r) {
217  }
218 };
219 
220 
222 
243 template <typename Getter, typename Setter>
244 class Accessor : public AccessorGetterPart<Getter>,
245  public AccessorSetterPart<Setter>
246 {
249 
250 public:
251  Accessor(const Getter& getter, const Setter& setter) :
253 
255  AccessorGetterPart<Getter>(getter) {} // for null setter
256 
257 public:
258 
259  static_assert(std::is_same<typename GetterPart::value_type,
260  typename SetterPart::value_type>::value,
261  "Type of value in Getter and Setter must be the same");
262 
264  typedef typename GetterPart::value_type value_type;
265 
267  typedef decltype(std::declval<GetterPart>().get()) GetReturnValueType;
268 
270  Accessor& operator=(const value_type& value) {
271  this->set(value);
272  return *this;
273  }
274 
279  operator GetReturnValueType() const { return this->get(); }
280 
281 public:
282 
284  // reflectRead and reflectWrite are implemented in AccessorGetterPart and
285  // AccessorSetterPart.
286 };
287 
288 // mark the Accessor as transparent serializable (internal detail)
289 template <typename Getter, typename Setter, typename SerializerTag>
290 class IsTransparentSerializable<Accessor<Getter,Setter>,SerializerTag> : public std::true_type {};
291 
293 
299 template <typename Getter, typename Setter>
302 }
303 
305 
306 } // namespace
307 
308 #endif
AccessorSetterPart(const NullSetter< T > &)
Definition: Accessor.h:209
AccessorSetterPart(const Setter< T > &iSetter)
Definition: Accessor.h:179
Type trait that indicates whether a type should be serialized "transparently", i.e.
Definition: IsTransparentSerializable.h:81
AccessorSetterPart()
Definition: Accessor.h:208
Accessor(const Getter &getter)
Definition: Accessor.h:254
void reflect(Reflector &r)
Definition: Accessor.h:123
T value_type
Definition: Accessor.h:75
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
#define MIRA_SPLIT_REFLECT_MEMBER
Macro that insert a class member reflect() method just splitting reflection into a reflectRead() and ...
Definition: SplitReflect.h:209
Setter< T > setter(void(*f)(const T &))
Creates a Setter for global or static class methods taking the argument by const reference.
Definition: GetterSetter.h:443
static const bool isDirectGetter
Definition: Accessor.h:77
decltype(std::declval< GetterPart >().get()) typedef GetReturnValueType
the type that is returned by the get() method of the GetterPart.
Definition: Accessor.h:267
Holds a boost::function object to a special setter function that must meet the signature "void method...
Definition: GetterSetter.h:395
T value_type
Definition: Accessor.h:176
Provides MIRA_SPLIT_REFLECT macros.
T value_type
Definition: Accessor.h:205
GetterPart::value_type value_type
the underlying type (as STL conform typedef)
Definition: Accessor.h:261
Implements the "setter part" of an Accessor.
Definition: Accessor.h:166
T value_type
Definition: Accessor.h:109
Getter< T > getter(T(*f)())
Creates a Getter for global or static class methods returning the result by value.
Definition: GetterSetter.h:136
void reflectWrite(Reflector &r)
Definition: Accessor.h:188
AccessorGetterPartInternalRedirect(const T &value)
Definition: Accessor.h:114
This class is used as additional reflection redirect.
Definition: Accessor.h:60
void reflectRead(Reflector &r)
Definition: Accessor.h:89
void reflectWrite(Reflector &r)
Definition: Accessor.h:216
The Accessor class is used as an adapter to reduce the code bloat within the reflection and serializa...
Definition: Accessor.h:244
static const bool isDirectGetter
Definition: Accessor.h:111
Holds a boost::function object to a special getter function that must meet the signature "T method()"...
Definition: GetterSetter.h:87
AccessorGetterPart(const Getter< T > &iGetter)
Definition: Accessor.h:141
AccessorGetterPart(const T &value)
Definition: Accessor.h:80
Accessor(const Getter &getter, const Setter &setter)
Definition: Accessor.h:251
value_type get() const
apply the getter, e.g. int val = mygetter.get();
Definition: GetterSetter.h:98
"Null-Setter" tag-class where the AccessorSetterPart does nothing.
Definition: Accessor.h:198
T value_type
Definition: Accessor.h:136
Accessor< Getter, Setter > makeAccessor(const Getter &getter, const Setter &setter)
Helper method that creates an accessor from a different combination of either direct access to a vari...
Definition: Accessor.h:300
Implements the "getter part" of an Accessor.
Definition: Accessor.h:72
void reflectRead(Reflector &r)
Definition: Accessor.h:150
Provides definition for getters and setters that are used with the serialization framework.