// SharedNullPolicy.h #ifndef SharedNullPolicy_H #define SharedNullPolicy_H // Shared policies hold a pointer to a shared policy table so that // policies created from one another point to the same table. This // table maintains the total number of policy references and the // number of those which manage their associated object. // // This policy provides no management an registers itself as such // in the table. // #ifdef DEFECT_NO_MEMBER_TEMPLATES_AT_ALL #include "ptr/AbsPolicy.h" #endif #include "ptr/SharedPolicyTable.h" #include "ptr/PtrException.h" #include class SharedNullPolicy #ifdef AbsPolicy_H : public AbsPolicy #endif { private: // attributes // The table. SharedPolicyTable* _ptable; private: // Output stream. void ostr(std::ostream& stream) const; // Drop this reference from the current table. // Delete the table if there are no other references. void drop_reference() { if ( _ptable == 0 ) return; _ptable->drop_non_managing_reference(); if ( _ptable->get_reference_count() == 0 ) delete _ptable; } public: // Methods for policy interface. // Constructor. // This starts a new table unless the referenced address is 0. SharedNullPolicy(const void* ptr) : _ptable( new SharedPolicyTable() ) { _ptable->add_non_managing_reference(); if ( ! ptr ) _ptable->set_invalid(); } // Constructor from another policy. // Set argument true to share management. // This will share the table in the passed policy. // The policy must have a _ptable attribute. #ifndef AbsPolicy_H template #endif SharedNullPolicy(const void* pvd, AbsPolicy& pol) : _ptable(pol.get_shared_policy_table()) { if ( ! _ptable ) _ptable = new SharedPolicyTable(); _ptable->add_non_managing_reference(); } // Destructor. ~SharedNullPolicy() { drop_reference(); } // Assignment from the same kind of policy. SharedNullPolicy& operator=(const SharedNullPolicy& rhs) { if ( _ptable == rhs._ptable ) return *this; drop_reference(); _ptable = rhs._ptable; if ( _ptable ) _ptable->add_non_managing_reference(); return *this; } // Has management. bool has_management() const { return false; } // Take management. bool take_management() { return false; } // Give up management. bool give_management() { return true; } // Return true if this is the last managing policy for the object. bool has_last_management() const { return false; } // Policy is valid if the table is valid. bool is_valid() const { return _ptable && _ptable->is_valid(); } // Return the shared policy table. SharedPolicyTable* get_shared_policy_table() { return _ptable; } // Delete if managing pointer. // We never manage. template void delete_if_managing(T* pt) { } // Output stream. friend std::ostream& operator<<(std::ostream& stream, const SharedNullPolicy& rhs); }; #endif