![]() |
The Simons
|
00001 00002 // 00003 // SCL : Simons Component Library 00004 // 00006 // 00007 // Filename: Object.h 00008 // Contents: Object class 00009 // Author: Anthony J H Simons 00010 // Revised: 31 October 2005 00011 00012 // This software is distributed free in the hope that others will 00013 // find it useful. However, it comes WITHOUT ANY WARRANTY. No 00014 // liability can be accepted for software failure, merchantability 00015 // or fitness for a particular purpose. You can redistribute this 00016 // software in its original form, or in a modified form, provided 00017 // that this disclaimer is retained in the file banner. 00018 00019 #ifndef SCL_OBJECT 00020 #define SCL_OBJECT 00021 00022 #include "Basic.h" // Include the basic types 00023 #include "ObjectID.h" // Include my pointer type 00024 00025 #include "StringID.h" // Depends on the pointer types 00026 #include "TypeID.h" 00027 #include "ReaderID.h" 00028 #include "WriterID.h" 00029 00030 #if defined(__LP64__) || defined (LP64_) 00031 #undef MEMORY_MODEL 00032 #define MEMORY_MODEL 64 00033 #endif 00034 00046 00047 class Object { 00048 private: 00049 static unsigned total; // total number of objects 00050 unsigned referenceCount; // the reference count 00051 protected: 00052 Object(const Object&); 00053 Void nullPointer(StringID) const; 00054 Void typeFailure(StringID) const; 00055 Void undefined(StringID) const; 00056 public: 00057 Object(); 00058 virtual ~Object(); 00059 Natural references() const; 00060 Natural acquire(); 00061 Natural release(); 00062 virtual ObjectID clone() const; 00063 virtual TypeID type() const; 00064 Natural identity() const; 00065 virtual Natural hash() const; 00066 virtual Order compare(ObjectID) const; 00067 virtual Void readOn(ReaderID); 00068 virtual Void writeOn(WriterID) const; 00069 virtual Boolean toBoolean() const; 00070 virtual Character toCharacter() const; 00071 virtual Natural toNatural() const; 00072 virtual Integer toInteger() const; 00073 virtual Decimal toDecimal() const; 00074 }; 00075 00085 00086 class Null : public Object { 00087 public: 00088 Null(); 00089 virtual ~Null(); 00090 }; 00091 00093 00097 inline Natural Object::references() const { 00098 return referenceCount; 00099 } 00100 00110 inline Natural Object::identity() const { 00111 #if MEMORY_MODEL == 32 00112 return reinterpret_cast<Natural>(this); 00113 #elif MEMORY_MODEL == 64 00114 const Natural* ptr = reinterpret_cast<const Natural*>(this); 00115 return (*ptr) ^ ( *(ptr + 1)); 00116 #else 00117 throw("No memory model defined"); 00118 #endif 00119 } 00120 00122 00124 inline Null::Null() {} 00125 00127 inline Null::~Null() {} 00128 00130 00134 inline ObjectID& ObjectID::operator=(const Object* object) { 00135 assign(object); 00136 return *this; 00137 } 00138 00142 inline ObjectID& ObjectID::operator=(const ObjectID& pointer) { 00143 assign(pointer); 00144 return *this; 00145 } 00146 00149 inline const Object* ObjectID::pointer() const { 00150 return basicPointer; 00151 } 00152 00153 00155 00157 inline bool operator==(const ObjectID& first, const ObjectID& second) { 00158 return first.basicPointer == second.basicPointer; 00159 } 00160 00162 inline bool operator!=(const ObjectID& first, const ObjectID& second) { 00163 return first.basicPointer != second.basicPointer; 00164 } 00165 00166 00177 template <class T> 00178 T* toType(const ObjectID& pointer) { 00179 T* object = dynamic_cast<T*>(pointer.basicPointer); 00180 if (object == 0) pointer.typeFailure(new T); 00181 return object; 00182 } 00183 00197 template <class T> 00198 T* tryType(const ObjectID& pointer) { 00199 return dynamic_cast<T*>(pointer.basicPointer); 00200 } 00201 00202 #endif