xtd 0.2.0
Loading...
Searching...
No Matches
format_enum_class_flags.cpp

Show how to use format xtd::format class enum classes flags.

#include <xtd/console>
#include <xtd/enum_class>
#include <xtd/environment>
#include <xtd/string>
using namespace xtd;
enum class text_styles {
normal = 0b0,
bold = 0b1,
italic = 0b10,
underline = 0b100,
strikeout = 0b1000,
};
// Add text_style enum class flags operators and register flags attribute.
flags_attribute_(, text_styles);
// Only this operator is needed for text_styles enum class to be recognized by xtd::string::format().
template<> struct xtd::enum_register<text_styles> {
explicit operator auto() const noexcept {return xtd::enum_collection<text_styles> {{text_styles::normal, "normal"}, {text_styles::bold, "bold"}, {text_styles::italic, "italic"}, {text_styles::underline, "underline"}, {text_styles::strikeout, "strikeout"}};}
};
auto main() -> int {
console::out << string::format("{}", text_styles::bold | text_styles::italic) << environment::new_line;
console::out << string::format("0b{:b}", text_styles::bold | text_styles::italic) << environment::new_line;
console::out << string::format("0b{:B}", text_styles::bold | text_styles::italic) << environment::new_line;
console::out << string::format("{:d}", text_styles::bold | text_styles::italic) << environment::new_line;
console::out << string::format("{:D}", text_styles::bold | text_styles::italic) << environment::new_line;
console::out << string::format("{:g}", text_styles::bold | text_styles::italic) << environment::new_line;
console::out << string::format("{:G}", text_styles::bold | text_styles::italic) << environment::new_line;
console::out << string::format("0{:o}", text_styles::bold | text_styles::italic) << environment::new_line;
console::out << string::format("0{:O}", text_styles::bold | text_styles::italic) << environment::new_line;
console::out << string::format("0x{:x}", text_styles::bold | text_styles::italic) << environment::new_line;
console::out << string::format("0x{:X}", text_styles::bold | text_styles::italic) << environment::new_line;
}
// This code produces the following output :
//
// saturday
// 0b101
// 0b101
// 5
// 5
// saturday
// saturday
// 05
// 05
// 0x5
// 0x5
#define flags_attribute_(namespace_name, enum_type)
Provides the set attribute struct for enumerations helper.
Definition flags_attribute.hpp:34
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.hpp:22
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Provides the registration struct for enumerations.
Definition enum_register.hpp:38