Jpp 19.3.0
the software that should make you happy
Loading...
Searching...
No Matches
JUPI_t.hh
Go to the documentation of this file.
1#ifndef __JDB__JUPI_T__
2#define __JDB__JUPI_T__
3
4#include <istream>
5#include <ostream>
6#include <sstream>
7#include <string>
8#include <cctype>
9#include <algorithm>
10
11#include <TROOT.h>
12
13#include "JDB/JPBS_t.hh"
14
15
16/**
17 * \author mdejong
18 */
19namespace JDATABASE {}
20namespace JPP { using namespace JDATABASE; }
21
22namespace JDATABASE {
23
24 /**
25 * Universal product identifier (%UPI).
26 *
27 * The %UPI consists of a %PBS, variant, version and serial number.\n
28 * The corresponding ASCII format reads <tt>\"<int>[.<int>]*</tt><tt>/<variant>/<int>.<int>\"</tt>.\n
29 * The version and serial number are not subject to I/O in case their value is default.
30 */
31 struct JUPI_t :
32 public JPBS_t
33 {
34 /**
35 * Separator between %PBS, variant and version.
36 */
37 static const char SEPARATOR = '/';
38
39 static const int DEFAULT_VERSION = -1; //!< Default version
40 static const int DEFAULT_NUMBER = -1; //!< Default number
41
42 /**
43 * Default constructor.
44 */
51
52
53 /**
54 * Constructor.
55 *
56 * \param pbs %PBS
57 */
58 JUPI_t(const JPBS_t& pbs) :
59 JPBS_t (pbs),
60 variant(),
63 {}
64
65
66 /**
67 * Constructor.
68 *
69 * \param pbs %PBS
70 * \param variant variant
71 * \param version version
72 * \param number serial number
73 */
74 JUPI_t(const JPBS_t& pbs,
75 const std::string& variant,
76 const int version,
77 const int number) :
78 JPBS_t (pbs),
82 {}
83
84
85 /**
86 * Constructor.
87 *
88 * \param upi %UPI
89 */
90 JUPI_t(const std::string& upi)
91 {
92 std::istringstream(upi) >> *this;
93 }
94
95
96 /**
97 * Get %UPI.
98 *
99 * \return %UPI
100 */
101 const JUPI_t& getUPI() const
102 {
103 return static_cast<const JUPI_t&>(*this);
104 }
105
106
107 /**
108 * Get variant.
109 *
110 * \return variant
111 */
112 const std::string& getVariant() const
113 {
114 return variant;
115 }
116
117
118 /**
119 * Get version.
120 *
121 * \return version
122 */
123 int getVersion() const
124 {
125 return version;
126 }
127
128
129 /**
130 * Get serial number.
131 *
132 * \return serial number
133 */
134 int getNumber() const
135 {
136 return number;
137 }
138
139
140 /**
141 * Convert %UPI.
142 *
143 * \return %UPI
144 */
145 std::string toString() const
146 {
147 std::ostringstream os;
148
149 os << *this;
150
151 return os.str();
152 }
153
154
155 /**
156 * Equality.
157 *
158 * \param first first %UPI
159 * \param second second %UPI
160 * \return true if %UPIs are equals; else false
161 */
162 friend inline bool operator==(const JUPI_t& first, const JUPI_t& second)
163 {
164 return (first.getPBS() == second.getPBS() &&
165 compare(first .getVariant(),
166 second.getVariant()) == 0 &&
167 first.getVersion() == second.getVersion() &&
168 first.getNumber() == second.getNumber());
169 }
170
171
172 /**
173 * Less-than operator.
174 *
175 * \param first first %UPI
176 * \param second second %UPI
177 * \return true if first %UPI less than second %UPI; else false
178 */
179 friend inline bool operator<(const JUPI_t& first, const JUPI_t& second)
180 {
181 if (first.getPBS() == second.getPBS()) {
182
183 if (compare(first .getVariant(),
184 second.getVariant()) == 0) {
185
186 if (first.getVersion() == second.getVersion())
187 return first.getNumber() < second.getNumber();
188 else
189 return first.getVersion() < second.getVersion();
190
191 } else {
192
193 return (compare(first .getVariant(),
194 second.getVariant()) < 0);
195 }
196
197 } else {
198
199 return first.getPBS() < second.getPBS();
200 }
201 }
202
203
204 /**
205 * Read %UPI from input stream.
206 *
207 * \param in input stream
208 * \param object %UPI
209 * \return input stream
210 */
211 friend inline std::istream& operator>>(std::istream& in, JUPI_t& object)
212 {
213 using namespace std;
214
215 object = JUPI_t();
216
217 if (in.peek() != (int) SEPARATOR) {
218 in >> static_cast<JPBS_t&>(object);
219 }
220
221 if (in.get() == (int) SEPARATOR) {
222
223 if (in.peek() == (int) SEPARATOR)
224 in.ignore();
225 else
226 getline(in, object.variant, SEPARATOR);
227
228 if (in.peek() == (int) DOT) {
229
230 in.ignore();
231
232 object.version = DEFAULT_VERSION;
233 object.number = DEFAULT_NUMBER;
234
235 return in;
236
237 } else if (in >> object.version &&
238 in.get() == (int) DOT &&
239 in >> object.number) {
240
241 return in;
242 }
243 }
244
245 in.setstate(ios::failbit);
246
247 return in;
248 }
249
250
251 /**
252 * Write %UPI to output stream.
253 *
254 * \param out output stream
255 * \param object %UPI
256 * \return output stream
257 */
258 friend inline std::ostream& operator<<(std::ostream& out, const JUPI_t& object)
259 {
260 using namespace std;
261
262 ostringstream os; // allow for width formatting
263
264 os << object.getPBS() << JUPI_t::SEPARATOR
265 << object.getVariant() << JUPI_t::SEPARATOR;
266
267 if (object.getVersion() != DEFAULT_VERSION) {
268 os << object.getVersion();
269 }
270
271 os << JUPI_t::DOT;
272
273 if (object.getNumber() != DEFAULT_NUMBER) {
274 os << object.getNumber();
275 }
276
277 return out << os.str();
278 }
279
281
282
283 /**
284 * Compare sub-strings.
285 *
286 * \param first first string
287 * \param second second string
288 * \return -1, 0, +1 if first sub-string is less, equal, greater than second sub-string
289 */
290 static int compare(const std::string& first, const std::string& second)
291 {
292 using namespace std;
293
294 const size_t pos = min(first.size(), second.size());
295
296 if (first.substr(0,pos) < second.substr(0,pos))
297 return -1;
298 else if (first.substr(0,pos) > second.substr(0,pos))
299 return +1;
300 else
301 return 0;
302 }
303
304 protected:
305 std::string variant;
308 };
309}
310
311#endif
Auxiliary classes and methods for database I/O.
Definition JAHRS.hh:14
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Product breakdown structure (PBS).
Definition JPBS_t.hh:29
static const char DOT
Separator between PBS values.
Definition JPBS_t.hh:33
const JPBS_t & getPBS() const
Get PBS.
Definition JPBS_t.hh:101
Universal product identifier (UPI).
Definition JUPI_t.hh:33
JUPI_t(const std::string &upi)
Constructor.
Definition JUPI_t.hh:90
std::string variant
Definition JUPI_t.hh:305
static const int DEFAULT_VERSION
Default version.
Definition JUPI_t.hh:39
std::string toString() const
Convert UPI.
Definition JUPI_t.hh:145
int getVersion() const
Get version.
Definition JUPI_t.hh:123
friend bool operator==(const JUPI_t &first, const JUPI_t &second)
Equality.
Definition JUPI_t.hh:162
static const int DEFAULT_NUMBER
Default number.
Definition JUPI_t.hh:40
friend std::ostream & operator<<(std::ostream &out, const JUPI_t &object)
Write UPI to output stream.
Definition JUPI_t.hh:258
JUPI_t(const JPBS_t &pbs, const std::string &variant, const int version, const int number)
Constructor.
Definition JUPI_t.hh:74
static int compare(const std::string &first, const std::string &second)
Compare sub-strings.
Definition JUPI_t.hh:290
JUPI_t(const JPBS_t &pbs)
Constructor.
Definition JUPI_t.hh:58
ClassDefNV(JUPI_t, 1)
friend bool operator<(const JUPI_t &first, const JUPI_t &second)
Less-than operator.
Definition JUPI_t.hh:179
friend std::istream & operator>>(std::istream &in, JUPI_t &object)
Read UPI from input stream.
Definition JUPI_t.hh:211
static const char SEPARATOR
Separator between PBS, variant and version.
Definition JUPI_t.hh:37
const JUPI_t & getUPI() const
Get UPI.
Definition JUPI_t.hh:101
const std::string & getVariant() const
Get variant.
Definition JUPI_t.hh:112
JUPI_t()
Default constructor.
Definition JUPI_t.hh:45
int getNumber() const
Get serial number.
Definition JUPI_t.hh:134