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

FXObject.h

00001 /********************************************************************************
00002 *                                                                               *
00003 *                         T o p l e v el   O b j e c t                          *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1997,2002 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (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 GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXObject.h,v 1.21 2002/09/26 22:44:15 fox Exp $                          *
00023 ********************************************************************************/
00024 #ifndef FXOBJECT_H
00025 #define FXOBJECT_H
00026 
00027 namespace FX {
00028 
00029 /// Minimum and maximum keys
00030 enum {
00031   MINKEY = 0,
00032   MAXKEY = 65535
00033   };
00034 
00035 
00036 /// Minimum and maximum types
00037 enum {
00038   MINTYPE = 0,
00039   MAXTYPE = 65535
00040   };
00041 
00042 
00043 // Association key
00044 typedef FXuint FXSelector;
00045 
00046 
00047 // Forward
00048 class FXObject;
00049 
00050 
00051 /// Describes a FOX object
00052 struct FXAPI FXMetaClass {
00053   const FXchar       *className;
00054   FXObject*         (*manufacture)();
00055   const FXMetaClass  *baseClass;
00056   const void         *assoc;
00057   FXuint              nassocs;
00058   FXuint              assocsz;
00059   FXuint              namelen;
00060 
00061   /// Check if metaclass is subclass of some other metaclass
00062   FXbool isSubClassOf(const FXMetaClass* metaclass) const;
00063 
00064   /// Make instance of some object
00065   FXObject* makeInstance() const;
00066 
00067   /// Ask class name
00068   const FXchar* getClassName() const { return className; }
00069 
00070   /// Ask base class
00071   const FXMetaClass* getBaseClass() const { return baseClass; }
00072 
00073   /// Find metaclass object
00074   static const FXMetaClass* getMetaClassFromName(const FXchar* name);
00075   
00076   /// Search message map
00077   const void* search(FXSelector key) const;
00078   };
00079 
00080 
00081 // Pre-runtime initializer
00082 class FXAPI __FXMETACLASSINITIALIZER__ {
00083 public:
00084   __FXMETACLASSINITIALIZER__(const FXMetaClass* meta);
00085   ~__FXMETACLASSINITIALIZER__();
00086   };
00087 
00088 
00089 /// Macro to set up class declaration
00090 #define FXDECLARE(classname) \
00091   public: \
00092    struct FXMapEntry { FX::FXSelector keylo; FX::FXSelector keyhi; long (classname::* func)(FX::FXObject*,FX::FXSelector,void*); }; \
00093    static const FX::FXMetaClass metaClass; \
00094    static FX::FXObject* manufacture(); \
00095    virtual long handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr); \
00096    virtual const FX::FXMetaClass* getMetaClass() const { return &metaClass; } \
00097    friend FX::FXStream& operator<<(FX::FXStream& store,const classname* obj){return store.saveObject((FX::FXObjectPtr)(obj));} \
00098    friend FX::FXStream& operator>>(FX::FXStream& store,classname*& obj){return store.loadObject((FX::FXObjectPtr&)(obj));} \
00099   private:
00100 
00101 
00102 /// Macro to set up class implementation
00103 #define FXIMPLEMENT(classname,baseclassname,mapping,nmappings) \
00104   FX::FXObject* classname::manufacture(){return new classname;} \
00105   const FX::FXMetaClass classname::metaClass={#classname,classname::manufacture,&baseclassname::metaClass,mapping,nmappings,sizeof(classname::FXMapEntry),sizeof(#classname)}; \
00106   FX::__FXMETACLASSINITIALIZER__ classname##Initializer(&classname::metaClass); \
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),sizeof(#classname)}; \
00128   __FXMETACLASSINITIALIZER__ classname##Initializer(&classname::metaClass); \
00129   long classname::handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr){ \
00130     const FXMapEntry* me=(const FXMapEntry*)metaClass.search(sel); \
00131     return me ? (this->* me->func)(sender,sel,ptr) : baseclassname::handle(sender,sel,ptr); \
00132     }
00133 
00134 
00135 /// MetaClass of a class
00136 #define FXMETACLASS(classname) (&classname::metaClass)
00137 
00138 
00139 /// Set up map type
00140 #define FXDEFMAP(classname) static const classname::FXMapEntry
00141 
00142 /// Define range of function types
00143 #define FXMAPTYPES(typelo,typehi,func) {MKUINT(MINKEY,typelo),MKUINT(MAXKEY,typehi),&func}
00144 
00145 /// Define range of function types
00146 #define FXMAPTYPE(type,func) {MKUINT(MINKEY,type),MKUINT(MAXKEY,type),&func}
00147 
00148 /// Define range of functions
00149 #define FXMAPFUNCS(type,keylo,keyhi,func) {MKUINT(keylo,type),MKUINT(keyhi,type),&func}
00150 
00151 /// Define one function
00152 #define FXMAPFUNC(type,key,func) {MKUINT(key,type),MKUINT(key,type),&func}
00153 
00154 
00155 /// Base of all FOX object
00156 class FXAPI FXObject {
00157   FXDECLARE(FXObject)
00158 public:
00159 
00160   /// Called for unhandled messages
00161   virtual long onDefault(FXObject*,FXSelector,void*);
00162 
00163 public:
00164 
00165   /// Get class name of some object
00166   const FXchar* getClassName() const;
00167 
00168   /// Check if object is member of metaclass
00169   FXbool isMemberOf(const FXMetaClass* metaclass) const;
00170 
00171   /// Save object to stream
00172   virtual void save(FXStream& store) const;
00173 
00174   /// Load object from stream
00175   virtual void load(FXStream& store);
00176 
00177   /// Virtual destructor
00178   virtual ~FXObject();
00179   };
00180 
00181 }
00182 
00183 #endif