UCommon
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
34namespace ucommon {
35
43class __EXPORT counter
44{
45private:
46 unsigned value, cycle;
47
48public:
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
104class __EXPORT SeqCounter : protected counter
105{
106private:
107 void *item;
108 size_t offset;
109
110protected:
111 SeqCounter(void *start, size_t size, unsigned count);
112
113 void *get(void);
114
115 void *get(unsigned idx);
116
117public:
122 inline void operator=(unsigned inc_offset) {
123 counter::operator=(inc_offset);
124 }
125};
126
131class __EXPORT toggle
132{
133private:
134 bool value;
135
136public:
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
162template <class T>
163class sequence : public SeqCounter
164{
165protected:
166 inline T *get(unsigned idx) {
167 return static_cast<T *>(SeqCounter::get(idx));
168 }
169
170public:
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
222
223} // namespace ucommon
224
225#endif
Various miscellaneous platform specific headers and defines.
Common namespace for all ucommon objects.
Definition access.h:47
toggle toggle_t
A convenience typecast for auto-toggled bools.
Definition counter.h:221
counter counter_t
A convenience typecast for integer counters.
Definition counter.h:216
Automatic integer counting class.
Definition counter.h:44
unsigned operator*()
Reference next counter value through pointer operation.
Definition counter.h:78
void operator=(unsigned value)
Assign the value of the counter.
counter(unsigned limit)
Initialize integer counter for a range of values.
unsigned get(void)
Get the next counter value.
counter()
Initialize integer counter of unknown size.
unsigned range(void)
Get the range of values before recycling.
Definition counter.h:70
Automatically return a sequence of untyped objects.
Definition counter.h:105
void operator=(unsigned inc_offset)
Used to directly assign sequence position in template.
Definition counter.h:122
Automatically toggle a bool on each reference.
Definition counter.h:132
A template to return a sequence of objects of a specified type.
Definition counter.h:164
T & operator*()
Return next typed member of the sequence by pointer reference.
Definition counter.h:191
sequence(T *array, unsigned size)
Create a template auto-sequence from a list of typed pointers.
Definition counter.h:176
T * get(void)
Return next typed member of the sequence.
Definition counter.h:183
T & operator[](unsigned offset)
Return a specific typed member from the sequence list.
Definition counter.h:208