// SharedPolicyTable.h #ifndef SharedPolicyTable_H #define SharedPolicyTable_H // This is the table which SharedPolicy objects share. It contains // the counts of references and managing references. It does not // maintain pointers to the policies. // // A flag indicates whether this table manages its associated object. // // Another flag indicates whether the object is valid. It is intiialized // to true and set false when set_invalid is called. // // There are methods to add managing and non-managing references. The // table is said to manage the object if it holds one or more managing // references. // #include #include class SharedPolicyTable { private: // attributes // Flag indicating that the associated object is valid. bool _valid; // Toltal count of managed references. int _ref_count; // Total count of managing references. int _mref_count; public: // constructor // constructor SharedPolicyTable() : _valid(true), _ref_count(0), _mref_count(0) { } public: // const methods // Return if the table has management. bool has_management() const { return _mref_count > 0; } // The table is valid if flag is set. bool is_valid() const { return _valid; } // Return the total number of references. int get_reference_count() const { return _ref_count; } // return the number of managing references. int get_managing_reference_count() const { return _mref_count; } public: // Non-const methods // Add a non-managing refernce void add_non_managing_reference() { ++_ref_count; }; // Add a managing reference. void add_managing_reference() { ++_ref_count; ++_mref_count; } // Drop a non-managing reference void drop_non_managing_reference() { --_ref_count; assert( _ref_count >= 0 ); }; // Drop a managing reference. void drop_managing_reference() { --_ref_count; --_mref_count; assert( _ref_count >= 0 ); assert( _mref_count >= 0 ); } // Set the object invalid. void set_invalid() { _valid = false; } }; std::ostream& operator<<(std::ostream& stream, const SharedPolicyTable& rhs); #endif