SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
CRegisteredDynamicTypes.hpp
1 /* This file is part of sUtil, a random collection of utilities.
2 
3 sUtil is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 3 of the License, or (at your option) any later version.
7 
8 Alternatively, you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12 
13 sUtil is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License and a copy of the GNU General Public License along with
20 sUtil. If not, see <http://www.gnu.org/licenses/>.
21  */
22 /* \file CRegisteredDynamicTypes.hpp
23  *
24  * Created on: Sep 9, 2011
25  *
26  * Copyright (C) 2011, Samir Menon <smenon@stanford.edu>
27  */
28 
29 #ifndef CREGISTEREDDYNAMICTYPES_HPP_
30 #define CREGISTEREDDYNAMICTYPES_HPP_
31 
32 #include <sutil/CMappedList.hpp>
33 #include <sutil/CSingleton.hpp>
34 
35 #ifdef DEBUG
36 #include <iostream>
37 #endif
38 
39 namespace sutil
40 {
41  // Read the class comments below
42  template <typename Idx> class CDynamicTypeBase; //Forward declaration
43 
80  template <typename Idx>
94  private CSingleton<CMappedPointerList<Idx,CDynamicTypeBase<Idx>,true> >
95  {
96  //A typedef for easy use;
98 
99  friend class CDynamicTypeBase<Idx>;
100 
101  public:
103  static bool typeRegistered(const Idx& arg_type_name)
104  {
105  CDynamicTypeBase<Idx>** mapped_type = singleton::getData()->at(arg_type_name);
106 
107  if(NULL == mapped_type)
108  { return false; }
109 
110  return true;
111  }
112 
115  static bool getObjectForType(const Idx& arg_type_name,
116  void*& ret_object)
117  {
118  CDynamicTypeBase<Idx>** mapped_type = singleton::getData()->at(arg_type_name);
119 
120  if(NULL == mapped_type)
121  {
122 #ifdef DEBUG
123  std::cerr<<"\nCRegisteredDynamicTypes::getObjectForType() Error :"
124  <<" The passed type has not been registered.";
125 #endif
126  return false;
127  }
128 
129  if(NULL != ret_object)
130  {
131 #ifdef DEBUG
132  std::cerr<<"\nCRegisteredDynamicTypes::getObjectForType() Error :"
133  <<" The passed void* pointer is not NULL.";
134 #endif
135  return false;
136  }
137 
138 
139  if(NULL == *mapped_type)
140  {
141 #ifdef DEBUG
142  std::cerr<<"\nCRegisteredDynamicTypes::getObjectForType() Error :"
143  <<" The typemap is corrupted. A type creator object pointer is NULL.";
144 #endif
145  return false;
146  }
147 
148  //Be really careful.
149  ret_object = (*mapped_type)->createObject();
150  if(NULL == ret_object)
151  {
152 #ifdef DEBUG
153  std::cerr<<"\nCRegisteredDynamicTypes::getObjectForType() Error :"
154  <<" The type object's createObject() function did not work.";
155 #endif
156  }
157 
158  return true;
159  }
160 
163  static bool resetDynamicTypes()
164  { return singleton::resetData(); }
165 
166  private:
169  static bool registerType(const Idx& arg_type_name,
170  CDynamicTypeBase<Idx>* arg_type_object)
171  {
172  if(typeRegistered(arg_type_name))
173  {
174 #ifdef DEBUG
175  std::cerr<<"\nCRegisteredDynamicTypes::registerType() Warning :"
176  <<" The passed type is already registered.";
177 #endif
178  return false;
179  }
180 
181  CDynamicTypeBase<Idx>** t = singleton::getData()->create(arg_type_name,arg_type_object);
182  if(NULL == t)
183  {
184 #ifdef DEBUG
185  std::cerr<<"\nCRegisteredDynamicTypes::registerType() Error :"
186  <<" Failed to create type in the type mapped list.";
187 #endif
188  return false;
189  }
190 
191  return true;
192  }
193 
196 
199 
202  };
203 
207  template <typename Idx>
208  class CDynamicTypeBase
209  {
210  public:
212  CDynamicTypeBase(const Idx &arg_type_name) : type_name_(arg_type_name){}
213 
215  virtual ~CDynamicTypeBase(){}
216 
219  virtual void* createObject()=0;
220 
221  protected:
222  virtual bool registerMyType(CDynamicTypeBase* arg_obj)
223  {
225  }
226 
229 
230  private:
237  };
238 
241  template <typename Idx, typename Type>
242  class CDynamicType : public CDynamicTypeBase<Idx>
243  {
244  public:
246  CDynamicType(const Idx &arg_type_name) :
247  CDynamicTypeBase<Idx>::CDynamicTypeBase(arg_type_name)
248  { }
249 
251  virtual ~CDynamicType(){}
252 
255  virtual void* createObject()
256  {
257  Type* obj = new Type();
258  return reinterpret_cast<void*>(obj);
259  }
260 
261  bool registerType()
262  {
263  bool flag;
265 
266  if(flag)//Type already registered. Do nothing and return false.
267  {
268 #ifdef DEBUG
269  std::cerr<<"\nCDynamicType::registerType() Warning :"
270  <<" This object's type has already been registered.";
271 #endif
272  return false;
273  }
274  else
275  {
276  CDynamicType<Idx,Type>* obj = new CDynamicType<Idx,Type>(
277  CDynamicTypeBase<Idx>::type_name_);
278  flag = CDynamicTypeBase<Idx>::registerMyType(obj);
279  if(!flag)
280  {
281 #ifdef DEBUG
282  std::cerr<<"\nCDynamicType::registerType() Error :"
283  <<" This object's type could not be registered.";
284 #endif
285  return false;
286  }
287  }
288  return true;
289  }
290 
291  private:
293  CDynamicType();
295  CDynamicType(const CDynamicType&);
298  };
299 
300 }
301 
302 #endif /* CREGISTEREDDYNAMICTYPES_HPP_ */
Definition: CRegisteredDynamicTypes.hpp:42
virtual void * createObject()=0
Definition: CRegisteredDynamicTypes.hpp:242
CDynamicTypeBase(const Idx &arg_type_name)
Definition: CRegisteredDynamicTypes.hpp:212
Definition: CRegisteredDynamicTypes.hpp:81
CDynamicTypeBase & operator=(const CDynamicTypeBase &)
Definition: CSingleton.hpp:47
virtual void * createObject()
Definition: CRegisteredDynamicTypes.hpp:255
Idx type_name_
Definition: CRegisteredDynamicTypes.hpp:228
virtual ~CDynamicType()
Definition: CRegisteredDynamicTypes.hpp:251
virtual T * at(const std::size_t arg_idx)
Definition: CMappedList.hpp:832
static bool registerType(const Idx &arg_type_name, CDynamicTypeBase< Idx > *arg_type_object)
Definition: CRegisteredDynamicTypes.hpp:169
static CMappedPointerList< Idx, CDynamicTypeBase< Idx >, true > * getData()
static bool resetDynamicTypes()
Definition: CRegisteredDynamicTypes.hpp:163
CDynamicType & operator=(const CDynamicType &)
static bool typeRegistered(const Idx &arg_type_name)
Definition: CRegisteredDynamicTypes.hpp:103
virtual ~CDynamicTypeBase()
Definition: CRegisteredDynamicTypes.hpp:215
virtual T * create(const Idx &arg_idx, const bool insert_at_start=true)
Definition: CMappedList.hpp:714
static bool getObjectForType(const Idx &arg_type_name, void *&ret_object)
Definition: CRegisteredDynamicTypes.hpp:115
CDynamicType(const Idx &arg_type_name)
Definition: CRegisteredDynamicTypes.hpp:246
CRegisteredDynamicTypes & operator=(const CRegisteredDynamicTypes &)