added uuid class; code rerfactoring
[public/netxms.git] / include / uuid.h
1 /*
2 * Public include file for the UUID library
3 *
4 * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU
8 * Library General Public License.
9 * %End-Header%
10 */
11
12 #ifndef _UUID_H_
13 #define _UUID_H_
14
15 #define UUID_LENGTH 16
16
17 #if !HAVE_UUID_T
18 #undef uuid_t
19 typedef unsigned char uuid_t[UUID_LENGTH];
20 #endif
21
22 /* UUID Variant definitions */
23 #define UUID_VARIANT_NCS 0
24 #define UUID_VARIANT_DCE 1
25 #define UUID_VARIANT_MICROSOFT 2
26 #define UUID_VARIANT_OTHER 3
27
28 void LIBNETXMS_EXPORTABLE uuid_clear(uuid_t uu);
29 int LIBNETXMS_EXPORTABLE uuid_compare(const uuid_t uu1, const uuid_t uu2);
30 void LIBNETXMS_EXPORTABLE uuid_generate(uuid_t out);
31 bool LIBNETXMS_EXPORTABLE uuid_is_null(const uuid_t uu);
32 int LIBNETXMS_EXPORTABLE uuid_parse(const TCHAR *in, uuid_t uu);
33 TCHAR LIBNETXMS_EXPORTABLE *uuid_to_string(const uuid_t uu, TCHAR *out);
34
35 #ifdef __cplusplus
36
37 /**
38 * Wrapper class for UUID
39 * Lower case used to avoid conflict with UUID struct defined in Windows headers
40 */
41 class LIBNETXMS_EXPORTABLE uuid
42 {
43 private:
44 uuid_t m_value;
45
46 public:
47 uuid() { uuid_clear(m_value); }
48 uuid(const uuid_t v) { memcpy(m_value, v, UUID_LENGTH); }
49
50 int compare(const uuid& u) const { return uuid_compare(m_value, u.m_value); }
51 const uuid_t& getValue() const { return m_value; }
52 bool isNull() const { return uuid_is_null(m_value); }
53 TCHAR *toString(TCHAR *buffer) const { return uuid_to_string(m_value, buffer); }
54 String toString() const { TCHAR buffer[64]; return String(uuid_to_string(m_value, buffer)); }
55
56 /**
57 * Generate new UUID
58 */
59 static uuid generate()
60 {
61 uuid_t u;
62 uuid_generate(u);
63 return uuid(u);
64 }
65
66 /**
67 * Parse string into UUID. Returns NULL UUID on error.
68 */
69 static uuid parse(const TCHAR *s)
70 {
71 uuid_t u;
72 if (uuid_parse(s, u) != 0)
73 return NULL_UUID;
74 return uuid(u);
75 }
76
77 static const uuid NULL_UUID;
78 };
79
80 #endif
81
82 #endif