xtd 1.0.0
Loading...
Searching...
No Matches
xtd::enum_register< enum_t > Struct Template Reference

Definition

template<typename enum_t>
struct xtd::enum_register< enum_t >

Provides the registration struct for enumerations.

template<typename enum_t>
Provides the registration struct for enumerations.
Definition enum_register.hpp:38
Header
#include <xtd/enum_register>
Namespace
xtd
Library
xtd.core
Remarks
The overloading of this operator is necessary for enum classes to be recognized by xtd::string::format().
For more information about enumeration, see enum class guide.
When an enumeration is registered, we can display its name instead of its value, we can format it and parse it.
See Enumeration Format Strings. For more information about formatting in general, see Formatting Types.
Examples
The following code show how to use xtd::enum_register operator for an enum.
#include <xtd/xtd>
enum class enum_test {
value_one,
value_two,
value_three,
value_four
};
template<> struct xtd::enum_register<enum_test> {
static auto values() noexcept {return xtd::enum_collection<enum_test> {{enum_test::value_one, "value_one"}, {enum_test::value_two, "value_two"}, {enum_test::value_three, "value_three"}, {enum_test::value_four, "value_four"}};}
};
auto main() -> int {
console::write_line("name = {}", enum_test::value_four);
console::write_line("value = {}", enum_object(enum_test::value_four).to_int32());
console::write_line("as<int> = {}", as<int>(enum_test::value_four));
console::write_line("values = {}", enum_object<>::get_values_as_int32<enum_test>());
console::write_line("names = {}", enum_object<>::get_names<enum_test>());
console::write_line("entries = {}", enum_object<>::get_entries_as_int32<enum_test>());
}
// This code produces the following output :
//
// name = value_four
// value = 3
// as<int> = 3
// values = [0, 1, 2, 3]
// names = [value_one, value_two, value_three, value_four]
// entries = [(0, value_one), (1, value_two), (2, value_three), (3, value_four)]
std::vector< std::pair< enum_t, xtd::string > > enum_collection
Represents a pair of an enum_t value and a string of an enum of type enum_t.
Definition enum_collection.hpp:24
static auto values() noexcept
Definition enum_register.hpp:55
The following code show how to use xtd::enum_register operator for an enum flags.
#include <xtd/xtd>
enum class text_attribute {
normal = 0b0,
bold = 0b1,
italic = 0b10,
underline = 0b100,
strikeout = 0b1000
};
text_attribute operator|(text_attribute lhs, text_attribute rhs) {return static_cast<text_attribute>(static_cast<std::underlying_type<text_attribute>::type>(lhs) | static_cast<std::underlying_type<text_attribute>::type>(rhs));}
template<> struct xtd::enum_set_attribute<text_attribute> {
static auto attribute() noexcept {return xtd::enum_attribute::flags;}
};
template<> struct xtd::enum_register<text_attribute> {
static auto values() noexcept {return xtd::enum_collection<text_attribute> {{text_attribute::normal, "normal"}, {text_attribute::bold, "bold"}, {text_attribute::italic, "italic"}, {text_attribute::underline, "underline"}, {text_attribute::strikeout, "strikeout"}};}
};
auto main() -> int {
console::write_line("name = {}", text_attribute::bold | text_attribute::italic);
console::write_line("value = {}", enum_object(text_attribute::bold | text_attribute::italic).to_int32());
console::write_line("as<int> = {}", as<int>(text_attribute::bold | text_attribute::italic));
console::write_line("values = {}", enum_object<>::get_values_as_int32<text_attribute>());
console::write_line("names = {}", enum_object<>::get_names<text_attribute>());
console::write_line("entries = {}", enum_object<>::get_entries_as_int32<text_attribute>());
}
@ flags
Enum flags attribute.
Definition enum_attribute.hpp:26
Provides the set attribute struct for enumerations.
Definition enum_set_attribute.hpp:36
static auto attribute() noexcept
Definition enum_set_attribute.hpp:55
Examples
enum_class_flags_without_helpers.cpp, enum_class_without_helper.cpp, format_enum.cpp, format_enum_class.cpp, format_enum_class_flags.cpp, format_enum_class_flags_without_helper.cpp, sprintf_enum.cpp, and sprintf_enum_class.cpp.

Static Public Member Functions

static auto values () noexcept

Member Function Documentation

◆ values()

template<typename enum_t>
auto xtd::enum_register< enum_t >::values ( )
inlinestaticnoexcept

Allows to register an enumeration that can be used by xtd::enum_object.

Returns
An xtd::enum_collection collection that represent enumeration.
Remarks
To register an enumeration just override xtd::enum_register.
Examples
The following code show how to register the values enum class.
enum class values {
value_one,
value_two
};
template<>
static auto values() noexcept {return xtd::enum_collection<values> {{values::value_one, "value_one"}, {values::value_two, "value_two"}};}
};
Examples
enum_class_flags_without_helpers.cpp, enum_class_without_helper.cpp, format_enum.cpp, format_enum_class.cpp, format_enum_class_flags.cpp, format_enum_class_flags_without_helper.cpp, sprintf_enum.cpp, and sprintf_enum_class.cpp.

The documentation for this struct was generated from the following file: