Jpp  master_rocky-37-gf0c5bc59d
the software that should make you happy
JUUID.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JUUID__
2 #define __JLANG__JUUID__
3 
4 #include <uuid/uuid.h>
5 #include <istream>
6 #include <ostream>
7 
8 #include "JLang/JComparable.hh"
9 
10 /**
11  * \author mdejong
12  */
13 
14 namespace JLANG {}
15 namespace JPP { using namespace JLANG; }
16 
17 namespace JLANG {
18 
19  /**
20  * Simple wrapper for UUID.
21  */
22  struct JUUID :
23  public JComparable<JUUID>
24  {
25 
26  static const int BUFFER_SIZE = 36; //!< number of characters for I/O of uuid_t without trailing '\0', see e.g.\ man uuid_parse
27 
28 
29  /**
30  * Default constructor.
31  */
33  {
34  clear();
35  }
36 
37 
38  /**
39  * Copy constructor.
40  *
41  * \param object UUID
42  */
43  JUUID(const uuid_t& object)
44  {
45  uuid_copy(this->uuid, object);
46  }
47 
48 
49  /**
50  * Copy constructor.
51  *
52  * \param object UUID
53  */
54  JUUID(const JUUID& object)
55  {
56  uuid_copy(this->uuid, object.uuid);
57  }
58 
59 
60  /**
61  * Randomizde this UUID.
62  *
63  * \return this UUID
64  */
65  const JUUID& operator()()
66  {
67  uuid_generate_random(this->uuid);
68 
69  return *this;
70  }
71 
72 
73  /**
74  * Generate random UUID.
75  *
76  * \return UUID
77  */
78  static inline const JUUID& rndm()
79  {
80  static JUUID id;
81 
82  return id();
83  }
84 
85 
86  /**
87  * Check validity.
88  *
89  * \return true if valid; else false
90  */
91  inline bool is_valid() const
92  {
93  return uuid_is_null(this->uuid) == 0;
94  }
95 
96 
97  /**
98  * Clear UUID.
99  */
100  inline void clear()
101  {
102  return uuid_clear(this->uuid);
103  }
104 
105 
106  /**
107  * Less than method.
108  *
109  * \param object UUID
110  * \return true if this UUID less than given UUID; else false
111  */
112  inline bool less(const JUUID& object) const
113  {
114  return uuid_compare(this->uuid, object.uuid) < 0;
115  }
116 
117 
118  /**
119  * Extract UUID.
120  *
121  * \param buffer UUID
122  * \return UUID
123  */
124  static JUUID valueOf(const std::string& buffer)
125  {
126  JUUID object;
127 
128  uuid_parse(buffer.c_str(), object.uuid);
129 
130  return object;
131  }
132 
133 
134  /**
135  * Read object identifier from input.
136  *
137  * \param in input stream
138  * \param object object identifier
139  * \return input stream
140  */
141  friend inline std::istream& operator>>(std::istream& in, JUUID& object)
142  {
143  char buffer[BUFFER_SIZE + 1];
144 
145  if (in.read(buffer, BUFFER_SIZE)) {
146 
147  buffer[BUFFER_SIZE] = '\0';
148 
149  uuid_parse(buffer, object.uuid);
150  }
151 
152  return in;
153  }
154 
155 
156  /**
157  * Write object identifier to output.
158  *
159  * \param out output stream
160  * \param object object identifier
161  * \return output stream
162  */
163  friend inline std::ostream& operator<<(std::ostream& out, const JUUID& object)
164  {
165  char buffer[BUFFER_SIZE + 1];
166 
167  uuid_unparse_lower(object.uuid, buffer);
168 
169  return out.write(buffer, BUFFER_SIZE);
170  }
171 
172  uuid_t uuid;
173  };
174 }
175 
176 #endif
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:139
Simple wrapper for UUID.
Definition: JUUID.hh:24
JUUID(const uuid_t &object)
Copy constructor.
Definition: JUUID.hh:43
friend std::istream & operator>>(std::istream &in, JUUID &object)
Read object identifier from input.
Definition: JUUID.hh:141
friend std::ostream & operator<<(std::ostream &out, const JUUID &object)
Write object identifier to output.
Definition: JUUID.hh:163
static const JUUID & rndm()
Generate random UUID.
Definition: JUUID.hh:78
bool is_valid() const
Check validity.
Definition: JUUID.hh:91
const JUUID & operator()()
Randomizde this UUID.
Definition: JUUID.hh:65
JUUID()
Default constructor.
Definition: JUUID.hh:32
static const int BUFFER_SIZE
number of characters for I/O of uuid_t without trailing '\0', see e.g. man uuid_parse
Definition: JUUID.hh:26
void clear()
Clear UUID.
Definition: JUUID.hh:100
uuid_t uuid
Definition: JUUID.hh:172
bool less(const JUUID &object) const
Less than method.
Definition: JUUID.hh:112
JUUID(const JUUID &object)
Copy constructor.
Definition: JUUID.hh:54
static JUUID valueOf(const std::string &buffer)
Extract UUID.
Definition: JUUID.hh:124