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 
88 struct GlobalReflectRead {
89  template<typename Reflector, typename T>
90  static void invoke(mira::ReflectorInterface<Reflector>& r, T& value) {
91  auto& reflector = static_cast<Reflector&>(r);
92  reflectRead(reflector, value);
93  }
94 };
95 
96 struct GlobalReflectWrite {
97  template<typename Reflector, typename T>
98  static void invoke(Reflector& r, T& value) {
99  reflectWrite(r, value);
100  }
101 };
102 
103 template<typename GlobalReflectReadOrWrite, typename Reflector, typename T>
104 inline void splitReflectInvoke(Reflector& r, T& value)
105 {
106  GlobalReflectReadOrWrite::invoke(r, value);
107 }
108 
120 template<typename Reflector, typename T>
121 inline void splitReflect(mira::ReflectorInterface<Reflector>& r, T& value)
122 {
123  auto& reflector = static_cast<Reflector&>(r);
124 
125  /* proper use of reflect barrier enables to e.g. potentially add a version to
126  the splitting mechanism itself (--> in splitReflectMemberInvoke()) */
127  static const std::string context = typeName<T>() + " splitReflect";
128 
129  if constexpr (Reflector::isReadOnly::value) {
130  MIRA_REFLECT_CALL(Reflector, reflector, context.c_str(),
131  (splitReflectInvoke<GlobalReflectRead>(reflector, value)));
132  }
133  else{
134  MIRA_REFLECT_CALL(Reflector, reflector, context.c_str(),
135  (splitReflectInvoke<GlobalReflectWrite>(reflector, value)));
136  }
137 }
139 
150 #define MIRA_SPLIT_REFLECT(Type) \
151  template<typename Reflector> \
152  void reflect(mira::ReflectorInterface<Reflector>& r, Type& value) \
153  { \
154  splitReflect(r, value); \
155  }
156 
157 template<typename Reflector, typename Class>
158 inline void splitReflectMemberInvoke(Reflector& r, Class* This)
159 {
160  if constexpr (Reflector::isReadOnly::value) {
161  This->reflectRead(r);
162  }
163  else {
164  This->reflectWrite(r);
165  }
166 }
167 
168 
170 
182 template<typename Reflector, typename Class>
184 {
185  auto& reflector = static_cast<Reflector&>(r);
186 
187  /* proper use of reflect barrier enables to e.g.
188  potentially add a version to the splitting mechanism
189  itself (--> in splitReflectMemberInvoke()) */
190 
191  static const std::string context = typeName<Class>() + " splitReflectMember";
192 
193  MIRA_REFLECT_CALL(Reflector, reflector, context.c_str(),
194  (splitReflectMemberInvoke(reflector, This)));
195 }
196 
209 #define MIRA_SPLIT_REFLECT_MEMBER \
210  template<typename Reflector> \
211  void reflect(mira::ReflectorInterface<Reflector>& r) \
212  { \
213  Reflector& reflector = static_cast<Reflector&>(r); \
214  \
215  /* proper use of reflect barrier enables to e.g. \
216  potentially add a version to the splitting mechanism \
217  itself (--> in splitReflectMemberInvoke()) */ \
218  \
219  typedef typename std::remove_reference<decltype(*this)>::type splitType; \
220  \
221  static const std::string context = typeName<splitType>() + " MIRA_SPLIT_REFLECT_MEMBER"; \
222  \
223  MIRA_REFLECT_CALL(Reflector, reflector, context.c_str(), \
224  (splitReflectMemberInvoke(reflector, this))); \
225  }
226 
228 
229 } // namespace
230 
231 #endif /* _MIRA_SPLITREFLECT_H_ */
void reflectWrite(Reflector &r, Buffer< T, Allocator > &c)
Specialization of the non-intrusive reflect for Buffer.
Definition: Buffer.h:581
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:158
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:183
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:565