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,2004 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.25 2004/02/08 17:17:34 fox Exp $ * 00023 ********************************************************************************/ 00024 #ifndef FXOBJECT_H 00025 #define FXOBJECT_H 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 // Forward 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 FXuint namelen; 00061 private: 00062 static const FXMetaClass **metaClassTable; 00063 static FXuint nmetaClassTable; 00064 static FXuint nmetaClasses; 00065 private: 00066 static void resize(FXuint n); 00067 public: 00068 FXMetaClass(const FXchar* name,FXObject *(fac)(),const FXMetaClass* base,const void* ass,FXuint nass,FXuint assz,FXuint len); 00069 00070 /// Check if metaclass is subclass of some other metaclass 00071 FXbool isSubClassOf(const FXMetaClass* metaclass) const; 00072 00073 /// Make instance of some object 00074 FXObject* makeInstance() const; 00075 00076 /// Ask class name 00077 const FXchar* getClassName() const { return className; } 00078 00079 /// Obtain class name length 00080 FXuint getClassNameLength() const { return namelen; } 00081 00082 /// Ask base class 00083 const FXMetaClass* getBaseClass() const { return baseClass; } 00084 00085 /// Find metaclass object 00086 static const FXMetaClass* getMetaClassFromName(const FXchar* name); 00087 00088 /// Search message map 00089 const void* search(FXSelector key) const; 00090 00091 ~FXMetaClass(); 00092 }; 00093 00094 00095 /// Macro to set up class declaration 00096 #define FXDECLARE(classname) \ 00097 public: \ 00098 struct FXMapEntry { FX::FXSelector keylo; FX::FXSelector keyhi; long (classname::* func)(FX::FXObject*,FX::FXSelector,void*); }; \ 00099 static const FX::FXMetaClass metaClass; \ 00100 static FX::FXObject* manufacture(); \ 00101 virtual long handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr); \ 00102 virtual const FX::FXMetaClass* getMetaClass() const { return &metaClass; } \ 00103 friend FX::FXStream& operator<<(FX::FXStream& store,const classname* obj){return store.saveObject((FX::FXObjectPtr)(obj));} \ 00104 friend FX::FXStream& operator>>(FX::FXStream& store,classname*& obj){return store.loadObject((FX::FXObjectPtr&)(obj));} \ 00105 private: 00106 00107 00108 /// Macro to set up class implementation 00109 #define FXIMPLEMENT(classname,baseclassname,mapping,nmappings) \ 00110 FX::FXObject* classname::manufacture(){return new classname;} \ 00111 const FX::FXMetaClass classname::metaClass(#classname,classname::manufacture,&baseclassname::metaClass,mapping,nmappings,sizeof(classname::FXMapEntry),sizeof(#classname)); \ 00112 long classname::handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr){ \ 00113 const FXMapEntry* me=(const FXMapEntry*)metaClass.search(sel); \ 00114 return me ? (this->* me->func)(sender,sel,ptr) : baseclassname::handle(sender,sel,ptr); \ 00115 } 00116 00117 00118 /// Macro to set up abstract class declaration 00119 #define FXDECLARE_ABSTRACT(classname) \ 00120 public: \ 00121 struct FXMapEntry { FX::FXSelector keylo; FX::FXSelector keyhi; long (classname::* func)(FX::FXObject*,FX::FXSelector,void*); }; \ 00122 static const FX::FXMetaClass metaClass; \ 00123 virtual long handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr); \ 00124 virtual const FX::FXMetaClass* getMetaClass() const { return &metaClass; } \ 00125 friend FX::FXStream& operator<<(FX::FXStream& store,const classname* obj){return store.saveObject((FX::FXObjectPtr)(obj));} \ 00126 friend FX::FXStream& operator>>(FX::FXStream& store,classname*& obj){return store.loadObject((FX::FXObjectPtr&)(obj));} \ 00127 private: 00128 00129 00130 /// Macro to set up abstract class implementation 00131 #define FXIMPLEMENT_ABSTRACT(classname,baseclassname,mapping,nmappings) \ 00132 const FX::FXMetaClass classname::metaClass(#classname,NULL,&baseclassname::metaClass,mapping,nmappings,sizeof(classname::FXMapEntry),sizeof(#classname)); \ 00133 long classname::handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr){ \ 00134 const FXMapEntry* me=(const FXMapEntry*)metaClass.search(sel); \ 00135 return me ? (this->* me->func)(sender,sel,ptr) : baseclassname::handle(sender,sel,ptr); \ 00136 } 00137 00138 00139 /// MetaClass of a class 00140 #define FXMETACLASS(classname) (&classname::metaClass) 00141 00142 00143 /// Set up map type 00144 #define FXDEFMAP(classname) static const classname::FXMapEntry 00145 00146 /// Define range of function types 00147 #define FXMAPTYPES(typelo,typehi,func) {MKUINT(MINKEY,typelo),MKUINT(MAXKEY,typehi),&func} 00148 00149 /// Define range of function types 00150 #define FXMAPTYPE(type,func) {MKUINT(MINKEY,type),MKUINT(MAXKEY,type),&func} 00151 00152 /// Define range of functions 00153 #define FXMAPFUNCS(type,keylo,keyhi,func) {MKUINT(keylo,type),MKUINT(keyhi,type),&func} 00154 00155 /// Define one function 00156 #define FXMAPFUNC(type,key,func) {MKUINT(key,type),MKUINT(key,type),&func} 00157 00158 00159 /// Base of all FOX object 00160 class FXAPI FXObject { 00161 FXDECLARE(FXObject) 00162 public: 00163 00164 /// Called for unhandled messages 00165 virtual long onDefault(FXObject*,FXSelector,void*); 00166 00167 public: 00168 00169 /// Get class name of some object 00170 const FXchar* getClassName() const; 00171 00172 /// Check if object is member of metaclass 00173 FXbool isMemberOf(const FXMetaClass* metaclass) const; 00174 00175 /// Save object to stream 00176 virtual void save(FXStream& store) const; 00177 00178 /// Load object from stream 00179 virtual void load(FXStream& store); 00180 00181 /// Virtual destructor 00182 virtual ~FXObject(); 00183 }; 00184 00185 } 00186 00187 #endif

Copyright © 1997-2004 Jeroen van der Zijp