reference_counted_object.hpp

Go to the documentation of this file.
00001 /* $Id: reference_counted_object.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #ifndef REFERENCE_COUNTED_OBJECT_HPP_INCLUDED
00017 #define REFERENCE_COUNTED_OBJECT_HPP_INCLUDED
00018 
00019 #include "boost/intrusive_ptr.hpp"
00020 
00021 class reference_counted_object
00022 {
00023 public:
00024     reference_counted_object() : count_(0) {}
00025     reference_counted_object(const reference_counted_object& /*obj*/) : count_(0) {}
00026     reference_counted_object& operator=(const reference_counted_object& /*obj*/) {
00027         return *this;
00028     }
00029     virtual ~reference_counted_object() {}
00030 
00031     void add_ref() const { ++count_; }
00032     void dec_ref() const { if(--count_ == 0) { delete const_cast<reference_counted_object*>(this); } }
00033 
00034     int refcount() const { return count_; }
00035 
00036 protected:
00037     void turn_reference_counting_off() const { count_ = 1000000; }
00038 private:
00039     mutable int count_;
00040 };
00041 
00042 inline void intrusive_ptr_add_ref(const reference_counted_object* obj) {
00043     obj->add_ref();
00044 }
00045 
00046 inline void intrusive_ptr_release(const reference_counted_object* obj) {
00047     obj->dec_ref();
00048 }
00049 
00050 typedef boost::intrusive_ptr<reference_counted_object> object_ptr;
00051 typedef boost::intrusive_ptr<const reference_counted_object> const_object_ptr;
00052 
00053 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:08 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs