ORE Studio 0.0.4
Loading...
Searching...
No Matches
tenant_id.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 */
19#ifndef ORES_UTILITY_UUID_TENANT_ID_HPP
20#define ORES_UTILITY_UUID_TENANT_ID_HPP
21
22#include <string>
23#include <ostream>
24#include <optional>
25#include <expected>
26#include <string_view>
27#include <boost/uuid/uuid.hpp>
28
29namespace ores::utility::uuid {
30
38inline constexpr char nil_uuid_str[] = "00000000-0000-0000-0000-000000000000";
39
47inline constexpr char max_uuid_str[] = "ffffffff-ffff-ffff-ffff-ffffffffffff";
48
66class tenant_id final {
67public:
77 static tenant_id system();
78
88 static std::expected<tenant_id, std::string>
89 from_uuid(const boost::uuids::uuid& uuid);
90
101 static std::expected<tenant_id, std::string>
102 from_string(std::string_view str);
103
109 bool is_system() const noexcept;
110
120 bool is_nil() const noexcept;
121
127 const boost::uuids::uuid& to_uuid() const noexcept;
128
134 std::string to_string() const;
135
139 bool operator==(const tenant_id& other) const noexcept = default;
140
141private:
147 explicit tenant_id(boost::uuids::uuid uuid);
148
149 boost::uuids::uuid uuid_;
150};
151
159inline std::ostream& operator<<(std::ostream& os, const tenant_id& id) {
160 return os << id.to_string();
161}
162
170inline std::ostream& operator<<(std::ostream& os, const std::optional<tenant_id>& id) {
171 if (id.has_value()) {
172 return os << id->to_string();
173 }
174 return os << "(none)";
175}
176
177}
178
179#endif
UUID generation and handling utilities.
Definition ores.utility.uuid.hpp:28
constexpr char max_uuid_str[]
String representation of the max UUID (all ones).
Definition tenant_id.hpp:47
std::ostream & operator<<(std::ostream &os, const tenant_id &id)
Stream insertion operator for tenant_id.
Definition tenant_id.hpp:159
constexpr char nil_uuid_str[]
String representation of the nil UUID (all zeros).
Definition tenant_id.hpp:38
A strongly-typed wrapper around a UUID representing a tenant identifier.
Definition tenant_id.hpp:66
static std::expected< tenant_id, std::string > from_uuid(const boost::uuids::uuid &uuid)
Creates a tenant_id from a boost UUID.
Definition tenant_id.cpp:47
bool operator==(const tenant_id &other) const noexcept=default
Equality comparison operator.
const boost::uuids::uuid & to_uuid() const noexcept
Returns the underlying boost UUID.
Definition tenant_id.cpp:77
bool is_system() const noexcept
Checks if this tenant_id represents the system tenant.
Definition tenant_id.cpp:68
std::string to_string() const
Converts the tenant_id to its string representation.
Definition tenant_id.cpp:81
bool is_nil() const noexcept
Checks if the underlying UUID is nil.
Definition tenant_id.cpp:73
static std::expected< tenant_id, std::string > from_string(std::string_view str)
Creates a tenant_id from a string representation.
Definition tenant_id.cpp:57
static tenant_id system()
Creates a tenant_id representing the system tenant.
Definition tenant_id.cpp:41