00001
00025 #ifndef __MAGIC_OBJECT_H__
00026 #define __MAGIC_OBJECT_H__
00027
00028 #include <stdio.h>
00029 #include <iostream>
00030 #include <typeinfo>
00031
00032 #include "magic/mdynamic.h"
00033 #include "magic/mdebug.h"
00034
00035 #ifndef BEGIN_NAMESPACE
00036 #define BEGIN_NAMESPACE(ns) namespace ns {
00037 #ifdef QT_VERSION
00038 #define END_NAMESPACE }
00039 #else
00040 #define END_NAMESPACE } using namespace MagiC;
00041 #endif
00042 #endif
00043
00044 namespace MagiC {
00045
00046 class Object;
00047 class Comparable;
00048
00049
00050 class Class;
00051 class OStream;
00052 class TextOStream;
00053 class TextIStream;
00054 class DataOStream;
00055 class DataIStream;
00056 template <class TYPE> class Array;
00057
00058 class String;
00059 }
00060
00061
00062 using namespace std;
00063 BEGIN_NAMESPACE (MagiC);
00064
00066
00067
00072 inline int isnull (const Object& obj) {return ! (&obj);}
00073
00074 #define isnullref(ref) ((&ref)==NULL)
00075
00076 ostream& operator<< (ostream& out, const Object& obj);
00077 istream& operator>> (istream& in, Object& obj);
00078 OStream& operator<< (OStream& out, const Object& obj);
00079 TextOStream& operator<< (TextOStream& out, const Object& obj);
00080 TextIStream& operator>> (TextIStream& in, Object& obj);
00081 DataOStream& operator<< (DataOStream& out, const Object& obj);
00082 DataIStream& operator>> (DataIStream& in, Object& obj);
00083
00084
00085
00087
00088
00089
00090
00091
00093
00094 class Object {
00095 decl_dynamic (Object);
00096
00097 public:
00098 Object () {mRefCount = 0;}
00099 virtual ~Object ();
00100 virtual ostream& operator>> (ostream&) const;
00101 virtual istream& operator<< (istream&);
00102 virtual OStream& operator>> (OStream&) const;
00103 virtual TextOStream& operator>> (TextOStream&) const;
00104 virtual TextIStream& operator<< (TextIStream&);
00105 virtual DataOStream& operator>> (DataOStream&) const;
00106 virtual DataIStream& operator<< (DataIStream&);
00107 virtual Object* clone () const;
00108 virtual void check () const;
00109 bool isOK () const;
00110 const String& getclassname () const;
00111 int is_a (const String& classname) const;
00112 inline void incRef () {mRefCount++;}
00113 inline int decRef () {mRefCount--; return mRefCount;}
00114 inline int refCount () const {return mRefCount;}
00116 #ifdef DEBUG_OBJECT_NEW
00117 #ifdef new
00118 #undef new
00119 void* operator new[] (size_t size, const char* filen, int lineno, const char* funcn);
00120 void* operator new (size_t size, const char* filen, int lineno, const char* funcn);
00121 void* operator new[] (size_t size);
00122 void* operator new (size_t size);
00123 #define new DEBUG_NEW
00124 #endif
00125 #endif
00126
00127 private:
00128 int mRefCount;
00129 };
00130
00131 extern const char nullchar;
00132
00133
00134
00136
00137
00138
00139
00140
00141
00143
00144 template <class TYPE>
00145 class Ref {
00146 public:
00147 Ref (TYPE* object=NULL) {mpObject = object; if (mpObject) mpObject->incRef();}
00148 Ref (const Ref<TYPE>& ref) {mpObject = ref.mpObject; if (mpObject) mpObject->incRef();}
00149 ~Ref () {if (mpObject && mpObject->decRef() == 0) delete mpObject;}
00150
00151 TYPE& object () {return *mpObject;}
00152 const TYPE& object () const {return *mpObject;}
00153 operator TYPE& () {return *mpObject;}
00154 operator const TYPE& () const {return *mpObject;}
00155 TYPE* operator -> () {return mpObject;}
00156 const TYPE* operator -> () const {return mpObject;}
00157 TYPE& operator = (TYPE* object) {if (mpObject && mpObject->decRef() == 0) delete mpObject; mpObject = object; if (mpObject) mpObject->incRef();}
00158 TYPE& operator = (const Ref<TYPE>& ref) {if (mpObject && mpObject->decRef() == 0) delete mpObject; mpObject = ref.mpObject; if (mpObject) mpObject->incRef();}
00159 bool isNull () const {return mpObject==NULL;}
00160 TYPE* getReleasedPtr () {TYPE* tmp=mpObject; mpObject->decRef(); mpObject = NULL; return tmp;}
00161
00162 private:
00163 TYPE* mpObject;
00164 };
00165
00166
00167
00169
00170
00171
00172
00173
00175
00179 class Comparable : public Object {
00180 decl_dynamic (Comparable);
00181 public:
00182 Comparable () {;}
00183
00191 virtual int hashfunc (int hashsize) const {return 0;}
00192
00194 virtual int operator== (const Comparable& other) const=0;
00195
00199 virtual int compare (const Comparable& other) const {return 0;}
00200 };
00201
00203 extern int compareComparable (const void* a, const void* b);
00204
00205
00206
00208
00209
00210
00211
00212
00214
00217 class Int : public Comparable {
00218 decl_dynamic (Int)
00219 long data;
00220 public:
00221
00222 Int () {data=0;}
00223 Int (const Int& o) {data=o.data;}
00224 explicit Int (const String& o);
00225 explicit Int (int o) {data=o;}
00226 explicit Int (long o) {data=o;}
00227 int toInt () const {return data;}
00228 long toLong () const {return data;}
00229 long& toLongRef () {return data;}
00230 int operator < (const Int& o) {return data<o.data;}
00231 int operator == (const Int& o) {return data==o.data;}
00232 int hashfunc (int hashsize) const {return data % hashsize;}
00233 int operator == (const Comparable& o) const;
00234 int operator != (const Int& o) {return data!=o.data;}
00235 Object* clone () const {return new Int (*this);}
00236 };
00237
00238
00239
00241
00242
00243
00244
00245
00247
00250 class Float : public Comparable {
00251 double mValue;
00252 public:
00253
00254 Float () : mValue (0) {}
00255 Float (float f) : mValue (f) {}
00256 Float (const Float& o) : mValue (o.mValue) {}
00257 int operator == (const Comparable& o) const {return 0;}
00258 Object* clone () const {return new Float (*this);}
00259 operator double () const {return mValue;}
00260 };
00261
00262
00263
00265
00266
00267
00268
00269
00271
00275 class CPPModule {
00276 char* name;
00277 public:
00278 CPPModule (const char* n);
00279 ~CPPModule () {delete name;}
00280 };
00281
00282 #define decl_module(modname) CPPModule mod_##modname (#modname);
00283
00284
00285
00286
00287
00288
00289 END_NAMESPACE;
00290
00292
00293
00294
00295 #include "magic/mstring.h"
00296
00297
00298 #include "magic/mmagisupp.h"
00299 #endif
00300
00301
00302
00303
00304