MIRA
Object.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_OBJECT_H_
48 #define _MIRA_OBJECT_H_
49 
50 #include <string>
51 
53 // class which will be abused as compile time error message
54 template< class T >
55 class ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR1__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________
56 {
57  virtual int FOR_CLASS();
58  virtual ~________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR1__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________() {}
59 };
60 
61 // class which will be abused as compile time error message
62 template< class T >
63 class ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR2__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________
64 {
65  virtual int FOR_CLASS();
66  virtual ~________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR2__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________() {}
67 };
68 
69 // class which will be abused as compile time error message
70 template< class T >
71 class ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR3__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________
72 {
73  virtual int FOR_CLASS();
74  virtual ~________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR3__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________() {}
75 };
76 
77 // class which will be abused as compile time error message
78 template< class T >
79 class ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR4__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________
80 {
81  virtual int FOR_CLASS();
82  virtual ~________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR4__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________() {}
83 };
84 
86 
87 namespace mira {
88 
90 
91 class Class;
92 class Object;
93 
94 // *****************************************************************************
95 // *** PseudoClass
96 // *****************************************************************************
97 
104 {
105 public:
106  std::string const& getIdentifier() const
107  {
108  static std::string tIdent("mira::Object");
109  return tIdent;
110  }
111 
117  Object* newInstance( std::string const& childIdentifier ) const;
118 
126  template <class CLASS>
127  CLASS* newInstance( std::string const& childIdentifier ) const;
128 
129 };
130 
131 // *****************************************************************************
132 // *** Object
133 // *****************************************************************************
134 
144 class Object
145 {
146  friend class ClassFactory;
147 
148  template<typename Class, typename Base>
150 
151 public:
152  Object() = default;
153  Object(const Object&) = default;
154  Object(Object&&) = default;
155  Object& operator=(const Object&) = default;
156  Object& operator=(Object&&) = default;
157  virtual ~Object() = default;
158 
162  Class const& getClass() const {
163  return internalGetClass();
164  }
165 
166  static PseudoClass const& CLASS() {
167  static mira::PseudoClass sClass;
168  return sClass;
169  }
170 
171 protected:
172  virtual Class const& internalGetClass() const = 0;
173 
174 };
175 
176 // *****************************************************************************
177 // *** MACROS
178 // *****************************************************************************
179 
180 // the macros should be used to enable the class factory to use the special
181 // constructors
182 
183 #define MIRA_OBJECT_CONSTRUCTOR1(T, A0) \
184 template <> int \
185  ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR1__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________ \
186  <T>::FOR_CLASS() { return 0; } \
187 namespace mira { \
188  template<> \
189  T* mira::ClassFactoryDefaultConstClassBuilder::invoke<1,T>( std::va_list ap) { \
190  A0 a0 = va_arg(ap, A0); \
191  return new T(a0); \
192  } \
193 }
194 
195 #define MIRA_OBJECT_CONSTRUCTOR2(T, A0, A1) \
196 template <> int \
197  ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR2__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________ \
198  <T>::FOR_CLASS() { return 0; } \
199 namespace mira { \
200  template<> \
201  T* mira::ClassFactoryDefaultConstClassBuilder::invoke<2,T>( std::va_list ap) { \
202  A0 a0 = va_arg(ap, A0); \
203  A1 a1 = va_arg(ap, A1); \
204  return new T( a0, a1 ); \
205  } \
206 }
207 
208 #define MIRA_OBJECT_CONSTRUCTOR3(T, A0, A1, A2) \
209 template <> int \
210  ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR3__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________ \
211  <T>::FOR_CLASS() { return 0; } \
212 namespace mira { \
213  template<> \
214  T* mira::ClassFactoryDefaultConstClassBuilder::invoke<3,T>( std::va_list ap) { \
215  A0 a0 = va_arg(ap, A0); \
216  A1 a1 = va_arg(ap, A1); \
217  A2 a2 = va_arg(ap, A2); \
218  return new T( a0, a1, a2 ); \
219  } \
220 }
221 
222 #define MIRA_OBJECT_CONSTRUCTOR4(T, A0, A1, A2, A3) \
223 template <> int \
224  ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR4__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________ \
225  <T>::FOR_CLASS() { return 0; } \
226 namespace mira { \
227  template<> \
228  T* mira::ClassFactoryDefaultConstClassBuilder::invoke<4,T>( std::va_list ap) { \
229  A0 a0 = va_arg(ap, A0); \
230  A1 a1 = va_arg(ap, A1); \
231  A2 a2 = va_arg(ap, A2); \
232  A3 a3 = va_arg(ap, A3); \
233  return new T( a0, a1, a2, a3 ); \
234  } \
235 }
236 
238 
239 } // namespace
240 
241 #endif /* _MIRA_OBJECT_H_ */
242 
What should i say, the class factory.
Definition: Factory.h:86
virtual Class const & internalGetClass() const =0
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
The object class acts as a generic base class for classes which should be used with the classFactory...
Definition: Object.h:144
friend struct FactoryRegisterClassHelper
Definition: Object.h:149
json_spirit::mObject Object
A representation of an object (class, struct) in JSON.
Definition: JSON.h:181
virtual ~Object()=default
Class const & getClass() const
call the virtual internalGetClass().
Definition: Object.h:162
Object()=default
Object & operator=(const Object &)=default
This is an auxiliary to enable more verbose exception messages.
Definition: Object.h:103
std::string const & getIdentifier() const
Definition: Object.h:106
static PseudoClass const & CLASS()
Definition: Object.h:166
Object * newInstance(std::string const &childIdentifier) const
Return a new instance of the class with the given identifier.
Definition: Factory.h:330