3#ifndef _aspose_system_enum_h_
4#define _aspose_system_enum_h_
6#include <system/string.h>
7#include <system/array.h>
8#include <system/exceptions.h>
9#include <system/type_info.h>
10#include <system/boxed_enum.h>
12#include <unordered_map>
21 template<
class E,
class G>
28 template <
class E,
class G,
class Guard =
void>
38 const auto& values = EnumMetaInfo<E>::values();
39 for (
auto val : values)
41 if (
static_cast<E
>(value) == val.first)
51 template <
class E,
class G>
52 struct EnumGetNameHelper<E, G, typename EnumMetaInfo<E>::Flags>
61 using UnderlyingType =
typename std::underlying_type<E>::type;
65 const auto& values = EnumMetaInfo<E>::values();
67 for (
auto val : values)
69 if (
static_cast<UnderlyingType
>(val.first))
71 if (
static_cast<E
>(value) == val.first)
87 else if (!
static_cast<UnderlyingType
>(value))
101 template <
class E,
class G,
class Guard =
void>
112 throw ArgumentException(
String::Format(u
"Requested value '{0}' was not found.", str));
120 template <
class E,
class G>
121 struct EnumParseHelper<E, G, typename EnumMetaInfo<E>::Flags>{
127 static E
Parse(
const String& str,
bool ignoreCase =
false)
130 const auto& vals = str.Split(u
',');
131 for (
int i = 0; i < vals->get_Count(); ++i)
136 if (vals->get_Count() == 1)
138 std::string s = vals[0].ToAsciiString();
139 if (!s.empty() && std::all_of(s.begin(), s.end(), ::isdigit))
140 return (E)std::stoi(s);
144 result = result | val;
156 template<class E, class Guard = typename std::enable_if<std::is_enum<E>::value>::type>
182 const auto& values = EnumMetaInfo<E>::values();
183 for(
auto val : values )
185 if( value == val.first )
195 static typename std::enable_if< std::is_convertible<T, UnderlyingType>::value,
bool>::type
IsDefined(T value)
213 static typename std::enable_if< std::is_same<T,E>::value || std::is_convertible<T, UnderlyingType>::value,
String>::type
GetName(T value)
224 static typename std::enable_if<std::is_same<T, E>::value || std::is_convertible<T, UnderlyingType>::value,
String>::type
GetDescription(T value)
229 const auto& values = EnumMetaInfo<E>::descriptions();
230 for (
auto val : values)
232 if (
static_cast<E
>(value) == val.first)
233 return val.second ==
nullptr ?
String() :
String(val.second);
254 return TryParse(str,
false, result);
264 const auto& values = ::EnumMetaInfo<E>::values();
265 for(
auto val : values )
280 auto res = MakeObject<Array<E>>();
281 const auto& values = ::EnumMetaInfo<E>::values();
282 for(
auto val : values )
284 res->data().push_back( val.first );
293 auto res = MakeObject<Array<String>>();
294 const auto& values = ::EnumMetaInfo<E>::values();
295 for(
auto val : values )
297 res->data().push_back( val.second );
307 template <typename T, typename _ = typename std::enable_if<std::is_arithmetic<T>::value || std::is_enum<T>::value>::type>
371 struct value_enumerators {
373 std::unordered_map<std::u16string, const EnumValuesBase*> m_enumerators;
376 static value_enumerators& GetEnumerators();
382 template <typename E, class Guard = typename std::enable_if<std::is_enum<E>::value>::type>
393 auto res = MakeObject<Array<int64_t>>();
395 for (
auto v : values->data())
396 res->data().push_back((int64_t)v);
411 auto values = ::EnumMetaInfo<E>::values();
415 for (
auto v : values)
417 return MakeObject<BoxedEnum<E>>(v.first);
422 for (
auto v : values)
424 return MakeObject<BoxedEnum<E>>(v.first);
426 char *str_end =
nullptr;
428 long val = std::strtol(ascii_str.c_str(), &str_end, 10);
429 if (ascii_str.c_str() != str_end)
430 return MakeObject<BoxedEnum<E>>((E)val);
437 return MakeObject<BoxedEnum<E>>((E)val);
A base class for a class that represents meta information of enumeration type.
Definition: enum.h:317
static SharedPtr< Object > Parse(const TypeInfo &type, const String &str, bool ignoreCase)
Returns an object that represents a value of enumeration constant of the specified enumeration type w...
virtual ArrayPtr< String > GetNames() const =0
Returns an array containing all names of enumeration E.
static ArrayPtr< String > GetNames(const TypeInfo &type)
Retrieves an array of the names of the constants in a specified enumeration.
static ArrayPtr< int64_t > GetValues(const TypeInfo &type)
Returns an array containing all values of the specified enumeration type.
virtual SharedPtr< Object > GetValueOf(const String &str, bool ignoreCase) const =0
Returns boxed value of the enum constant with the specified name.
static const System::TypeInfo & GetUnderlyingType(const TypeInfo &type)
Returns the underlying type of the specified enumeration.
virtual ArrayPtr< int64_t > GetValues() const =0
Returns an array containing all values of enumeration E.
static SharedPtr< Object > ToObject(const TypeInfo &type, uint64_t value)
Converts the specified 64-bit unsigned integer value to an enumeration member.
virtual SharedPtr< Object > GetValueOf(long val) const =0
Returns boxed value of the enum constant with the specified value.
EnumValuesBase(const TypeInfo &type)
Constructs an instance that represents metainformation for enum of the specified type.
static SharedPtr< Object > ToObject(const TypeInfo &type, const SharedPtr< Object > &value)
Converts the specified object with an integer value to an enumeration member.
virtual ~EnumValuesBase()
Destructor.
virtual const System::TypeInfo & GetUnderlyingType() const =0
Returns the underlying type of the specified enumeration.
Provides meta information about enumeration constants of enum type E.
Definition: enum.h:384
virtual ArrayPtr< int64_t > GetValues() const override
Returns an array containing all values of enumeration E.
Definition: enum.h:391
virtual ~EnumValues()
Destructor.
Definition: enum.h:389
virtual SharedPtr< Object > GetValueOf(const String &str, bool ignoreCase) const override
Returns boxed value of the enum constant with the specified name.
Definition: enum.h:409
virtual const System::TypeInfo & GetUnderlyingType() const override
Returns the underlying type of the specified enumeration.
Definition: enum.h:440
virtual SharedPtr< Object > GetValueOf(long val) const override
Returns boxed value of the enum constant with the specified value.
Definition: enum.h:435
EnumValues()
Constructs an instance.
Definition: enum.h:387
virtual ArrayPtr< String > GetNames() const override
Returns an array containing all names of enumeration E.
Definition: enum.h:400
static const TypeInfo & Type()
Implements C# typeof(System.Object) construct.
Definition: object.h:172
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
std::u16string ToU16Str() const
Converts string to std::u16string.
String ToLower() const
Converts all string's characters to lower case.
static String Format(const SharedPtr< IFormatProvider > &fp, const String &format, const Args &... args)
Formats string in C# style.
Definition: string.h:1405
bool Equals(const String &str, System::StringComparison comparison_type) const
String equality comparison. Several modes provided by StringComparison enumeration are supported.
std::string ToAsciiString() const
Converts string to std::string. Uses ASCII encoding.
Represents a particular type and provides information about it.
Definition: type_info.h:109
System::ExceptionWrapper< Details_InvalidEnumArgumentException > InvalidEnumArgumentException
Definition: exceptions.h:867
Definition: db_command.h:9
static bool IsEnumMetaInfoDefined(T value)
@ OrdinalIgnoreCase
Use ordinal sort rules, but ignore case.
@ Ordinal
Use ordinal sort rules.
static System::String EnumGetName(T value)
Helper class that provides functionality of geting the strting name of enum constant.
Definition: enum.h:30
static String GetName(T value)
Returns the string name of the specified enum constant value.
Definition: enum.h:36
Provides methods that perform some operations on values of enum type. This is a static type with no i...
Definition: enum.h:158
typename std::underlying_type< E >::type UnderlyingType
Alias for the enum's underlying type.
Definition: enum.h:160
static int Compare(E a, T b)
Performs the arithmetic comparison of the values of the specified enumeration constants.
Definition: enum.h:308
static bool HasFlag(E value, E mask)
Determines if the specified bits are set in a bitary representation of the specified enum value.
Definition: enum.h:172
static bool TryParse(const String &str, bool ignoreCase, E &result)
Tries to convert the specified string into equivalent enum constant.
Definition: enum.h:262
static std::enable_if< std::is_same< T, E >::value||std::is_convertible< T, UnderlyingType >::value, String >::type GetName(T value)
Returns the name of the enumeration constant that has the specified value.
Definition: enum.h:213
static bool IsDefined(const String &name)
Determines if the value with the specified name is among members of enum E.
Definition: enum.h:203
static std::enable_if< std::is_convertible< T, UnderlyingType >::value, bool >::type IsDefined(T value)
Determines whether the specified value is a member of enumeration type T.
Definition: enum.h:195
static E Parse(const String &str, bool ignoreCase=false)
Converts the specfied string into equivalent enum constant.
Definition: enum.h:243
static ArrayPtr< String > GetNames()
Returns an array containing names of all members of enumeration E.
Definition: enum.h:291
static bool TryParse(const String &str, E &result)
Tries to convert the specified string into equivalent enum constant.
Definition: enum.h:252
static ArrayPtr< E > GetValues()
Returns an array containing all members of enumeration E.
Definition: enum.h:278
static const System::TypeInfo & GetUnderlyingType()
Returns the underlying type of the enumeration.
Definition: enum.h:163
static bool IsDefined(E value)
Determines whether the specified value is a member of enumeration type E.
Definition: enum.h:180
static std::enable_if< std::is_same< T, E >::value||std::is_convertible< T, UnderlyingType >::value, String >::type GetDescription(T value)
Returns the name of the enumeration constant that has the specified value.
Definition: enum.h:224
Helper class that provides functionality of converting a string representation of enum consnant into ...
Definition: enum.h:102
static E Parse(const String &str, bool ignoreCase=false)
Converts the specfied string into equivalent enum constant value.
Definition: enum.h:108