xtd 0.2.0
xtd::enum_object< enum_t > Class Template Reference

Definition

template<typename enum_t = std::nullptr_t>
class xtd::enum_object< enum_t >

Provides the base class for enumerations.

Header
#include <xtd/enum_object>
Namespace
xtd
Library
xtd.core
Remarks
For more information about enumeration, see enum class guide.
For more information about types, see Native types, boxing and unboxing. The following code show how to use xtd::enum_object class 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
);
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)]
The following code show how to use xtd::enum_object class 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
);
flags_attribute_(, text_attribute);
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));
}
// This code produces the following output :
//
// name = bold, italic
// value = 3
// as<int> = 3
// values = [0, 1, 2, 4, 8]
// names = [normal, bold, italic, underline, strikeout]
// entries = [(0, normal), (1, bold), (2, italic), (4, underline), (8, strikeout)]
Examples:
colored_forms.cpp, enum.cpp, enum_class.cpp, enum_class_flags.cpp, enum_class_flags_introspection.cpp, enum_class_flags_without_helpers.cpp, enum_class_parse.cpp, enum_class_ut.cpp, enum_class_without_helper.cpp, enum_struct.cpp, enum_struct_ut.cpp, enum_ut.cpp, form_background_image2.cpp, parse_enum.cpp, parse_enum_class.cpp, and parse_enum_struct.cpp.

Alias

using enum_type = enum_t
 

Constructors

 enum_object () noexcept=default
 Initializes a new instance of the xtd::enum_object class. More...
 
 enum_object (enum_type value)
 Initializes a new instance of the xtd::enum_object class with specified value. More...
 

Properties

bool has_flag (enum_type flag) const noexcept
 Retrieves an array of the values of the constants in a specified enumeration. More...
 
enum_type value () const noexcept
 Gets the value of the enum. More...
 
enum_objectvalue (enum_type value)
 Sets the value of the enum. More...
 

Methods

int32 compare_to (const enum_object &value) const noexcept override
 Compares the current instance with another object of the same type. More...
 
xtd::byte to_byte () const noexcept
 Converts this instance to byte. More...
 
int16 to_int16 () const noexcept
 Converts this instance to int16. More...
 
int32 to_int32 () const noexcept
 Converts this instance to int32. More...
 
int64 to_int64 () const noexcept
 Converts this instance to int64. More...
 
sbyte to_sbyte () const noexcept
 Converts this instance to signed byte. More...
 
uint16 to_uint16 () const noexcept
 Converts this instance to unsigned int16. More...
 
uint32 to_uint32 () const noexcept
 Converts this instance to unsigned int32. More...
 
uint64 to_uint64 () const noexcept
 Converts this instance to unsigned int64. More...
 
xtd::ustring to_string () const noexcept override
 Returns a sxd::ustring that represents the current object. More...
 
xtd::ustring to_string (const xtd::ustring &format) const
 Converts the value of this instance to its equivalent string representation using the specified format. More...
 

Additional Inherited Members

- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object. More...
 
bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object. More...
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type. More...
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance. More...
 
template<typename object_t >
std::unique_ptr< object_t > memberwise_clone () const noexcept
 Creates a shallow copy of the current object. More...
 
- Public Member Functions inherited from xtd::icomparable< enum_object< enum_t > >
- Public Member Functions inherited from xtd::iequatable< enum_object< enum_t > >
virtual bool equals (const enum_object< enum_t > &) const noexcept=0
 Indicates whether the current object is equal to another object of the same type. More...
 
- Static Public Member Functions inherited from xtd::object
static bool equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are considered equal. More...
 
static bool reference_equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are the same instance. More...
 

Constructor & Destructor Documentation

◆ enum_object() [1/2]

template<typename enum_t = std::nullptr_t>
xtd::enum_object< enum_t >::enum_object ( )
defaultnoexcept

Initializes a new instance of the xtd::enum_object class.

◆ enum_object() [2/2]

template<typename enum_t = std::nullptr_t>
xtd::enum_object< enum_t >::enum_object ( enum_type  value)
inlineexplicit

Initializes a new instance of the xtd::enum_object class with specified value.

Parameters
valueValue to set to this instance.

Member Function Documentation

◆ compare_to()

template<typename enum_t = std::nullptr_t>
int32 xtd::enum_object< enum_t >::compare_to ( const enum_object< enum_t > &  obj) const
inlineoverridevirtualnoexcept

Compares the current instance with another object of the same type.

Parameters
objAn object to compare with this instance.
Returns
A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings:
Value Condition
Less than zero This instance is less than obj.
Zero This instance is equal to obj.
Greater than zero This instance is greater than obj.

Implements xtd::icomparable< enum_object< enum_t > >.

◆ has_flag()

template<typename enum_t = std::nullptr_t>
bool xtd::enum_object< enum_t >::has_flag ( enum_type  flag) const
inlinenoexcept

Retrieves an array of the values of the constants in a specified enumeration.

Parameters
flagAn enumeration value.
Returns
true if the bit field or bit fields that are set in flag are also set in the current instance; otherwise, false.
Remarks
The has_flag method returns the result of the following bool expression : this_instance And flag = flag

◆ to_byte()

template<typename enum_t = std::nullptr_t>
xtd::byte xtd::enum_object< enum_t >::to_byte ( ) const
inlinenoexcept

Converts this instance to byte.

Returns
A new xtd::byte object converted from this instance.

◆ to_int16()

template<typename enum_t = std::nullptr_t>
int16 xtd::enum_object< enum_t >::to_int16 ( ) const
inlinenoexcept

Converts this instance to int16.

Returns
A new to_int16 object converted from this instance.

◆ to_int32()

template<typename enum_t = std::nullptr_t>
int32 xtd::enum_object< enum_t >::to_int32 ( ) const
inlinenoexcept

Converts this instance to int32.

Returns
A new to_int32 object converted from this instance.

◆ to_int64()

template<typename enum_t = std::nullptr_t>
int64 xtd::enum_object< enum_t >::to_int64 ( ) const
inlinenoexcept

Converts this instance to int64.

Returns
A new to_int64 object converted from this instance.

◆ to_sbyte()

template<typename enum_t = std::nullptr_t>
sbyte xtd::enum_object< enum_t >::to_sbyte ( ) const
inlinenoexcept

Converts this instance to signed byte.

Returns
A new sbyte object converted from this instance.

◆ to_string() [1/2]

template<typename enum_t = std::nullptr_t>
xtd::ustring xtd::enum_object< enum_t >::to_string ( ) const
inlineoverridevirtualnoexcept

Returns a sxd::ustring that represents the current object.

Returns
A string that represents the current object.
Examples
The following code example demonstrates what to_string returns.

Reimplemented from xtd::object.

◆ to_string() [2/2]

template<typename enum_t = std::nullptr_t>
xtd::ustring xtd::enum_object< enum_t >::to_string ( const xtd::ustring format) const
inline

Converts the value of this instance to its equivalent string representation using the specified format.

Parameters
formatA format string.
Returns
The string representation of the value of this instance as specified by format.
Exceptions
xtd::format_exceptionformat contains an invalid specification.
Remarks
The format parameter can be one of the following format strings: "G" or "g", "D" or "d", "X" or "x", and "F" or "f" (the format string is not case-sensitive). If format is null or an empty string (""), the general format specifier ("G") is used. For more information about the enumeration format strings and formatting enumeration values, see Enumeration Format Strings. For more information about formatting in general, see Formatting Types.
Notes to caller
If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return. For example, the following enumeration defines two members, shade::gray and shade::grey, that have the same underlying value.
enum shade {
white = 0, gray = 1, grey = 1, black = 2
};
The following method call attempts to retrieve the name of a member of the shade enumeration whose underlying value is 1. The method can return either "gray" or "grey", and your code should not make any assumptions about which string will be returned.
ustring shade_name = enum_object<shade>(as<shade>(1)).to_string("F");

◆ to_uint16()

template<typename enum_t = std::nullptr_t>
uint16 xtd::enum_object< enum_t >::to_uint16 ( ) const
inlinenoexcept

Converts this instance to unsigned int16.

Returns
A new to_uint16 object converted from this instance.

◆ to_uint32()

template<typename enum_t = std::nullptr_t>
uint32 xtd::enum_object< enum_t >::to_uint32 ( ) const
inlinenoexcept

Converts this instance to unsigned int32.

Returns
A new to_uint32 object converted from this instance.

◆ to_uint64()

template<typename enum_t = std::nullptr_t>
uint64 xtd::enum_object< enum_t >::to_uint64 ( ) const
inlinenoexcept

Converts this instance to unsigned int64.

Returns
A new to_uint64 object converted from this instance.

◆ value() [1/2]

template<typename enum_t = std::nullptr_t>
enum_type xtd::enum_object< enum_t >::value ( ) const
inlinenoexcept

Gets the value of the enum.

Returns
The value of the enum.

◆ value() [2/2]

template<typename enum_t = std::nullptr_t>
enum_object& xtd::enum_object< enum_t >::value ( enum_type  value)
inline

Sets the value of the enum.

Parameters
valueThe value of the enum.

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