ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

counter.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
27 #ifndef _UCOMMON_COUNTER_H_
28 #define _UCOMMON_COUNTER_H_
29 
30 #ifndef _UCOMMON_CONFIG_H_
31 #include <ucommon/platform.h>
32 #endif
33 
34 namespace ucommon {
35 
44 {
45 private:
46  unsigned value, cycle;
47 
48 public:
52  counter();
53 
58  counter(unsigned limit);
59 
64  unsigned get(void);
65 
70  inline unsigned range(void) {
71  return cycle;
72  }
73 
78  inline unsigned operator*() {
79  return get();
80  }
81 
86  inline operator unsigned() {
87  return get();
88  }
89 
94  void operator=(unsigned value);
95 };
96 
104 class __EXPORT SeqCounter : protected counter
105 {
106 private:
107  void *item;
108  size_t offset;
109 
110 protected:
111  SeqCounter(void *start, size_t size, unsigned count);
112 
113  void *get(void);
114 
115  void *get(unsigned idx);
116 
117 public:
122  inline void operator=(unsigned inc_offset) {
123  counter::operator=(inc_offset);
124  }
125 };
126 
132 {
133 private:
134  bool value;
135 
136 public:
137  inline toggle() {
138  value = false;
139  }
140 
141  bool get(void);
142 
143  inline bool operator*() {
144  return get();
145  }
146 
147  inline void operator=(bool v) {
148  value = v;
149  }
150 
151  inline operator bool() {
152  return value;
153  }
154 };
155 
162 template <class T>
163 class sequence : public SeqCounter
164 {
165 protected:
166  inline T *get(unsigned idx) {
167  return static_cast<T *>(SeqCounter::get(idx));
168  }
169 
170 public:
176  inline sequence(T *array, unsigned size) :
177  SeqCounter(array, sizeof(T), size) {}
178 
183  inline T* get(void) {
184  return static_cast<T *>(SeqCounter::get());
185  }
186 
191  inline T& operator*() {
192  return reference_cast<T&>(SeqCounter::get());
193  }
194 
199  inline operator T&() {
200  return reference_cast<T&>(SeqCounter::get());
201  }
202 
208  inline T& operator[](unsigned offset) {
209  return reference_cast<T&>(get(offset));
210  }
211 };
212 
217 
221 typedef toggle toggle_t;
222 
223 } // namespace ucommon
224 
225 #endif
ucommon::toggle::toggle
toggle()
Definition: counter.h:137
ucommon::limit
T &() limit(T &value, T &low, T &high)
Definition: generics.h:468
ucommon::toggle::operator*
bool operator*()
Definition: counter.h:143
ucommon::counter_t
counter counter_t
Definition: counter.h:216
ucommon::SeqCounter::offset
size_t offset
Definition: counter.h:108
ucommon
Definition: access.cpp:23
ucommon::toggle_t
toggle toggle_t
Definition: counter.h:221
ucommon::sequence::operator[]
T & operator[](unsigned offset)
Definition: counter.h:208
ucommon::counter::operator*
unsigned operator*()
Definition: counter.h:78
ucommon::SeqCounter
Definition: counter.h:104
ucommon::counter::operator=
void operator=(unsigned value)
Definition: counter.cpp:41
ucommon::sequence
Definition: counter.h:163
ucommon::counter::range
unsigned range(void)
Definition: counter.h:70
ucommon::SeqCounter::operator=
void operator=(unsigned inc_offset)
Definition: counter.h:122
__EXPORT
#define __EXPORT
Definition: config.h:49
ucommon::sequence::operator*
T & operator*()
Definition: counter.h:191
ucommon::SeqCounter::get
void * get(void)
Definition: counter.cpp:65
ucommon::sequence::get
T * get(unsigned idx)
Definition: counter.h:166
ucommon::counter
Definition: counter.h:43
ucommon::toggle
Definition: counter.h:131
ucommon::sequence::get
T * get(void)
Definition: counter.h:183
platform.h
ucommon::SeqCounter::item
void * item
Definition: counter.h:107
ucommon::toggle::operator=
void operator=(bool v)
Definition: counter.h:147
ucommon::sequence::sequence
sequence(T *array, unsigned size)
Definition: counter.h:176
ucommon::toggle::value
bool value
Definition: counter.h:134
ucommon::counter::value
unsigned value
Definition: counter.h:46