Jpp
JUPI.hh
Go to the documentation of this file.
1 #ifndef __JDB__JUPI__
2 #define __JDB__JUPI__
3 
4 #include <istream>
5 #include <ostream>
6 #include <sstream>
7 #include <string>
8 #include <cctype>
9 
10 #include "JLang/JEquals.hh"
11 #include "JDB/JPBS.hh"
12 
13 
14 /**
15  * \author mdejong
16  */
17 namespace JDATABASE {}
18 namespace JPP { using namespace JDATABASE; }
19 
20 namespace JDATABASE {
21 
22  /**
23  * Universal product identifier (%UPI).
24  *
25  * The %UPI consists of a %PBS and a variant, seperated by JPBS::SEPARATOR.\n
26  * The ASCII format should therefore read <tt>"<int>[.<int>]+/<variant>"</tt>.\n
27  * The variant cannot start with any kind of white space.
28  */
29  struct JUPI :
30  public JPBS,
31  public JLANG::JEquals<JUPI>
32  {
33  /**
34  * Default constructor.
35  */
36  JUPI()
37  {}
38 
39 
40  /**
41  * Constructor.
42  *
43  * \param pbs %PBS
44  * \param variant variant
45  */
46  JUPI(const JPBS& pbs, const std::string& variant = "") :
47  JPBS (pbs),
49  {}
50 
51 
52  /**
53  * Constructor.
54  *
55  * \param upi %UPI
56  */
57  JUPI(const std::string& upi)
58  {
59  std::istringstream(upi) >> *this;
60  }
61 
62 
63  /**
64  * Get %UPI.
65  *
66  * \return %UPI
67  */
68  const JUPI& getUPI() const
69  {
70  return static_cast<const JUPI&>(*this);
71  }
72 
73 
74  /**
75  * Get variant.
76  *
77  * \return variant
78  */
79  const std::string& getVariant() const
80  {
81  return variant;
82  }
83 
84 
85  /**
86  * Equality.
87  *
88  * \param upi %UPI
89  * \return true if %UPIs are equals; else false
90  */
91  bool equals(const JUPI& upi) const
92  {
93  return (this->getPBS() == upi.getPBS() &&
94  this->getVariant() == upi.getVariant());
95  }
96 
97 
98  /**
99  * Less-than operator.
100  *
101  * \param first first %UPI
102  * \param second second %UPI
103  * \return true if first %UPI higher in hierarchy or lesser variant; else false
104  */
105  friend inline bool operator<(const JUPI& first, const JUPI& second)
106  {
107  if (first.getPBS() == second.getPBS())
108  return first.getVariant() < second.getVariant();
109  else
110  return first.getPBS() < second.getPBS();
111  }
112 
113 
114  /**
115  * Read %UPI from input stream.
116  *
117  * \param in input stream
118  * \param object %UPI
119  * \return input stream
120  */
121  friend inline std::istream& operator>>(std::istream& in, JUPI& object)
122  {
123  using namespace std;
124 
125  object.variant.clear();
126 
127  if (in >> static_cast<JPBS&>(object)) {
128 
129  if (in.get() == (int) JPBS::SEPARATOR) {
130 
131  const int c = in.peek();
132 
133  if (c != EOF && !isspace(c))
134  in >> object.variant;
135  else
136  in.setstate(ios::failbit);
137 
138  } else {
139 
140  in.setstate(ios::failbit);
141  }
142  }
143 
144  return in;
145  }
146 
147 
148  /**
149  * Write %UPI to output stream.
150  *
151  * \param out output stream
152  * \param object %UPI
153  * \return output stream
154  */
155  friend inline std::ostream& operator<<(std::ostream& out, const JUPI& object)
156  {
157  return out << object.getPBS() << JPBS::SEPARATOR << object.getVariant();
158  }
159 
160  protected:
161  std::string variant;
162  };
163 }
164 
165 #endif
JDATABASE::JUPI::variant
std::string variant
Definition: JUPI.hh:161
JDATABASE::JUPI::JUPI
JUPI()
Default constructor.
Definition: JUPI.hh:36
JDATABASE::JUPI::equals
bool equals(const JUPI &upi) const
Equality.
Definition: JUPI.hh:91
JDATABASE::JPBS::SEPARATOR
static const char SEPARATOR
Separator between PBS and variant.
Definition: JPBS.hh:40
JLANG::JEquals
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
JPBS.hh
JEquals.hh
JDATABASE
Auxiliary classes and methods for database I/O.
Definition: JAHRS.hh:12
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JDATABASE::JUPI::getUPI
const JUPI & getUPI() const
Get UPI.
Definition: JUPI.hh:68
JDATABASE::JUPI::operator<
friend bool operator<(const JUPI &first, const JUPI &second)
Less-than operator.
Definition: JUPI.hh:105
JDATABASE::JPBS
Product breakdown structure (PBS).
Definition: JPBS.hh:27
JDATABASE::JUPI::operator<<
friend std::ostream & operator<<(std::ostream &out, const JUPI &object)
Write UPI to output stream.
Definition: JUPI.hh:155
std
Definition: jaanetDictionary.h:36
JDATABASE::JUPI::JUPI
JUPI(const JPBS &pbs, const std::string &variant="")
Constructor.
Definition: JUPI.hh:46
JDATABASE::JUPI::operator>>
friend std::istream & operator>>(std::istream &in, JUPI &object)
Read UPI from input stream.
Definition: JUPI.hh:121
JDATABASE::JUPI::getVariant
const std::string & getVariant() const
Get variant.
Definition: JUPI.hh:79
JDATABASE::JPBS::getPBS
const JPBS & getPBS() const
Get PBS.
Definition: JPBS.hh:106
JDATABASE::JUPI::JUPI
JUPI(const std::string &upi)
Constructor.
Definition: JUPI.hh:57
JDATABASE::JUPI
Universal product identifier (UPI).
Definition: JUPI.hh:29