basicobject.h

Go to the documentation of this file.
00001 
00002 #ifndef BASICOBJECT_H
00003 #define BASICOBJECT_H
00004 
00005 #include "SDL/SDL.h" // just for DECLSPEC
00006 #include <list>
00007 
00014 #include "tinyxml.h"
00015 
00043 #define DECLARE_SLOT(name,classname,func)\
00044  static BasicSlot classname ## __ ## name(#name,#classname,func);
00045 
00046 class BasicObject;
00047 
00056 class BasicSlot
00057 {
00058 public:
00066     typedef void (*Callback)(void *sender, BasicObject *reciever, void *data);
00075     DECLSPEC BasicSlot(const char *fname, const char *classname, Callback func);
00077     virtual DECLSPEC ~BasicSlot();
00079     Callback getFunction()  {return pFunc;}
00081     const char *getName()   {return strName;}
00087     bool matchName(const char *name)    {return strcmp(strName, name) == 0;}
00089     BasicSlot *beNext;
00094     static BasicSlot *getFirst()    {return beFirst;}
00103     static Callback getFunction(const char *name, const char *classname = NULL);
00104 private:
00105     Callback pFunc;
00106     char *strName, *strClassName;
00107     // pointer to the first event in the global event list
00108     static BasicSlot *beFirst;
00109 };
00110 
00133 class BasicObject
00134 {
00135 public:
00137     typedef BasicSlot::Callback Callback;
00143     struct FunctionData
00144     {
00146         Callback func;
00148         BasicObject *obj;
00149         FunctionData(): func(NULL), obj(NULL)
00150         {
00151         }
00152         FunctionData(Callback f, BasicObject *p = NULL): func(f), obj(p)
00153         {
00154         }
00155     };
00164     DECLSPEC BasicObject(const char *name, BasicObject *parent);
00171     DECLSPEC BasicObject( TiXmlElement *el, BasicObject *p );
00172     virtual DECLSPEC ~BasicObject();
00177     TiXmlElement *getXmlData()  { return xmlData; }
00178     
00183     virtual const char *getClassName()  { return "BasicObject"; }
00190     DECLSPEC bool matchName(const char *name);
00197     DECLSPEC FunctionData getFunctionByName(const char *name);
00203     DECLSPEC Callback getFuncByName( const char *name );
00211     DECLSPEC BasicObject *getObjectByName( const char *name );
00217     DECLSPEC BasicObject *getChildWithName( const char *name );
00219     const char *getName() const { return strName; }
00221     BasicObject *getParent() const  { return parent; }
00223     unsigned int numChildren()  { return children.size(); }
00225     virtual bool isUiObject() const { return false; }
00227     virtual bool isNetSocket() const    { return false; }
00228     
00230     void init() { onInit(); }
00235     void destroy()  {onDestroy();}
00236     
00237     static BasicObject *getObject(const char *name) { return pBase? pBase->getObjectByName(name) : NULL; }
00238     static Callback getFunc(const char *name)   { return pBase? pBase->getFuncByName(name) : NULL; }
00239     DECLSPEC static FunctionData getFunction(const char *name);
00240 
00241 protected:
00242     typedef std::list<BasicObject*> child_list;
00243     typedef child_list::iterator child_iter;
00244     typedef child_list::reverse_iterator child_reverse_iter;
00245     typedef std::list<BasicSlot *>::iterator EventIter;
00246     
00255     virtual DECLSPEC void onInit();
00261     virtual DECLSPEC void onDestroy();
00262 
00267     TiXmlElement *xmlData;
00268     // the object's children
00269     child_list children;
00270     // item of this object in the parent's children list
00271     child_iter parentitem;
00272 private:
00273     // adds a child, name must not be NULL
00274     child_iter addChild( BasicObject *obj, const char *name, bool top = true );
00275     // removes a child and frees its name
00276     void removeChild( BasicObject *obj );
00277     
00278     // pointer to the parent object
00279     BasicObject *parent;
00280     // allocated/freed by parent
00281     char *strName;
00282     
00283     // pointer to the last object, created without parent
00284     static BasicObject *pBase;
00285     friend class UiManager;
00286     friend class UiObject;
00287 };
00288 
00289 class TiXmlElement;
00290 
00302 class SignalMultiplier : public BasicObject
00303 {
00304 public:
00305     DECLSPEC SignalMultiplier(const char *name, BasicObject *parent);
00311     DECLSPEC SignalMultiplier(TiXmlElement *el, BasicObject *parent);
00312     DECLSPEC ~SignalMultiplier();
00313     DECLSPEC void addSlot(Callback func, BasicObject *aim);
00314     DECLSPEC void removeSlot(Callback func, BasicObject *aim);
00315     
00316     static void event(void *sender, BasicObject *aim, void *data)
00317     {
00318         ((SignalMultiplier*)aim)->onevent(sender, data);
00319     }
00320 protected:
00321     DECLSPEC void onInit();
00322     
00323 private:
00324     DECLSPEC void onevent(void *sender, void *data);
00325     typedef std::list<BasicObject::FunctionData> cblist;
00326     cblist aims;
00327 };
00328 
00329 #endif

Generated on Wed May 9 17:35:55 2007 for netrinjo by  doxygen 1.5.1