00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __MAGIC_OBJECT_H__
00026 #define __MAGIC_OBJECT_H__
00027
00028 #include <stdio.h>
00029 #include <iostream.h>
00030 #include <typeinfo>
00031 #include "magic/mdynamic.h"
00032 #include "magic/mdebug.h"
00033
00034 #define CArchive DumpContext
00035
00036 class Object;
00037 class Comparable;
00038
00039
00040 template <class TYPE> class Array;
00041 class Class;
00042 class DumpContext;
00043 class OStream;
00044 class IStream;
00045
00046 namespace MagiC {
00047 class String;
00048 }
00049 using namespace MagiC;
00050
00052
00053 typedef unsigned int uint;
00054 typedef unsigned char uchar;
00055
00060 inline int isnull (const Object& obj) {return ! (&obj);}
00061
00062 #define isnullref(ref) ((&ref)==NULL)
00063
00073 ostream& operator<< (ostream& out, const Object& obj);
00074
00084 OStream& operator<< (OStream& out, const Object& obj);
00085
00089 istream& operator>> (istream& in, Object& obj);
00090
00097 CArchive& operator<< (CArchive& out, const Object& obj);
00098
00099
00106 IStream& operator>> (IStream& in, Object& obj);
00107
00109
00110
00111
00112
00113
00115
00120 class Object {
00121 decl_dynamic (Object)
00122 public:
00123 Object () {mRefCount = 0;}
00124 virtual ~Object ();
00125
00129 virtual DumpContext& operator>> (DumpContext&) const;
00130
00133 virtual void operator>> (OStream&) const;
00134
00138 virtual IStream& operator<< (IStream&);
00139
00141 virtual ostream& operator>> (ostream&) const;
00142
00144 virtual istream& operator<< (istream&);
00145
00149 virtual Object* clone () const;
00150
00154 virtual void check () const {}
00155
00159 bool isOK () const;
00160
00164 const String& getclassname () const;
00165
00169 int is_a (const String& classname) const;
00170
00172 void incRef () {mRefCount++;}
00173
00175 int decRef () {mRefCount--; return mRefCount;}
00176
00178 int refCount () const {return mRefCount;}
00179
00180 #ifdef DEBUG_OBJECT_NEW
00181 #ifdef new
00182 #undef new
00183
00194 void* operator new[] (size_t size, const char* filen,
00195 int lineno, const char* funcn);
00196
00208 void* operator new (size_t size, const char* filen,
00209 int lineno, const char* funcn);
00210
00215 void* operator new[] (size_t size);
00216
00221 void* operator new (size_t size);
00222 #define new DEBUG_NEW
00223 #endif
00224 #endif
00225
00226 private:
00227 int mRefCount;
00228 };
00229
00230
00231
00232 extern const char nullchar;
00233
00234
00235
00237
00238
00239
00240
00241
00242
00244
00245 template <class TYPE>
00246 class Ref {
00247 public:
00248 Ref (TYPE* object) {mpObject = object; if (mpObject) mpObject->incRef();}
00249 Ref (const Ref<TYPE>& ref) {mpObject = ref.mpObject; if (mpObject) mpObject->incRef();}
00250 ~Ref () {if (mpObject && mpObject->decRef() == 0) delete mpObject;}
00251
00252 TYPE& object () {return *mpObject;}
00253 const TYPE& object () const {return *mpObject;}
00254 operator TYPE& () {return *mpObject;}
00255 operator const TYPE& () const {return *mpObject;}
00256 TYPE* operator -> () {return mpObject;}
00257 const TYPE* operator -> () const {return mpObject;}
00258 bool isNull () const {return mpObject==NULL;}
00259
00260 private:
00261 TYPE* mpObject;
00262 };
00263
00264
00265
00267
00268
00269
00270
00271
00273
00277 class Comparable : public Object {
00278 decl_dynamic (Comparable);
00279 public:
00280 Comparable () {;}
00281
00289 virtual int hashfunc (int hashsize) const {return 0;}
00290
00292 virtual int operator== (const Comparable& other) const=0;
00293
00297 virtual int compare (const Comparable& other) const {return 0;}
00298 };
00299
00301 extern int compareComparable (const void* a, const void* b);
00302
00303
00304
00306
00307
00308
00309
00310
00312
00315 class Int : public Comparable {
00316 decl_dynamic (Int)
00317 long data;
00318 public:
00319
00320 Int () {data=0;}
00321 Int (const Int& o) {data=o.data;}
00322 explicit Int (const String& o);
00323 explicit Int (int o) {data=o;}
00324 explicit Int (long o) {data=o;}
00325 int toInt () const {return data;}
00326 long toLong () const {return data;}
00327 long& toLongRef () {return data;}
00328 int operator < (const Int& o) {return data<o.data;}
00329 int operator == (const Int& o) {return data==o.data;}
00330 int hashfunc (int hashsize) const {return data % hashsize;}
00331 int operator == (const Comparable& o) const;
00332 int operator != (const Int& o) {return data!=o.data;}
00333 Object* clone () const {return new Int (*this);}
00334 };
00335
00336
00337
00339
00340
00341
00342
00343
00345
00348 class Float : public Comparable {
00349 double mValue;
00350 public:
00351
00352 Float () : mValue (0) {}
00353 Float (float f) : mValue (f) {}
00354 Float (const Float& o) : mValue (o.mValue) {}
00355 int operator == (const Comparable& o) const {return 0;}
00356 Object* clone () const {return new Float (*this);}
00357 operator double () const {return mValue;}
00358 };
00359
00360
00361
00363
00364
00365
00366
00367
00369
00373 class CPPModule {
00374 char* name;
00375 public:
00376 CPPModule (const char* n);
00377 ~CPPModule () {delete name;}
00378 };
00379
00380 #define decl_module(modname) CPPModule mod_##modname (#modname);
00381
00382
00383
00384
00385
00387
00388
00389
00390 #include "magic/mstring.h"
00391
00392
00393 #include "magic/mmagisupp.h"
00394
00395 #endif
00396
00397
00398
00399