tagval 0.2.0
Modern C++23 header-only library of tagged values (open/closed enumerations with metadata)
Loading...
Searching...
No Matches
parcel.hpp
Go to the documentation of this file.
1#pragma once
2
17
18#if !defined(TAGVAL_WITH_PARCEL)
19#if __has_include(<parcel/parcel.h>)
20#define TAGVAL_WITH_PARCEL 1
21#else
22#define TAGVAL_WITH_PARCEL 0
23#endif
24#endif
25
26#if TAGVAL_WITH_PARCEL
27
28#include <tagval/base.hpp>
30
31#include <concepts>
32#include <memory>
33#include <string>
34#include <string_view>
35
36#include <parcel/parcel.h>
37
38namespace tagval {
39
40template <typename TagT>
41 requires std::derived_from<TagT, detail::TagValBaseTag>
42class TagValCell : public ::parcel::BaseCell<TagValCell<TagT>, TagT> {
43 using base_t = ::parcel::BaseCell<TagValCell<TagT>, TagT>;
44
45public:
46 using base_t::base_t;
47 using base_t::operator=;
48
49 static constexpr std::string_view kind_id = "tagval";
50
51 [[nodiscard]] std::string to_string() const override {
52 return std::string{this->value.code()};
53 }
54
55 static ::parcel::cell_t from_json(::parcel::json_t const& j, ::parcel::ParcelRegistry const&) {
56 auto v = base_t::template cell_from_json<TagT>(j, kind_id);
57 auto cell = std::make_shared<TagValCell>(v);
58 base_t::absorb_display_info(j, cell);
59 return cell;
60 }
61
62 static ::parcel::cell_type_descriptor_t descriptor() {
63 // The parcel descriptor is per-type and process-wide static (shared
64 // across every value of TagT), so per-value icon/color cannot live
65 // here — those stay reachable via the handle (e.g. cell.value.icon()).
66 // Source the type-level name plus the kind-level (per-type) icon/color
67 // from TagT::descriptor().
68 static const auto d = [] {
69 constexpr auto kd = TagT::descriptor(); // per-type TagValDescriptor
70 ::parcel::DisplayInfo info{};
71 info.name = "tagval::TagVal";
72 if (kd.icon) {
73 info.icon = *kd.icon;
74 }
75 if (kd.color) {
76 info.color = *kd.color;
77 }
78 return std::make_shared<::parcel::SimpleCellTypeDescriptor<TagValCell>>(info);
79 }();
80 return d;
81 }
82};
83
84} // namespace tagval
85
86#endif // TAGVAL_WITH_PARCEL
CRTP HandleBase used by ClosedEnded and OpenEnded.
Definition parcel.hpp:42
static constexpr std::string_view kind_id
Definition parcel.hpp:49
::parcel::cell_type_descriptor_t descriptor()
Definition parcel.hpp:62
std::string to_string() const override
Definition parcel.hpp:51
::parcel::cell_t from_json(::parcel::json_t const &j, ::parcel::ParcelRegistry const &)
Definition parcel.hpp:55
Optional nlohmann::json integration.
Definition base.hpp:26