00001 /* $Id: reference_counter.hpp 53319 2012-02-29 08:32:13Z shadowmaster $ */ 00002 /* 00003 Copyright (C) 2004 - 2012 by Philippe Plantier <ayin@anathas.org> 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 UTILS_REFERENCE_COUTER_H_INCLUDED 00017 #define UTILS_REFERENCE_COUTER_H_INCLUDED 00018 00019 /** 00020 * @file 00021 */ 00022 00023 00024 #include <limits> 00025 #include <boost/static_assert.hpp> 00026 00027 namespace n_ref_counter { 00028 00029 00030 /** 00031 @class t_ref_counter 00032 @brief t_ref_counter is a reference counter. If the counter overflows it stops counting. 00033 So any negative count disables reference counting. 00034 **/ 00035 template <typename T_integral> class t_ref_counter { 00036 BOOST_STATIC_ASSERT( std::numeric_limits<T_integral>::is_signed); 00037 00038 T_integral count_; 00039 00040 public: 00041 enum {NEW=0, NOT_COUNTED = -1}; 00042 00043 explicit t_ref_counter(T_integral x = 0) : count_(x) {} 00044 t_ref_counter(t_ref_counter const &a) : count_(a.count_) {} 00045 t_ref_counter & operator=(t_ref_counter const a){count_ = a.count_; return *this;} 00046 00047 operator T_integral const () const {return count_;} 00048 00049 T_integral const set(T_integral const a) { count_=a; return count_; } 00050 T_integral const inc(){ 00051 if (count_ >= 0) { count_ += 1; } 00052 return count_; } 00053 T_integral const dec(){ 00054 if( count_ > 0) { count_ -= 1; } 00055 return count_; } 00056 T_integral const enable_count(){ 00057 if (count_ < 0) {count_ = 0;} 00058 return count_; } 00059 T_integral const disable_count(){ 00060 count_= NOT_COUNTED; 00061 return count_; } 00062 00063 T_integral const operator++(){return inc();} 00064 T_integral const operator++(int){T_integral ret(count_); inc(); return ret;} 00065 T_integral const operator--(){return dec();} 00066 T_integral const operator--(int){T_integral ret(count_); dec(); return ret;} 00067 }; 00068 00069 00070 }//end namepace 00071 00072 #endif
| Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:14 for The Battle for Wesnoth | Gna! | Forum | Wiki | CIA | devdocs |