generator  1.2
Interval.h
Go to the documentation of this file.
1 /*
2  This file is part of the GeneratorOfDatasets program.
3 
4  GeneratorOfDatasets is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  GeneratorOfDatasets is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with GeneratorOfDatasets. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #ifndef HLIDAC_PES_INTERVAL_H
18 #define HLIDAC_PES_INTERVAL_H
19 
26 #include <iostream>
27 #include <stdexcept>
28 
29 
30 template <class T>
31 class Interval;
32 
33 template <class T>
34 std::istream& operator>>(std::istream&, Interval<T>&);
35 
36 
41 template <class T>
42 class Interval {
43  public:
45  Interval() : mFrom(T()), mTo(T()) { }
51  Interval(T f, T t) : mFrom(f), mTo(t) {
52  if (mFrom > mTo)
53  throw std::invalid_argument("Interval<T>::Interval(T f, T t): Invalid range!");
54  }
55 
56  T from() const { return mFrom; }
57  T to() const { return mTo; }
58 
59  private:
60 
62  T mFrom;
64  T mTo;
65 
67  friend std::istream& operator>> <>(std::istream&, Interval<T>&);
68 };
69 
75 template <class T>
76 std::istream& operator>>(std::istream& in, Interval<T>& t) {
77  in>>t.mFrom>>t.mTo;
78  if ((in.fail() && !in.eof()) || t.mFrom > t.mTo)
79  throw std::invalid_argument("istream& operator>>(istream&, Interval<T>&): Invalid range!");
80  return in;
81 }
82 
88 template <class T>
89 std::ostream& operator<<(std::ostream& out, const Interval<T>& t) {
90  out<<"<"<<t.from()<<","<<t.to()<<">";
91  return out;
92 }
93 
94 #endif
Interval(T f, T t)
It constructs the interval [f,t] from two endpoint values.
Definition: Interval.h:51
T mFrom
The left endpoint of the interval.
Definition: Interval.h:62
The template class for intervals.
Definition: Interval.h:31
Interval()
Default constructor creates an empty interval.
Definition: Interval.h:45
T mTo
The right endpoint of the interval.
Definition: Interval.h:64
std::istream & operator>>(std::istream &, Interval< T > &)
Overloading of the input operator for the Interval class.
Definition: Interval.h:76