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  virtual ~Object()
153  {
154  }
155 
159  Class const& getClass() const {
160  return internalGetClass();
161  }
162 
163  static PseudoClass const& CLASS() {
164  static mira::PseudoClass sClass;
165  return sClass;
166  }
167 
168 protected:
169  virtual Class const& internalGetClass() const = 0;
170 
171 };
172 
173 // *****************************************************************************
174 // *** MACROS
175 // *****************************************************************************
176 
177 // the macros should be used to enable the class factory to use the special
178 // constructors
179 
180 #define MIRA_OBJECT_CONSTRUCTOR1(T, A0) \
181 template <> int \
182  ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR1__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________ \
183  <T>::FOR_CLASS() { return 0; } \
184 namespace mira { \
185  template<> \
186  T* mira::ClassFactoryDefaultConstClassBuilder::invoke<1,T>( std::va_list ap) { \
187  A0 a0 = va_arg(ap, A0); \
188  return new T(a0); \
189  } \
190 }
191 
192 #define MIRA_OBJECT_CONSTRUCTOR2(T, A0, A1) \
193 template <> int \
194  ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR2__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________ \
195  <T>::FOR_CLASS() { return 0; } \
196 namespace mira { \
197  template<> \
198  T* mira::ClassFactoryDefaultConstClassBuilder::invoke<2,T>( std::va_list ap) { \
199  A0 a0 = va_arg(ap, A0); \
200  A1 a1 = va_arg(ap, A1); \
201  return new T( a0, a1 ); \
202  } \
203 }
204 
205 #define MIRA_OBJECT_CONSTRUCTOR3(T, A0, A1, A2) \
206 template <> int \
207  ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR3__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________ \
208  <T>::FOR_CLASS() { return 0; } \
209 namespace mira { \
210  template<> \
211  T* mira::ClassFactoryDefaultConstClassBuilder::invoke<3,T>( std::va_list ap) { \
212  A0 a0 = va_arg(ap, A0); \
213  A1 a1 = va_arg(ap, A1); \
214  A2 a2 = va_arg(ap, A2); \
215  return new T( a0, a1, a2 ); \
216  } \
217 }
218 
219 #define MIRA_OBJECT_CONSTRUCTOR4(T, A0, A1, A2, A3) \
220 template <> int \
221  ________________________________PLEASE_USE_THE__MIRA_OBJECT_CONSTRUCTOR4__MACRO_IN_GLOBAL_NAMESPACE_ONLY________________________________ \
222  <T>::FOR_CLASS() { return 0; } \
223 namespace mira { \
224  template<> \
225  T* mira::ClassFactoryDefaultConstClassBuilder::invoke<4,T>( std::va_list ap) { \
226  A0 a0 = va_arg(ap, A0); \
227  A1 a1 = va_arg(ap, A1); \
228  A2 a2 = va_arg(ap, A2); \
229  A3 a3 = va_arg(ap, A3); \
230  return new T( a0, a1, a2, a3 ); \
231  } \
232 }
233 
235 
236 } // namespace
237 
238 #endif /* _MIRA_OBJECT_H_ */
239 
What should i say, the class factory.
Definition: Factory.h:88
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:183
Class const & getClass() const
call the virtual internalGetClass().
Definition: Object.h:159
virtual ~Object()
Definition: Object.h:152
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:163
Object * newInstance(std::string const &childIdentifier) const
Return a new instance of the class with the given identifier.
Definition: Factory.h:332