plugify 1.2.8
Loading...
Searching...
No Matches
types.hpp
1#pragma once
2
3#include <optional>
4#include <source_location>
5#include <string>
6#include <vector>
7
8// #include "plg/uuid.hpp"
9#include "plg/enum.hpp"
10#include "plg/expected.hpp"
11#include "plg/inplace_vector.hpp"
12#include "plg/format.hpp"
13#include "plg/path.hpp"
14#include "plg/version.hpp"
15
16namespace plugify {
24 using Version = plg::version<>;
25
29 using Constraint = plg::range_set<>;
30
35 struct UniqueId {
36 using Value = std::ptrdiff_t;
37
38 constexpr UniqueId() noexcept = default;
39
40 constexpr explicit UniqueId(Value v) noexcept
41 : value(v) {
42 }
43
44 // conversion to underlying value
45 operator auto() const noexcept {
46 return value;
47 }
48
49 // prefix increment / decrement
50 constexpr UniqueId& operator++() noexcept {
51 ++value;
52 return *this;
53 }
54
55 constexpr UniqueId& operator--() noexcept {
56 --value;
57 return *this;
58 }
59
60 // postfix increment / decrement
61 constexpr UniqueId operator++(int) noexcept {
62 UniqueId old{ value };
63 ++value;
64 return old;
65 }
66
67 constexpr UniqueId operator--(int) noexcept {
68 UniqueId old{ value };
69 --value;
70 return old;
71 }
72
73 // compound assign helpers
74 constexpr UniqueId& operator+=(Value d) noexcept {
75 value += d;
76 return *this;
77 }
78
79 constexpr UniqueId& operator-=(Value d) noexcept {
80 value -= d;
81 return *this;
82 }
83
84 constexpr bool operator==(UniqueId other) const noexcept {
85 return value == other.value;
86 }
87
88 constexpr auto operator<=>(UniqueId other) const noexcept {
89 return value <=> other.value;
90 }
91
92 constexpr void SetName(const std::string& str) noexcept {
93 name = str.c_str();
94 }
95
96 private:
97 Value value{ -1 };
98 const char* name{ nullptr }; // Debug-only pointer.
99 };
100
108 enum class ExtensionType {
109 Unknown,
110 Module,
111 Plugin
112 };
113
124 template <typename T>
125 using Result = std::expected<T, std::string>;
126
127 // Helper for creating errors with context
128
129 template <plg::string_like First>
130 PLUGIFY_FORCE_INLINE auto MakeError(First&& error) {
131 return std::unexpected(std::forward<First>(error));
132 }
133
134 template <typename... Args>
135 PLUGIFY_FORCE_INLINE auto MakeError(std::format_string<Args...> fmt, Args&&... args) {
136 return std::unexpected(std::format(fmt, std::forward<Args>(args)...));
137 }
138}
139
140namespace std {
141 template <>
142 struct hash<plugify::UniqueId> {
143 std::size_t operator()(plugify::UniqueId id) const noexcept {
144 return std::hash<plugify::UniqueId::Value>{}(id);
145 }
146 };
147}
148
149#ifdef FMT_HEADER_ONLY
150namespace fmt {
151#else
152namespace std {
153#endif
154 template <>
155 struct formatter<plugify::UniqueId> {
156 constexpr auto parse(std::format_parse_context& ctx) {
157 return ctx.begin();
158 }
159
160 template <class FormatContext>
161 auto format(const plugify::UniqueId& id, FormatContext& ctx) const {
162 return std::format_to(ctx.out(), "{}", plugify::UniqueId::Value{ id });
163 }
164 };
165}