00001 /* 00002 * DetachFinalizer.hpp 00003 * 00004 * Copyright 2001-2008 by Oracle. All rights reserved. 00005 * 00006 * Oracle is a registered trademarks of Oracle Corporation and/or its 00007 * affiliates. 00008 * 00009 * This software is the confidential and proprietary information of Oracle 00010 * Corporation. You shall not disclose such confidential and proprietary 00011 * information and shall use it only in accordance with the terms of the 00012 * license agreement you entered into with Oracle. 00013 * 00014 * This notice may not be removed or altered. 00015 */ 00016 #ifndef COH_DETACH_FINALIZER_HPP 00017 #define COH_DETACH_FINALIZER_HPP 00018 00019 #include "coherence/lang/compatibility.hpp" 00020 00021 #include "coherence/lang/FinalizableBlock.hpp" 00022 00023 COH_OPEN_NAMESPACE2(coherence,lang) 00024 00025 00026 /** 00027 * Finalizer which detaches from an Object upon deletion. The finalizer is 00028 * templated allowing it to work with both const and non-const attachments. 00029 */ 00030 template<class T> class DetachFinalizer 00031 : public FinalizableBlock::Finalizer 00032 { 00033 // ----- constructor ---------------------------------------------------- 00034 00035 public: 00036 /** 00037 * Construct a DetachFinalizer to automatically detach from a 00038 * previously attached Object. The caller must have an unmatched 00039 * attacment to the Object. 00040 * 00041 * @param pDetach pointer to Object to detach from during 00042 * finalization 00043 */ 00044 DetachFinalizer(T* pDetach = NULL) 00045 : m_pDetach(pDetach) 00046 { 00047 } 00048 00049 /** 00050 * Destruct the DetachFinalizer, detaching from the Object in the 00051 * process. 00052 */ 00053 virtual ~DetachFinalizer() 00054 { 00055 T* pDetach = m_pDetach; 00056 if (NULL != pDetach) 00057 { 00058 pDetach->_detach(); 00059 } 00060 } 00061 00062 // ----- DetachFinalizer interface -------------------------------------- 00063 00064 public: 00065 /** 00066 * Set the Object which the finalizer will detach from. If there was 00067 * already an Object associated with this finalizer, it will be 00068 * detached as part of this call. 00069 * 00070 * @param pDetach the Object to detach from upon destruction 00071 * 00072 * @return a pointer to this DetachFinalizer 00073 */ 00074 DetachFinalizer* set(T* pDetach) 00075 { 00076 T* pOld = m_pDetach; 00077 m_pDetach = pDetach; 00078 00079 if (NULL != pOld) 00080 { 00081 pOld->_detach(); 00082 } 00083 return this; 00084 } 00085 00086 // ----- data members --------------------------------------------------- 00087 00088 private: 00089 /** 00090 * Pointer to Object to detach from. 00091 */ 00092 T* m_pDetach; 00093 }; 00094 00095 COH_CLOSE_NAMESPACE2 00096 00097 #endif // COH_DETACH_FINALIZER_HPP