Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JLimit.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JLIMIT__
2 #define __JSUPPORT__JLIMIT__
3 
4 #include <limits>
5 #include <string>
6 #include <istream>
7 #include <ostream>
8 #include <sstream>
9 
10 #include "JROOT/JCounter.hh"
11 
12 #include "JTools/JRange.hh"
13 #include "JLang/JComparable.hh"
14 
15 
16 /**
17  * \file
18  * Auxiliaries for defining the range of iterations of objects.
19  * \author mdejong
20  */
21 namespace JSUPPORT {}
22 namespace JPP { using namespace JSUPPORT; }
23 
24 namespace JSUPPORT {
25 
26  using JTOOLS::JRange;
27  using JLANG::JComparable;
28  using JROOT::counter_type;
29 
30 
31  /**
32  * Auxiliary class for defining the range of iterations of objects.
33  *
34  * Possible input syntax:
35  * <pre>
36  * <begin of iteration>:<number of iterations>
37  * <number of iterations>
38  * </pre>
39  * where the iteration starts at 0.
40  */
41  struct JLimit :
42  public JRange<counter_type>,
43  public JComparable<JLimit>,
44  public JComparable<JLimit, counter_type>
45  {
46 
48 
49 
50  /**
51  * Default constructor.
52  */
53  JLimit() :
55  {}
56 
57 
58  /**
59  * Constructor.
60  *
61  * \param counter end of iteration
62  */
63  JLimit(const counter_type counter) :
64  JRange<counter_type>(JLimit::min(), counter)
65  {}
66 
67 
68  /**
69  * Get limit.
70  *
71  * \return this limit
72  */
73  const JLimit& getLimit() const
74  {
75  return static_cast<const JLimit&>(*this);
76  }
77 
78 
79  /**
80  * Get limit.
81  *
82  * \return this limit
83  */
85  {
86  return static_cast<JLimit&>(*this);
87  }
88 
89 
90  /**
91  * Set limit.
92  *
93  * \param limit limit
94  */
95  void setLimit(const JLimit& limit)
96  {
97  static_cast<JLimit&>(*this) = limit;
98  }
99 
100 
101  /**
102  * Get minimum counter value.
103  *
104  * \return value
105  */
106  static counter_type min()
107  {
108  return 0;
109  }
110 
111 
112  /**
113  * Get maximum counter value.
114  *
115  * \return value
116  */
117  static counter_type max()
118  {
119  return std::numeric_limits<counter_type>::max();
120  }
121 
122 
123  /**
124  * Compare limit.
125  *
126  * \param limit limit
127  * \return true if this upper limit is less then given upper limit; else false
128  */
129  bool less(const JLimit& limit) const
130  {
131  return getUpperLimit() < limit.getUpperLimit();
132  }
133 
134 
135  /**
136  * Compare counter.
137  *
138  * \param counter counter
139  * \return true if this upper limit is less than counter; else false
140  */
141  bool less(const counter_type counter) const
142  {
143  return getUpperLimit() < counter;
144  }
145 
146 
147  /**
148  * Compare counter.
149  *
150  * \param counter counter
151  * \return true if this upper limit is more than counter; else false
152  */
153  bool more(const counter_type counter) const
154  {
155  return getUpperLimit() > counter;
156  }
157 
158 
159  /**
160  * Read limit from input.
161  *
162  * Note that input <tt>n</tt> will set the begin and end of the iteration to <tt>0</tt> and <tt>n</tt>, respectively;
163  * and input <tt>i:n</tt> will set the begin and end of the iteration to <tt>i</tt> and <tt>i+n</tt>, respectively.
164  *
165  * \param in input stream
166  * \param limit limit
167  * \return input stream
168  */
169  friend inline std::istream& operator>>(std::istream& in, JLimit& limit)
170  {
171  using namespace std;
172 
173  limit.setLowerLimit(JLimit::min());
174  limit.setUpperLimit(JLimit::max());
175 
176  string buffer;
177 
178  if (in >> buffer) {
179 
180  string::size_type pos = buffer.find(SEPARATOR);
181 
182  if (pos != string::npos) {
183 
184  buffer[pos] = ' ';
185 
186  istringstream is(buffer);
187 
188  if (is >> static_cast<range_type&>(limit)) {
189  limit.setUpperLimit(limit.getLowerLimit() + limit.getUpperLimit());
190  }
191 
192  } else {
193 
194  counter_type value;
195 
196  istringstream is(buffer);
197 
198  is >> value;
199 
200  limit.setUpperLimit(value);
201  }
202  }
203 
204  return in;
205  }
206 
207 
208  static const char SEPARATOR = ':'; //!< separator between values
209  };
210 
211 
212  /**
213  * Type definition of limit.
214  */
215  typedef JLimit JLimit_t;
216 }
217 
218 #endif
bool less(const JLimit &limit) const
Compare limit.
Definition: JLimit.hh:129
JRange< counter_type > range_type
Definition: JLimit.hh:47
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:202
Type definition for counter for ROOT TTree and auxiliary methods.
static counter_type max()
Get maximum counter value.
Definition: JLimit.hh:117
Long64_t counter_type
Type definition for counter.
is
Definition: JDAQCHSM.chsm:167
void setUpperLimit(argument_type y)
Set upper limit.
Definition: JRange.hh:235
bool more(const counter_type counter) const
Compare counter.
Definition: JLimit.hh:153
JLimit JLimit_t
Type definition of limit.
Definition: JLimit.hh:215
JLimit()
Default constructor.
Definition: JLimit.hh:53
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:213
static const char SEPARATOR
separator between values
Definition: JLimit.hh:208
friend std::istream & operator>>(std::istream &in, JLimit &limit)
Read limit from input.
Definition: JLimit.hh:169
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:24
Range of values.
Definition: JRange.hh:38
bool less(const counter_type counter) const
Compare counter.
Definition: JLimit.hh:141
Auxiliary class to define a range between two values.
JLimit(const counter_type counter)
Constructor.
Definition: JLimit.hh:63
static counter_type min()
Get minimum counter value.
Definition: JLimit.hh:106
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:73
void setLimit(const JLimit &limit)
Set limit.
Definition: JLimit.hh:95
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
JLimit & getLimit()
Get limit.
Definition: JLimit.hh:84
void setLowerLimit(argument_type x)
Set lower limit.
Definition: JRange.hh:224