// TestPolicy.h #ifndef TestPolicy_H #define TestPolicy_H // This is a class to test the policy interface and to provide // a flexible policy for testing other policies. // It takes management if it can. // // It will not give up management if it does not have it. // Include the PolicyPtr header if template mebers are not allowed. #ifdef DEFECT_NO_MEMBER_TEMPLATES_AT_ALL #include "ptr/AbsPolicy.h" #endif #include class SharedPolicyTable; class TestPolicy #ifdef AbsPolicy_H : public AbsPolicy #endif { private: // attributes bool _mgmt; bool _valid; private: // methods // output stream void ostr(std::ostream& stream) const { stream << "Test Policy has management " << int(_mgmt) << " and validity " << int(_valid); } public: // Test constructor. // set argument true to manage or false not to manage. TestPolicy(bool mgmt) : _mgmt(mgmt), _valid(true) { } // Simple constructor for Ptr interface. TestPolicy(const void* ptr) : _mgmt(ptr!=0), _valid(ptr!=0) { } //TestPolicy(void* ptr) : _mgmt(ptr!=0) { } // Copy constructor for Ptr interface. // We try to take management but do not if refused. #ifndef AbsPolicy_H template #endif TestPolicy(const void* ptr, AbsPolicy& pol) : _mgmt(false), _valid(true) { if ( pol.has_management() ) { if ( pol.give_management() ) { _mgmt = true; } } } // Has management. bool has_management() const { return _mgmt; } // Return true if this is the last policy for the object. bool has_last_management() const { return has_management(); } // Request to take management. bool take_management() { _mgmt = true; return has_management(); } // Request to give up management. bool give_management() { if ( _mgmt ) { _mgmt = false; return true; } else { return false; } } // Policy is valid. bool is_valid() const { return _valid; } // Policy is not shared. SharedPolicyTable* get_shared_policy_table() { return 0; }; // Set policy invalid. // Delete the pointer. template void delete_if_managing(T* pt) { if ( _mgmt ) { delete pt; } } // Output stream. friend std::ostream& operator<<(std::ostream& stream, const TestPolicy& rhs) { rhs.ostr(stream); return stream; } }; #endif