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

RefCount

00001 /*
00002  * NOTE: "zlib/libpng" style License
00003  *
00004  *                 ----=== s t d n e t ===----
00005  *
00006  * Copyright (c) 2002 Warwick Molloy   w-molloy@users.sf.net
00007  *
00008  * Copyright (c) 2002 Stdnet Community
00009  * HTTP://Sourceforge.net/projects/stdnet
00010  *
00011  * All references to "software" refer to the stdnet.
00012  *
00013  * This software is provided 'as-is', without any express or
00014  * implied warranty. In no event will the authors be held liable
00015  * for any damages arising from the use of this software.
00016  *
00017  * Permission is granted to anyone to use this software for any
00018  * purpose, including commercial applications, and to alter it
00019  * and redistribute it freely, subject to the following
00020  * restrictions:
00021  *
00022  * 1.  The origin of this software must not be misrepresented;
00023  *     you must not claim that you wrote the original software.
00024  *     If you use this software in a product, an acknowledgment
00025  *     in the product documentation would be appreciated but
00026  *     is not required.
00027  *
00028  * 2.  Altered source versions must be plainly marked as such,
00029  *     and must not be misrepresented as being the original
00030  *     software.
00031  *
00032  * 3.  This notice may not be removed or altered from any source
00033  *     distribution.
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 //  Let the world know that a reference type has been
00045 //  defined.
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     } // -- namespace system --
00195 } // ------ namespace stdbase --
00196 
00197 //  ----- Global Operators ------
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 

Generated at Tue Aug 13 14:19:39 2002 for stdnet2 by doxygen1.2.9.1 written by Dimitri van Heesch, © 1997-2001