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

RefCount.cpp

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 #include <stdnet/config.h>
00037 #include <stdnet/system/RefCount>
00038 #include <stdnet/system/Object>
00039 
00040 //  The null reference...
00041 //
00042 const stdbase::system::ObjectRef null =
00043     stdbase::system::ObjectRef(
00044                 static_cast<stdbase::system::Object*>( NULL )
00045             );
00046 
00047 namespace stdbase {
00048     namespace system {
00049 
00050 ObjectRef:: ObjectRef( Abstract * obj)
00051     :   pointer( obj )
00052 {
00053     if ( pointer != 0 )
00054     {
00055         pointer-> inc();
00056     }
00057 }
00058 
00059 ObjectRef:: ObjectRef( Object * obj)
00060     :   pointer( dynamic_cast< Abstract *>(obj) )
00061 {
00062     if ( pointer != 0 )
00063     {
00064         pointer-> inc();
00065     }
00066 }
00067 
00068 ObjectRef::  ObjectRef( const ObjectRef & r)
00069     :   pointer( r.pointer )
00070 {
00071     if ( pointer != 0 )
00072     {
00073         pointer-> inc();
00074     }
00075 }
00076 
00077 ObjectRef:: ~ObjectRef( void )
00078 {
00079     if ( pointer != 0 &&
00080          pointer-> dec() == 0 )
00081     {
00082         delete pointer;
00083     }
00084 }
00085 
00086 const ObjectRef &
00087 ObjectRef:: operator =( const ObjectRef & r) const
00088 {
00089     if ( pointer != 0 && pointer-> dec() == 0)
00090     {
00091         delete pointer;
00092     }
00093     pointer = r.pointer;
00094     if ( pointer != 0 )
00095     {
00096         pointer-> inc();
00097     }
00098     return *this;
00099 }
00100 
00101 bool
00102 ObjectRef:: operator == ( const ObjectRef & r) const
00103 {
00104     return pointer == r.pointer;
00105 }
00106 
00107 bool
00108 ObjectRef:: operator != ( const ObjectRef & r) const
00109 {
00110     return pointer != r.pointer;
00111 }
00112 
00113 Object * ObjectRef::
00114     get()
00115 {   return dynamic_cast< Object *>(pointer); }
00116 
00117 const Object * ObjectRef::
00118     get() const
00119 {   return dynamic_cast< const Object *>(pointer); }
00120 
00121     } // -- namespace system --
00122 } // ------ namespace stdbase --
00123 

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