My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
Range.h
Go to the documentation of this file.
1 #ifndef GCP_UTIL_RANGE_H
2 #define GCP_UTIL_RANGE_H
3 
11 #include "gcp/util/common/Exception.h"
12 #include "gcp/util/common/LogStream.h"
13 
14 namespace gcp {
15  namespace util {
16 
17  template<class Type>
18  class Range;
19 
20  template <class Type>
21  std::ostream& operator<<(std::ostream& os,
22  const Range<Type>& range);
23 
24  // Class for managing a range
25 
26  template<class Type>
27  class Range {
28  public:
29 
33  Range();
34 
38  Range(Type start, Type stop);
39 
43  virtual ~Range();
44 
45  void setStart(Type start);
46  void setStop(Type stop);
47 
48  Type start();
49  Type stop();
50 
54  friend std::ostream& gcp::util::operator << <>
55  (std::ostream& os, const Range<Type>& range);
56 
60  Range& operator+=(Type incr);
61 
65  Range& operator*=(Type mult);
66 
67  private:
68 
69  Type start_;
70  bool startInit_;
71  Type stop_;
72  bool stopInit_;
73 
74  }; // End class Range
75 
79  template<class Type>
81  startInit_ = false;
82  stopInit_ = false;
83  };
84 
85  // Constructor with initialization
86 
87  template<class Type>
88  Range<Type>::Range(Type start, Type stop) {
89  startInit_ = false;
90  stopInit_ = false;
91 
92  setStart(start);
93  setStop(stop);
94  };
95 
99  template<class Type>
101 
105  template<class Type>
106  void Range<Type>::setStart(Type start) {
107  start_ = start;
108  startInit_ = true;
109  }
110 
114  template<class Type>
115  void Range<Type>::setStop(Type stop) {
116  stop_ = stop;
117  stopInit_ = true;
118  }
119 
123  template<class Type>
125  if(!startInit_) {
126  LogStream errStr;
127  errStr.appendMessage(true, "Start value has not been initialized\n");
128  throw Error(errStr);
129  }
130  return start_;
131  }
132 
136  template<class Type>
138  if(!startInit_) {
139  LogStream errStr;
140  errStr.appendMessage(true, "End value has not been initialized\n");
141  throw Error(errStr);
142  }
143  return stop_;
144  }
145 
149  template <class Type>
150  std::ostream& operator<<(std::ostream& os,
151  const Range<Type>& range)
152  {
153  os << "(" << range.start_ << "-" << range.stop_ << ")";
154  return os;
155  }
156 
157 
161  template <class Type>
163  {
164  start_ += incr;
165  stop_ += incr;
166 
167  return *this;
168  }
169 
173  template <class Type>
175  {
176  start_ *= mult;
177  stop_ *= mult;
178  return *this;
179  }
180 
181  } // End namespace util
182 } // End namespace gcp
183 
184 
185 
186 
187 #endif // End #ifndef GCP_UTIL_RANGE_H
Range & operator*=(Type mult)
Definition: Range.h:174
void setStop(Type stop)
Definition: Range.h:115
Range & operator+=(Type incr)
Definition: Range.h:162
Type stop()
Definition: Range.h:137
void setStart(Type start)
Definition: Range.h:106
Type start()
Definition: Range.h:124
Definition: LogStream.h:21
Range()
Definition: Range.h:80
virtual ~Range()
Definition: Range.h:100
Definition: Range.h:18