tagval 0.2.0
Modern C++23 header-only library of tagged values (open/closed enumerations with metadata)
Loading...
Searching...
No Matches
closed_ended.hpp
Go to the documentation of this file.
1#pragma once
2
12
13#include <tagval/base.hpp>
14#include <tagval/entry.hpp>
15#include <tagval/open_ended.hpp> // for detail::HasValuesT
16#include <tagval/values.hpp>
17
18#include <commons/fixed_string.hpp>
19
20#include <expected>
21#include <span>
22#include <string_view>
23
24namespace tagval {
25
36template <comms::FixedString Id, typename Derived>
37class ClosedEnded : public detail::HandleBase<Id, Derived> {
39
40public:
41 using base_t::base_t;
42
47 template <typename E>
48 [[nodiscard]] static const Derived& value() noexcept {
49 static_assert(detail::HasValuesT<Derived>,
50 "tagval: kind type is missing `using values_t = tagval::Values<...>;`");
51 static_assert(Derived::values_t::template contains<E>,
52 "Entry type is not listed in Derived::values_t");
53 static const Derived h = base_t::make_handle(base_t::find_in(all_values(), E::code));
54 return h;
55 }
56
59 [[nodiscard]] static std::span<const TagValMetadata> all_values() noexcept {
60 static_assert(detail::HasValuesT<Derived>,
61 "tagval: kind type is missing `using values_t = tagval::Values<...>;`");
62 static constexpr auto arr = values_metadata_array<typename Derived::values_t>();
63 return std::span<const TagValMetadata>{arr};
64 }
65
67 [[nodiscard]] static Derived of(std::string_view code) {
69 }
70
72 [[nodiscard]] static std::expected<Derived, ParseError> try_of(std::string_view code) noexcept {
74 }
75};
76
77} // namespace tagval
CRTP HandleBase used by ClosedEnded and OpenEnded.
CRTP base for tag-value kinds whose values are fixed at compile time.
Definition closed_ended.hpp:37
static std::span< const TagValMetadata > all_values() noexcept
All values for this kind, in declaration order.
Definition closed_ended.hpp:59
static Derived of(std::string_view code)
Look up a value by code. Throws UnknownCodeError on miss.
Definition closed_ended.hpp:67
static const Derived & value() noexcept
Compile-time canonical handle for an entry.
Definition closed_ended.hpp:48
static std::expected< Derived, ParseError > try_of(std::string_view code) noexcept
Look up a value by code. Returns ParseError on miss.
Definition closed_ended.hpp:72
CRTP handle.
Definition base.hpp:49
static Derived of_impl(const std::span< const TagValMetadata > values, const std::string_view code)
Definition base.hpp:126
static const TagValMetadata * find_in(const std::span< const TagValMetadata > values, const std::string_view code) noexcept
Linear search a metadata range by code.
Definition base.hpp:116
static std::expected< Derived, ParseError > try_of_impl(const std::span< const TagValMetadata > values, const std::string_view code) noexcept
Definition base.hpp:141
static Derived make_handle(const TagValMetadata *m) noexcept
Build a Derived handle from a stable metadata pointer.
Definition base.hpp:109
std::string_view code() const noexcept
Definition base.hpp:55
Definition open_ended.hpp:42
Compile-time Entry NTTP and the runtime-view TagValMetadata struct.
Definition base.hpp:26
OpenEnded base — same in-class declaration shape as ClosedEnded (TAGVAL_ENTRY + using values_t = tagv...
Compile-time list of Entry types for ClosedEnded kinds.