00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef __system_RefCount__
00037 #define __system_RefCount__
00038
00039 #include <stdnet/system/Abstract>
00040
00041 namespace stdbase {
00042 namespace system {
00043
00044
00045
00046
00047 #define __system_Reference_Type__
00048
00049
00050 class Object;
00051
00059 class ObjectRef
00060 {
00061 private:
00062 mutable Abstract * pointer;
00063
00064 public:
00071 ObjectRef( Object * obj);
00072
00079 ObjectRef( Abstract * obj);
00080
00087 ObjectRef( const ObjectRef & r);
00088
00092 ~ObjectRef( void );
00093
00097 const ObjectRef &
00098 operator =( const ObjectRef & r) const;
00099
00103 bool
00104 operator == ( const ObjectRef & r) const;
00105
00109 bool
00110 operator != ( const ObjectRef & r) const;
00111
00116 Object * get();
00117
00122 const Object * get() const;
00123 };
00124
00129 template < class derived_object >
00130 class reference
00131 {
00132 public:
00133 typedef derived_object object_class;
00134
00135 private:
00136 mutable ObjectRef ref;
00137
00138 public:
00139 reference( object_class * p)
00140 : ref( dynamic_cast<Object *>(p) )
00141 {};
00142
00143 reference( Object * p)
00144 : ref( p )
00145 {};
00146
00147 reference( const reference<object_class> & r)
00148 : ref( r.get() )
00149 {};
00150
00151 reference( const ObjectRef & o)
00152 : ref( o )
00153 {};
00154
00155 ~reference()
00156 {};
00157
00158 const reference<object_class> &
00159 operator = ( const reference<object_class> & r)
00160 {
00161 ref = r.ref;
00162 return *this;
00163 }
00164
00165 object_class *
00166 operator -> ()
00167 { return dynamic_cast<object_class*>( ref.get() ); }
00168
00169 const object_class *
00170 operator -> () const
00171 { return dynamic_cast<const object_class *>( ref.get() ); }
00172
00173 bool
00174 operator ==( const reference & r) const
00175 { return ref == r.ref; }
00176
00177 bool
00178 operator ==( const ObjectRef & object_ref) const
00179 { return ref == object_ref; }
00180
00181 bool
00182 operator !=( const reference & r) const
00183 { return ref != r.ref; }
00184
00185 bool
00186 operator !=( const ObjectRef & object_ref) const
00187 { return ref != object_ref; }
00188
00189 const ObjectRef &
00190 get() const
00191 { return ref; }
00192 };
00193
00194 }
00195 }
00196
00197
00198
00199
00209 template <class obj_dst, class ref_src>
00210 stdbase::system::reference< obj_dst>
00211 ref_cast(
00212 const ref_src & original
00213 )
00214 {
00215 return stdbase::system::
00216 reference< obj_dst>( original.get() );
00217 }
00218
00224 extern const stdbase::system::ObjectRef null;
00225
00226 #endif // __system_RefCount__
00227