MIRA
SplitReflect.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 
47 #ifndef _MIRA_SPLITREFLECT_H_
48 #define _MIRA_SPLITREFLECT_H_
49 
50 #include <platform/Typename.h>
52 
53 namespace mira {
54 
55 template <typename Derived>
56 class ReflectorInterface;
57 
59 
61 // default
62 template<typename DerivedReflector, typename T>
64 {
65  // #########################################################################
66  // If you get a compiler error here, you used the splitReflect() method
67  // or the MIRA_SPLIT_REFLECT macro, but forgot to specialize and implement
68  // the reflectRead() and reflectWrite() methods for your type. Additionally,
69  // make sure, that these functions are placed within the namespace "mira".
70  // #########################################################################
71  static_assert(sizeof(T)==0, "You must implement reflectRead() "
72  "if you use the MIRA_SPLIT_REFLECT macro");
73 }
74 
75 template<typename DerivedReflector, typename T>
77 {
78  // #########################################################################
79  // If you get a compiler error here, you used the splitReflect() method
80  // or the MIRA_SPLIT_REFLECT macro, but forgot to specialize and implement
81  // the reflectRead() and reflectWrite() methods for your type. Additionally,
82  // make sure, that these functions are placed within the namespace "mira".
83  // #########################################################################
84  static_assert(sizeof(T)==0, "You must implement reflectWrite() "
85  "if you use the MIRA_SPLIT_REFLECT macro");
86 }
87 
99 template<typename Reflector, typename T>
100 inline void splitReflect(mira::ReflectorInterface<Reflector>& r, T& value)
101 {
102  auto& reflector = static_cast<Reflector&>(r);
103 
104  /* proper use of reflect barrier enables to e.g. potentially add a version to
105  the splitting mechanism itself (--> in splitReflectMemberInvoke()) */
106  static const std::string context = typeName<T>() + " splitReflect";
107 
108  if constexpr (Reflector::isReadOnly::value) {
109  auto& reflector = static_cast<Reflector&>(r);
110  MIRA_REFLECT_CALL(Reflector, reflector, context.c_str(),
111  (reflectRead(reflector, value)));
112  }
113  else{
114  MIRA_REFLECT_CALL(Reflector, reflector, context.c_str(),
115  (reflectWrite(reflector, value)));
116  }
117 }
119 
130 #define MIRA_SPLIT_REFLECT(Type) \
131  template<typename Reflector> \
132  void reflect(mira::ReflectorInterface<Reflector>& r, Type& value) \
133  { \
134  splitReflect(r, value); \
135  }
136 
138 
139 template<typename Reflector, typename Class>
140 inline void splitReflectMemberInvoke(Reflector& r, Class* This)
141 {
142  if constexpr (Reflector::isReadOnly::value) {
143  This->reflectRead(r);
144  }
145  else {
146  This->reflectWrite(r);
147  }
148 }
149 
150 
162 template<typename Reflector, typename Class>
164 {
165  auto& reflector = static_cast<Reflector&>(r);
166 
167  /* proper use of reflect barrier enables to e.g.
168  potentially add a version to the splitting mechanism
169  itself (--> in splitReflectMemberInvoke()) */
170 
171  static const std::string context = typeName<Class>() + " splitReflectMember";
172 
173  MIRA_REFLECT_CALL(Reflector, reflector, context.c_str(),
174  (splitReflectMemberInvoke(reflector, This)));
175 }
176 
189 #define MIRA_SPLIT_REFLECT_MEMBER \
190  template<typename Reflector> \
191  void reflect(mira::ReflectorInterface<Reflector>& r) \
192  { \
193  Reflector& reflector = static_cast<Reflector&>(r); \
194  \
195  /* proper use of reflect barrier enables to e.g. \
196  potentially add a version to the splitting mechanism \
197  itself (--> in splitReflectMemberInvoke()) */ \
198  \
199  typedef typename std::remove_reference<decltype(*this)>::type splitType; \
200  \
201  static const std::string context = typeName<splitType>() + " MIRA_SPLIT_REFLECT_MEMBER"; \
202  \
203  MIRA_REFLECT_CALL(Reflector, reflector, context.c_str(), \
204  (splitReflectMemberInvoke(reflector, this))); \
205  }
206 
208 
209 } // namespace
210 
211 #endif /* _MIRA_SPLITREFLECT_H_ */
void reflectWrite(Reflector &r, Buffer< T, Allocator > &c)
Specialization of the non-intrusive reflect for Buffer.
Definition: Buffer.h:577
void reflectWrite(Reflector &r)
Implementation of class member reflection.
Definition: Class.h:762
Macros for use with reflectors.
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Class object which supports some kind of class reflection.
Definition: Class.h:97
void splitReflectMemberInvoke(Reflector &r, Class *This)
Definition: SplitReflect.h:140
Get compiler and platform independent typenames.
void splitReflectMember(mira::ReflectorInterface< Reflector > &r, Class *This)
This method is used to split reflection into a reflectRead() and reflectWrite() method.
Definition: SplitReflect.h:163
This is the public interface of all reflectors that are able to visit a class&#39; reflect() method...
Definition: ReflectorInterface.h:111
MIRA_SPLIT_REFLECT_MEMBER void reflectRead(Reflector &r)
Implementation of class member reflection.
#define MIRA_REFLECT_CALL(ReflectorType, reflector, context, COMMAND)
Whenever a reflection function calls another function that is independently maintained, the call should be marked to the reflector.
Definition: ReflectorMacros.h:109
void reflectRead(Reflector &r, Buffer< T, Allocator > &c)
Specialization of the non-intrusive reflect for Buffer.
Definition: Buffer.h:561