--- uuid++.cc 2005/08/31 14:29:56 1.1
+++ uuid++.cc 2005/08/31 20:07:29 1.2
@@ -32,254 +32,256 @@
#include "uuid++.hh"
-using namespace std;
-
/*
-** OBJECT CONSTRUCTORS AND DESTRUCTOR
+** ==== Low-Level C++ API (direct mapping of C API) ====
*/
/* standard constructor */
uuid::uuid()
{
- if (uuid_create(&ctx) != UUID_RC_OK)
- throw "unable to create UUID context";
-}
-
-/* standard destructor */
-uuid::~uuid()
-{
- uuid_destroy(ctx);
+ uuid_rc_t rc;
+ if ((rc = uuid_create(&ctx)) != UUID_RC_OK)
+ throw uuid_error_t(rc);
}
-/* construction: import of other C++ API object (= special C++ copy constructor) */
+/* copy constructor */
uuid::uuid(const uuid &obj)
{
/* Notice: the copy constructor is the same as the assignment
- operator (with the object as the RHS) 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. */
- if (uuid_clone(obj.ctx, &ctx) != UUID_RC_OK)
- throw "unable to clone UUID context";
+ 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);
}
-/* construction: import of other C API object */
-uuid::uuid(uuid_t *obj)
-{
- if (obj != NULL)
- ctx = obj;
- else {
- if (uuid_create(&ctx) != UUID_RC_OK)
- throw "unable to create UUID context";
- }
+/* 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);
}
-/* construction: import of binary representation */
-uuid::uuid(const unsigned char *bin)
+/* extra constructor via binary representation */
+uuid::uuid(const void *bin)
{
- if (uuid_create(&ctx) != UUID_RC_OK)
- throw "unable to create UUID context";
- if (bin != NULL) {
- if (uuid_import(ctx, UUID_FMT_BIN, bin, UUID_LEN_BIN) != UUID_RC_OK)
- throw "unable to import UUID from binary representation";
- }
+ 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);
}
-/* construction: import of string representation */
+/* extra constructor via string representation */
uuid::uuid(const char *str)
{
- if (uuid_create(&ctx) != UUID_RC_OK)
- throw "unable to create UUID context";
- if (str != NULL) {
- if (uuid_import(ctx, UUID_FMT_STR, str, strlen(str)) != UUID_RC_OK)
- throw "unable to import UUID from string representation";
- }
+ 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);
}
-/*
-** IMPORT METHODS
-*/
+/* standard destructor */
+uuid::~uuid()
+{
+ uuid_destroy(ctx);
+}
-/* assignment: import of other C++ API object */
-uuid &uuid::operator=(const uuid &rhs)
+/* assignment operator: import of other C++ API object */
+uuid &uuid::operator=(const uuid &obj)
{
- if (this == &rhs)
+ uuid_rc_t rc;
+ if (this == &obj)
return *this;
- if (uuid_destroy(ctx) != UUID_RC_OK)
- throw "failed to destroy old UUID context";
- if (uuid_clone(rhs.ctx, &ctx) != UUID_RC_OK)
- throw "unable to clone UUID context";
+ 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: import of other C API object */
-uuid &uuid::operator=(const uuid_t *rhs)
+/* assignment operator: import of other C API object */
+uuid &uuid::operator=(const uuid_t *obj)
{
- if (rhs == NULL)
- throw "invalid RHS C object";
- if (uuid_clone((uuid_t *)rhs, &ctx) != UUID_RC_OK)
- throw "unable to clone UUID context";
+ 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: import of binary representation */
-uuid &uuid::operator=(const unsigned char *rhs)
+/* assignment operator: import of binary representation */
+uuid &uuid::operator=(const void *bin)
{
- if (rhs == NULL)
- throw "invalid RHS string";
- if (uuid_import(ctx, UUID_FMT_BIN, (const void *)rhs, UUID_LEN_BIN) != UUID_RC_OK)
- throw "unable to import into source UUID context";
+ if (bin == NULL)
+ throw uuid_error_t(UUID_RC_ARG);
+ import(bin);
return *this;
}
-/* assignment: import of string representation */
-uuid &uuid::operator=(const char *rhs)
+/* assignment operator: import of string representation */
+uuid &uuid::operator=(const char *str)
{
- if (rhs == NULL)
- throw "invalid RHS string";
- if (uuid_import(ctx, UUID_FMT_STR, (const void *)rhs, strlen(rhs)) != UUID_RC_OK)
- throw "unable to import into source UUID context";
+ if (str == NULL)
+ throw uuid_error_t(UUID_RC_ARG);
+ import(str);
return *this;
}
-/*
-** GENERATION METHODS
-*/
+/* method: clone object */
+uuid uuid::clone(void)
+{
+ return new uuid(this);
+}
-/* generation: loading existing UUID by name */
+/* method: loading existing UUID by name */
void uuid::load(const char *name)
{
+ uuid_rc_t rc;
if (name == NULL)
- throw "invalid UUID name";
- if (uuid_load(ctx, name) != UUID_RC_OK)
- throw "unable to load UUID by name";
+ throw uuid_error_t(UUID_RC_ARG);
+ if ((rc = uuid_load(ctx, name)) != UUID_RC_OK)
+ throw uuid_error_t(rc);
return;
}
-/* generation: making new UUID one from scratch */
+/* method: making new UUID one from scratch */
void uuid::make(unsigned int mode, ...)
{
- uuid_rc_t rv;
+ uuid_rc_t rc;
va_list ap;
va_start(ap, mode);
if ((mode & UUID_MAKE_V3) || (mode & UUID_MAKE_V5)) {
- const char *ns = (const char *)va_arg(ap, char *);
+ 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 "invalid arguments";
- rv = uuid_make(ctx, mode, ns, name);
+ throw uuid_error_t(UUID_RC_ARG);
+ rc = uuid_make(ctx, mode, ns->ctx, name);
}
else
- rv = uuid_make(ctx, mode);
+ rc = uuid_make(ctx, mode);
va_end(ap);
- if (rv != UUID_RC_OK)
- throw "unable to make new UUID";
+ if (rc != UUID_RC_OK)
+ throw uuid_error_t(rc);
return;
}
-/*
-** COMPARISON METHODS
-*/
-
-/* comparison: is-Nil-UUID */
+/* method: comparison for Nil UUID */
int uuid::isnil(void)
{
+ uuid_rc_t rc;
int rv;
- if (uuid_isnil(ctx, &rv) != UUID_RC_OK)
- throw "unable to compare UUID for being Nil-UUID";
+ if ((rc = uuid_isnil(ctx, &rv)) != UUID_RC_OK)
+ throw uuid_error_t(rc);
return rv;
}
-/* comparison: equality */
-int uuid::operator==(const uuid &rhs)
+/* method: comparison against other object */
+int uuid::compare(const uuid &obj)
{
+ uuid_rc_t rc;
int rv;
- if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
- throw "unable to compare UUID contexts";
- return (rv == 0);
+ if ((rc = uuid_compare(ctx, obj.ctx, &rv)) != UUID_RC_OK)
+ throw uuid_error_t(rc);
+ return rv;
}
-/* comparison: inequality */
-int uuid::operator!=(const uuid &rhs)
+/* method: comparison for equality */
+int uuid::operator==(const uuid &obj)
{
- int rv;
-
- if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
- throw "unable to compare UUID contexts";
- return (rv != 0);
+ return (compare(obj) == 0);
}
-/* comparison: lower-than */
-int uuid::operator<(const uuid &rhs)
+/* method: comparison for inequality */
+int uuid::operator!=(const uuid &obj)
{
- int rv;
-
- if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
- throw "unable to compare UUID contexts";
- return (rv < 0);
+ return (compare(obj) != 0);
}
-/* comparison: lower-than-or-equal */
-int uuid::operator<=(const uuid &rhs)
+/* method: comparison for lower-than */
+int uuid::operator<(const uuid &obj)
{
- int rv;
-
- if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
- throw "unable to compare UUID contexts";
- return (rv <= 0);
+ return (compare(obj) < 0);
}
-/* comparison: greater-than */
-int uuid::operator>(const uuid &rhs)
+/* method: comparison for lower-than-or-equal */
+int uuid::operator<=(const uuid &obj)
{
- int rv;
-
- if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
- throw "unable to compare UUID contexts";
- return (rv > 0);
+ return (compare(obj) <= 0);
}
-/* comparison: greater-than-or-equal */
-int uuid::operator>=(const uuid &rhs)
+/* method: comparison for greater-than */
+int uuid::operator>(const uuid &obj)
{
- int rv;
-
- if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
- throw "unable to compare UUID contexts";
- return (rv >= 0);
+ return (compare(obj) > 0);
}
-/*
-** EXPORT METHODS
-*/
-
-/* export: object representation */
-uuid_t *uuid::object(void)
+/* method: comparison for greater-than-or-equal */
+int uuid::operator>=(const uuid &obj)
{
- return ctx;
+ return (compare(obj) >= 0);
}
-/* export: binary representation */
-unsigned char *uuid::binary(void)
+/* method: import binary representation */
+void uuid::import(const void *bin)
{
- unsigned char *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);
+}
- bin = NULL;
- if (uuid_export(ctx, UUID_FMT_BIN, (void **)&bin, NULL) != UUID_RC_OK)
- throw "unable to export UUID context";
+/* method: import string 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)
+ throw uuid_error_t(rc);
+}
+
+/* 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;
}
-/* export: string representation */
+/* method: export string representation */
char *uuid::string(void)
{
- char *str;
-
- str = NULL;
- if (uuid_export(ctx, UUID_FMT_STR, (void **)&str, NULL) != UUID_RC_OK)
- throw "unable to export UUID context";
+ 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 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();
+}
+
|