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 
34 namespace ucommon {
35 
43 class __EXPORT counter
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 
131 class __EXPORT toggle
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
Automatically return a sequence of untyped objects.
Definition: counter.h:104
A template to return a sequence of objects of a specified type.
Definition: counter.h:163
counter counter_t
A convenience typecast for integer counters.
Definition: counter.h:216
void operator=(unsigned inc_offset)
Used to directly assign sequence position in template.
Definition: counter.h:122
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
Definition: generics.h:468
T & operator *()
Return next typed member of the sequence by pointer reference.
Definition: counter.h:191
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
Automatic integer counting class.
Definition: counter.h:43
T & operator[](unsigned offset)
Return a specific typed member from the sequence list.
Definition: counter.h:208
T * get(void)
Return next typed member of the sequence.
Definition: counter.h:183
Automatically toggle a bool on each reference.
Definition: counter.h:131
sequence(T *array, unsigned size)
Create a template auto-sequence from a list of typed pointers.
Definition: counter.h:176
unsigned range(void)
Get the range of values before recycling.
Definition: counter.h:70
void operator=(unsigned value)
Assign the value of the counter.