Main Page   Class Hierarchy   Compound List   File List   Compound Members  

mobject.h

00001 /***************************************************************************
00002  *   This file is part of the MagiC++ library.                             *
00003  *                                                                         *
00004  *   Copyright (C) 1998-2001 Marko Grönroos <magi@iki.fi>                  *
00005  *                                                                         *
00006  ***************************************************************************
00007  *                                                                         *
00008  *  This library is free software; you can redistribute it and/or          *
00009  *  modify it under the terms of the GNU Library General Public            *
00010  *  License as published by the Free Software Foundation; either           *
00011  *  version 2 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  *  Library General Public License for more details.                       *
00017  *                                                                         *
00018  *  You should have received a copy of the GNU Library General Public      *
00019  *  License along with this library; see the file COPYING.LIB.  If         *
00020  *  not, write to the Free Software Foundation, Inc., 59 Temple Place      *
00021  *  - Suite 330, Boston, MA 02111-1307, USA.                               *
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>         // Used by dynamic_cast
00031 #include "magic/mdynamic.h"
00032 #include "magic/mdebug.h"
00033 
00034 #define CArchive DumpContext
00035 
00036 class Object;
00037 class Comparable;
00038 
00039 // External classes:
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 //                       ___          o                                      //
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 //          ----              /  ___          o                 \            //
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 //                |     |     | V |  __   ---| |   | | /   )                //
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 // Ok, include this here now when the Object has been well defined
00384 // #include "magic/mclass.h"
00385 
00387 // Now we can include these default includes
00388 
00389 // Required by magisupp
00390 #include "magic/mstring.h"
00391 
00392 // All exception and assertion handling, etc.
00393 #include "magic/mmagisupp.h"
00394 
00395 #endif
00396 
00397 //
00398 
00399 

Generated at Tue Dec 4 19:53:26 2001 for MagiC++ by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001