59 [[nodiscard]]
virtual std::string_view
kind() const noexcept = 0;
93 static constexpr std::string_view KIND = Kind.view();
95 [[nodiscard]] std::string_view
kind() const noexcept
override {
99 [[nodiscard]] std::unique_ptr<IOrigin>
clone()
const override {
100 return std::make_unique<Derived>(
static_cast<const Derived&
>(*
this));
104 return Derived::display_info();
116 .description =
"Registered by the host core itself.",
117 .icon = Icon::from(
"mdi:home"),
129 .description =
"Registered by an internal subsystem.",
130 .icon = Icon::from(
"mdi:cog"),
142 explicit ExternalOrigin(std::string src) : source(std::move(src)) {}
147 .description =
"Registered by an external source.",
148 .icon = Icon::from(
"mdi:web"),
160 .description =
"Provenance is unknown.",
161 .icon = Icon::from(
"mdi:help-circle"),
179 std::map<std::string, Factory, std::less<>> factories_;
197 return factories_.emplace(std::string{kind}, factory).second;
202 if (
const auto it = factories_.find(kind); it != factories_.end()) {
209 [[nodiscard]]
bool contains(
const std::string_view kind)
const {
210 return factories_.contains(kind);
221 GlobalOriginRegistry::instance().register_kind(
222 T::KIND, []() ->
OriginPtr {
return std::make_unique<T>(); });
231#define COMMONS_REGISTER_ORIGIN(Ident) \
232 inline const ::comms::OriginRegistrar<Ident> commons_origin_registrar_##Ident {}
250 return std::string{o.
kind()};
253inline std::ostream& operator<<(std::ostream& os,
const IOrigin& o) {
254 return os << o.kind();
272 requires std::derived_from<T, comms::IOrigin>
273struct std::formatter<T> {
274 constexpr auto parse(
const std::format_parse_context& ctx) {
275 const auto* it = ctx.begin();
276 if (it != ctx.end() && *it !=
'}') {
277 throw std::format_error(
"commons: IOrigin takes no format spec");
282 auto format(
const comms::IOrigin& o, std::format_context& ctx)
const {
283 return std::format_to(ctx.out(),
"{}", comms::to_string(o));
The definition was registered by the host core itself.
Definition origin.hpp:111
The definition came from an external source, named by source.
Definition origin.hpp:137
std::string source
Free-form identifier of the external source.
Definition origin.hpp:139
A program-wide registry mapping an origin kind string to a factory.
Definition origin.hpp:173
OriginPtr create(const std::string_view kind) const
Create a fresh origin for kind, or nullptr if the kind is unknown.
Definition origin.hpp:201
bool register_kind(const std::string_view kind, Factory factory)
Register factory under kind.
Definition origin.hpp:196
bool contains(const std::string_view kind) const
Whether kind has a registered factory.
Definition origin.hpp:209
OriginPtr(*)() Factory
Produces a fresh, default-constructed origin of one kind.
Definition origin.hpp:176
Abstract provenance envelope.
Definition origin.hpp:54
virtual std::unique_ptr< IOrigin > clone() const =0
A deep, independent copy.
virtual std::string_view kind() const noexcept=0
The stable discriminator for this origin (e.g. "core").
virtual const DisplayInfo & info() const =0
Presentation metadata for this origin's kind (the description), sourced from the concrete type's stat...
The definition was registered by an internal subsystem.
Definition origin.hpp:124
CRTP base wiring kind(), clone(), and info() from a compile-time kind string and the concrete Derived...
Definition origin.hpp:87
std::unique_ptr< IOrigin > clone() const override
A deep, independent copy.
Definition origin.hpp:99
std::string_view kind() const noexcept override
The stable discriminator for this origin (e.g. "core").
Definition origin.hpp:95
const DisplayInfo & info() const override
Presentation metadata for this origin's kind (the description), sourced from the concrete type's stat...
Definition origin.hpp:103
Provenance is unknown.
Definition origin.hpp:155
std::string to_string(const Color &c)
Color as its canonical hex string (#RRGGBB, or #RRGGBBAA when not opaque).
Definition color.hpp:1388
Presentation metadata for a type/value — name, description, icon, color, all optional — plus the trai...
const DisplayInfo & display_info()
Retrieve the DisplayInfo for T via whichever mechanism is present.
Definition display_info.hpp:73
NTTP-friendly fixed-size string usable as a non-type template parameter.
#define COMMONS_REGISTER_ORIGIN(Ident)
Register an already-defined origin type Ident into the GlobalOriginRegistry.
Definition origin.hpp:231
std::unique_ptr< IOrigin > OriginPtr
A heap-owned origin. The canonical way to carry an IOrigin by value.
Definition origin.hpp:79
Presentation metadata for a type/value.
Definition display_info.hpp:43
std::optional< std::string > name
Human-readable label.
Definition display_info.hpp:44
Compile-time fixed-size string usable as a non-type template parameter.
Definition fixed_string.hpp:21
A self-registering object: constructing one registers T's factory into the GlobalOriginRegistry.
Definition origin.hpp:219