ObjectRef.h

Go to the documentation of this file.
00001 #ifndef JAVA_LANG_COMPARABLE_H
00002 #include <java_lang/Comparable.h>
00003 #endif /* JAVA_LANG_COMPARABLE_H */
00004 // All header files that are included in Object.h should move its includes to top
00005 
00006 #ifndef JAVA_LANG_OBJECTREF_H
00007 #define JAVA_LANG_OBJECTREF_H 1
00008 
00009 namespace java {
00010   namespace lang {
00011 
00042     class JAVA_LANG_SPEC ObjectRef : public Comparable {
00043       
00044     public:
00045       
00046       ObjectRef() : value(0) {}
00047         
00048       ObjectRef(const Object* src) : value((Object*)src) {
00049         if (value) {
00050           refCount = -1;
00051           (value->refCount)++;
00052         }
00053       }
00054 
00055       ObjectRef(const Object& src) : value((Object*)&src) {}
00056         
00057       ObjectRef(const ObjectRef& src) 
00058         : value(src.value) {
00059         refCount = src.refCount;
00060         if (refCount)
00061           (value->refCount)++;
00062       }
00063 
00064       ObjectRef& operator=(const Object* src) {
00065         if (refCount && !(--(value->refCount)))
00066           delete value;
00067         value = (Object*)src;
00068         if (value) {
00069           refCount = -1;
00070           (value->refCount)++;
00071         } else refCount = 0;
00072         return *this;
00073       }
00074       
00075       ObjectRef& operator=(const Object& src) {
00076         if (refCount && !(--(value->refCount)))
00077           delete value;
00078         value = (Object*)&src;
00079         refCount = 0;
00080         return *this;
00081       }
00082         
00083       inline ObjectRef& operator=(const ObjectRef& src) {
00084         if (this == &src) return *this;
00085         if (src.refCount) (src.value->refCount)++;
00086         if (refCount && !(--(value->refCount)))
00087           delete value;
00088         value = src.value;
00089         refCount = src.refCount;
00090         return *this;
00091       }
00092 
00093       jboolean operator==(const Object* ptr) const { return value == ptr;}
00094       jboolean operator==(const Object& obj) const;
00095       jboolean operator!=(const Object* ptr) const { return value != ptr;}
00096       jboolean operator!=(const Object& obj) const { return !(*this == obj);}
00097       Object* operator->() const { return value;}
00098       Object& operator*() const { return *value;}
00099       virtual ~ObjectRef() {         
00100         if (refCount && !(--(value->refCount)))
00101           delete value;
00102       }
00103       virtual jboolean equals(const Object&) const;
00104       virtual jboolean equals(const ObjectRef&) const;
00105       virtual jint hashCode() const { return (value) ? value->hashCode() : 0;}
00106       virtual String toString() const;
00107       virtual jint compareTo(const Object&) const;
00108       virtual jint compareTo(const ObjectRef&) const;
00109 
00114       ObjectRef set(const ObjectRef& src = 0) {
00115         ObjectRef old;
00116         old.value = value;
00117         old.refCount = refCount;
00118         if (src == 0) {
00119           value = 0;
00120           refCount = 0;
00121         } else {
00122           if (src.refCount) (src.value->refCount)++;
00123           value = src.value;
00124           refCount = src.refCount;
00125         }
00126         return old;
00127       }
00128 
00135       jboolean dup();
00136 
00142       jboolean weaken();
00143 
00144     protected:
00145 
00146       Object* value;
00147 
00148     private:
00149 
00150       REGISTER_MEMBERS;
00151 
00152     };
00153 
00157     template <class Type>
00158       class Ref : public ObjectRef {
00159 
00160     public:
00161 
00162       Ref(const Type *src = 0);
00163       Ref(const Type& src);
00164       Ref(const ObjectRef& src);
00165       Ref(const Ref& src);
00166       Ref& operator=(const Type* src);
00167       Ref& operator=(const Type& src);
00168       Ref& operator=(const ObjectRef& src); 
00169       Ref& operator=(const Ref& src);
00170       Type* operator->() const { return tvalue;}
00171       Type& operator*() const { return *tvalue;}
00172       Ref set(const Ref& src = 0);
00173 
00174     protected:
00175 
00176       Type *tvalue;
00177 
00178     private:
00179 
00180       REGISTER_MEMBERS;
00181 
00182     };
00183 
00184   } // namespace lang
00185 } // namespace java
00186 
00187 #endif /* JAVA_LANG_OBJECTREF_H */

Generated on Fri May 16 11:54:43 2008 for CrossPlatformJavaLikeC++API by  doxygen 1.5.3