00001 /* *\ 00002 ** Copyright (C) 2009-2010 ** 00003 ** Dominik Charousset < Dominik.Charousset (at) haw-hamburg.de > ** 00004 ** ** 00005 ** MMM MMMMMM MMMMMMMMM MMMMMMM MMM MMM ** 00006 ** MMMMM MMMMMMMM MMMMMMMMM MMMMMMMM MMM MMMMM ** 00007 ** MMM MMM MMM MMMM MMM MMM MMM MMM MMM ** 00008 ** MMM MMM MMM MMMMMMMMM MMM MMM MMM MMM MMM ** 00009 ** MMMMMMMMMMM MMMM MMM MMMM MMM MMM MMM MMMMMMMMMMM ** 00010 ** MMM MMM MMMMMMMM MMMMMMMMM MMMMMMMM MMM MMM MMM ** 00011 ** MMM MMM MMMMMM MMMMMMMMM MMMMMMM MMM MMM MMM ** 00012 ** ** 00013 ** ** 00014 ** This file is part of the acedia library. ** 00015 ** ** 00016 ** This library is free software: you can redistribute it and/or modify ** 00017 ** it under the terms of the GNU Lesser General Public License as published ** 00018 ** by the Free Software Foundation, either version 3 of the License, or ** 00019 ** (at your option) any later version. ** 00020 ** ** 00021 ** This library is distributed in the hope that it will be useful, ** 00022 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00023 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00024 ** GNU Lesser General Public License for more details. ** 00025 ** ** 00026 ** You should have received a copy of the GNU Lesser General Public License ** 00027 ** along with acedia. If not, see <http://www.gnu.org/licenses/>. ** 00028 \* */ 00029 00030 #ifndef ACEDIA_REFERENCE_COUNTED_H 00031 #define ACEDIA_REFERENCE_COUNTED_H 00032 00033 #include "acedia_atomic.hpp" 00034 00035 namespace acedia 00036 { 00045 class ReferenceCounted 00046 { 00047 00048 volatile boost::int32_t value; 00049 00050 public: 00051 00052 virtual ~ReferenceCounted(); 00053 00057 inline ReferenceCounted(boost::int32_t initValue) throw() : value(initValue) { } 00058 00062 inline void ref() { (void) atomic::addAndFetch(&value, 1); } 00063 00070 inline bool deref() { return 0 != atomic::addAndFetch(&value, -1); } 00071 00077 inline bool isUnreferenced() { return 0 == value; } 00078 00084 inline bool isUnique() { return 1 == value; } 00085 00086 private: 00087 00088 ReferenceCounted(const ReferenceCounted &other); 00089 ReferenceCounted &operator=(const ReferenceCounted &other); 00090 00091 }; 00092 00093 } // namespace acedia 00094 00095 #endif