feisty meow concerns codebase  2.140
roller.h
Go to the documentation of this file.
1 #ifndef ROLLER_CLASS
2 #define ROLLER_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : roller *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1996-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include <basis/definitions.h>
19 
20 namespace structures {
21 
23 
34 template <class contents>
35 class roller
36 {
37 public:
38  roller(contents start_of_range, contents end_of_range);
40 
47 
48  // these report the constructor parameters.
49  contents minimum() { return _start_of_range; }
51  contents maximum() { return _end_of_range; }
53 
54  contents next_id();
56 
57  contents current() const;
59 
64  void set_current(contents new_current);
66 
68 private:
69  contents _current_id;
70  contents _start_of_range;
71  contents _end_of_range;
72 };
73 
75 
77 
78 class int_roller : public roller<int>
79 {
80 public:
81  int_roller(int start_of_range, int end_of_range)
82  : roller<int>(start_of_range, end_of_range) {}
83 };
84 
86 
87 // implementations below...
88 
89 template <class contents>
90 roller<contents>::roller(contents start, contents end)
91 : _current_id(start), _start_of_range(start), _end_of_range(end) {}
92 
93 template <class contents>
94 void roller<contents>::set_current(contents new_current)
95 {
96  _current_id = new_current;
97  if (_current_id >= _end_of_range) _current_id = _start_of_range;
98 }
99 
100 template <class contents> roller<contents>::~roller() {}
101 
102 template <class contents> contents roller<contents>::current() const
103 { return _current_id; }
104 
105 template <class contents> contents roller<contents>::next_id()
106 {
107  contents to_return = _current_id;
108  if (to_return == _end_of_range) {
109  // somehow the id to return is at the end of the range. this probably
110  // means the end of range condition wasn't detected last time due to an
111  // error in the parameters or the operation of == or ++ in the templated
112  // class.
113  _current_id = _start_of_range;
114  to_return = _current_id;
115  }
116  _current_id++; // next id.
117  if (_current_id == _end_of_range) _current_id = _start_of_range;
118  // reset the current position when hits the end of the range.
119  return to_return;
120 }
121 
122 } //namespace.
123 
124 #endif
125 
A roller that's based on integers. This is the most common type so far.
Definition: roller.h:79
int_roller(int start_of_range, int end_of_range)
Definition: roller.h:81
Maintains a pseudo-unique identifier number and issues a new one on demand.
Definition: roller.h:36
roller(contents start_of_range, contents end_of_range)
constructs a roller between the start and end ranges.
Definition: roller.h:90
contents current() const
returns the current id to be used; be careful!
Definition: roller.h:102
contents maximum()
the outer limit of the roller; it should never reach this.
Definition: roller.h:51
void set_current(contents new_current)
allows the current id to be manipulated.
Definition: roller.h:94
contents next_id()
returns a unique (per instance of this type) id.
Definition: roller.h:105
contents minimum()
the smallest value that the roller can have.
Definition: roller.h:49
Constants and objects used throughout HOOPLE.
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55