libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_uuid.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblUuid generator, stringifier, validator, etc
3 * \ingroup utils
4 *
5 * LibGimbal provides basic UUID functionality by allowing
6 * you to deserialize existing UUIDs from strings, generate
7 * new version 4 UUIDs, serialize UUIDs, and compare two UUIDs,
8 * based on RFC 4122.
9 *
10 * \todo
11 * - fix reliance on apparently determinsitic random mechanism
12 * - GblVariant support, stored as a GblRef*?
13 *
14 * \author 2023 Falco Girgis
15 * \copyright MIT License
16 */
17#ifndef GIMBAL_UUID_H
18#define GIMBAL_UUID_H
19
20#include "../core/gimbal_ctx.h"
21
22#define GBL_UUID_BYTE_COUNT 16 //!< Total number of bytes to representa a UUID, per RFC 4122.
23#define GBL_UUID_NODE_COUNT 6 //!< Number of node bytes in a UUID, per RFC 4122.
24#define GBL_UUID_STRING_SIZE 37 //!< Byte size of a string needed to represent a UUID, including NULL terminator.
25#define GBL_UUID_STRING_LENGTH 36 //!< Length of string needed to represent a UUID, excluding NULL terminator.
26
27#define GBL_SELF_TYPE GblUuid
28
30
31/*! \brief Universally unique identifier
32 * \ingroup utils
33 *
34 * RFC 4122-based UUID type.
35 */
36typedef struct GblUuid {
37 union {
39 uint32_t time_low;
40 uint16_t time_mid;
41 uint16_t time_hi_and_version;
42 uint8_t clock_seq_hi_and_reserved;
43 uint8_t clock_seq_low;
44 uint8_t node[GBL_UUID_NODE_COUNT];
46 uint8_t bytes[GBL_UUID_BYTE_COUNT];
47 };
48} GblUuid;
49
50/*! \name Initialization
51 * \brief Methods for initializing and setting
52 * \relatesalso GblUuid
53 * @{
54 */
55//! Initializes the given GblUuid structure to a new, randomly generated UUID per RFC 4122 (version 4).
57//! Initializes the given GblUuid structure by deserializing the string representation given by pStrBuffer.
58GBL_EXPORT GBL_RESULT GblUuid_parse (GBL_SELF, const char* pBuffer) GBL_NOEXCEPT;
59//! Initializes the given GblUuid structure to a "nil" UUID, per RFC 4122.
61//! @}
62
63//! Fills in the given 37-byte string buffer with the standardized string representation of the given UUID, also returning it
64GBL_EXPORT const char* GblUuid_string (GBL_CSELF, char* pBuffer) GBL_NOEXCEPT;
65//! Returns true if the given UUID is equal to the NIL UUID, per RFC 4122 (all zeroes),
67//! Lexicographically Compares the given UUID to another, returning 0 if equal, -1 if less than, or 1 if greater than/
68GBL_EXPORT int GblUuid_compare (GBL_CSELF, const GblUuid* pOther) GBL_NOEXCEPT;
69//! Returns the UUID version type, according to RFC 4122.
71
73
74#undef GBL_SELF_TYPE
75
76#endif // GIMBAL_UUID_H
77
78/*!
79 * \fn GBL_RESULT GblUuid_setNil(GblUuid* pSelf)
80 * Returns GBL_RESULT_SUCCESS upon success, or GBL_RESULT_ERROR_INVALID_POINTER,
81 * if a valid pointer was not passed as pSelf.
82 * \param pSelf pointer to a GblUuid
83 * \returns success or invalid pointer
84 * \relatesalso GblUuid
85*/
86/*!
87 * \fn GBL_RESULT GblUuid_genV4(GblUuid* pSelf)
88 * Returns GBL_RESULT_SUCCESS upon success, or
89 * GBL_RESULT_ERROR_INVALID_POINTER, if a valid pointer was not passed as pSelf.
90 * \param pSelf pointer to a GblUuid
91 * \returns success or invalid pointer
92 * \relatesalso GblUuid
93*/
94/*!
95 * \fn GBL_RESULT GblUuid_parse(GblUuid* pSelf, const char* pStrBuffer)
96 * Returns GBL_RESULT_SUCCESS upon success, or an error if the provided
97 * arguments were not valid.
98 * \param pSelf pointer to a GblUuid
99 * \param pStrBuffer Standard 36-char UUID string format (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
100 * \returns success or error message
101 * \relatesalso GblUuid
102*/
103/*!
104 * \fn const char* GblUuid_string(const GblUuid* pSelf, char* pStrBuffer)
105 * \param pSelf pointer to a GblUuid
106 * \param pStrBuffer string buffer of at least 37 bytes (36 plus NULL terminator)
107 * \returns pStrBuffer or NULL upon failure
108 * \relatesalso GblUuid
109*/
110/*!
111 * \fn int GblUuid_compare(const GblUuid* pSelf, const GblUuid* pOther)
112 * Errors are thrown if either argument is invalid.
113 * \param pSelf pointer to a GblUuid
114 * \param pOther pointer to another GblUuid
115 * \returns 0, -1, or 1
116 * \relatesalso GblUuid
117*/
118/*!
119 * \fn unsigned GblUuid_version(const GblUuid* pSelf)
120 * Throws an error if the given pointer is invalid.
121 * \param pSelf pointer to a GblUuid
122 * \returns UUID version
123 * \relatesalso GblUuid
124*/
125/*!
126 * \fn GblBool GblUuid_isNil(const GblUuid* pSelf)
127 * Throws an error if pSelf is invalid.
128 * \param pSelf pointer to a GblUuid
129 * \returns whether the given UUID is nil or not
130 * \relatesalso GblUuid
131*/
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_PRIVATE_BEGIN
#define GBL_EXPORT
#define GBL_PRIVATE_END
Private data structure.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
unsigned GblUuid_version(const GblUuid *pSelf)
Returns the UUID version type, according to RFC 4122.
#define GBL_UUID_BYTE_COUNT
Total number of bytes to representa a UUID, per RFC 4122.
Definition gimbal_uuid.h:22
int GblUuid_compare(const GblUuid *pSelf, const GblUuid *pOther)
Lexicographically Compares the given UUID to another, returning 0 if equal, -1 if less than,...
GblBool GblUuid_isNil(const GblUuid *pSelf)
Returns true if the given UUID is equal to the NIL UUID, per RFC 4122 (all zeroes),...
#define GBL_UUID_NODE_COUNT
Number of node bytes in a UUID, per RFC 4122.
Definition gimbal_uuid.h:23
Universally unique identifier.
Definition gimbal_uuid.h:36
GBL_RESULT GblUuid_genV4(GblUuid *pSelf)
Initializes the given GblUuid structure to a new, randomly generated UUID per RFC 4122 (version 4).
GBL_RESULT GblUuid_setNil(GblUuid *pSelf)
Initializes the given GblUuid structure to a "nil" UUID, per RFC 4122.
const char * GblUuid_string(const GblUuid *pSelf, char *pBuffer)
Fills in the given 37-byte string buffer with the standardized string representation of the given UUI...
GBL_RESULT GblUuid_parse(GblUuid *pSelf, const char *pBuffer)
Initializes the given GblUuid structure by deserializing the string representation given by pStrBuffe...