uuid.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 #ifndef ZORBA_UUID_H
19 #define ZORBA_UUID_H
20 
21 #include <algorithm>
22 #include <iostream>
23 
24 #include <zorba/config.h>
25 
26 namespace zorba {
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 
30 /**
31  * A %uuid contains the raw bytes for a UUID. Note that this is intentially a
32  * \c struct with no constructors, no destructor, and no user-defined
33  * assignment operators so that it remains a POD.
34  *
35  * See also:
36  * - RFC 4122: A Universally Unique IDentifier (UUID) URN Namespace.
37  * - http://en.wikipedia.org/wiki/Universally_unique_identifier
38  *
39  * The UUID data is stored as an array of 16 8-bit bytes comprising the 128
40  * bits that is a UUID. The data is intentionally \e not stored as shown in
41  * "Appendix A - Sample Implementation" of RFC 4122 (the \c struct with
42  * individial fields such as \c time_low, \c time_mid, etc.) since C++ does not
43  * guarantee that the \c struct fields will be laid out in memory as shown in
44  * section "4.1.2. Layout and Byte Order" due to possible padding.
45  */
46 struct ZORBA_DLL_PUBLIC uuid {
47  typedef unsigned char value_type;
49  typedef value_type const& const_reference;
50  typedef value_type* pointer;
51  typedef value_type const* const_pointer;
52  typedef std::size_t size_type;
53  typedef std::ptrdiff_t difference_type;
54 
55  typedef pointer iterator;
57 
58  enum variant {
59  ncs, ///< NCS backward compatibility
60  rfc4122, ///< RFC 4122
61  microsoft, ///< Microsoft compatibility
62  future ///< Reserved for future use
63  };
64 
65  enum version {
67  time_based = 0x10,
68  dce_security = 0x20,
69  name_based_md5 = 0x30,
70  random_number_based = 0x40,
71  name_based_sha1 = 0x50
72  };
73 
74  /**
75  * The raw UUID data.
76  */
77  value_type data[16];
78 
79  /**
80  * Creates a UUID. The variant and version of the UUID created is
81  * platform-dependent.
82  *
83  * @param result A pointer to the result.
84  */
85  static void create( uuid *result );
86 
87  /**
88  * Creates an iterator to the beginning of the data.
89  *
90  * @return Returns said iterator.
91  */
93  return data;
94  }
95 
96  /**
97  * Creates a const_iterator to the beginning of the data.
98  *
99  * @return Returns said iterator.
100  */
102  return data;
103  }
104 
105  /**
106  * Creates an iterator to one past the end of the data.
107  *
108  * @return Returns said iterator.
109  */
111  return data + size();
112  }
113 
114  /**
115  * Creates a const_iterator to one past the end of the data.
116  *
117  * @return Returns said iterator.
118  */
119  const_iterator end() const {
120  return data + size();
121  }
122 
123  /**
124  * Gets the size of the UUID data.
125  *
126  * @return Always returns 16.
127  */
128  size_type size() const {
129  return sizeof( data );
130  }
131 
132  /**
133  * Swaps this UUID's data with that of another.
134  *
135  * @param that The other UUID to swap data with.
136  */
137  void swap( uuid &that ) {
138  std::swap_ranges( begin(), end(), that.begin() );
139  }
140 
141  /**
142  * Gets the variant of this UUID.
143  *
144  * @return Returns said variant.
145  */
146  variant get_variant() const;
147 
148  /**
149  * Gets the version of this UUID.
150  *
151  * @return Returns said version.
152  */
153  version get_version() const;
154 };
155 
156 ////////// Functions //////////////////////////////////////////////////////////
157 
158 /**
159  * Swaps two UUIDs' data.
160  *
161  * @param u1 The first UUID.
162  * @param u2 The second UUID.
163  */
164 inline void swap( uuid &u1, uuid &u2 ) {
165  u1.swap( u2 );
166 }
167 
168 /**
169  * Compares two UUIDs for equality.
170  *
171  * @param u1 The first UUID.
172  * @param u2 The second UUID.
173  * @return Returns \c true only if the two UUIDs are equal.
174  */
175 inline bool operator==( uuid const &u1, uuid const &u2 ) {
176  return std::equal( u1.begin(), u1.end(), u2.begin() );
177 }
178 
179 /**
180  * Compares two UUIDs for inequality.
181  *
182  * @param u1 The first UUID.
183  * @param u2 The second UUID.
184  * @return Returns \c true only if the two UUIDs are not equal.
185  */
186 inline bool operator!=( uuid const &u1, uuid const &u2 ) {
187  return !(u1 == u2);
188 }
189 
190 /**
191  * Compares two UUIDs for less-than.
192  *
193  * @param u1 The first UUID.
194  * @param u2 The second UUID.
195  * @return Returns \c true only if the first UUID is less than the second.
196  */
197 inline bool operator<( uuid const &u1, uuid const &u2 ) {
198  return std::lexicographical_compare(
199  u1.begin(), u1.end(), u2.begin(), u2.end()
200  );
201 }
202 
203 /**
204  * Compares two UUIDs for less-than-or-equal-to.
205  *
206  * @param u1 The first UUID.
207  * @param u2 The second UUID.
208  * @return Returns \c true only if the first UUID is less than or equal to the
209  * second.
210  */
211 inline bool operator<=( uuid const &u1, uuid const &u2 ) {
212  return !(u2 < u1);
213 }
214 
215 /**
216  * Compares two UUIDs for greater-than.
217  *
218  * @param u1 The first UUID.
219  * @param u2 The second UUID.
220  * @return Returns \c true only if the first UUID is greater than the second.
221  */
222 inline bool operator>( uuid const &u1, uuid const &u2 ) {
223  return u2 < u1;
224 }
225 
226 /**
227  * Compares two UUIDs for greater-than-or-equal-to.
228  *
229  * @param u1 The first UUID.
230  * @param u2 The second UUID.
231  * @return Returns \c true only if the first UUID is greater than or equal to
232  * the second.
233  */
234 inline bool operator>=( uuid const &u1, uuid const &u2 ) {
235  return !(u1 < u2);
236 }
237 
238 /**
239  * Emits the given UUID to the given ostream in canonical UUID format.
240  *
241  * @param os The ostream to emit to.
242  * @param u The UUID to emit.
243  * @return Returns \a os.
244  */
245 ZORBA_DLL_PUBLIC
246 std::ostream& operator<<( std::ostream &os, uuid const &u );
247 
248 ///////////////////////////////////////////////////////////////////////////////
249 
250 } // namespace zorba
251 
252 #endif /* ZORBA_UUID_H */
253 /*
254  * Local variables:
255  * mode: c++
256  * End:
257  */
258 /* vim:set et sw=2 ts=2: */
void swap(uuid &that)
Swaps this UUID's data with that of another.
Definition: uuid.h:137
bool operator<(uuid const &u1, uuid const &u2)
Compares two UUIDs for less-than.
Definition: uuid.h:197
size_type size() const
Gets the size of the UUID data.
Definition: uuid.h:128
Microsoft compatibility.
Definition: uuid.h:61
const_iterator end() const
Creates a const_iterator to one past the end of the data.
Definition: uuid.h:119
Ossetian; Ossetic.
Definition: locale.h:157
unsigned char value_type
Definition: uuid.h:47
bool operator<=(uuid const &u1, uuid const &u2)
Compares two UUIDs for less-than-or-equal-to.
Definition: uuid.h:211
A uuid contains the raw bytes for a UUID.
Definition: uuid.h:46
pointer iterator
Definition: uuid.h:55
bool operator!=(Diagnostic const &d1, Diagnostic const &d2)
Compares two diagnostics for inequality.
Definition: diagnostic.h:388
iterator end()
Creates an iterator to one past the end of the data.
Definition: uuid.h:110
const_iterator begin() const
Creates a const_iterator to the beginning of the data.
Definition: uuid.h:101
bool operator>=(uuid const &u1, uuid const &u2)
Compares two UUIDs for greater-than-or-equal-to.
Definition: uuid.h:234
NCS backward compatibility.
Definition: uuid.h:59
value_type const * const_pointer
Definition: uuid.h:51
iterator begin()
Creates an iterator to the beginning of the data.
Definition: uuid.h:92
const_pointer const_iterator
Definition: uuid.h:56
value_type const & const_reference
Definition: uuid.h:49
std::ptrdiff_t difference_type
Definition: uuid.h:53
bool operator==(Diagnostic const &d1, Diagnostic const &d2)
Compares two diagnostics for equality.
Definition: diagnostic.h:377
value_type * pointer
Definition: uuid.h:50
value_type & reference
Definition: uuid.h:48
void swap(uuid &u1, uuid &u2)
Swaps two UUIDs' data.
Definition: uuid.h:164
bool operator>(uuid const &u1, uuid const &u2)
Compares two UUIDs for greater-than.
Definition: uuid.h:222
ZORBA_DLL_PUBLIC std::ostream & operator<<(std::ostream &os, const QueryLocation &aQuery)
std::size_t size_type
Definition: uuid.h:52
RFC 4122.
Definition: uuid.h:60