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

FXObject.h
Go to the documentation of this file.
1 /********************************************************************************
2 * *
3 * T o p l e v el O b j e c t *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,2006 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXObject.h,v 1.35.2.1 2006/08/05 00:58:29 fox Exp $ *
23 ********************************************************************************/
24 #ifndef FXOBJECT_H
25 #define FXOBJECT_H
26 
27 
28 namespace FX {
29 
30 /// Minimum and maximum message id
31 enum {
32  MINKEY = 0,
33  MAXKEY = 65535
34  };
35 
36 
37 /// Minimum and maximum message type
38 enum {
39  MINTYPE = 0,
40  MAXTYPE = 65535
41  };
42 
43 
44 /// Association key
45 typedef FXuint FXSelector;
46 
47 
48 class FXObject;
49 
50 
51 /// Describes a FOX object
52 class FXAPI FXMetaClass {
53 private:
54  const FXchar *className;
55  FXObject* (*manufacture)();
56  const FXMetaClass *baseClass;
57  const void *assoc;
58  FXuint nassocs;
59  FXuint assocsz;
60 private:
61  static const FXMetaClass **metaClassTable;
62  static FXuint nmetaClassTable;
63  static FXuint nmetaClasses;
64 private:
65  static void resize(FXuint n);
66 public:
67  FXMetaClass(const FXchar* name,FXObject *(fac)(),const FXMetaClass* base,const void* ass,FXuint nass,FXuint assz);
68 
69  /// Check if metaclass is subclass of some other metaclass
70  bool isSubClassOf(const FXMetaClass* metaclass) const;
71 
72  /// Make instance of some object
73  FXObject* makeInstance() const;
74 
75  /// Ask class name
76  const FXchar* getClassName() const { return className; }
77 
78  /// Ask base class
79  const FXMetaClass* getBaseClass() const { return baseClass; }
80 
81  /// Find metaclass object
82  static const FXMetaClass* getMetaClassFromName(const FXchar* name);
83 
84  /// Search message map
85  const void* search(FXSelector key) const;
86 
87  ~FXMetaClass();
88  };
89 
90 
91 /// Macro to set up class declaration
92 #define FXDECLARE(classname) \
93  public: \
94  struct FXMapEntry { FX::FXSelector keylo; FX::FXSelector keyhi; long (classname::* func)(FX::FXObject*,FX::FXSelector,void*); }; \
95  static const FX::FXMetaClass metaClass; \
96  static FX::FXObject* manufacture(); \
97  virtual long handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr); \
98  virtual const FX::FXMetaClass* getMetaClass() const { return &metaClass; } \
99  friend FX::FXStream& operator<<(FX::FXStream& store,const classname* obj){return store.saveObject((FX::FXObjectPtr)(obj));} \
100  friend FX::FXStream& operator>>(FX::FXStream& store,classname*& obj){return store.loadObject((FX::FXObjectPtr&)(obj));} \
101  private:
102 
103 
104 /// Macro to set up class implementation
105 #define FXIMPLEMENT(classname,baseclassname,mapping,nmappings) \
106  FX::FXObject* classname::manufacture(){return new classname;} \
107  const FX::FXMetaClass classname::metaClass(#classname,classname::manufacture,&baseclassname::metaClass,mapping,nmappings,sizeof(classname::FXMapEntry)); \
108  long classname::handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr){ \
109  const FXMapEntry* me=(const FXMapEntry*)metaClass.search(sel); \
110  return me ? (this->* me->func)(sender,sel,ptr) : baseclassname::handle(sender,sel,ptr); \
111  }
112 
113 
114 /// Macro to set up abstract class declaration
115 #define FXDECLARE_ABSTRACT(classname) \
116  public: \
117  struct FXMapEntry { FX::FXSelector keylo; FX::FXSelector keyhi; long (classname::* func)(FX::FXObject*,FX::FXSelector,void*); }; \
118  static const FX::FXMetaClass metaClass; \
119  virtual long handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr); \
120  virtual const FX::FXMetaClass* getMetaClass() const { return &metaClass; } \
121  friend FX::FXStream& operator<<(FX::FXStream& store,const classname* obj){return store.saveObject((FX::FXObjectPtr)(obj));} \
122  friend FX::FXStream& operator>>(FX::FXStream& store,classname*& obj){return store.loadObject((FX::FXObjectPtr&)(obj));} \
123  private:
124 
126 /// Macro to set up abstract class implementation
127 #define FXIMPLEMENT_ABSTRACT(classname,baseclassname,mapping,nmappings) \
128  const FX::FXMetaClass classname::metaClass(#classname,NULL,&baseclassname::metaClass,mapping,nmappings,sizeof(classname::FXMapEntry)); \
129  long classname::handle(FX::FXObject* sender,FX::FXSelector sel,void* ptr){ \
130  const FXMapEntry* me=(const FXMapEntry*)metaClass.search(sel); \
131  return me ? (this->* me->func)(sender,sel,ptr) : baseclassname::handle(sender,sel,ptr); \
132  }
133 
135 /// MetaClass of a class
136 #define FXMETACLASS(classname) (&classname::metaClass)
137 
139 /// Set up map type
140 #define FXDEFMAP(classname) static const classname::FXMapEntry
142 /// Define range of function types
143 #define FXMAPTYPES(typelo,typehi,func) {FXSEL(typelo,FX::MINKEY),FXSEL(typehi,FX::MAXKEY),&func}
145 /// Define range of function types
146 #define FXMAPTYPE(type,func) {FXSEL(type,FX::MINKEY),FXSEL(type,FX::MAXKEY),&func}
148 /// Define range of functions
149 #define FXMAPFUNCS(type,keylo,keyhi,func) {FXSEL(type,keylo),FXSEL(type,keyhi),&func}
151 /// Define one function
152 #define FXMAPFUNC(type,key,func) {FXSEL(type,key),FXSEL(type,key),&func}
153 
154 
155 /**
156 * Object is the base class for all objects in FOX; in order to receive
157 * messages from the user interface, your class must derive from Object.
158 * The Object class also provides serialization facilities, with which
159 * you can save and restore the object's state. If you've subclassed
160 * from Object, you can save your subclasses' state by overloading the
161 * save() and load() functions and use the stream API to serialize its
162 * member data.
163 */
164 class FXAPI FXObject {
165  FXDECLARE(FXObject)
166 public:
167 
168  /// Called for unhandled messages
169  virtual long onDefault(FXObject*,FXSelector,void*);
170 
171 public:
172 
173  /// Get class name of some object
174  const FXchar* getClassName() const;
175 
176  /// Check if object is member of metaclass
177  bool isMemberOf(const FXMetaClass* metaclass) const;
178 
179  /// Try handle message safely
180  virtual long tryHandle(FXObject* sender,FXSelector sel,void* ptr);
181 
182  /// Save object to stream
183  virtual void save(FXStream& store) const;
184 
185  /// Load object from stream
186  virtual void load(FXStream& store);
187 
188  /// Virtual destructor
189  virtual ~FXObject();
190  };
191 
192 }
193 
194 #endif
Definition: FXObject.h:36
char FXchar
Definition: fxdefs.h:387
const FXchar * getClassName() const
Ask class name.
Definition: FXObject.h:76
Definition: FXObject.h:46
unsigned int FXuint
Definition: fxdefs.h:396
Describes a FOX object.
Definition: FXObject.h:65
Definition: FXObject.h:47
FXuint FXSelector
Association key.
Definition: FXObject.h:53
#define FXAPI
Definition: fxdefs.h:122
FXString name(const FXString &file)
Return name and extension part of the path name.
FXString search(const FXString &pathlist, const FXString &file)
Search path list for this file, return full path name for first occurrence.
A stream is a way to serialize data and objects into a byte stream.
Definition: FXStream.h:99
const FXMetaClass * getBaseClass() const
Ask base class.
Definition: FXObject.h:79
Definition: FXObject.h:35
Object is the base class for all objects in FOX; in order to receive messages from the user interface...
Definition: FXObject.h:166
#define FXDECLARE(classname)
Macro to set up class declaration.
Definition: FXObject.h:92

Copyright © 1997-2005 Jeroen van der Zijp