![]() |
ircproxy The Ultimate Cyborg |
00001 // ircproxy -- An IRC bouncer. 00002 // 00003 //! @file Counter.h 00004 //! @brief This file contains the declaration of class Counter. 00005 // 00006 // Copyright (C) 2007 by 00007 // 00008 // Carlo Wood, Run on IRC <carlo@alinoe.com> 00009 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt 00010 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61 00011 // 00012 // This file may be distributed under the terms of the Q Public License 00013 // version 1.0 as appearing in the file LICENSE.QPL included in the 00014 // packaging of this file. 00015 00016 #ifndef COUNTER_H 00017 #define COUNTER_H 00018 00019 #include <boost/noncopyable.hpp> 00020 #include "debug.h" 00021 00022 class Increment; 00023 00024 /*! @brief A counter with scope-safe increment/decrement. 00025 * 00026 * Use this class like: 00027 * 00028 * Counter counter; 00029 * 00030 * if (counter == 0) 00031 * 00032 * To increment the counter, create an Increment object in a scope: 00033 * 00034 * Increment increment(counter); 00035 * 00036 * When increment is destructed, the counter will automatically be decremented again. 00037 */ 00038 class Counter : public boost::noncopyable { 00039 private: 00040 friend class Increment; //!< Has direct access to M_counter. 00041 int M_counter; //!< Non-zero when locked. 00042 public: 00043 //! Constructor. 00044 Counter(void) : M_counter(0) { } 00045 //! Destructor. 00046 ~Counter() { ASSERT(M_counter == 0); } 00047 00048 //! Automatic conversion to int. 00049 operator int() const { return M_counter; } 00050 }; 00051 00052 /*! @brief Helper object for class Counter. 00053 * 00054 * This class increments/decrements a Counter object. 00055 */ 00056 class Increment : public boost::noncopyable { 00057 private: 00058 Counter& M_counter; //!< A reference to the underlying counter. 00059 public: 00060 //! Constructor; increment reference counter \a counter. 00061 Increment(Counter& counter) : M_counter(counter) { ++M_counter.M_counter; } 00062 //! Destructor: decrement reference counter. 00063 ~Increment() { --M_counter.M_counter; } 00064 }; 00065 00066 #endif // COUNTER_H
Copyright © 2005-2007 Carlo Wood. All rights reserved. |
---|