The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
reference_counter.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2016 by Philippe Plantier <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef UTILS_REFERENCE_COUTER_H_INCLUDED
16 #define UTILS_REFERENCE_COUTER_H_INCLUDED
17 
18 /**
19  * @file
20  */
21 
22 
23 #include <limits>
24 
25 namespace n_ref_counter {
26 
27 
28 /**
29  @class t_ref_counter
30  @brief t_ref_counter is a reference counter. If the counter overflows it stops counting.
31  So any negative count disables reference counting.
32 **/
33 template <typename T_integral> class t_ref_counter {
34  static_assert(std::numeric_limits<T_integral>::is_signed, "Reference counter must be a signed integer");
35 
36  T_integral count_;
37 
38 public:
39  enum {NEW=0, NOT_COUNTED = -1};
40 
41  explicit t_ref_counter(T_integral x = 0) : count_(x) {}
42  t_ref_counter(t_ref_counter const &a) : count_(a.count_) {}
43  t_ref_counter & operator=(t_ref_counter const &a){count_ = a.count_; return *this;}
44 
45  operator T_integral const () const {return count_;}
46 
47  T_integral const set(T_integral const a) { count_=a; return count_; }
48  T_integral const inc(){
49  if (count_ >= 0) { count_ += 1; }
50  return count_; }
51  T_integral const dec(){
52  if( count_ > 0) { count_ -= 1; }
53  return count_; }
54  T_integral const enable_count(){
55  if (count_ < 0) {count_ = 0;}
56  return count_; }
57  T_integral const disable_count(){
58  count_= NOT_COUNTED;
59  return count_; }
60 
61  T_integral const operator++(){return inc();}
62  T_integral const operator++(int){T_integral ret(count_); inc(); return ret;}
63  T_integral const operator--(){return dec();}
64  T_integral const operator--(int){T_integral ret(count_); dec(); return ret;}
65 };
66 
67 
68 }//end namepace
69 
70 #endif
void set(CURSOR_TYPE type)
Use the default parameter to reset cursors.
Definition: cursor.cpp:154
t_ref_counter is a reference counter.
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
GLint GLint GLint GLint GLint x
Definition: glew.h:1220