summaryrefslogtreecommitdiff
path: root/shared/ossp_uuid/uuid++.cc
blob: d710cef42bf18a4f7762fe6070d55f7c61c23fac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
**  OSSP uuid - Universally Unique Identifier
**  Copyright (c) 2004-2008 Ralf S. Engelschall <rse@engelschall.com>
**  Copyright (c) 2004-2008 The OSSP Project <http://www.ossp.org/>
**
**  This file is part of OSSP uuid, a library for the generation
**  of UUIDs which can found at http://www.ossp.org/pkg/lib/uuid/
**
**  Permission to use, copy, modify, and distribute this software for
**  any purpose with or without fee is hereby granted, provided that
**  the above copyright notice and this permission notice appear in all
**  copies.
**
**  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
**  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
**  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
**  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
**  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
**  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
**  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
**  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
**  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
**  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
**  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
**  SUCH DAMAGE.
**
**  uuid++.cc: library C++ API implementation
*/

#include <string.h>
#include <stdarg.h>

#include "uuid++.hh"

/*  standard constructor */
uuid::uuid()
{
    uuid_rc_t rc;
    if ((rc = uuid_create(&ctx)) != UUID_RC_OK)
        throw uuid_error_t(rc);
}

/*  copy constructor */
uuid::uuid(const uuid &obj)
{
    /* Notice: the copy constructor is the same as the assignment
       operator (with the object as the argument) below, except that
       (1) no check for self-assignment is required, (2) no existing
       internals have to be destroyed and (3) no return value is given back. */
    uuid_rc_t rc;
    if ((rc = uuid_clone(obj.ctx, &ctx)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return;
}

/*  extra constructor via C API object */
uuid::uuid(const uuid_t *obj)
{
    uuid_rc_t rc;
    if (obj == NULL)
        throw uuid_error_t(UUID_RC_ARG);
    if ((rc = uuid_clone(obj, &ctx)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return;
}

/*  extra constructor via binary representation */
uuid::uuid(const void *bin)
{
    uuid_rc_t rc;
    if (bin == NULL)
        throw uuid_error_t(UUID_RC_ARG);
    if ((rc = uuid_create(&ctx)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    import(bin);
    return;
}

/*  extra constructor via string representation */
uuid::uuid(const char *str)
{
    uuid_rc_t rc;
    if (str == NULL)
        throw uuid_error_t(UUID_RC_ARG);
    if ((rc = uuid_create(&ctx)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    import(str);
    return;
}

/*  standard destructor */
uuid::~uuid()
{
    uuid_destroy(ctx);
    return;
}

/*  assignment operator: import of other C++ API object */
uuid &uuid::operator=(const uuid &obj)
{
    uuid_rc_t rc;
    if (this == &obj)
        return *this;
    if ((rc = uuid_destroy(ctx)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    if ((rc = uuid_clone(obj.ctx, &ctx)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return *this;
}

/*  assignment operator: import of other C API object */
uuid &uuid::operator=(const uuid_t *obj)
{
    uuid_rc_t rc;
    if (obj == NULL)
        throw uuid_error_t(UUID_RC_ARG);
    if ((rc = uuid_clone(obj, &ctx)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return *this;
}

/*  assignment operator: import of binary representation */
uuid &uuid::operator=(const void *bin)
{
    if (bin == NULL)
        throw uuid_error_t(UUID_RC_ARG);
    import(bin);
    return *this;
}

/*  assignment operator: import of string representation */
uuid &uuid::operator=(const char *str)
{
    if (str == NULL)
        throw uuid_error_t(UUID_RC_ARG);
    import(str);
    return *this;
}

/*  method: clone object */
uuid uuid::clone(void)
{
    return new uuid(this);
}

/*  method: loading existing UUID by name */
void uuid::load(const char *name)
{
    uuid_rc_t rc;
    if (name == NULL)
        throw uuid_error_t(UUID_RC_ARG);
    if ((rc = uuid_load(ctx, name)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return;
}

/*  method: making new UUID one from scratch */
void uuid::make(unsigned int mode, ...)
{
    uuid_rc_t rc;
    va_list ap;

    va_start(ap, mode);
    if ((mode & UUID_MAKE_V3) || (mode & UUID_MAKE_V5)) {
        const uuid *ns = (const uuid *)va_arg(ap, const uuid *);
        const char *name = (const char *)va_arg(ap, char *);
        if (ns == NULL || name == NULL)
            throw uuid_error_t(UUID_RC_ARG);
        rc = uuid_make(ctx, mode, ns->ctx, name);
    }
    else
        rc = uuid_make(ctx, mode);
    va_end(ap);
    if (rc != UUID_RC_OK)
        throw uuid_error_t(rc);
    return;
}

/*  method: comparison for Nil UUID */
int uuid::isnil(void)
{
    uuid_rc_t rc;
    int rv;

    if ((rc = uuid_isnil(ctx, &rv)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return rv;
}

/*  method: comparison against other object */
int uuid::compare(const uuid &obj)
{
    uuid_rc_t rc;
    int rv;

    if ((rc = uuid_compare(ctx, obj.ctx, &rv)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return rv;
}

/*  method: comparison for equality */
int uuid::operator==(const uuid &obj)
{
    return (compare(obj) == 0);
}

/*  method: comparison for inequality */
int uuid::operator!=(const uuid &obj)
{
    return (compare(obj) != 0);
}

/*  method: comparison for lower-than */
int uuid::operator<(const uuid &obj)
{
    return (compare(obj) < 0);
}

/*  method: comparison for lower-than-or-equal */
int uuid::operator<=(const uuid &obj)
{
    return (compare(obj) <= 0);
}

/*  method: comparison for greater-than */
int uuid::operator>(const uuid &obj)
{
    return (compare(obj) > 0);
}

/*  method: comparison for greater-than-or-equal */
int uuid::operator>=(const uuid &obj)
{
    return (compare(obj) >= 0);
}

/*  method: import binary representation */
void uuid::import(const void *bin)
{
    uuid_rc_t rc;
    if ((rc = uuid_import(ctx, UUID_FMT_BIN, bin, UUID_LEN_BIN)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return;
}

/*  method: import string or single integer value representation */
void uuid::import(const char *str)
{
    uuid_rc_t rc;
    if ((rc = uuid_import(ctx, UUID_FMT_STR, str, UUID_LEN_STR)) != UUID_RC_OK)
        if ((rc = uuid_import(ctx, UUID_FMT_SIV, str, UUID_LEN_SIV)) != UUID_RC_OK)
            throw uuid_error_t(rc);
    return;
}

/*  method: export binary representation */
void *uuid::binary(void)
{
    uuid_rc_t rc;
    void *bin = NULL;
    if ((rc = uuid_export(ctx, UUID_FMT_BIN, &bin, NULL)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return bin;
}

/*  method: export string representation */
char *uuid::string(void)
{
    uuid_rc_t rc;
    char *str = NULL;
    if ((rc = uuid_export(ctx, UUID_FMT_STR, (void **)&str, NULL)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return str;
}

/*  method: export single integer value representation */
char *uuid::integer(void)
{
    uuid_rc_t rc;
    char *str = NULL;
    if ((rc = uuid_export(ctx, UUID_FMT_SIV, (void **)&str, NULL)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return str;
}

/*  method: export textual summary representation */
char *uuid::summary(void)
{
    uuid_rc_t rc;
    char *txt = NULL;
    if ((rc = uuid_export(ctx, UUID_FMT_TXT, (void **)&txt, NULL)) != UUID_RC_OK)
        throw uuid_error_t(rc);
    return txt;
}

/*  method: return library version */
unsigned long uuid::version(void)
{
    return uuid_version();
}

bgstack15