// DeletePolicy.h #ifndef DeletePolicy_H #define DeletePolicy_H // This policy takes management and will not relinquish. // It throws an exception if it is copied from a policy that will // not relinquish. For safety, it also throws an exception if it // is constructed from a policy that does not have management. // management, e.g. a policy of the same type. #include // Include the PolicyPtr header if template mebers are not allowed. #ifdef DEFECT_NO_MEMBER_TEMPLATES_AT_ALL #include "ptr/AbsPolicy.h" #endif #include "ptr/PtrException.h" class SharedPolicyTable; class DeletePolicy #ifdef AbsPolicy_H : public AbsPolicy #endif { private: // Output stream. void ostr(std::ostream& stream) const; public: // Default constructor. DeletePolicy(const void* ptr) { } // Constructor from another policy. #ifndef AbsPolicy_H template #endif DeletePolicy(const void* ptr, AbsPolicy& pol) { if ( pol.has_management() ) { if ( ! pol.give_management() ) throw PtrCannotGetManagement(); } else throw PtrDoesNotHaveManagement(); } // Has management. bool has_management() const { return true; } // Request to take management. bool take_management() { return true; } // Request to give up management. bool give_management() { return false; } // Return true if this is the last policy for the object. bool has_last_management() const { return true; } // Policy is always valid. bool is_valid() const { return true; } // Policy is not shared. SharedPolicyTable* get_shared_policy_table() { return 0; }; // Delete object (we always manage). // Syntax is a little strange so we can delete const pointers // on compilers which do not allow it (MSVC++). template void delete_if_managing(const T* pt) { delete (T*) pt; } // Output stream. friend std::ostream& operator<<(std::ostream& stream, const DeletePolicy& rhs); }; #endif