tagval 0.2.0
Modern C++23 header-only library of tagged values (open/closed enumerations with metadata)
Loading...
Searching...
No Matches
entry.hpp
Go to the documentation of this file.
1#pragma once
2
12
13#include <commons/color.hpp>
14#include <commons/fixed_string.hpp>
15#include <commons/icon.hpp>
16
17#include <optional>
18#include <string_view>
19
20namespace tagval {
21
28template <typename Owner,
29 comms::FixedString Code,
30 comms::FixedString Label = "",
31 comms::FixedString Icon = "",
32 comms::FixedString Color = "">
33struct Entry {
34 static_assert(Code.size() > 0, "tagval::Entry: code must not be empty");
35
36 using owner_t = Owner;
37 static constexpr auto code = Code;
38 static constexpr auto label = Label;
39 static constexpr auto icon = Icon;
40 static constexpr auto color = Color;
41};
42
47 std::string_view code;
48 std::string_view label;
49 std::optional<comms::Icon> icon;
50 std::optional<comms::Color> color;
51
52 [[nodiscard]] friend constexpr bool operator==(const TagValMetadata&,
53 const TagValMetadata&) noexcept = default;
54};
55
58template <typename E>
59[[nodiscard]] constexpr std::string_view label_or_code() noexcept {
60 if constexpr (constexpr std::string_view l = E::label; l.empty()) {
61 return std::string_view{E::code};
62 } else {
63 return l;
64 }
65}
66
67template <typename E>
68[[nodiscard]] constexpr TagValMetadata metadata_of() noexcept {
69 return TagValMetadata{
70 .code = std::string_view{E::code},
71 .label = label_or_code<E>(),
72 .icon = comms::Icon::parse(std::string_view{E::icon}),
73 .color = comms::Color::parse(std::string_view{E::color}),
74 };
75}
76
82template <typename E>
83inline constexpr TagValMetadata metadata_v = metadata_of<E>();
84
85} // namespace tagval
Definition base.hpp:26
constexpr std::string_view label_or_code() noexcept
If the user declared an empty label, fall back to the code so that label() never returns an empty str...
Definition entry.hpp:59
constexpr TagValMetadata metadata_of() noexcept
Definition entry.hpp:68
constexpr TagValMetadata metadata_v
Pinned, per-entry TagValMetadata constant.
Definition entry.hpp:83
Compile-time entry.
Definition entry.hpp:33
static constexpr auto icon
Definition entry.hpp:39
Owner owner_t
Definition entry.hpp:36
static constexpr auto label
Definition entry.hpp:38
static constexpr auto color
Definition entry.hpp:40
static constexpr auto code
Definition entry.hpp:37
Runtime view of an entry's metadata.
Definition entry.hpp:46
std::string_view code
Definition entry.hpp:47
friend constexpr bool operator==(const TagValMetadata &, const TagValMetadata &) noexcept=default
std::string_view label
Definition entry.hpp:48
std::optional< comms::Icon > icon
Definition entry.hpp:49
std::optional< comms::Color > color
Definition entry.hpp:50