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,2006 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.35 2006/01/22 17:58:06 fox Exp $                          *
00023 ********************************************************************************/
00024 #ifndef FXOBJECT_H
00025 #define FXOBJECT_H
00026 
00027 
00028 namespace FX {
00029 
00030 /// Minimum and maximum message id
00031 enum {
00032   MINKEY = 0,
00033   MAXKEY = 65535
00034   };
00035 
00036 
00037 /// Minimum and maximum message type
00038 enum {
00039   MINTYPE = 0,
00040   MAXTYPE = 65535
00041   };
00042 
00043 
00044 /// Association key
00045 typedef FXuint FXSelector;
00046 
00047 
00048 class FXObject;
00049 
00050 
00051 /// Describes a FOX object
00052 class FXAPI FXMetaClass {
00053 private:
00054   const FXchar              *className;
00055   FXObject*                (*manufacture)();
00056   const FXMetaClass         *baseClass;
00057   const void                *assoc;
00058   FXuint                     nassocs;
00059   FXuint                     assocsz;
00060 private:
00061   static const FXMetaClass **metaClassTable;
00062   static FXuint              nmetaClassTable;
00063   static FXuint              nmetaClasses;
00064 private:
00065   static void resize(FXuint n);
00066 public:
00067   FXMetaClass(const FXchar* name,FXObject *(fac)(),const FXMetaClass* base,const void* ass,FXuint nass,FXuint assz);
00068 
00069   /// Check if metaclass is subclass of some other metaclass
00070   bool isSubClassOf(const FXMetaClass* metaclass) const;
00071 
00072   /// Make instance of some object
00073   FXObject* makeInstance() const;
00074 
00075   /// Ask class name
00076   const FXchar* getClassName() const { return className; }
00077 
00078   /// Ask base class
00079   const FXMetaClass* getBaseClass() const { return baseClass; }
00080 
00081   /// Find metaclass object
00082   static const FXMetaClass* getMetaClassFromName(const FXchar* name);
00083 
00084   /// Search message map
00085   const void* search(FXSelector key) const;
00086 
00087  ~FXMetaClass();
00088   };
00089 
00090 
00091 /// Macro to set up class declaration
00092 #define FXDECLARE(classname) \
00093   public: \
00094    struct FXMapEntry { FX::FXSelector keylo; FX::FXSelector keyhi; long (classname::* func)(FX::FXObject*,FX::FXSelector,void*); }; \
00095    static const FX::FXMetaClass metaClass; \
00096    static FX::FXObject* manufacture(); \
00097    virtual long handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr); \
00098    virtual const FX::FXMetaClass* getMetaClass() const { return &metaClass; } \
00099    friend FX::FXStream& operator<<(FX::FXStream& store,const classname* obj){return store.saveObject((FX::FXObjectPtr)(obj));} \
00100    friend FX::FXStream& operator>>(FX::FXStream& store,classname*& obj){return store.loadObject((FX::FXObjectPtr&)(obj));} \
00101   private:
00102 
00103 
00104 /// Macro to set up class implementation
00105 #define FXIMPLEMENT(classname,baseclassname,mapping,nmappings) \
00106   FX::FXObject* classname::manufacture(){return new classname;} \
00107   const FX::FXMetaClass classname::metaClass(#classname,classname::manufacture,&baseclassname::metaClass,mapping,nmappings,sizeof(classname::FXMapEntry)); \
00108   long classname::handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr){ \
00109     const FXMapEntry* me=(const FXMapEntry*)metaClass.search(sel); \
00110     return me ? (this->* me->func)(sender,sel,ptr) : baseclassname::handle(sender,sel,ptr); \
00111     }
00112 
00113 
00114 /// Macro to set up abstract class declaration
00115 #define FXDECLARE_ABSTRACT(classname) \
00116   public: \
00117    struct FXMapEntry { FX::FXSelector keylo; FX::FXSelector keyhi; long (classname::* func)(FX::FXObject*,FX::FXSelector,void*); }; \
00118    static const FX::FXMetaClass metaClass; \
00119    virtual long handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr); \
00120    virtual const FX::FXMetaClass* getMetaClass() const { return &metaClass; } \
00121    friend FX::FXStream& operator<<(FX::FXStream& store,const classname* obj){return store.saveObject((FX::FXObjectPtr)(obj));} \
00122    friend FX::FXStream& operator>>(FX::FXStream& store,classname*& obj){return store.loadObject((FX::FXObjectPtr&)(obj));} \
00123   private:
00124 
00125 
00126 /// Macro to set up abstract class implementation
00127 #define FXIMPLEMENT_ABSTRACT(classname,baseclassname,mapping,nmappings) \
00128   const FX::FXMetaClass classname::metaClass(#classname,NULL,&baseclassname::metaClass,mapping,nmappings,sizeof(classname::FXMapEntry)); \
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 /**
00156 * Object is the base class for all objects in FOX; in order to receive
00157 * messages from the user interface, your class must derive from Object.
00158 * The Object class also provides serialization facilities, with which
00159 * you can save and restore the object's state.  If you've subclassed
00160 * from Object, you can save your subclasses' state by overloading the
00161 * save() and load() functions and use the stream API to serialize its
00162 * member data.
00163 */
00164 class FXAPI FXObject {
00165   FXDECLARE(FXObject)
00166 public:
00167 
00168   /// Called for unhandled messages
00169   virtual long onDefault(FXObject*,FXSelector,void*);
00170 
00171 public:
00172 
00173   /// Get class name of some object
00174   const FXchar* getClassName() const;
00175 
00176   /// Check if object is member of metaclass
00177   bool isMemberOf(const FXMetaClass* metaclass) const;
00178 
00179   /// Try handle message safely
00180   virtual long tryHandle(FXObject* sender,FXSelector sel,void* ptr);
00181 
00182   /// Save object to stream
00183   virtual void save(FXStream& store) const;
00184 
00185   /// Load object from stream
00186   virtual void load(FXStream& store);
00187 
00188   /// Virtual destructor
00189   virtual ~FXObject();
00190   };
00191 
00192 }
00193 
00194 #endif

Copyright © 1997-2005 Jeroen van der Zijp