xtd 0.2.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.h: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/as>
#include <xtd/console>
#include <xtd/enum_class>
using namespace xtd;
enum class enum_test {
value_one,
value_two,
value_three,
value_four
};
template<> struct xtd::enum_register<enum_test> {
explicit operator auto() const 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));
}
// 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)]
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Provides the base class for enumerations.
Definition enum_object.h:42
std::vector< xtd::collections::generic::key_value_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.h:22
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
The following code show how to use xtd::enum_register operator for an enum flags.
#include <xtd/as>
#include <xtd/console>
#include <xtd/enum_class>
using namespace 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> {
explicit operator auto() const noexcept {return xtd::enum_attribute::flags;}
};
template<> struct xtd::enum_register<text_attribute> {
explicit operator auto() const 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));
}
@ flags
Enum flags attribute.
@ normal
Represent the exit mode when the terminates normally (via xtd::environment::exit or returning from th...
Provides the set attribute struct for enumerations.
Definition enum_set_attribute.h:31
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.

Public Member Functions

 operator auto () const noexcept
 

Member Function Documentation

◆ operator auto()

template<typename enum_t >
xtd::enum_register< enum_t >::operator auto ( ) const
inlineexplicitnoexcept

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<>
explicit operator auto() const noexcept {return xtd::enum_collection<values> {{values::value_one, "value_one"}, {values::value_two, "value_two"}};}
};

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