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

mobject.h

Go to the documentation of this file.
00001 
00025 #ifndef __MAGIC_OBJECT_H__
00026 #define __MAGIC_OBJECT_H__
00027 
00028 #include <stdio.h>
00029 #include <iostream>
00030 #include <typeinfo>         // Used by dynamic_cast
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     // Internal predeclarations
00046     class Object;
00047     class Comparable;
00048     
00049     // External predeclarations
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 // using namespace MagiC;
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 //                       ___          o                                      //
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 //          ----              /  ___          o                 \            //
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 //                |     |     | V |  __   ---| |   | | /   )                //
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 // Ok, include this here now when the Object has been well defined
00286 // #include "magic/mclass.h"
00287 
00288 
00289 END_NAMESPACE;
00290 
00292 // Now we can include these default includes
00293 
00294 // Required by magisupp
00295 #include "magic/mstring.h"
00296 
00297 // All exception and assertion handling, etc.
00298 #include "magic/mmagisupp.h"
00299 #endif
00300 
00301 //
00302 
00303 
00304 

Generated on Thu Feb 10 20:06:42 2005 for LibMagiC by doxygen1.2.18