xtd 0.2.0
Loading...
Searching...
No Matches

◆ flags_attribute_

#define flags_attribute_ (   namespace_name,
  enum_type 
)

#include <xtd.core/include/xtd/flags_attribute.h>

Provides the set attribute struct for enumerations helper.

Namespace
xtd
Library
xtd.core
Parameters
namespace_nameThe name of the the namespace. Empty if no namespace.
enum_typeThe name of the enum flags.
Remarks
This helper is created to facilitate to set the xtd::enum_set_attribute with the xtd::enum_attribute::flags atribute and the implementation of the following operators for enum flags:
Operator Name
^= Bitwise XOR assignment
&= Bitwise AND assignment
|= Bitwise OR assignment
+= Addition assignment
-= Subtraction assignment
Bitwise XOR
& Bitwise AND
| Bitwise OR
+ Addition
- Subtraction
~ Bitwise NOT
Warning
The helper as one limitiation :
  • The enum's flags cannot be in a class or struct. The enum must be in the global namespace or in a namespace hierarchy. If the enum flags is in a class or struct, add operators manually and use xtd::enum_set_attribute to register the xtd::enum_attribute::flags attribute.
Examples
The following code show how to use flags_attribute_ helper.
#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.h: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.h:22
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Provides the registration struct for enumerations.
Definition enum_register.h:38
Examples
enum_class_flags.cpp, enum_class_flags_introspection.cpp, and format_enum_class_flags.cpp.