00001 /* 00002 * DetachFinalizer.hpp 00003 * 00004 * Copyright (c) 2000, 2020, Oracle and/or its affiliates. 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 * attachment to the Object. 00040 * 00041 * @param pDetach pointer to Object to detach from during 00042 * finalization 00043 * @param fEscaped if the attachment was escaped 00044 */ 00045 DetachFinalizer(T* pDetach = NULL, bool fEscaped = true) 00046 : m_pDetach(pDetach), m_fEscaped(fEscaped) 00047 { 00048 } 00049 00050 /** 00051 * Destruct the DetachFinalizer, detaching from the Object in the 00052 * process. 00053 */ 00054 virtual ~DetachFinalizer() 00055 { 00056 T* pDetach = m_pDetach; 00057 if (NULL != pDetach) 00058 { 00059 pDetach->_detach(m_fEscaped); 00060 } 00061 } 00062 00063 // ----- DetachFinalizer interface -------------------------------------- 00064 00065 public: 00066 /** 00067 * Set the Object which the finalizer will detach from. If there was 00068 * already an Object associated with this finalizer, it will be 00069 * detached as part of this call. 00070 * 00071 * @param pDetach the Object to detach from upon destruction 00072 * @param fEscaped if the attachment was escaped 00073 * 00074 * @return a pointer to this DetachFinalizer 00075 */ 00076 DetachFinalizer* set(T* pDetach, bool fEscaped = true) 00077 { 00078 T* pOld = m_pDetach; 00079 bool fEscapedOld = m_fEscaped; 00080 00081 m_pDetach = pDetach; 00082 m_fEscaped = fEscaped; 00083 00084 if (NULL != pOld) 00085 { 00086 pOld->_detach(fEscapedOld); 00087 } 00088 return this; 00089 } 00090 00091 // ----- data members --------------------------------------------------- 00092 00093 private: 00094 /** 00095 * Pointer to Object to detach from. 00096 */ 00097 T* m_pDetach; 00098 00099 /** 00100 * The detach escape type. 00101 */ 00102 bool m_fEscaped; 00103 }; 00104 00105 COH_CLOSE_NAMESPACE2 00106 00107 #endif // COH_DETACH_FINALIZER_HPP