CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
enum.h
1
3#ifndef _aspose_system_enum_h_
4#define _aspose_system_enum_h_
5#include <type_traits>
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>
11
12#include <unordered_map>
13#include <string>
14#include <cctype>
15
16template<class T>
17struct EnumMetaInfo; // declared out of System namespace
18
19namespace System
20{
21 template<class E, class G>
22 struct Enum;
23
28 template <class E, class G, class Guard = void>
30 {
35 template<class T>
36 static String GetName(T value)
37 {
38 const auto& values = EnumMetaInfo<E>::values();
39 for (auto val : values)
40 {
41 if (static_cast<E>(value) == val.first)
42 return String(val.second);
43 }
44
45 return String();
46 }
47 };
48
51 template <class E, class G>
52 struct EnumGetNameHelper<E, G, typename EnumMetaInfo<E>::Flags>
53 {
58 template<class T>
59 static String GetName(T value)
60 {
61 using UnderlyingType = typename std::underlying_type<E>::type;
62 // If you got error here it means that enum E was translated without metainformation
63 // Add [CodePorting.Translator.Cs2Cpp.CppEnumEnableMetadata] attribute to fix the problem.
64 String result;
65 const auto& values = EnumMetaInfo<E>::values();
66 bool first = true;
67 for (auto val : values)
68 {
69 if (static_cast<UnderlyingType>(val.first))
70 {
71 if (static_cast<E>(value) == val.first)
72 return val.second;
73
74 if (Enum<E,G>::HasFlag(static_cast<E>(value), val.first))
75 {
76 if (!first)
77 {
78 result += u", ";
79 }
80 else
81 {
82 first = false;
83 }
84 result += val.second;
85 }
86 }
87 else if (!static_cast<UnderlyingType>(value))
88 {
89 return val.second;
90 }
91 }
92 return result;
93 }
94 };
96
101 template <class E, class G, class Guard = void>
108 static E Parse(const String& str, bool ignoreCase = false)
109 {
110 E result;
111 if (!Enum<E, G>::TryParse(str, ignoreCase, result))
112 throw ArgumentException(String::Format(u"Requested value '{0}' was not found.", str));
113
114 return result;
115 }
116 };
117
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)
128 {
129 E result = (E)0;
130 const auto& vals = str.Split(u',');
131 for (int i = 0; i < vals->get_Count(); ++i)
132 {
133 E val;
134 if (!Enum<E, G>::TryParse(vals[i].Trim(), ignoreCase, val))
135 {
136 if (vals->get_Count() == 1)
137 {
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);
141 }
142 throw ArgumentException(String::Format(u"Requested value '{0}' was not found.", str));
143 }
144 result = result | val;
145 }
146 return result;
147 }
148 };
150
156 template<class E, class Guard = typename std::enable_if<std::is_enum<E>::value>::type>
157 struct Enum
158 {
160 using UnderlyingType = typename std::underlying_type<E>::type;
161
164 {
166 }
167
172 static bool HasFlag(E value, E mask)
173 {
174 return (static_cast<UnderlyingType>(value) & static_cast<UnderlyingType>(mask)) == static_cast<UnderlyingType>(mask);
175 }
176
180 static bool IsDefined(E value)
181 {
182 const auto& values = EnumMetaInfo<E>::values();
183 for( auto val : values )
184 {
185 if( value == val.first )
186 return true;
187 }
188 return false;
189 }
190
194 template<class T>
195 static typename std::enable_if< std::is_convertible<T, UnderlyingType>::value, bool>::type IsDefined(T value)
196 {
197 return IsDefined(static_cast<E>(value));
198 }
199
203 static bool IsDefined(const String& name)
204 {
205 E result;
206 return TryParse(name, result);
207 }
208
212 template<class T>
213 static typename std::enable_if< std::is_same<T,E>::value || std::is_convertible<T, UnderlyingType>::value, String>::type GetName(T value)
214 {
215 // If you got error here it means that enum E was translated without metainformation
216 // Add [CodePorting.Translator.Cs2Cpp.CppEnumEnableMetadata] attribute to fix the problem.
218 }
219
223 template <class T>
224 static typename std::enable_if<std::is_same<T, E>::value || std::is_convertible<T, UnderlyingType>::value, String>::type GetDescription(T value)
225 {
226 // If you got error here it means that enum E was translated without metainformation
227 // Enable option "generate_enum_descriptions"
228 // and add [CodePorting.Translator.Cs2Cpp.CppEnumEnableMetadata] attribute to fix the problem.
229 const auto& values = EnumMetaInfo<E>::descriptions();
230 for (auto val : values)
231 {
232 if (static_cast<E>(value) == val.first)
233 return val.second == nullptr ? String() : String(val.second);
234 }
235 return String();
236 }
237
243 static E Parse(const String& str, bool ignoreCase = false)
244 {
245 return EnumParseHelper<E, Guard>::Parse(str, ignoreCase);
246 }
247
252 static bool TryParse(const String& str, E& result)
253 {
254 return TryParse(str, false, result);
255 }
256
262 static bool TryParse(const String& str, bool ignoreCase, E& result)
263 {
264 const auto& values = ::EnumMetaInfo<E>::values();
265 for( auto val : values )
266 {
267 if (str.Equals(val.second, ignoreCase ? StringComparison::OrdinalIgnoreCase : StringComparison::Ordinal))
268 {
269 result = val.first;
270 return true;
271 }
272 }
273
274 return false;
275 }
276
279 {
280 auto res = MakeObject<Array<E>>();
281 const auto& values = ::EnumMetaInfo<E>::values();
282 for( auto val : values )
283 {
284 res->data().push_back( val.first );
285 }
286
287 return res;
288 }
289
292 {
293 auto res = MakeObject<Array<String>>();
294 const auto& values = ::EnumMetaInfo<E>::values();
295 for( auto val : values )
296 {
297 res->data().push_back( val.second );
298 }
299
300 return res;
301 }
302
307 template <typename T, typename _ = typename std::enable_if<std::is_arithmetic<T>::value || std::is_enum<T>::value>::type>
308 static int Compare(E a, T b)
309 {
310 return static_cast<UnderlyingType>(a) < static_cast<UnderlyingType>(b) ? -1 :
311 (static_cast<UnderlyingType>(a) > static_cast<UnderlyingType>(b) ? 1 : 0);
312 }
313 };
314
316 class ASPOSECPP_SHARED_CLASS EnumValuesBase
317 {
318 protected:
320 ASPOSECPP_SHARED_API EnumValuesBase(const TypeInfo &type);
322 virtual ASPOSECPP_SHARED_API ~EnumValuesBase();
324 virtual ASPOSECPP_SHARED_API ArrayPtr<int64_t> GetValues() const = 0;
326 virtual ASPOSECPP_SHARED_API ArrayPtr<String> GetNames() const = 0;
331 virtual ASPOSECPP_SHARED_API SharedPtr<Object> GetValueOf(const String& str, bool ignoreCase) const = 0;
335 virtual ASPOSECPP_SHARED_API SharedPtr<Object> GetValueOf(long val) const = 0;
337 virtual ASPOSECPP_SHARED_API const System::TypeInfo& GetUnderlyingType() const = 0;
338
339 public:
343 static ASPOSECPP_SHARED_API ArrayPtr<int64_t> GetValues(const TypeInfo& type);
347 static ASPOSECPP_SHARED_API ArrayPtr<String> GetNames(const TypeInfo& type);
353 static ASPOSECPP_SHARED_API SharedPtr<Object> Parse(const TypeInfo& type, const String& str, bool ignoreCase);
358 static ASPOSECPP_SHARED_API SharedPtr<Object> ToObject(const TypeInfo& type, uint64_t value);
363 static ASPOSECPP_SHARED_API SharedPtr<Object> ToObject(const TypeInfo& type, const SharedPtr<Object>& value);
367 static ASPOSECPP_SHARED_API const System::TypeInfo& GetUnderlyingType(const TypeInfo& type);
368
369 private:
371 struct value_enumerators {
373 std::unordered_map<std::u16string, const EnumValuesBase*> m_enumerators;
374 };
376 static value_enumerators& GetEnumerators();
377 static const EnumValuesBase& GetEnumerator(const TypeInfo& type);
378 };
379
382 template <typename E, class Guard = typename std::enable_if<std::is_enum<E>::value>::type>
384 {
385 public:
387 EnumValues() : EnumValuesBase(TypeInfo::BoxedValueType<E>()) {}
389 virtual ~EnumValues() {}
391 virtual ArrayPtr<int64_t> GetValues() const override
392 {
393 auto res = MakeObject<Array<int64_t>>();
395 for (auto v : values->data())
396 res->data().push_back((int64_t)v);
397 return res;
398 }
399
400 virtual ArrayPtr<String> GetNames() const override
401 {
402 return Enum<E>::GetNames();
403 }
404
409 virtual SharedPtr<Object> GetValueOf(const String& str, bool ignoreCase) const override
410 {
411 auto values = ::EnumMetaInfo<E>::values();
412 if (ignoreCase)
413 {
414 auto lower = str.ToLower();
415 for (auto v : values)
416 if (System::String(v.second).ToLower() == lower)
417 return MakeObject<BoxedEnum<E>>(v.first);
418 }
419 else
420 {
421 auto wcs = str.ToU16Str();
422 for (auto v : values)
423 if (v.second == wcs)
424 return MakeObject<BoxedEnum<E>>(v.first);
425 }
426 char *str_end = nullptr;
427 const auto ascii_str = str.ToAsciiString();
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);
431
433 }
434
435 virtual SharedPtr<Object> GetValueOf(long val) const override
436 {
437 return MakeObject<BoxedEnum<E>>((E)val);
438 }
439
440 virtual const System::TypeInfo& GetUnderlyingType() const override
441 {
443 }
444 };
445
446 template<class T>
447 static bool IsEnumMetaInfoDefined(T value) {
448 return Enum<T>::IsDefined(value);
449 }
450
451 template <class T>
452 static System::String EnumGetName(T value) {
453 return Enum<T>::GetName(value);
454 }
455} // namespace System
456
457#endif // _aspose_system_enum_h_
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