CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
boxed_value.h
1
3#ifndef _aspose_system_boxed_value_h_
4#define _aspose_system_boxed_value_h_
5
6#include <fwd.h>
7#include <system/get_hash_code.h>
8#include <system/object.h>
9#include <system/object_type.h>
10#include <system/boxable_traits.h>
11#include <system/timespan.h>
12#include <system/decimal.h>
13#include <system/guid.h>
14#include <system/value_type.h>
15#include <system/type_code.h>
16
17#include <typeinfo>
18#include <type_traits>
19
20namespace System
21{
23 template <typename T> class Nullable;
24
25 namespace BoxedValueDetail
26 {
27 // Although two floating point NaNs are defined by IEC 60559:1989 to always compare as unequal, the contract for System.Object.Equals,
28 // requires that overrides must satisfy the requirements for an equivalence operator. Therefore, System.Double.Equals and System.Single.Equals
29 // return True when comparing two NaNs, while the equality operator returns False in that case, as required by the standard.
35 template<typename T>
36 typename std::enable_if<detail::has_operator_equal<T>::value, bool>::type
37 Equals(T value1, T value2)
38 {
39 return value1 == value2;
40 }
41
47 template<typename T>
48 typename std::enable_if<detail::has_only_method_equals<T>::value, bool>::type
49 Equals(T value1, T value2)
50 {
51 return value1.Equals(value2);
52 }
53
58 template<>
59 inline bool Equals<float>(float value1, float value2)
60 {
61 return (std::isnan(value1) && std::isnan(value2)) ? true : value1 == value2;
62 }
63
68 template<>
69 inline bool Equals<double>(double value1, double value2)
70 {
71 return (std::isnan(value1) && std::isnan(value2)) ? true : value1 == value2;
72 }
73 }
74
80 class ASPOSECPP_SHARED_CLASS BoxedValueBase : public Object
81 {
82 RTTI_INFO(System::BoxedValueBase, ::System::BaseTypesInfo<System::Object>);
83
84 public:
87 virtual ASPOSECPP_SHARED_API bool IsBoxedEnum() = 0;
88
91 virtual ASPOSECPP_SHARED_API uint64_t GetUnsignedLongLongValue() const = 0;
92
95 virtual ASPOSECPP_SHARED_API TypeCode GetTypeCode() const = 0;
96
97 using Object::ToString;
98
100 ASPOSECPP_SHARED_API System::String ToString(const System::String& format) const;
101
102 // Parsing enums
112 static ASPOSECPP_SHARED_API SharedPtr<Object> Parse(const TypeInfo& type, const String& str, bool ignoreCase);
119 static ASPOSECPP_SHARED_API SharedPtr<Object> Parse(const TypeInfo& type, const String& str);
120 };
121
122 namespace TypeCodeHelper
123 {
127 template<typename T>
129 {
130 return TypeCode::Object;
131 }
132
134 template<> inline TypeCode GetTypeCodeFor<bool>() { return TypeCode::Boolean; }
136 template<> inline TypeCode GetTypeCodeFor<char_t>() { return TypeCode::Char; }
138 template<> inline TypeCode GetTypeCodeFor<int8_t>() { return TypeCode::SByte; }
140 template<> inline TypeCode GetTypeCodeFor<uint8_t>() { return TypeCode::Byte; }
142 template<> inline TypeCode GetTypeCodeFor<int16_t>() { return TypeCode::Int16; }
144 template<> inline TypeCode GetTypeCodeFor<uint16_t>() { return TypeCode::UInt16; }
146 template<> inline TypeCode GetTypeCodeFor<int32_t>() { return TypeCode::Int32; }
148 template<> inline TypeCode GetTypeCodeFor<uint32_t>() { return TypeCode::UInt32; }
150 template<> inline TypeCode GetTypeCodeFor<int64_t>() { return TypeCode::Int64; }
152 template<> inline TypeCode GetTypeCodeFor<uint64_t>() { return TypeCode::UInt64; }
154 template<> inline TypeCode GetTypeCodeFor<float>() { return TypeCode::Single; }
156 template<> inline TypeCode GetTypeCodeFor<double>() { return TypeCode::Double; }
158 template<> inline TypeCode GetTypeCodeFor<Decimal>() { return TypeCode::Decimal; }
162 template<> inline TypeCode GetTypeCodeFor<String>() { return TypeCode::String; }
163 }
164
170 template<class T>
171 class ASPOSECPP_SHARED_CLASS BoxedValue : public BoxedValueBase
172 {
174 typedef System::BaseTypesInfo<System::Object> baseTypes;
175
177
178 RTTI_INFO_BOXED_CUSTOM(System::BoxedValue<T>, System::BaseTypesInfo<System::Object>);
179
180 friend class System::Nullable<T>;
181
182 public:
185 const System::TypeInfo& GetType() const override
186 {
187 return System::ObjectType::GetType<T>();
188 }
189
192 BoxedValue(const T& value)
193 : m_original_typeid_hash( typeid(T).hash_code() )
194 , m_value_holder(value)
195 {
196 static_assert(System::IsBoxable<T>::value,
197 "Only arithmetic, enums and specified in IsBoxable<> types are supported, use specializations to extend");
198 }
199
202 const T& unbox() const
203 {
204 return m_value_holder;
205 }
206
211 template<class V>
212 bool is() const
213 {
214 return typeid(V).hash_code() == m_original_typeid_hash;
215 }
216
220 bool Equals(ptr obj) override
221 {
222 if( !obj || !obj->Is(GetType()) )
223 return false;
224
225 auto boxed = obj.Cast<BoxedValue<T>>();
226 return BoxedValueDetail::Equals(m_value_holder, boxed->m_value_holder);
227 }
228
230 int GetHashCode() const override
231 {
232 return System::GetHashCode<T>(m_value_holder);
233 }
234
237 String ToString() const override
238 {
239 return ToStringHelper(m_value_holder);
240 }
241
244 bool IsBoxedEnum() override
245 {
246 return false;
247 }
248
250 uint64_t GetUnsignedLongLongValue() const override
251 {
252 return ToUInt64Helper(m_value_holder);
253 }
254
257 TypeCode GetTypeCode() const override
258 {
259 return TypeCodeHelper::GetTypeCodeFor<T>();
260 }
261
262 private:
263
264 // Do not make m_original_typeid_hash public!
265 // The particular values returned by typeid(T).hash_code() are implementation-defined and may vary between executions of the same program
267 size_t m_original_typeid_hash;
268
270 T m_value_holder;
271
275 template<class C>
276 static typename std::enable_if<
277 std::is_arithmetic<C>::value || std::is_enum<C>::value, String>::type
278 ToStringHelper(const C& value)
279 {
280 return String::Format(u"{0}", value);
281 }
282
286 template<typename C>
287 static typename std::enable_if<
288 (!std::is_arithmetic<C>::value && !std::is_enum<C>::value && !IsSmartPtr<C>::value), String>::type
289 ToStringHelper(const C& obj)
290 {
291 return obj.ToString();
292 }
293
297 template<typename C>
298 static typename std::enable_if<IsSmartPtr<C>::value, String>::type
299 ToStringHelper(const C& obj)
300 {
301 return obj->ToString();
302 }
303
307 static String ToStringHelper(bool b)
308 {
309 return b ? u"True" : u"False";
310 }
311
313 template <typename C>
314 static typename std::enable_if<std::is_convertible<C, uint64_t>::value, uint64_t>::type
315 ToUInt64Helper(C value)
316 {
317 return static_cast<uint64_t>(value);
318 }
319
321 template <typename C>
322 static typename std::enable_if<!std::is_convertible<C, uint64_t>::value, uint64_t>::type
323 ToUInt64Helper(C)
324 {
325 return 0;
326 }
327 };
328
333 template <class T>
334 class ASPOSECPP_SHARED_CLASS DefaultBoxedValue : public Object
335 {
337 typedef System::BaseTypesInfo<System::Object> baseTypes;
338
340
341 RTTI_INFO_BOXED_CUSTOM(System::DefaultBoxedValue<T>, System::BaseTypesInfo<System::Object>);
342
343 public:
346 const System::TypeInfo& GetType() const override
347 {
348 return System::ObjectType::GetType<T>();
349 }
350
354 DefaultBoxedValue(const T& value)
355 : m_original_typeid_hash(typeid(T).hash_code())
356 , m_value_holder(value)
357 {
358 }
359
362 const T& unbox() const
363 {
364 return m_value_holder;
365 }
366
371 template<class V>
372 bool is() const
373 {
374 return typeid(V).hash_code() == m_original_typeid_hash;
375 }
376
380 bool Equals(ptr obj) override
381 {
382 if (!obj || !obj->Is(Type()))
383 return false;
384
385 auto boxed = obj.Cast<BoxedValue<T>>();
386 return m_value_holder == boxed->unbox();
387 }
388
390 int GetHashCode() const override
391 {
392 return m_value_holder.GetHashCode();
393 }
394
396 String ToString() const override
397 {
398 return m_value_holder.ToString();
399 }
400
401 private:
402 // Do not make m_original_typeid_hash public!
403 // The particular values returned by typeid(T).hash_code() are implementation-defined and may vary between executions of the same program
405 size_t m_original_typeid_hash;
407 T m_value_holder;
408 };
409
411
414 template <typename T>
415 struct IsBoxedValue : std::false_type {};
416
419 template <typename T>
420 struct IsBoxedValue<BoxedValue<T>> : std::true_type {};
421
423
424} // namespace System
425
426#endif // _aspose_system_boxed_value_h_
A base class that defines an interface and implements some fundamental methods of a descendant class ...
Definition: boxed_value.h:81
static SharedPtr< Object > Parse(const TypeInfo &type, const String &str)
Boxes the value of enumeration constant of the specified enumeration with the specified name.
System::String ToString(const System::String &format) const
Converts boxed object to string using specified format string.
virtual uint64_t GetUnsignedLongLongValue() const =0
Converts the boxed represented by the current object to 64-bit integer value.
virtual TypeCode GetTypeCode() const =0
Returns the value representing the type of the boxed value represented by the current object.
virtual bool IsBoxedEnum()=0
Determines if current object represents a boxed value of enum type.
static SharedPtr< Object > Parse(const TypeInfo &type, const String &str, bool ignoreCase)
Boxes the value of enumeration constant of the specified enumeration with the specified name....
Represents a boxed value. Objects of this class should only be allocated using System::MakeObject() f...
Definition: boxed_value.h:172
const T & unbox() const
Unboxes the value represented by the current object.
Definition: boxed_value.h:202
BoxedValue(const T &value)
Constructs an object that represents the specified value boxed.
Definition: boxed_value.h:192
bool Equals(ptr obj) override
Determines the equality of the boxed values represented by the current and specified objects.
Definition: boxed_value.h:220
int GetHashCode() const override
Returns a hash code for the current object.
Definition: boxed_value.h:230
uint64_t GetUnsignedLongLongValue() const override
Returns numeric value of boxed object if it can be cast too, zero otherwise.
Definition: boxed_value.h:250
bool is() const
Determines if the type of the boxed value represented by the current object is V.
Definition: boxed_value.h:212
bool IsBoxedEnum() override
Determines if current object represents a boxed value of enum type.
Definition: boxed_value.h:244
TypeCode GetTypeCode() const override
Returns the value representing the type of the boxed value represented by the current object.
Definition: boxed_value.h:257
const System::TypeInfo & GetType() const override
Gets actual type of object.
Definition: boxed_value.h:185
String ToString() const override
Converts boxed value represented by current object to string.
Definition: boxed_value.h:237
BoxedValue class implementation. Allows it BoxingValue specializations to be declared without duplica...
Definition: boxed_value.h:335
DefaultBoxedValue(const T &value)
Constructs a new instance of DefaultBoxedValue class that represents the specified value.
Definition: boxed_value.h:354
const T & unbox() const
Unboxes the boxed value.
Definition: boxed_value.h:362
int GetHashCode() const override
Returns a hash code for the current object.
Definition: boxed_value.h:390
bool is() const
Determines if the type of the boxed value represented by the current object is V.
Definition: boxed_value.h:372
bool Equals(ptr obj) override
Determines the equality of the boxed values represented by the current and specified objects.
Definition: boxed_value.h:380
String ToString() const override
Returns the string representation of the boxed value.
Definition: boxed_value.h:396
const System::TypeInfo & GetType() const override
Gets actual type of object.
Definition: boxed_value.h:346
Forward declaration.
Definition: nullable.h:74
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:62
virtual bool Is(const TypeInfo &targetType) const
Check if object represents an instance of type described by targetType. Analog of C# 'is' operator.
virtual String ToString() const
Analog of C# Object.ToString() method. Enables converting custom objects to string.
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
std::enable_if_t< std::is_same< Y, T >::value, SmartPtr< Y > > Cast() const
Casts pointer to its type itself.
Definition: smart_ptr.h:742
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
static String Format(const SharedPtr< IFormatProvider > &fp, const String &format, const Args &... args)
Formats string in C# style.
Definition: string.h:1405
String ToString() const
Wrapper for handling String class in contexts where ToString() is being called on value type objects.
Definition: string.h:494
Represents a particular type and provides information about it.
Definition: type_info.h:109
bool Equals< float >(float value1, float value2)
Compares two single-precision floating-point values.
Definition: boxed_value.h:59
bool Equals< double >(double value1, double value2)
Compares two double-precision floating-point values.
Definition: boxed_value.h:69
std::enable_if< detail::has_operator_equal< T >::value, bool >::type Equals(T value1, T value2)
Determines the equality of the specified value using operator==().
Definition: boxed_value.h:37
TypeCode GetTypeCodeFor< float >()
Template specialization for float.
Definition: boxed_value.h:154
TypeCode GetTypeCodeFor< uint16_t >()
Template specialization for uint16_t.
Definition: boxed_value.h:144
TypeCode GetTypeCodeFor< String >()
Template specialization for System::String.
Definition: boxed_value.h:162
TypeCode GetTypeCodeFor< double >()
Template specialization for double.
Definition: boxed_value.h:156
TypeCode GetTypeCodeFor< int64_t >()
Template specialization for int64_t.
Definition: boxed_value.h:150
TypeCode GetTypeCodeFor< bool >()
Template specialization for bool.
Definition: boxed_value.h:134
TypeCode GetTypeCodeFor< char_t >()
Template specialization for char_t.
Definition: boxed_value.h:136
TypeCode GetTypeCodeFor< uint32_t >()
Template specialization for uint32_t.
Definition: boxed_value.h:148
TypeCode GetTypeCodeFor< Decimal >()
Template specialization for System::Decimal.
Definition: boxed_value.h:158
TypeCode GetTypeCodeFor< int16_t >()
Template specialization for int16_t.
Definition: boxed_value.h:142
TypeCode GetTypeCodeFor()
A template function that returns an enum value representing the function's template type argument.
Definition: boxed_value.h:128
TypeCode GetTypeCodeFor< int32_t >()
Template specialization for int32_t.
Definition: boxed_value.h:146
TypeCode GetTypeCodeFor< DateTime >()
Template specialization for System::DateTime.
Definition: boxed_value.h:160
TypeCode GetTypeCodeFor< uint64_t >()
Template specialization for uint64_t.
Definition: boxed_value.h:152
TypeCode GetTypeCodeFor< uint8_t >()
Template specialization for uint8_t.
Definition: boxed_value.h:140
TypeCode GetTypeCodeFor< int8_t >()
Template specialization for int8_t.
Definition: boxed_value.h:138
Definition: db_command.h:9
TypeCode
Represents the type of an object.
Definition: type_code.h:19
@ String
A sealed class type representing Unicode character strings.
@ Boolean
A simple type representing Boolean values of true or false.
@ Int16
An integral type representing signed 16-bit integers with values between -32768 and 32767.
@ Object
A general type representing any reference or value type not explicitly represented by another TypeCod...
@ Single
A floating point type representing values ranging from approximately 1.5 x 10 -45 to 3....
@ SByte
An integral type representing signed 8-bit integers with values between -128 and 127.
@ UInt16
An integral type representing unsigned 16-bit integers with values between 0 and 65535.
@ DateTime
A type representing a date and time value.
@ Char
An integral type representing unsigned 16-bit integers with values between 0 and 65535.
@ Decimal
A simple type representing values ranging from 1.0 x 10 -28 to approximately 7.9 x 10 28 with 28-29 s...
@ Byte
An integral type representing unsigned 8-bit integers with values between 0 and 255.
@ UInt64
An integral type representing unsigned 64-bit integers with values between 0 and 18446744073709551615...
@ Int32
An integral type representing signed 32-bit integers with values between -2147483648 and 2147483647.
@ Double
A floating point type representing values ranging from approximately 5.0 x 10 -324 to 1....
@ UInt32
An integral type representing unsigned 32-bit integers with values between 0 and 4294967295.
@ Int64
An integral type representing signed 64-bit integers with values between -9223372036854775808 and 922...
Template predicate that checks if boxing of the specified type is supported.
Definition: boxable_traits.h:16
TypeInfo structure for BoxedValue class.
Definition: type_info.h:367