SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
CRegisteredCallbacks.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 CRegisteredCallbacks.hpp
23  *
24  * Created on: Sep 13, 2011
25  *
26  * Copyright (C) 2011, Samir Menon <smenon@stanford.edu>
27  */
28 
29 #ifndef CREGISTEREDCALLBACKS_HPP_
30 #define CREGISTEREDCALLBACKS_HPP_
31 
32 #include <sutil/CSingleton.hpp>
33 #include <sutil/CMappedList.hpp>
34 
35 #include <vector>
36 #ifdef DEBUG
37 #include <iostream>
38 #endif
39 
40 namespace sutil
41 {
42  // These are forward declarations. Read the class comments for details.
43  template <typename Idx> class CCallbackSuperBase;
44  template <typename Idx, typename ArgumentTuple, typename Data> class CCallbackBase;
45  template <typename Idx> class CRegisteredCallbacks;
46 
88  namespace callbacks
89  {
92  template<typename Idx, typename ArgumentTuple, typename Data=bool >
93  bool call(const Idx& arg_callback_name, ArgumentTuple& args)
94  {
95  CCallbackSuperBase<Idx>** mapped_callback =
96  CRegisteredCallbacks<Idx>::getCallbacks()->at(arg_callback_name);
97 
98  if(0 == mapped_callback) { return false; } //Function not found.
99 
101  dynamic_cast<CCallbackBase<Idx, ArgumentTuple, Data>*>(*mapped_callback);
102 
103  if(NULL == callback)
104  { return false; }
105 
106  callback->call(args); //The actual function call
107 
108  return true; //Everything worked out
109  }
110 
114  template< typename CallbackClass, typename Idx,
115  typename ArgumentTuple, typename Data=bool >
116  bool add(const Idx& arg_callback_name)
117  {
118  CallbackClass f;
119  return f.sutil::CCallbackBase<Idx, ArgumentTuple, Data>:: registerCallback(arg_callback_name);
120  }
121 
122  template< typename CallbackClass, typename Idx,
123  typename ArgumentTuple, typename Data>
124  bool add(const Idx& arg_callback_name, Data* arg_data)
125  {
126  CallbackClass f;
127  return f.sutil::CCallbackBase<Idx, ArgumentTuple, Data>:: registerCallback(arg_callback_name, arg_data);
128  }
129 
131  template<typename Idx>
132  bool list(std::vector<Idx>& idxlist)
133  { typedef CMappedPointerList<Idx,CCallbackSuperBase<Idx>,true> map;
135  if(NULL == m) { return false; }
136  typename map::iterator it,ite;
137  idxlist.clear();
138  for(it = m->begin(), ite = m->end();it!=ite;++it)
139  { idxlist.push_back(!it); }//Add the index to the vector
140  return true;
141  }
142  }
143 
149  template <typename Idx>
150  class CRegisteredCallbacks : private CSingleton<CMappedPointerList<Idx,CCallbackSuperBase<Idx>,true> >
151  {
152  //A typedef for easy use;
153  typedef CSingleton<CMappedPointerList<Idx,CCallbackSuperBase<Idx>,true> > singleton;
154 
155  //So that the base class can call the register function
156  friend class CCallbackSuperBase<Idx>;
157 
158  public:
160  static bool callbackRegistered(const Idx& arg_callback_name)
161  {
162  CCallbackSuperBase<Idx>** mapped_callback = singleton::getData()->at(arg_callback_name);
163  if(0 == mapped_callback) { return false; }
164  return true;
165  }
166 
169  static bool resetCallbacks()
170  { return singleton::resetData(); }
173  static CMappedPointerList<Idx,CCallbackSuperBase<Idx>,true>* getCallbacks()
174  { return singleton::getData(); }
176  private:
180  static bool registerCallback(const Idx& arg_callback_name,
181  CCallbackSuperBase<Idx>* arg_callback_object)
182  {
183  if(callbackRegistered(arg_callback_name))
184  {
185 #ifdef DEBUG
186  std::cerr<<"\nCRegisteredCallbacks::registerCallback() Warning :"
187  <<" The passed callback is already registered.";
188 #endif
189  return false;
190  }
191 
192  // Creates an object that points to the passed callback object
194  create(arg_callback_name,arg_callback_object);
195  if(0 == t)
196  {
197 #ifdef DEBUG
198  std::cerr<<"\nCRegisteredCallbacks::registerCallback() Error :"
199  <<" Failed to create callback in the callback mapped list.";
200 #endif
201  return false;
202  }
203 
204  return true;
205  }
206 
209 
212 
215  };
216 
222  template <typename Idx>
223  class CCallbackSuperBase
224  {
225  public:
227  virtual ~CCallbackSuperBase(){}
228 
229  protected:
232 
234  virtual bool registerCallbackSuper(
235  const Idx &arg_callback_name,
237  {
239  registerCallback(arg_callback_name,arg_obj);
240  }
241 
242  private:
247  };
248 
253  template <typename Idx, typename ArgumentTuple, typename Data=bool>
254  class CCallbackBase : public CCallbackSuperBase<Idx>
255  {
256  public:
260  virtual void call(ArgumentTuple& args) = 0;
261 
263  virtual CCallbackBase<Idx, ArgumentTuple, Data>* createObject()=0;
264 
265  virtual bool registerCallback(
266  const Idx& arg_callback_name,
267  Data* arg_data = 0)
268  {
269  bool flag;
270  flag = CRegisteredCallbacks<Idx>::callbackRegistered(arg_callback_name);
271 
272  if(flag)//Callback already registered. Do nothing and return false.
273  { return false; }
274  else
275  {
276  //Duplicate this object and register the new one with the singleton
277  CCallbackBase<Idx,ArgumentTuple,Data>* obj = createObject();
278 
279  //Set up data for this function object
280  if(0 != arg_data)
281  { obj->data_ = arg_data; }
282 
284  arg_callback_name,
285  dynamic_cast<CCallbackSuperBase<Idx>*>(obj) );
286  if(!flag)
287  { delete obj; return false; }
288  }
289  return true;
290  }
291 
293  virtual ~CCallbackBase(){}
294 
295  protected:
297  CCallbackBase() : data_(NULL){}
298 
299  Data* data_;
300 
301  private:
303  CCallbackBase& operator= (const CCallbackBase&);
304  };
305 }
306 
307 #endif /* CREGISTEREDCALLBACKS_HPP_ */
308 
static bool resetCallbacks()
Definition: CRegisteredCallbacks.hpp:171
virtual ~CCallbackSuperBase()
Definition: CRegisteredCallbacks.hpp:229
static bool registerCallback(const Idx &arg_callback_name, CCallbackSuperBase< Idx > *arg_callback_object)
Definition: CRegisteredCallbacks.hpp:182
Definition: CRegisteredCallbacks.hpp:43
bool add(const Idx &arg_callback_name)
Definition: CRegisteredCallbacks.hpp:116
CCallbackSuperBase & operator=(const CCallbackSuperBase &)
virtual bool registerCallbackSuper(const Idx &arg_callback_name, CCallbackSuperBase *arg_obj)
Definition: CRegisteredCallbacks.hpp:236
static CMappedPointerList< Idx, CCallbackSuperBase< Idx >, true > * getCallbacks()
Definition: CRegisteredCallbacks.hpp:175
bool list(std::vector< Idx > &idxlist)
Definition: CRegisteredCallbacks.hpp:134
virtual T * at(const std::size_t arg_idx)
Definition: CMappedList.hpp:832
static bool callbackRegistered(const Idx &arg_callback_name)
Definition: CRegisteredCallbacks.hpp:162
virtual void call(ArgumentTuple &args)=0
static CMappedPointerList< Idx, CCallbackSuperBase< Idx >, true > * getData()
Definition: CRegisteredCallbacks.hpp:44
virtual CCallbackBase< Idx, ArgumentTuple, Data > * createObject()=0
CRegisteredCallbacks & operator=(const CRegisteredCallbacks &)
bool call(const Idx &arg_callback_name, ArgumentTuple &args)
Definition: CRegisteredCallbacks.hpp:93
virtual ~CCallbackBase()
Definition: CRegisteredCallbacks.hpp:295
CCallbackBase()
Definition: CRegisteredCallbacks.hpp:299
Definition: CRegisteredCallbacks.hpp:45
CCallbackSuperBase()
Definition: CRegisteredCallbacks.hpp:233