66template <FixedString Name>
68 static constexpr std::string_view name = Name.view();
77template <FixedString Name,
typename Category = UnsetFlagCategory>
79 static constexpr std::string_view name = Name.view();
80 using category = Category;
81 static constexpr std::string_view category_name = Category::name;
92 { C::name } -> std::convertible_to<std::string_view>;
99 { F::name } -> std::convertible_to<std::string_view>;
100 typename F::category;
105template <
typename F,
typename Category>
127 FlagRef r{.
name = F::name, .category = F::category::name};
129 r.display = &display_info<F>();
134 bool operator==(
const FlagRef& o)
const noexcept {
135 return name == o.name;
148 std::vector<FlagRef> items_;
155 return insert(FlagRef::of<F>());
160 if (contains(f.
name)) {
177 [[nodiscard]]
bool contains()
const {
178 return contains(F::name);
181 [[nodiscard]]
bool contains(std::string_view name)
const {
182 return std::ranges::any_of(items_, [name](
const FlagRef& f) {
return f.name == name; });
186 [[nodiscard]] std::size_t
count(
const std::string_view name)
const {
187 return contains(name) ? 1U : 0U;
192 return erase(F::name);
196 bool erase(
const std::string_view name) {
197 for (
auto it = items_.begin(); it != items_.end(); ++it) {
198 if (it->name == name) {
206 void clear() noexcept {
210 [[nodiscard]]
bool empty() const noexcept {
211 return items_.empty();
214 [[nodiscard]] std::size_t size() const noexcept {
215 return items_.size();
218 [[nodiscard]]
auto begin() const noexcept {
219 return items_.begin();
222 [[nodiscard]]
auto end() const noexcept {
229 std::map<std::string_view, std::vector<FlagRef>> groups;
230 for (
const auto& f : items_) {
231 groups[f.category].push_back(f);
236 bool operator==(
const FlagSet&)
const =
default;
263 return flags_.
insert<F>();
270 [[nodiscard]]
const FlagSet& flags()
const noexcept {
275 [[nodiscard]] std::optional<FlagRef>
find(
const std::string_view name)
const {
276 for (
const auto& f : flags_) {
277 if (f.
name == name) {
284 [[nodiscard]]
auto group_by_category()
const {
295 GlobalFlagRegistry::instance().add<F>();
306template <
typename F,
typename... Categories>
322 [[nodiscard]]
bool has_flag()
const {
323 return flags().template contains<F>();
326 [[nodiscard]]
bool has_flag(
const std::string_view name)
const {
327 return flags().contains(name);
333 IHasFlags() =
default;
334 IHasFlags(
const IHasFlags&) =
default;
335 IHasFlags(IHasFlags&&) =
default;
336 IHasFlags& operator=(
const IHasFlags&) =
default;
337 IHasFlags& operator=(IHasFlags&&) =
default;
346template <
typename... Categories>
372template <
typename Derived,
typename... Categories>
384 flags_.template insert<F>();
396 requires AllowedFlag<F, Categories...>
398 return insert_flag<F>();
407 requires AllowedFlag<F, Categories...>
410 flags_.template insert<F>();
412 flags_.template erase<F>();
418 Derived& remove_flag() {
419 flags_.template erase<F>();
423 Derived& remove_flag(
const std::string_view name) {
430 flags_ = std::move(
flags);
434 Derived& clear_flags() {
449 Derived& self() noexcept {
450 return static_cast<Derived&
>(*this);
458template <
typename Derived,
typename... Categories>
480#define COMMONS_FLAG_CATEGORY(Ident, Name) \
481 struct Ident : ::comms::FlagCategory<Name> {}
484#define COMMONS_FLAG(Ident, Name) \
485 struct Ident : ::comms::Flag<Name> {}
488#define COMMONS_FLAG_IN(Ident, Name, Cat) \
489 struct Ident : ::comms::Flag<Name, Cat> {}
493#define COMMONS_REGISTER_FLAG(Ident) \
494 inline const ::comms::FlagRegistrar<Ident> commons_flag_registrar_##Ident {}
498#define COMMONS_DEFINE_FLAG(Ident, Name) \
499 struct Ident : ::comms::Flag<Name> {}; \
500 inline const ::comms::FlagRegistrar<Ident> commons_flag_registrar_##Ident
504#define COMMONS_DEFINE_FLAG_IN(Ident, Name, Cat) \
505 struct Ident : ::comms::Flag<Name, Cat> {}; \
506 inline const ::comms::FlagRegistrar<Ident> commons_flag_registrar_##Ident
514#define COMMONS_FLAG_FAMILY(CatIdent, Name, FlagTmpl, ConceptName) \
515 struct CatIdent : ::comms::FlagCategory<Name> {}; \
516 template <::comms::FixedString N> \
517 struct FlagTmpl : ::comms::Flag<N, CatIdent> {}; \
518 template <typename F> \
519 concept ConceptName = ::comms::FlagInCategory<F, CatIdent>
A builder that is also observable: combines FlagBuilderMixin's fluent mutators with a public,...
Definition flag.hpp:459
const FlagSet & flags() const noexcept override
The owned flag set (insertion order preserved).
Definition flag.hpp:461
CRTP helper for builders.
Definition flag.hpp:373
Derived & flag()
Alias for insert_flag that reads naturally in a fluent chain (builder.flag<F>()).
Definition flag.hpp:397
Derived & set_flags(FlagSet flags)
Replace the whole set.
Definition flag.hpp:429
static constexpr bool flag_allowed
True when a flag in F's category is permitted by this builder's constraint.
Definition flag.hpp:377
Derived & insert_flag()
Insert flag F (no-op if already present).
Definition flag.hpp:383
Derived & set_flag(const bool on)
Set flag F's presence: insert when on, erase otherwise.
Definition flag.hpp:408
const FlagSet & flags() const noexcept
Read access kept protected and used internally; FlagBuilderGetters publishes it through IHasFlags.
Definition flag.hpp:444
An insertion-ordered set of unique flags, deduplicated by name.
Definition flag.hpp:147
bool insert(const FlagRef &f)
Insert a FlagRef, deduplicating by name. Returns true if newly added.
Definition flag.hpp:159
static FlagSet of()
Build a set from a flag type pack, inserting each in order.
Definition flag.hpp:170
bool insert()
Insert a flag by type.
Definition flag.hpp:154
bool erase(const std::string_view name)
Erase the flag with the given name. Returns true if one was removed.
Definition flag.hpp:196
std::map< std::string_view, std::vector< FlagRef > > group_by_category() const
Flags grouped by category name.
Definition flag.hpp:228
std::size_t count(const std::string_view name) const
0 or 1 — flags are unique by name, so this mirrors std::set::count.
Definition flag.hpp:186
A program-wide registry of every known flag.
Definition flag.hpp:245
std::optional< FlagRef > find(const std::string_view name) const
Resolve a flag by name. Used by the JSON from_json path.
Definition flag.hpp:275
Implementation of IHasFlags: embeds a FlagSet and exposes it publicly.
Definition flag.hpp:347
const FlagSet & flags() const noexcept override
The owned flag set (insertion order preserved).
Definition flag.hpp:349
static constexpr bool flag_allowed
True when a flag in F's category is permitted by this set's constraint.
Definition flag.hpp:355
Abstract read-only view of a type that owns a FlagSet.
Definition flag.hpp:314
virtual const FlagSet & flags() const noexcept=0
The owned flag set (insertion order preserved).
Whether flag F may go into a set constrained to Categories...: any flag when the list is empty (uncon...
Definition flag.hpp:307
Any type that exposes a name convertible to std::string_view — satisfied by FlagCategory<Name> and ty...
Definition flag.hpp:91
Any flag type: a name plus a category member type that is itself an AnyFlagCategory.
Definition flag.hpp:98
True when T has display metadata — via a member display_info() or a HasDisplayInfo<T> specialization.
Definition display_info.hpp:82
A flag whose category is exactly Category.
Definition flag.hpp:106
Presentation metadata for a type/value — name, description, icon, color, all optional — plus the trai...
NTTP-friendly fixed-size string usable as a non-type template parameter.
Presentation metadata for a type/value.
Definition display_info.hpp:43
A flag category, identified by a FixedString name.
Definition flag.hpp:67
A type-erased, non-owning runtime handle to a flag.
Definition flag.hpp:118
std::string_view name
Flag name; also the identity used for ==.
Definition flag.hpp:119
const DisplayInfo * display
null when the flag is not Displayable
Definition flag.hpp:121
std::string_view category
Owning category name.
Definition flag.hpp:120
static FlagRef of() noexcept
Build a FlagRef from a compile-time flag type, capturing its display metadata only if F is Displayabl...
Definition flag.hpp:126
A self-registering object: constructing one registers F into the GlobalFlagRegistry.
Definition flag.hpp:293
A compile-time flag: a distinct type named by Name and belonging to Category (defaulting to UnsetFlag...
Definition flag.hpp:78
The default category for a Flag declared without an explicit one.
Definition flag.hpp:72
Fixed-width numeric aliases shared across the C++ libraries.