ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
provsql_utils_cpp.h
Go to the documentation of this file.
1/**
2 * @file provsql_utils_cpp.h
3 * @brief C++ utility functions for UUID manipulation.
4 *
5 * This header declares C++ helper functions that convert between
6 * PostgreSQL's @c pg_uuid_t / @c Datum representations and
7 * @c std::string, compute hash values for UUIDs (enabling use in
8 * @c std::unordered_map and Boost containers), and define equality
9 * comparison for @c pg_uuid_t values.
10 *
11 * These utilities are used throughout the C++ circuit and evaluation
12 * code wherever UUID tokens need to be stored in STL containers or
13 * compared by value.
14 */
15#ifndef PROVSQL_UTILS_CPP_H
16#define PROVSQL_UTILS_CPP_H
17
18extern "C" {
19#include "postgres.h"
20#include "provsql_utils.h"
21}
22
23#include <string>
24
25/**
26 * @brief Convert a PostgreSQL @c Datum holding a UUID to a @c std::string.
27 *
28 * Detoasts and formats the UUID value as the standard 36-character
29 * hyphenated hex string (@c xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
30 *
31 * @param token A @c Datum containing a @c pg_uuid_t value.
32 * @return The UUID as a @c std::string.
33 */
34std::string UUIDDatum2string(Datum token);
35
36/**
37 * @brief Format a @c pg_uuid_t as a @c std::string.
38 *
39 * @param uuid The UUID to format.
40 * @return The UUID as a 36-character hyphenated hex @c std::string.
41 */
42std::string uuid2string(pg_uuid_t uuid);
43
44/**
45 * @brief Parse a UUID string into a @c pg_uuid_t.
46 *
47 * Accepts the standard 36-character hyphenated hex representation.
48 * Throws @c std::invalid_argument if the string is malformed.
49 *
50 * @param source The UUID string to parse.
51 * @return The parsed @c pg_uuid_t.
52 */
53pg_uuid_t string2uuid(const std::string &source);
54
55/**
56 * @brief Compute a hash value for a @c pg_uuid_t.
57 *
58 * Reinterprets the 16-byte UUID as two 64-bit integers and XOR-combines
59 * their standard hashes. Used by Boost's hash infrastructure so that
60 * @c pg_uuid_t can be used as a key in Boost multi-index containers.
61 *
62 * @param u The UUID to hash.
63 * @return A @c std::size_t hash value.
64 */
65std::size_t hash_value(const pg_uuid_t &u);
66
67/**
68 * @brief Test two @c pg_uuid_t values for equality.
69 *
70 * Compares all 16 bytes of the UUID data.
71 *
72 * @param u First UUID.
73 * @param v Second UUID.
74 * @return @c true if @p u and @p v represent the same UUID.
75 */
76bool operator==(const pg_uuid_t &u, const pg_uuid_t &v);
77
78#endif
Core types, constants, and utilities shared across ProvSQL.
pg_uuid_t string2uuid(const std::string &source)
Parse a UUID string into a pg_uuid_t.
std::string UUIDDatum2string(Datum token)
Convert a PostgreSQL Datum holding a UUID to a std::string.
std::size_t hash_value(const pg_uuid_t &u)
Compute a hash value for a pg_uuid_t.
bool operator==(const pg_uuid_t &u, const pg_uuid_t &v)
Test two pg_uuid_t values for equality.
std::string uuid2string(pg_uuid_t uuid)
Format a pg_uuid_t as a std::string.
UUID structure.