MetaClass.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011, SRI International (R)
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #ifndef __OpenKarto_MetaClass_h__
21 #define __OpenKarto_MetaClass_h__
22 
23 #include <OpenKarto/MetaType.h>
26 #include <OpenKarto/Referenced.h>
27 #include <OpenKarto/List.h>
28 
29 namespace karto
30 {
31 
33 
34 
35  //@cond EXCLUDE
36 
40 
41  struct MetaArgsPrivate;
42 
46  class KARTO_EXPORT MetaArguments
47  {
48  public:
52  MetaArguments();
53 
58  MetaArguments(const Any& a0);
59 
65  MetaArguments(const Any& a0, const Any& a1);
66 
73  MetaArguments(const Any& a0, const Any& a1, const Any& a2);
74 
82  MetaArguments(const Any& a0, const Any& a1, const Any& a2, const Any& a3);
83 
92  MetaArguments(const Any& a0, const Any& a1, const Any& a2, const Any& a3, const Any& a4);
93 
97  ~MetaArguments();
98 
99  public:
103  kt_size_t GetCount() const;
104 
105  public:
111  const Any& operator[](kt_size_t index) const;
112 
113  public:
118  static const MetaArguments& Empty()
119  {
120  static MetaArguments dummy;
121 
122  return dummy;
123  }
124 
125  private:
126  MetaArgsPrivate* m_pPrivate;
127  };
128 
132 
138  template <typename T>
139  kt_bool CheckArgumentType(const Any& rAny)
140  {
141  return any_cast<T>(rAny) != NULL;
142  }
143 
147 
151  class MetaConstructor
152  {
153  public:
157  virtual ~MetaConstructor()
158  {
159  }
160 
161  public:
168  virtual kt_bool CheckArguments(const MetaArguments& rArgs) const = 0;
169 
176  virtual void* Create(const MetaArguments& rArgs) const = 0;
177  };
178 
182 
186  template <typename T>
187  class MetaConstructorImpl0 : public MetaConstructor
188  {
189  public:
190  virtual kt_bool CheckArguments(const MetaArguments& rArgs) const
191  {
192  return (rArgs.GetCount() == 0);
193  }
194 
195  virtual void* Create(const MetaArguments&) const
196  {
197  return new T();
198  }
199  };
200 
204 
208  template <typename T, typename A0>
209  class MetaConstructorImpl1 : public MetaConstructor
210  {
211  public:
212  virtual kt_bool CheckArguments(const MetaArguments& rArgs) const
213  {
214  return (rArgs.GetCount() == 1) && CheckArgumentType<A0>(rArgs[0]);
215  }
216 
217  virtual void* Create(const MetaArguments& rArgs) const
218  {
219  return new T(any_cast<A0>(rArgs[0]));
220  }
221  };
222 
226 
230  template <typename T, typename A0, typename A1>
231  class MetaConstructorImpl2 : public MetaConstructor
232  {
233  public:
234  virtual kt_bool CheckArguments(const MetaArguments& rArgs) const
235  {
236  return (rArgs.GetCount() == 2) && CheckArgumentType<A0>(rArgs[0]) && CheckArgumentType<A1>(rArgs[1]);
237  }
238 
239  virtual void* Create(const MetaArguments& rArgs) const
240  {
241  return new T(any_cast<A0>(rArgs[0]), any_cast<A1>(rArgs[1]));
242  }
243  };
244 
245  // @endcond
246 
250 
251  template <typename T> class MetaClassHelper;
252 
256 
257  class MetaConstructor;
258 
263  {
264  public:
270  template <typename T>
272  {
273  MetaClass& newClass = MetaClassManager::GetInstance().RegisterNew(rName, KartoTypeId<T>::Get(false));
274  return MetaClassHelper<T>(newClass);
275  }
276 
277  public:
282  const karto::String& GetName() const;
283 
288  kt_size_t GetBaseSize() const;
289 
296  const MetaClass& GetBase(kt_size_t index) const;
297 
303  template <typename T>
304  T* Create(const MetaArguments& rArgs = MetaArguments::Empty()) const
305  {
306  void* pObject = NULL;
307 
309  {
310  const MetaConstructor* pConstructor = *iter;
311 
312  if (pConstructor->CheckArguments(rArgs))
313  {
314  pObject = pConstructor->Create(rArgs);
315  }
316  }
317 
318  if (pObject == NULL)
319  {
320  throw karto::Exception("Unable to create object '" + GetName() + "'. Please verify that .Constructor is defined for MetaClass");
321  }
322 
323  return static_cast<T*>(pObject);
324  }
325 
329  template <typename T>
330  void Destroy(const T* pObject) const
331  {
332  delete pObject;
333  }
334 
335  public:
341  kt_bool operator==(const MetaClass& rOther) const;
342 
348  kt_bool operator!=(const MetaClass& rOther) const;
349 
350  private:
351  MetaClass(const karto::String& rName);
352  ~MetaClass();
353 
354  private:
355  template <typename T> friend class MetaClassHelper;
356  friend class MetaClassManager;
357 
360 
362  };
363 
365 
366 }
367 
368 #endif // __OpenKarto_MetaClass_h__
karto::String m_Name
Definition: MetaClass.h:358
bool kt_bool
Definition: Types.h:145
std::size_t kt_size_t
Definition: Types.h:138
T * any_cast(Any *pAny)
Definition: Any.h:206
TFSIMD_FORCE_INLINE bool operator==(const Matrix3x3 &m1, const Matrix3x3 &m2)
#define KARTO_EXPORT
Definition: Macros.h:78
List< const MetaConstructor * > m_Constructors
Definition: MetaClass.h:361
void Destroy(const T *pObject) const
Definition: MetaClass.h:330
List< const MetaClass * > m_BaseClasses
Definition: MetaClass.h:359
TFSIMD_FORCE_INLINE bool operator!=(const Vector3 &other) const
static MetaClassHelper< T > Register(const karto::String &rName)
Definition: MetaClass.h:271
T * Create(const MetaArguments &rArgs=MetaArguments::Empty()) const
Definition: MetaClass.h:304
Definition: Any.cpp:20
#define karto_const_forEach(listtype, list)
Definition: Macros.h:136


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Tue Nov 7 2017 06:02:36