Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
cyclic_value.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_CYCLIC_VALUE__
30 #define __ETL_CYCLIC_VALUE__
31 
32 #include <stddef.h>
33 
37 
38 #include <algorithm>
39 
40 #include "static_assert.h"
41 #include "exception.h"
42 
43 namespace etl
44 {
45  //***************************************************************************
52  //***************************************************************************
53  template <typename T, const T FIRST = T(), const T LAST = T()>
55  {
56  public:
57 
58  template <typename U, const U OTHER_FIRST, const U OTHER_LAST> friend class cyclic_value;
59 
60  //*************************************************************************
64  //*************************************************************************
66  : value(FIRST),
67  first_value(FIRST),
68  last_value(LAST)
69  {
70  }
71 
72  //*************************************************************************
77  //*************************************************************************
78  cyclic_value(const T& first_, const T& last_)
79  : value(first_),
80  first_value(first_),
81  last_value(last_)
82  {
83  }
84 
85  //*************************************************************************
90  //*************************************************************************
91  void set(const T& first_, const T& last_)
92  {
93  first_value = first_;
94  last_value = last_;
95  value = first_;
96  }
97 
98  //*************************************************************************
100  //*************************************************************************
101  void to_first()
102  {
103  value = first_value;
104  }
105 
106  //*************************************************************************
108  //*************************************************************************
109  void to_last()
110  {
111  value = last_value;
112  }
113 
114  //*************************************************************************
117  //*************************************************************************
118  void advance(int n)
119  {
120  if (n > 0)
121  {
122  for (int i = 0; i < n; ++i)
123  {
124  operator ++();
125  }
126  }
127  else
128  {
129  for (int i = 0; i < -n; ++i)
130  {
131  operator --();
132  }
133  }
134  }
135 
136  //*************************************************************************
139  //*************************************************************************
140  operator T()
141  {
142  return value;
143  }
144 
145  //*************************************************************************
148  //*************************************************************************
149  operator const T() const
150  {
151  return value;
152  }
153 
154  //*************************************************************************
156  //*************************************************************************
158  {
159  if (value == last_value)
160  {
161  value = first_value;
162  }
163  else
164  {
165  ++value;
166  }
167 
168  return *this;
169  }
170 
171  //*************************************************************************
173  //*************************************************************************
175  {
176  cyclic_value temp(*this);
177 
178  operator++();
179 
180  return temp;
181  }
182 
183  //*************************************************************************
185  //*************************************************************************
187  {
188  if (value == first_value)
189  {
190  value = last_value;
191  }
192  else
193  {
194  --value;
195  }
196 
197  return *this;
198  }
199 
200  //*************************************************************************
202  //*************************************************************************
204  {
205  cyclic_value temp(*this);
206 
207  operator--();
208 
209  return temp;
210  }
211 
212  //*************************************************************************
214  //*************************************************************************
216  {
217  value = t;
218  return *this;
219  }
220 
221  //*************************************************************************
223  //*************************************************************************
224  template <const T OTHER_FIRST, const T OTHER_LAST>
226  {
227  value = other.value;
228  first_value = other.first_value;
229  last_value = other.last_value;
230  return *this;
231  }
232 
233  //*************************************************************************
235  //*************************************************************************
236  const T& first() const
237  {
238  return first_value;
239  }
240 
241  //*************************************************************************
243  //*************************************************************************
244  const T& last() const
245  {
246  return last_value;
247  }
248 
249  //*************************************************************************
251  //*************************************************************************
252  template <const T OTHER_FIRST, const T OTHER_LAST>
254  {
255  std::swap(first_value, other.first_value);
256  std::swap(last_value, other.last_value);
257  std::swap(value, other.value);
258  }
259 
260  private:
261 
262  T value;
263  T first_value;
264  T last_value;
265  };
266 
267  //*************************************************************************
269  //*************************************************************************
270  template <typename T, const T LHS_FIRST, const T LHS_LAST, const T RHS_FIRST, const T RHS_LAST>
271  void swap(cyclic_value<T, LHS_FIRST, LHS_LAST>& lhs,
272  cyclic_value<T, RHS_FIRST, RHS_LAST>& rhs)
273  {
274  lhs.swap(rhs);
275  }
276 
277  //*************************************************************************
279  //*************************************************************************
280  template <typename T, const T LHS_FIRST, const T LHS_LAST, const T RHS_FIRST, const T RHS_LAST>
281  bool operator == (const cyclic_value<T, LHS_FIRST, LHS_LAST>& lhs,
282  const cyclic_value<T, RHS_FIRST, RHS_LAST>& rhs)
283  {
284  return static_cast<T>(lhs) == static_cast<T>(rhs);
285  }
286 
287  //*************************************************************************
289  //*************************************************************************
290  template <typename T, const T LHS_FIRST, const T LHS_LAST, const T RHS_FIRST, const T RHS_LAST>
291  bool operator != (const cyclic_value<T, LHS_FIRST, RHS_LAST>& lhs,
292  const cyclic_value<T, RHS_FIRST, RHS_LAST>& rhs)
293  {
294  return !(lhs == rhs);
295  }
296 }
297 
298 #endif
void swap(etl::bitset< N > &lhs, etl::bitset< N > &rhs)
swap
Definition: bitset.h:1200
cyclic_value & operator--()
– operator.
Definition: cyclic_value.h:186
Definition: algorithm.h:43
cyclic_value(const T &first_, const T &last_)
Definition: cyclic_value.h:78
cyclic_value & operator=(T t)
= operator.
Definition: cyclic_value.h:215
const T & last() const
Gets the last value.
Definition: cyclic_value.h:244
Definition: cyclic_value.h:54
const T & first() const
Gets the first value.
Definition: cyclic_value.h:236
void set(const T &first_, const T &last_)
Definition: cyclic_value.h:91
cyclic_value & operator++()
++ operator.
Definition: cyclic_value.h:157
void advance(int n)
Definition: cyclic_value.h:118
void to_first()
Resets the value to the first in the range.
Definition: cyclic_value.h:101
void to_last()
Resets the value to the last in the range.
Definition: cyclic_value.h:109
void swap(cyclic_value< T, OTHER_FIRST, OTHER_LAST > &other)
Swaps the values.
Definition: cyclic_value.h:253
cyclic_value()
The initial value is set to the first value.
Definition: cyclic_value.h:65