// Secured.h -- Secured template class // (c) Copyright Schlumberger Technology Corp., unpublished work, created // 1999. This computer program includes Confidential, Proprietary // Information and is a Trade Secret of Schlumberger Technology Corp. All // use, disclosure, and/or reproduction is prohibited unless authorized // in writing. All Rights Reserved. #if !defined(SLBCSP_SECURED_H) #define SLBCSP_SECURED_H #include "RsrcCtrlr.h" #include "Retained.h" // Using the "resource acquisition is initialization" idiom, the // Secured template manages acquiring and releasing a Securable object // (the resource). Classes derived from Securable may need to // specialize this template's c'tor and d'tor rather than take the // default. template class Secured : public Retained { public: // Types // C'tors/D'tors explicit Secured(T const &rResource = T()); Secured(Secured const &rhs); virtual ~Secured() throw(); // Operators Secured & operator=(T const &rhs); // Operations // Access // Predicates protected: // Types // C'tors/D'tors // Operators // Operations void Acquire(); virtual void DoAfterAssignment(); virtual void DoBeforeAssignment(); void Release(); // Access // Predicates // Variables private: // Types // C'tors/D'tors // Operators // Operations void DoAcquire(); void DoRelease(); // Access // Predicates // Variables bool m_fIsSecured; }; ///////////////////////// TEMPLATE METHODS ////////////////////////////// /////////////////////////// PUBLIC ///////////////////////////////// // Types // C'tors/D'tors template Secured::Secured(T const &rResource) : Retained(rResource), m_fIsSecured(false) { Acquire(); } template Secured::Secured(Secured const &rhs) : Retained(rhs.m_Resource), m_fIsSecured(false) { Acquire(); } template Secured::~Secured() throw() { try { Release(); } catch (...) { // don't allow exceptions to propagate out of destructors } } // Operators template Secured & Secured::operator=(T const &rhs) { Retained::operator=(rhs); return *this; } // Operations // Access // Predicates // Static Variables /////////////////////////// PROTECTED ///////////////////////////////// // C'tors/D'tors // Operators // Operations template void Secured::Acquire() { if (m_Resource && !m_fIsSecured) { DoAcquire(); m_fIsSecured = true; } } template void Secured::DoAfterAssignment() { Retained::DoAfterAssignment(); Acquire(); } template void Secured::DoBeforeAssignment() { Release(); Retained::DoBeforeAssignment(); } template void Secured::Release() { if (m_Resource && m_fIsSecured) { DoRelease(); m_fIsSecured = false; } } // Access // Predicates // Static Variables /////////////////////////// PRIVATE ///////////////////////////////// // C'tors/D'tors // Operators // Operations template void Secured::DoAcquire() { m_Resource->Secure(); } template void Secured::DoRelease() { m_Resource->Abandon(); } // Access // Predicates // Static Variables #endif // SLBCSP_SECURED_H