ds: uuid: convert to new object system
This commit is contained in:
@@ -1,25 +1,30 @@
|
||||
#ifndef BLUELIB_UUID_H_
|
||||
#define BLUELIB_UUID_H_
|
||||
#ifndef BLUE_DS_UUID_H_
|
||||
#define BLUE_DS_UUID_H_
|
||||
|
||||
#include <blue/core/macros.h>
|
||||
#include <blue/core/status.h>
|
||||
#include <blue/ds/object.h>
|
||||
#include <blue/ds/type.h>
|
||||
|
||||
#define B_UUID(p) ((b_uuid *)(p))
|
||||
#include <blue/ds/string.h>
|
||||
|
||||
#define B_UUID_NBYTES 16
|
||||
#define B_UUID_STRING_MAX 37
|
||||
|
||||
struct b_string;
|
||||
struct b_stringstream;
|
||||
B_DECLS_BEGIN;
|
||||
|
||||
typedef struct b_uuid b_uuid;
|
||||
#define B_TYPE_UUID (b_uuid_get_type())
|
||||
|
||||
B_DECLARE_TYPE(b_uuid);
|
||||
|
||||
B_TYPE_CLASS_DECLARATION_BEGIN(b_uuid)
|
||||
B_TYPE_CLASS_DECLARATION_END(b_uuid)
|
||||
|
||||
typedef struct b_uuid_bytes {
|
||||
unsigned char uuid_bytes[B_UUID_NBYTES];
|
||||
} b_uuid_bytes;
|
||||
|
||||
BLUE_API b_uuid *b_uuid_create(void);
|
||||
BLUE_API b_type b_uuid_get_type(void);
|
||||
|
||||
B_TYPE_DEFAULT_CONSTRUCTOR(b_uuid, B_TYPE_UUID);
|
||||
|
||||
BLUE_API b_uuid *b_uuid_create_from_bytes(
|
||||
unsigned char u00, unsigned char u01, unsigned char u02,
|
||||
unsigned char u03, unsigned char u04, unsigned char u05,
|
||||
@@ -28,19 +33,10 @@ BLUE_API b_uuid *b_uuid_create_from_bytes(
|
||||
unsigned char u13, unsigned char u14, unsigned char u15);
|
||||
BLUE_API b_uuid *b_uuid_create_from_bytev(const unsigned char bytes[B_UUID_NBYTES]);
|
||||
BLUE_API b_uuid *b_uuid_create_from_uuid_bytes(const b_uuid_bytes *bytes);
|
||||
BLUE_API b_uuid *b_uuid_create_from_string(const struct b_string *string);
|
||||
BLUE_API b_uuid *b_uuid_create_from_string(const b_string *string);
|
||||
BLUE_API b_uuid *b_uuid_create_from_cstr(const char *s);
|
||||
|
||||
static inline b_uuid *b_uuid_retain(b_uuid *uuid)
|
||||
{
|
||||
return B_UUID(b_retain(B_DSREF(uuid)));
|
||||
}
|
||||
static inline void b_uuid_release(b_uuid *uuid)
|
||||
{
|
||||
b_release(B_DSREF(uuid));
|
||||
}
|
||||
|
||||
BLUE_API b_status b_uuid_to_string(const b_uuid *uuid, struct b_string *out);
|
||||
BLUE_API b_status b_uuid_to_string(const b_uuid *uuid, b_string *out);
|
||||
BLUE_API b_status b_uuid_to_cstr(const b_uuid *uuid, char out[B_UUID_STRING_MAX]);
|
||||
BLUE_API b_status b_uuid_to_stringstream(
|
||||
const b_uuid *uuid, struct b_stringstream *out);
|
||||
@@ -49,4 +45,6 @@ BLUE_API void b_uuid_get_bytes(
|
||||
BLUE_API void b_uuid_get_uuid_bytes(const b_uuid *uuid, b_uuid_bytes *bytes);
|
||||
BLUE_API b_uuid_bytes *b_uuid_ptr(b_uuid *uuid);
|
||||
|
||||
B_DECLS_END;
|
||||
|
||||
#endif
|
||||
|
||||
202
ds/uuid.c
202
ds/uuid.c
@@ -1,87 +1,105 @@
|
||||
#include "uuid.h"
|
||||
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <blue/ds/type.h>
|
||||
#include <blue/ds/uuid.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static struct b_dsref_type uuid_type = {
|
||||
.t_name = "corelib::uuid",
|
||||
.t_flags = B_DSREF_FUNDAMENTAL,
|
||||
.t_id = B_DSREF_TYPE_UUID,
|
||||
.t_instance_size = sizeof(struct b_uuid),
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
struct b_uuid_p {
|
||||
struct b_uuid_bytes uuid_bytes;
|
||||
};
|
||||
|
||||
struct b_uuid *b_uuid_create(void)
|
||||
{
|
||||
struct b_uuid *out
|
||||
= (struct b_uuid *)b_dsref_type_instantiate(&uuid_type);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
return out;
|
||||
static b_status uuid_to_cstr(
|
||||
const struct b_uuid_p *uuid, char out[B_UUID_STRING_MAX])
|
||||
{
|
||||
snprintf(
|
||||
out, B_UUID_STRING_MAX,
|
||||
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%"
|
||||
"02x%02x",
|
||||
uuid->uuid_bytes.uuid_bytes[0], uuid->uuid_bytes.uuid_bytes[1],
|
||||
uuid->uuid_bytes.uuid_bytes[2], uuid->uuid_bytes.uuid_bytes[3],
|
||||
uuid->uuid_bytes.uuid_bytes[4], uuid->uuid_bytes.uuid_bytes[5],
|
||||
uuid->uuid_bytes.uuid_bytes[6], uuid->uuid_bytes.uuid_bytes[7],
|
||||
uuid->uuid_bytes.uuid_bytes[8], uuid->uuid_bytes.uuid_bytes[9],
|
||||
uuid->uuid_bytes.uuid_bytes[10], uuid->uuid_bytes.uuid_bytes[11],
|
||||
uuid->uuid_bytes.uuid_bytes[12], uuid->uuid_bytes.uuid_bytes[13],
|
||||
uuid->uuid_bytes.uuid_bytes[14], uuid->uuid_bytes.uuid_bytes[15]);
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
struct b_uuid *b_uuid_create_from_bytes(
|
||||
static void uuid_get_bytes(
|
||||
const struct b_uuid_p *uuid, unsigned char bytes[B_UUID_NBYTES])
|
||||
{
|
||||
memcpy(bytes, uuid->uuid_bytes.uuid_bytes, B_UUID_NBYTES);
|
||||
}
|
||||
|
||||
static void uuid_get_uuid_bytes(
|
||||
const struct b_uuid_p *uuid, struct b_uuid_bytes *bytes)
|
||||
{
|
||||
memcpy(bytes, &uuid->uuid_bytes, sizeof *bytes);
|
||||
}
|
||||
|
||||
static struct b_uuid_bytes *uuid_ptr(struct b_uuid_p *uuid)
|
||||
{
|
||||
return &uuid->uuid_bytes;
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
b_uuid *b_uuid_create_from_bytes(
|
||||
unsigned char u00, unsigned char u01, unsigned char u02,
|
||||
unsigned char u03, unsigned char u04, unsigned char u05,
|
||||
unsigned char u06, unsigned char u07, unsigned char u08,
|
||||
unsigned char u09, unsigned char u10, unsigned char u11,
|
||||
unsigned char u12, unsigned char u13, unsigned char u14, unsigned char u15)
|
||||
{
|
||||
struct b_uuid *uuid = b_uuid_create();
|
||||
b_uuid *uuid = b_uuid_create();
|
||||
if (!uuid) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uuid->uuid_bytes.uuid_bytes[0] = u00;
|
||||
uuid->uuid_bytes.uuid_bytes[1] = u01;
|
||||
uuid->uuid_bytes.uuid_bytes[2] = u02;
|
||||
uuid->uuid_bytes.uuid_bytes[3] = u03;
|
||||
uuid->uuid_bytes.uuid_bytes[4] = u04;
|
||||
uuid->uuid_bytes.uuid_bytes[5] = u05;
|
||||
uuid->uuid_bytes.uuid_bytes[6] = u06;
|
||||
uuid->uuid_bytes.uuid_bytes[7] = u07;
|
||||
uuid->uuid_bytes.uuid_bytes[8] = u08;
|
||||
uuid->uuid_bytes.uuid_bytes[9] = u09;
|
||||
uuid->uuid_bytes.uuid_bytes[10] = u10;
|
||||
uuid->uuid_bytes.uuid_bytes[11] = u11;
|
||||
uuid->uuid_bytes.uuid_bytes[12] = u12;
|
||||
uuid->uuid_bytes.uuid_bytes[13] = u13;
|
||||
uuid->uuid_bytes.uuid_bytes[14] = u14;
|
||||
uuid->uuid_bytes.uuid_bytes[15] = u15;
|
||||
struct b_uuid_p *p = b_object_get_private(uuid, B_TYPE_UUID);
|
||||
|
||||
p->uuid_bytes.uuid_bytes[0] = u00;
|
||||
p->uuid_bytes.uuid_bytes[1] = u01;
|
||||
p->uuid_bytes.uuid_bytes[2] = u02;
|
||||
p->uuid_bytes.uuid_bytes[3] = u03;
|
||||
p->uuid_bytes.uuid_bytes[4] = u04;
|
||||
p->uuid_bytes.uuid_bytes[5] = u05;
|
||||
p->uuid_bytes.uuid_bytes[6] = u06;
|
||||
p->uuid_bytes.uuid_bytes[7] = u07;
|
||||
p->uuid_bytes.uuid_bytes[8] = u08;
|
||||
p->uuid_bytes.uuid_bytes[9] = u09;
|
||||
p->uuid_bytes.uuid_bytes[10] = u10;
|
||||
p->uuid_bytes.uuid_bytes[11] = u11;
|
||||
p->uuid_bytes.uuid_bytes[12] = u12;
|
||||
p->uuid_bytes.uuid_bytes[13] = u13;
|
||||
p->uuid_bytes.uuid_bytes[14] = u14;
|
||||
p->uuid_bytes.uuid_bytes[15] = u15;
|
||||
|
||||
return uuid;
|
||||
}
|
||||
|
||||
struct b_uuid *b_uuid_create_from_bytev(const unsigned char bytes[B_UUID_NBYTES])
|
||||
b_uuid *b_uuid_create_from_bytev(const unsigned char bytes[B_UUID_NBYTES])
|
||||
{
|
||||
struct b_uuid *uuid = b_uuid_create();
|
||||
b_uuid *uuid = b_uuid_create();
|
||||
if (!uuid) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(uuid->uuid_bytes.uuid_bytes, bytes, B_UUID_NBYTES);
|
||||
struct b_uuid_p *p = b_object_get_private(uuid, B_TYPE_UUID);
|
||||
|
||||
memcpy(p->uuid_bytes.uuid_bytes, bytes, B_UUID_NBYTES);
|
||||
|
||||
return uuid;
|
||||
}
|
||||
|
||||
struct b_uuid *b_uuid_create_from_uuid_bytes(const struct b_uuid_bytes *bytes)
|
||||
{
|
||||
return b_uuid_create_from_bytev(bytes->uuid_bytes);
|
||||
}
|
||||
|
||||
struct b_uuid *b_uuid_create_from_string(const struct b_string *string)
|
||||
{
|
||||
return b_uuid_create_from_cstr(b_string_ptr(string));
|
||||
}
|
||||
|
||||
struct b_uuid *b_uuid_create_from_cstr(const char *str)
|
||||
b_uuid *b_uuid_create_from_cstr(const char *str)
|
||||
{
|
||||
struct b_uuid_bytes bytes;
|
||||
|
||||
@@ -141,34 +159,39 @@ struct b_uuid *b_uuid_create_from_cstr(const char *str)
|
||||
return b_uuid_create_from_uuid_bytes(&bytes);
|
||||
}
|
||||
|
||||
b_status b_uuid_to_string(const struct b_uuid *uuid, struct b_string *out)
|
||||
b_status b_uuid_to_cstr(const b_uuid *uuid, char out[B_UUID_STRING_MAX])
|
||||
{
|
||||
char str[B_UUID_STRING_MAX];
|
||||
b_uuid_to_cstr(uuid, str);
|
||||
b_string_append_cstr(out, str);
|
||||
|
||||
return B_SUCCESS;
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_UUID, uuid_to_cstr, uuid, out);
|
||||
}
|
||||
|
||||
b_status b_uuid_to_cstr(const struct b_uuid *uuid, char out[B_UUID_STRING_MAX])
|
||||
void b_uuid_get_bytes(const b_uuid *uuid, unsigned char bytes[B_UUID_NBYTES])
|
||||
{
|
||||
snprintf(
|
||||
out, B_UUID_STRING_MAX,
|
||||
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%"
|
||||
"02x%02x",
|
||||
uuid->uuid_bytes.uuid_bytes[0], uuid->uuid_bytes.uuid_bytes[1],
|
||||
uuid->uuid_bytes.uuid_bytes[2], uuid->uuid_bytes.uuid_bytes[3],
|
||||
uuid->uuid_bytes.uuid_bytes[4], uuid->uuid_bytes.uuid_bytes[5],
|
||||
uuid->uuid_bytes.uuid_bytes[6], uuid->uuid_bytes.uuid_bytes[7],
|
||||
uuid->uuid_bytes.uuid_bytes[8], uuid->uuid_bytes.uuid_bytes[9],
|
||||
uuid->uuid_bytes.uuid_bytes[10], uuid->uuid_bytes.uuid_bytes[11],
|
||||
uuid->uuid_bytes.uuid_bytes[12], uuid->uuid_bytes.uuid_bytes[13],
|
||||
uuid->uuid_bytes.uuid_bytes[14], uuid->uuid_bytes.uuid_bytes[15]);
|
||||
return B_SUCCESS;
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_UUID, uuid_get_bytes, uuid, bytes);
|
||||
}
|
||||
|
||||
b_status b_uuid_to_stringstream(
|
||||
const struct b_uuid *uuid, struct b_stringstream *out)
|
||||
void b_uuid_get_uuid_bytes(const b_uuid *uuid, struct b_uuid_bytes *bytes)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_UUID, uuid_get_uuid_bytes, uuid, bytes);
|
||||
}
|
||||
|
||||
struct b_uuid_bytes *b_uuid_ptr(b_uuid *uuid)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_UUID, uuid_ptr, uuid);
|
||||
}
|
||||
|
||||
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/
|
||||
|
||||
b_uuid *b_uuid_create_from_uuid_bytes(const struct b_uuid_bytes *bytes)
|
||||
{
|
||||
return b_uuid_create_from_bytev(bytes->uuid_bytes);
|
||||
}
|
||||
|
||||
b_uuid *b_uuid_create_from_string(const b_string *string)
|
||||
{
|
||||
return b_uuid_create_from_cstr(b_string_ptr(string));
|
||||
}
|
||||
|
||||
b_status b_uuid_to_stringstream(const b_uuid *uuid, struct b_stringstream *out)
|
||||
{
|
||||
char str[B_UUID_STRING_MAX];
|
||||
b_uuid_to_cstr(uuid, str);
|
||||
@@ -177,22 +200,45 @@ b_status b_uuid_to_stringstream(
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
void b_uuid_get_bytes(const struct b_uuid *uuid, unsigned char bytes[B_UUID_NBYTES])
|
||||
b_status b_uuid_to_string(const b_uuid *uuid, b_string *out)
|
||||
{
|
||||
memcpy(bytes, uuid->uuid_bytes.uuid_bytes, B_UUID_NBYTES);
|
||||
char str[B_UUID_STRING_MAX];
|
||||
b_uuid_to_cstr(uuid, str);
|
||||
b_string_append_cstr(out, str);
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
void b_uuid_get_uuid_bytes(const struct b_uuid *uuid, struct b_uuid_bytes *bytes)
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void uuid_init(b_object *obj, void *priv)
|
||||
{
|
||||
memcpy(bytes, &uuid->uuid_bytes, sizeof *bytes);
|
||||
struct b_uuid_p *uuid = priv;
|
||||
}
|
||||
|
||||
struct b_uuid_bytes *b_uuid_ptr(struct b_uuid *uuid)
|
||||
static void uuid_fini(b_object *obj, void *priv)
|
||||
{
|
||||
return &uuid->uuid_bytes;
|
||||
struct b_uuid_p *uuid = priv;
|
||||
}
|
||||
|
||||
b_dsref_type_id b_uuid_type_id(void)
|
||||
static void uuid_to_string(const b_object *uuid, struct b_stream *out)
|
||||
{
|
||||
return (b_dsref_type_id)&uuid_type;
|
||||
char str[B_UUID_STRING_MAX];
|
||||
b_uuid_to_cstr(uuid, str);
|
||||
b_stream_write_string(out, str, NULL);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
B_TYPE_CLASS_DEFINITION_BEGIN(b_uuid)
|
||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
||||
B_INTERFACE_ENTRY(to_string) = uuid_to_string;
|
||||
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
||||
B_TYPE_CLASS_DEFINITION_END(b_uuid)
|
||||
|
||||
B_TYPE_DEFINITION_BEGIN(b_uuid)
|
||||
B_TYPE_ID(0x17037068, 0x92f7, 0x4582, 0xad1f, 0x0dea43b628de);
|
||||
B_TYPE_CLASS(b_uuid_class);
|
||||
B_TYPE_INSTANCE_INIT(uuid_init);
|
||||
B_TYPE_INSTANCE_INIT(uuid_fini);
|
||||
B_TYPE_DEFINITION_END(b_uuid)
|
||||
|
||||
Reference in New Issue
Block a user