reference_counted_object.hpp

Go to the documentation of this file.
00001 #ifndef REFERENCE_COUNTED_OBJECT_HPP_INCLUDED
00002 #define REFERENCE_COUNTED_OBJECT_HPP_INCLUDED
00003 
00004 #include "boost/intrusive_ptr.hpp"
00005 
00006 class reference_counted_object
00007 {
00008 public:
00009     reference_counted_object() : count_(0) {}
00010     reference_counted_object(const reference_counted_object& /*obj*/) : count_(0) {}
00011     reference_counted_object& operator=(const reference_counted_object& /*obj*/) {
00012         return *this;
00013     }
00014     virtual ~reference_counted_object() {}
00015 
00016     void add_ref() const { ++count_; }
00017     void dec_ref() const { if(--count_ == 0) { delete const_cast<reference_counted_object*>(this); } }
00018 
00019     int refcount() const { return count_; }
00020 
00021 protected:
00022     void turn_reference_counting_off() { count_ = 1000000; }
00023 private:
00024     mutable int count_;
00025 };
00026 
00027 inline void intrusive_ptr_add_ref(const reference_counted_object* obj) {
00028     obj->add_ref();
00029 }
00030 
00031 inline void intrusive_ptr_release(const reference_counted_object* obj) {
00032     obj->dec_ref();
00033 }
00034 
00035 typedef boost::intrusive_ptr<reference_counted_object> object_ptr;
00036 typedef boost::intrusive_ptr<const reference_counted_object> const_object_ptr;
00037 
00038 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs