00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file True_RefCount_Policy.h 00006 * 00007 * $Id: True_RefCount_Policy.h 69182 2005-11-03 17:38:46Z ossama $ 00008 * 00009 * Header file for TAO's true reference count policy (unrelated to 00010 * CORBA policies). 00011 * 00012 * @author Ossama Othman <ossama@dre.vanderbilt.edu> 00013 */ 00014 //============================================================================= 00015 00016 #ifndef TAO_TRUE_REFCOUNT_POLICY_H 00017 #define TAO_TRUE_REFCOUNT_POLICY_H 00018 00019 #include /**/ "ace/pre.h" 00020 00021 #include "tao/AnyTypeCode/TAO_AnyTypeCode_Export.h" 00022 00023 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00024 # pragma once 00025 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00026 00027 #include "tao/orbconf.h" 00028 00029 #include "ace/Thread_Mutex.h" 00030 #include "ace/Atomic_Op.h" 00031 00032 TAO_BEGIN_VERSIONED_NAMESPACE_DECL 00033 00034 namespace TAO 00035 { 00036 /** 00037 * @class True_RefCount_Policy 00038 * 00039 * @brief True reference counting policy. 00040 * 00041 * This class is intended to be used as a "policy" argument to a 00042 * host class template that requires true/functional reference 00043 * counting. That class would then inherit privately from it like 00044 * so: 00045 * 00046 * @code 00047 * template <class RefCountPolicy> 00048 * class MyHostClass : private RefCountPolicy 00049 * { 00050 * public: 00051 * void my_add_ref (void) { this->RefCountPolicy::add_ref (); } 00052 * void my_remove_ref (void) { this->RefCountPolicy::remove_ref (); } 00053 * }; 00054 * @endcode 00055 * 00056 * and use it like so: 00057 * 00058 * @code 00059 * typedef MyHostClass<TAO::True_RefCount_Policy> MyRefCountedClass; 00060 * MyRefCountedClass * p = new MyRefCountedClass; 00061 * ... 00062 * p->my_remove_ref (); 00063 * @endcode 00064 * 00065 * @note Ideally, the host class should declare a protected 00066 * destructor to enforce proper memory management through the 00067 * reference counting mechanism, i.e. to prevent clients from 00068 * calling @c operator @c delete() directly on the host class 00069 * object. 00070 */ 00071 class TAO_AnyTypeCode_Export True_RefCount_Policy 00072 { 00073 public: 00074 00075 /// Increase the reference count on this object. 00076 void add_ref (void); 00077 00078 /// Decrease the reference count on this object. 00079 /** 00080 * Decrease the reference count on this object. Once the 00081 * reference count drops to zero, call @c operator @c delete() 00082 * on this object. 00083 */ 00084 void remove_ref (void); 00085 00086 protected: 00087 00088 /// Constructor. 00089 /** 00090 * @note This constructor is protected since it not meant to be 00091 * instantiated/used as a standalone object. 00092 */ 00093 True_RefCount_Policy (void); 00094 00095 /// Destructor. 00096 /** 00097 * @note The destructor must be virtual to ensure that subclass 00098 * destructors are called when the reference count drops to 00099 * zero, i.e. when @c remove_ref() calls @c operator 00100 * @c delete @c this. 00101 */ 00102 virtual ~True_RefCount_Policy (void); 00103 00104 private: 00105 /// Reference counter. 00106 ACE_Atomic_Op<TAO_SYNCH_MUTEX, unsigned long> refcount_; 00107 }; 00108 00109 } // End namespace TAO 00110 00111 TAO_END_VERSIONED_NAMESPACE_DECL 00112 00113 #ifdef __ACE_INLINE__ 00114 # include "tao/AnyTypeCode/True_RefCount_Policy.inl" 00115 #endif /* __ACE_INLINE__ */ 00116 00117 #include /**/ "ace/post.h" 00118 00119 #endif /* TAO_TRUE_REFCOUNT_POLICY_H */