Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
UUID.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __BIASUUID_hh__
27 #define __BIASUUID_hh__
28 
29 #include <ctime>
30 #include <climits>
31 #include <cstring>
32 #include <Base/Debug/Error.hh>
33 #include <Base/Debug/Debug.hh>
34 
35 #include <Base/Common/BIASpragmaStart.hh>
36 #include <ios>
37 
38 #ifdef WIN32
39 #include <Winsock.h> // for timeval
40 //# ifdef UNICODE
41 //# error using BIAS uuid with UNICODE is untested. Please disable UNICODE or this warning if you really know what you do. (look at UuidFromStringA/UuidFromStringW in rpcdce.h) (JW 12/2005)
42 //# endif // UNICODE
43 // on Win32 there are two types: GUID and UUID, both pointing to the same struct
44 # include <rpc.h>
45 //# include <rpcdce.h>
46 // remove UUID to avoid name clash with our class
47 # undef UUID
48 # undef uuid_t
49 typedef GUID uuid_t; // let our internal data type be of type GUID
50 #else // WIN32
51 # include <uuid/uuid.h>
52 #endif //WIN32
53 
54 #include <time.h>
55 
56  // true to generate consecutive IDS as default instead of MAC like ids
57 #ifndef DEFAULT_UUID_CONSECUTIVELY
58 # define DEFAULT_UUID_CONSECUTIVELY false
59 #endif
60 
61 namespace BIAS {
62 
63  /// forward declaration for stream operators
64  class BIASImageBase_EXPORT UUID;
65 
66  /** @relates UUID
67  @brief write a UUID to a stream */
68  BIASImageBase_EXPORT std::ostream& operator<<(std::ostream & os, const UUID& id);
69  /** @relates UUID
70  @brief reads a UUID from a stream */
71  BIASImageBase_EXPORT std::istream& operator>>(std::istream & is, UUID& id);
72 
73 
74  /** @class UUID
75  @ingroup g_utils
76  @test MyTestFile.cpp
77  @brief interface class for
78  producing/storing Universally Unique IDentifiers
79 
80  A UUID is a u unique structure.
81  It is usually a 128 bit value computed from the MAC address of the
82  network
83  adapter (which means "spatial uniqueness") and a high resolution
84  timestamp providing "temporal uniqueness".
85  //In addition you
86 
87  @attention temporal uniqueness is only obtained if time is monotonic,
88  i.e. you MUST NOT turn back the clock (-> NTP ?)
89 
90  For information: generating 1.000.000 UIDS on WIN32 takes approx.
91  180 msec. (jw)
92 
93  \todo The UUID representation is *not* platform independant,
94  in particular generating a consecutive ID on Windows
95  and reading it on Linux will not work! (JW)
96 
97  @author koeser 09/2003, W32: Jan Woetzel 08/2005 */
98  class BIASImageBase_EXPORT UUID : public Debug
99  {
100  public:
101 
102  // destructor
103  virtual ~UUID();
104 
105  /** @brief default constructor
106  does NOT create a valid UUID to avoid performance decrease
107  in case someone wants to create many UIDS that are not really used.
108  (Jan Woetzel 08/2005) */
109  UUID();
110 
111  /** @brief use this if you want to create a UUID "manually" */
112  explicit UUID(const uuid_t &id);
113 
114  /** @brief copy constructor, calls operator= internally. */
115  UUID(const BIAS::UUID& id);
116 
117  /** @brief construct from string containing ascii uuid */
118  explicit UUID(const std::string &sID);
119 
120  /** @brief construct from string containing a unique long.
121  User has to assert uniqueness himself, e.g. by a global counter.
122  @author Jan Woetzel 2005 */
123  explicit UUID(const unsigned long & n);
124 
125  /** NULLS the class members,
126  invalidates this UID ( not unique anymore).
127  Jan Woetzel 08/2005 */
128  void clear();
129 
130  /* @return true if it is a NIL/NULL ID which is not unique
131  Jan Woetzel 08/2005 */
132  bool IsNil() const;
133 
134  /** @brief static function which simply produces a uuid and returns
135  @attention calling this function on an uninitialized UUID object does
136  not initialize it. It is _really_ a static function.
137  @return a new uuid which can be assigned to a UUID object
138  @author Jan Woetzel (rewrite 2005/08) */
139  static UUID GenerateUUID(const bool & consecutively=
140  DEFAULT_UUID_CONSECUTIVELY);
141  /// jw
142  static UUID GenerateUUID_Consecutive();
143  /// jw
144  static UUID GenerateUUID_Global();
145 
146 
147  /** @brief function to assign a fresh, unique ID.
148  @param consecutively: true to use a sequential, dense counter instead
149  of 16 Byte MAC like UID.
150  The ID will be unique for each program run.
151  Please mind the differnec to a sequential (local unique only) UUID w
152  hich is unique for the machine, not only the run.
153  False to generate universally valid one.
154  @author Jan Woetzel 08/2005 */
155  void AssignUUID(const bool & consecutively=DEFAULT_UUID_CONSECUTIVELY);
156  /// jw
157  void AssignUUID_Global();
158  /// jw
159  void AssignUUID_Consecutive();
160 
161 
162  /** @brief construct from string containing ascii uuid.
163  Thsiu routine is slow! (1.000.000 times take approx 16 seconds, JW)
164  @return true if succesful */
165  bool SetFromString(const std::string &sID);
166 
167  /** @brief construct from a unique long.
168  User has to assert uniqueness himslef, e.g. by a global counter.
169  @return true if succesful
170  @author Jan Woetzel 2005 */
171  bool SetFromLong(const unsigned long &n);
172 
173  /** @brief get least significant value of UID as long value.
174  Useful in conjunction with SetFromLong if you want to use unique
175  counters instead of real UIDs.
176  !!Must not be called for non-consecutive uuids!!
177  @return least significant UID value
178  @author Jan Woetzel 2005 */
179  unsigned long GetLong() const;
180 
181  /** @brief returns the time used to create the uuid (just seconds) */
182  time_t GetUUIDCreationTime() const;
183 
184  /** @brief returns the time used to create the uuid (seconds and usecs)*/
185  timeval GetUUIDCreationTimeStruct() const;
186 
187  /** @brief comparison operator only compares ids, no check of validities */
188  inline bool operator==(const UUID& id) const;
189 
190  /** @brief "not equal"-operator, uses result of operator== */
191  inline bool operator!=(const UUID& id) const
192  { return (!((*this)==id)); };
193 
194  /** @brief the operator< does not express temporal ordering, it does only
195  compare the data areas of the two ids, e.g. for sorting, map-storage */
196  inline bool operator<(const UUID& id) const;
197 
198  /** @brief operator<= does not express temporal ordering, it does only
199  compare the data areas of the two ids !!! */
200  inline bool operator<=(const UUID& id) const;
201 
202  /** @brief standard assignment operator */
203  inline UUID& operator=(const UUID& id);
204 
205  /** @brief writes the UUID into a string object */
206  void GetString(std::string& sUUID) const;
207  std::string GetString() const;
208 
209  /** @brief checks whether this uuid is valid(true) or unitialized(false) */
210  inline bool IsValid() const { return IsValid_; };
211 
212  /** @brief sets the valid flag to false and clears data
213  JW 09/2005: added clear in addition to just setting bool flag
214  invalid. */
215  inline void Invalidate(){this->clear(); };
216 
217  /** @brief get the pointer to the Uc[16] array with the raw uuid */
218  const unsigned char *GetUUIDRaw() const;
219 
220  void SetUUIDRaw(const unsigned char * rawid);
221 
222  /// undefined behaviour if uuid is invalid
223  bool IsConsecutive() const;
224 
225  protected:
226 
227  friend BIASImageBase_EXPORT std::ostream&
228  operator<<(std::ostream & os, const UUID& id);
229  friend BIASImageBase_EXPORT std::istream&
230  operator>>(std::istream & is, UUID& id);
231 
232  /// the data fields of the uuid
233  uuid_t theID_;
234 
235  /// flag indicating whether this uuid has been assigned a value yet
236  bool IsValid_;
237 
238  /// when creating a uuid, current time is compared against the time
239  /// saved during the last uuid generation
240  ///
241  /// Hint: If you have WIN32 compilation problems look for
242  /// WIN32_LEAN_AND_MEAN and windows.h (JW)
243  static timeval LastGenerationTime_;
244 
245  /// global counter to create next unique consecutive program run-unique ID.
246  static long g_uniqueCounter;
247  }; // class BIAS::UUID
248 
249 
250  /** @brief Helper class for ascending sort of sequential BIAS::UUID instances.
251  Sample usage:
252  \verbatim
253  #include <algorithm>
254  std::vector<BIAS::UUID> v;
255  std::sort(v.begin(), v.end(), BIAS::UUIDsortAscending() );
256  \endverbatim
257  \todo operator< of UUD may be inverse, TOOD: compare uuid_compare on
258  Linux and WIN32.
259  @author Jan Woetzel */
261  {
262  public:
264  { return ! (a < b); }
265  };
266 
267  ////////////////////////////////////////////////////////////////////
268  // inline implementation
269  ////////////////////////////////////////////////////////////////////
270 
271  bool BIAS::UUID::operator==(const UUID& id) const
272  {
273 #ifdef WIN32
274  RPC_STATUS status;
275  // TODO: this is not ANSI compatible 4238
276  //return (!UuidCompare(&((GUID)id.theID_),&((GUID)theID_), &status));
277  // TODO PERFORMANCE warning because of additional constructors!
278  GUID id1 = (GUID)id.theID_;
279  GUID id2 = (GUID)this->theID_;
280  int result = UuidCompare(&id1, &id2, &status);
281  return (result==0);
282 #else
283  return (!uuid_compare(id.theID_,theID_));
284 #endif
285  }
286 
287 
288  bool BIAS::UUID::operator<(const UUID& id) const
289  {
290 #ifdef WIN32
291  RPC_STATUS dummy;
292  return (UuidCompare(&((GUID)id.theID_),&((GUID)theID_),&dummy)<0);
293 #else
294  return (uuid_compare(id.theID_,theID_)<0);
295 #endif
296  }
297 
298 
299  bool BIAS::UUID::operator<=(const UUID& id) const
300  {
301 #ifdef WIN32
302  RPC_STATUS dummy;
303  return (UuidCompare(&((GUID)id.theID_),&((GUID)theID_),&dummy)<=0);
304 #else
305  return (uuid_compare(id.theID_,theID_)<=0);
306 #endif
307  }
308 
309 
311  {
312  memcpy((void*)&theID_,(void*)&id.theID_,sizeof(uuid_t));
313  IsValid_ = id.IsValid_; return (*this);
314  }
315 
316 } // namespace
317 
318 #include <Base/Common/BIASpragmaEnd.hh>
319 
320 #endif // __BIASUUID_hh__
321 
bool operator()(BIAS::UUID &a, BIAS::UUID &b)
Definition: UUID.hh:263
static long g_uniqueCounter
global counter to create next unique consecutive program run-unique ID.
Definition: UUID.hh:246
Helper class for ascending sort of sequential BIAS::UUID instances.
Definition: UUID.hh:260
bool operator!=(const UUID &id) const
&quot;not equal&quot;-operator, uses result of operator==
Definition: UUID.hh:191
static timeval LastGenerationTime_
when creating a uuid, current time is compared against the time saved during the last uuid generation...
Definition: UUID.hh:243
bool operator<(const UUID &id) const
the operator&lt; does not express temporal ordering, it does only compare the data areas of the two ids...
Definition: UUID.hh:288
bool operator<(const BIAS::Polynom &p1, const BIAS::Polynom &p2)
Definition: Polynom.hh:293
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
class BIASImageBase_EXPORT UUID
forward declaration for stream operators
Definition: UUID.hh:64
UUID & operator=(const UUID &id)
standard assignment operator
Definition: UUID.hh:310
interface class for producing/storing Universally Unique IDentifiers
Definition: UUID.hh:98
bool IsValid_
flag indicating whether this uuid has been assigned a value yet
Definition: UUID.hh:236
void Invalidate()
sets the valid flag to false and clears data JW 09/2005: added clear in addition to just setting bool...
Definition: UUID.hh:215
uuid_t theID_
the data fields of the uuid
Definition: UUID.hh:233
bool operator==(const UUID &id) const
comparison operator only compares ids, no check of validities
Definition: UUID.hh:271
bool operator<=(const UUID &id) const
operator&lt;= does not express temporal ordering, it does only compare the data areas of the two ids !!!...
Definition: UUID.hh:299
bool IsValid() const
checks whether this uuid is valid(true) or unitialized(false)
Definition: UUID.hh:210
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157