Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXObject.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                         T o p l e v el   O b j e c t                          *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1997,2009 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 *********************************************************************************
00021 * $Id: FXObject.h,v 1.44 2009/01/06 13:07:26 fox Exp $                          *
00022 ********************************************************************************/
00023 #ifndef FXOBJECT_H
00024 #define FXOBJECT_H
00025 
00026 
00027 namespace FX {
00028 
00029 /// Minimum and maximum message id
00030 enum {
00031   MINKEY = 0,
00032   MAXKEY = 65535
00033   };
00034 
00035 
00036 /// Minimum and maximum message type
00037 enum {
00038   MINTYPE = 0,
00039   MAXTYPE = 65535
00040   };
00041 
00042 
00043 /// Association key
00044 typedef FXuint FXSelector;
00045 
00046 
00047 class FXObject;
00048 
00049 
00050 /// Describes a FOX object
00051 class FXAPI FXMetaClass {
00052 private:
00053   const FXchar              *className;
00054   FXObject*                (*manufacture)();
00055   const FXMetaClass         *baseClass;
00056   const void                *assoc;
00057   FXuint                     nassocs;
00058   FXuint                     assocsz;
00059 private:
00060   static const FXMetaClass **metaClassTable;
00061   static FXuint              nmetaClassTable;
00062   static FXuint              nmetaClasses;
00063 private:
00064   static void resize(FXuint n);
00065 public:
00066   FXMetaClass(const FXchar* name,FXObject *(fac)(),const FXMetaClass* base,const void* ass,FXuint nass,FXuint assz);
00067 
00068   /// Check if metaclass is subclass of some other metaclass
00069   FXbool isSubClassOf(const FXMetaClass* metaclass) const;
00070 
00071   /// Make instance of some object
00072   FXObject* makeInstance() const;
00073 
00074   /// Ask class name
00075   const FXchar* getClassName() const { return className; }
00076 
00077   /// Ask base class
00078   const FXMetaClass* getBaseClass() const { return baseClass; }
00079 
00080   /// Find metaclass object
00081   static const FXMetaClass* getMetaClassFromName(const FXchar* name);
00082 
00083   /// Search message map
00084   const void* search(FXSelector key) const;
00085 
00086  ~FXMetaClass();
00087   };
00088 
00089 
00090 /// Macro to set up class declaration
00091 #define FXDECLARE(classname) \
00092   public: \
00093    struct FXMapEntry { FX::FXSelector keylo; FX::FXSelector keyhi; long (classname::* func)(FX::FXObject*,FX::FXSelector,void*); }; \
00094    static const FX::FXMetaClass metaClass; \
00095    static FX::FXObject* manufacture(); \
00096    virtual long handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr); \
00097    virtual const FX::FXMetaClass* getMetaClass() const { return &metaClass; } \
00098    friend FX::FXStream& operator<<(FX::FXStream& store,const classname* obj){return store.saveObject((FX::FXObjectPtr)(obj));} \
00099    friend FX::FXStream& operator>>(FX::FXStream& store,classname*& obj){return store.loadObject((FX::FXObjectPtr&)(obj));} \
00100   private:
00101 
00102 
00103 /// Macro to set up class implementation
00104 #define FXIMPLEMENT(classname,baseclassname,mapping,nmappings) \
00105   FX::FXObject* classname::manufacture(){return new classname;} \
00106   const FX::FXMetaClass classname::metaClass(#classname,classname::manufacture,&baseclassname::metaClass,mapping,nmappings,sizeof(classname::FXMapEntry)); \
00107   long classname::handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr){ \
00108     const FXMapEntry* me=(const FXMapEntry*)metaClass.search(sel); \
00109     return me ? (this->* me->func)(sender,sel,ptr) : baseclassname::handle(sender,sel,ptr); \
00110     }
00111 
00112 
00113 /// Macro to set up abstract class declaration
00114 #define FXDECLARE_ABSTRACT(classname) \
00115   public: \
00116    struct FXMapEntry { FX::FXSelector keylo; FX::FXSelector keyhi; long (classname::* func)(FX::FXObject*,FX::FXSelector,void*); }; \
00117    static const FX::FXMetaClass metaClass; \
00118    virtual long handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr); \
00119    virtual const FX::FXMetaClass* getMetaClass() const { return &metaClass; } \
00120    friend FX::FXStream& operator<<(FX::FXStream& store,const classname* obj){return store.saveObject((FX::FXObjectPtr)(obj));} \
00121    friend FX::FXStream& operator>>(FX::FXStream& store,classname*& obj){return store.loadObject((FX::FXObjectPtr&)(obj));} \
00122   private:
00123 
00124 
00125 /// Macro to set up abstract class implementation
00126 #define FXIMPLEMENT_ABSTRACT(classname,baseclassname,mapping,nmappings) \
00127   const FX::FXMetaClass classname::metaClass(#classname,NULL,&baseclassname::metaClass,mapping,nmappings,sizeof(classname::FXMapEntry)); \
00128   long classname::handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr){ \
00129     const FXMapEntry* me=(const FXMapEntry*)metaClass.search(sel); \
00130     return me ? (this->* me->func)(sender,sel,ptr) : baseclassname::handle(sender,sel,ptr); \
00131     }
00132 
00133 
00134 /// MetaClass of a class
00135 #define FXMETACLASS(classname) (&classname::metaClass)
00136 
00137 
00138 /// Set up map type
00139 #define FXDEFMAP(classname) static const classname::FXMapEntry
00140 
00141 /// Define range of function types
00142 #define FXMAPTYPES(typelo,typehi,func) {FXSEL(typelo,FX::MINKEY),FXSEL(typehi,FX::MAXKEY),&func}
00143 
00144 /// Define range of function types
00145 #define FXMAPTYPE(type,func) {FXSEL(type,FX::MINKEY),FXSEL(type,FX::MAXKEY),&func}
00146 
00147 /// Define range of functions
00148 #define FXMAPFUNCS(type,keylo,keyhi,func) {FXSEL(type,keylo),FXSEL(type,keyhi),&func}
00149 
00150 /// Define one function
00151 #define FXMAPFUNC(type,key,func) {FXSEL(type,key),FXSEL(type,key),&func}
00152 
00153 
00154 /**
00155 * Object is the base class for all objects in FOX; in order to receive
00156 * messages from the user interface, your class must derive from Object.
00157 * The Object class also provides serialization facilities, with which
00158 * you can save and restore the object's state.  If you've subclassed
00159 * from Object, you can save your subclasses' state by overloading the
00160 * save() and load() functions and use the stream API to serialize its
00161 * member data.
00162 */
00163 class FXAPI FXObject {
00164   FXDECLARE(FXObject)
00165 public:
00166 
00167   /// Called for unhandled messages
00168   virtual long onDefault(FXObject*,FXSelector,void*);
00169 
00170 public:
00171 
00172   /// Get class name of some object
00173   const FXchar* getClassName() const;
00174 
00175   /// Check if object is member of metaclass
00176   FXbool isMemberOf(const FXMetaClass* metaclass) const;
00177 
00178   /// Try handle message safely, catching certain exceptions
00179   virtual long tryHandle(FXObject* sender,FXSelector sel,void* ptr);
00180 
00181   /// Save object to stream
00182   virtual void save(FXStream& store) const;
00183 
00184   /// Load object from stream
00185   virtual void load(FXStream& store);
00186 
00187   /// Virtual destructor
00188   virtual ~FXObject();
00189   };
00190 
00191 }
00192 
00193 #endif

Copyright © 1997-2009 Jeroen van der Zijp