CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
nullable.h
1
3#ifndef _aspose_system_nullable_h_
4#define _aspose_system_nullable_h_
5
6#include <system/get_hash_code.h>
7#include <system/boxable_traits.h>
8#include <system/default.h>
9#include <system/string.h>
10#include <type_traits>
11#include <functional>
12#include <algorithm>
13
14#include "detail.h"
15
16namespace System
17{
18 template <typename T>
19 class Nullable;
20
22 template <class T>
23 struct IsNullable : System::detail::is_a<T, System::Nullable> {};
24
25 namespace Details {
27 [[noreturn]]
28 ASPOSECPP_SHARED_API void ThrowNullableObjectMustHaveAValue();
29 ASPOSECPP_SHARED_API void ThrowNullableNullReference();
30 }
31
73 template<typename T>
75 {
76 RTTI_INFO_VALUE_TYPE_TEMPLATE(Nullable<T>);
78 template<class T2> friend class System::Nullable;
79 public:
81 typedef T ValueType;
82
84 Nullable() : m_has_value(false)
85 {}
86
88 Nullable(std::nullptr_t) : m_has_value(false)
89 {}
90
95 template<typename T1>
96 Nullable(const T1& value) : m_has_value(true), m_value(value)
97 {}
98
103 template<typename T1>
104 Nullable(const Nullable<T1>& value) : m_has_value(value.m_has_value), m_value(value.m_value)
105 {}
106
109 template<typename T1, typename = typename std::enable_if<std::is_null_pointer<T1>::value>>
110 Nullable<T> operator=(std::nullptr_t) { return Nullable<T>(); }
111
116 template<typename T1>
117 typename std::enable_if<!IsNullable<T1>::value && !std::is_null_pointer<T1>::value, Nullable<T>&>::type operator=(const T1& x)
118 {
119 this->m_has_value = true;
120 this->m_value = static_cast<T>(x);
121
122 return *this;
123 }
124
129 template<typename T1>
131 {
132 this->m_has_value = x.m_has_value;
133 if (x.m_has_value)
134 {
135 this->m_value = x.m_value;
136 }
137
138 return *this;
139 }
140
144 T get_Value() const
145 {
146 if (!m_has_value)
147 Details::ThrowNullableObjectMustHaveAValue();
148
149 return m_value;
150 }
151
154 bool get_HasValue() const { return m_has_value; }
155
159 operator const T&() const
160 {
161 if (!m_has_value)
162 Details::ThrowNullableObjectMustHaveAValue();
163
164 return m_value;
165 }
166
168 void reset() { m_has_value = false; }
169
172 bool IsNull() const { return !m_has_value; }
173
180 template<typename T1>
181 bool NullableBoolHelper(const T1& other, const std::function<bool()>& f, bool default_if_both_are_null = false) const
182 {
183 if (IsNull())
184 {
185 if (other.IsNull())
186 {
187 return default_if_both_are_null;
188 }
189 else
190 {
191 return false;
192 }
193 }
194
195 if (other.IsNull())
196 {
197 return false;
198 }
199
200 return f();
201 }
202
205 bool operator==(std::nullptr_t) const { return IsNull(); }
206
211 template<typename T1>
212 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator==(const T1& other) const
213 {
214 return m_has_value && m_value == other;
215 }
216
221 template<typename T1>
222 bool operator==(const Nullable<T1>& other) const
223 {
224 return NullableBoolHelper<Nullable<T1>>(other,
225 [&] {return m_value == other.get_Value(); },
226 true);
227 }
228
233 template<typename T1>
234 typename std::enable_if<IsNullable<T1>::value, bool>::type Equals(const T1& other) const
235 {
236 return ((!m_has_value) && (!other.m_has_value)) || (m_has_value && other.m_has_value && (m_value == other.get_Value()));
237 }
238
241 bool operator!=(std::nullptr_t) const { return !IsNull(); }
242
247 template<typename T1>
248 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator!=(const T1& other) const
249 {
250 return IsNull() ? true : m_value != other;
251 }
252
257 template<typename T1>
258 bool operator!=(const Nullable<T1>& other) const
259 {
260 return NullableBoolHelper<Nullable<T1>>(other,
261 [&] {return m_value != other.get_Value(); }
262 );
263 }
264
266 bool operator>(std::nullptr_t) const { return false; }
267
272 template<typename T1>
273 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator>(const T1& other) const
274 {
275 return m_has_value && m_value > other;
276 }
277
282 template<typename T1>
283 bool operator>(const Nullable<T1>& other) const
284 {
285 return NullableBoolHelper<Nullable<T1>>(other,
286 [&] {return m_value > other.get_Value(); }
287 );
288 }
289
291 bool operator<(std::nullptr_t) const { return false; }
292
297 template<typename T1>
298 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator<(const T1& other) const
299 {
300 return m_has_value && m_value < other;
301 }
302
307 template<typename T1>
308 bool operator<(const Nullable<T1>& other) const
309 {
310 return NullableBoolHelper<Nullable<T1>>(other,
311 [&] {return m_value < other.get_Value(); }
312 );
313 }
314
317 bool operator>=(std::nullptr_t) const { return false; }
318
323 template<typename T1>
324 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator>=(const T1& other) const
325 {
326 return m_has_value && m_value >= other;
327 }
328
333 template<typename T1>
334 bool operator>=(const Nullable<T1>& other) const
335 {
336 return NullableBoolHelper<Nullable<T1>>(other,
337 [&] {return m_value >= other.get_Value(); }
338 );
339 }
340
342 bool operator<=(std::nullptr_t) const { return false; }
343
348 template<typename T1>
349 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator<=(const T1& other) const
350 {
351 return m_has_value && m_value <= other;
352 }
353
358 template<typename T1>
359 bool operator<=(const Nullable<T1>& other) const
360 {
361 return NullableBoolHelper<Nullable<T1>>(other,
362 [&] {return m_value <= other.get_Value(); }
363 );
364 }
365
367 Nullable<T> operator+(std::nullptr_t) const { return Nullable<T>(); }
368
373 template<typename T1, typename = typename std::enable_if<!IsNullable<T1>::value, int>::type>
374 auto operator+(const T1 &other) const -> Nullable<decltype(get_Value() + other)>
375 {
377 (get_Value() + other);
378 }
379
384 template<typename T1>
385 auto operator+(const Nullable<T1> &other) const -> System::Nullable<decltype(get_Value() + other.get_Value())>
386 {
387 return (IsNull() || other.IsNull()) ? nullptr :
388 Nullable<decltype(get_Value() + other.get_Value())>
389 (get_Value() + other.get_Value());
390 }
391
395 template<typename T1, typename = typename std::enable_if<std::is_null_pointer<T1>::value>::type>
397 static_assert(std::is_null_pointer<T1>::value, "Operator '-' is ambiguous on operands of type 'Nullable<T>' and '<null>'");
398 return Nullable<T>();
399 }
400
405 template<typename T1, typename = typename std::enable_if<!IsNullable<T1>::value, int>::type>
406 auto operator-(const T1 &other) const -> Nullable<decltype(get_Value() - other)>
407 {
408 return IsNull() ? nullptr : Nullable<decltype(get_Value() - other)>
409 (get_Value() - other);
410 }
411
416 template<typename T1>
417 auto operator-(const Nullable<T1> &other) const -> System::Nullable<decltype(get_Value() - other.get_Value())>
418 {
419 return (IsNull() || other.IsNull()) ? nullptr :
420 Nullable<decltype(get_Value() - other.get_Value())>
421 (get_Value() - other.get_Value());
422 }
423
426 Nullable<T> operator+=(std::nullptr_t)
427 {
428 m_has_value = false;
429 return *this;
430 }
431
436 template<typename T1>
437 typename std::enable_if<!IsNullable<T1>::value, Nullable<T>>::type operator+=(const T1& other)
438 {
439 if (m_has_value) {
440 m_value += other;
441 }
442 return *this;
443 }
444
449 template<typename T1>
451 {
452 m_has_value = m_has_value && !other.IsNull();
453 if (m_has_value) {
454 m_value += other.get_Value();
455 }
456 return *this;
457 }
458
460 template<typename T1, typename = typename std::enable_if<std::is_null_pointer<T1>::value>::type>
462 static_assert(std::is_null_pointer<T1>::value, "Operator '-=' is ambiguous on operands of type 'Nullable<T>' and '<null>'");
463 return Nullable<T>();
464 }
465
470 template<typename T1, typename = typename std::enable_if<!std::is_null_pointer<T1>::value, T1>::type>
471 typename std::enable_if<!IsNullable<T1>::value, Nullable<T>>::type operator-=(const T1& other)
472 {
473 if (m_has_value) {
474 m_value -= other;
475 }
476 return *this;
477 }
478
483 template<typename T1 = T>
484 typename std::enable_if<std::is_same<T1, bool>::value, Nullable<T>>::type operator|=(bool other)
485 {
486 if (m_has_value)
487 {
488 m_value |= other;
489 }
490 else if (other)
491 {
492 m_has_value = true;
493 m_value = other;
494 }
495 return *this;
496 }
497
502 template<typename T1 = T>
503 typename std::enable_if<std::is_same<T1, bool>::value, Nullable<T>>::type operator&=(bool other)
504 {
505 if (m_has_value)
506 {
507 m_value &= other;
508 }
509 else if (!other)
510 {
511 m_has_value = true;
512 m_value = other;
513 }
514 return *this;
515 }
516
517
522 template<typename T1>
524 {
525 m_has_value = m_has_value && !other.IsNull();
526 if (m_has_value) {
527 m_value -= other.get_Value();
528 }
529 return *this;
530 }
531
533 int GetHashCode() const
534 {
535 return m_has_value ? System::GetHashCode<T>(m_value) : 0;
536 }
537
541 {
542 return m_has_value ? ToStringHelper(m_value) : u"";
543 }
544
548 T GetValueOrDefault(T default_value)
549 {
550 return m_has_value ? m_value : default_value;
551 }
552
554 {
555 return m_has_value ? m_value : Default<T>();
556 }
557 private:
558
560 bool m_has_value;
561
563 T m_value;
564
568 template<class C>
569 static typename std::enable_if<
570 std::is_arithmetic<C>::value || std::is_enum<C>::value, String>::type
571 ToStringHelper(const C& value)
572 {
573 return String::Format(u"{0}", value);
574 }
575
579 template<typename C>
580 static typename std::enable_if<
581 (!std::is_arithmetic<C>::value && !std::is_enum<C>::value && !IsSmartPtr<C>::value && !IsExceptionWrapper<C>::value), String>::type
582 ToStringHelper(const C& obj)
583 {
584 return obj.ToString();
585 }
586
590 template<typename C>
591 static typename std::enable_if<
592 IsSmartPtr<C>::value || IsExceptionWrapper<C>::value, String>::type
593 ToStringHelper(const C& obj)
594 {
595 return obj->ToString();
596 }
597
601 static String ToStringHelper(bool b)
602 {
603 return b ? u"True" : u"False";
604 }
605 };
606
610 template<typename T>
611 bool operator==(std::nullptr_t, const Nullable<T>& other) { return other.IsNull(); }
612
616 template<typename T>
617 bool operator!=(std::nullptr_t, const Nullable<T>& other) { return !other.IsNull(); }
618
620 template<typename T>
621 bool operator>(std::nullptr_t, const Nullable<T>&) { return false; }
622
624 template<typename T>
625 bool operator<(std::nullptr_t, const Nullable<T>&) { return false; }
626
628 template<typename T>
629 bool operator>=(std::nullptr_t, const Nullable<T>&) { return false; }
630
632 template<typename T>
633 bool operator<=(std::nullptr_t, const Nullable<T>&) { return false; }
634
635
642 template<typename T1, typename T2>
643 typename std::enable_if<!IsNullable<T1>::value, bool>::type
644 operator==(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some == other.get_Value(); }
645
652 template<typename T1, typename T2>
653 typename std::enable_if<!IsNullable<T1>::value, bool>::type
654 operator!=(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? true : some != other.get_Value(); }
655
662 template<typename T1, typename T2>
663 typename std::enable_if<!IsNullable<T1>::value, bool>::type
664 operator>(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some > other.get_Value(); }
665
672 template<typename T1, typename T2>
673 typename std::enable_if<!IsNullable<T1>::value, bool>::type
674 operator<(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some < other.get_Value(); }
675
682 template<typename T1, typename T2>
683 typename std::enable_if<!IsNullable<T1>::value, bool>::type
684 operator>=(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some >= other.get_Value(); }
685
692 template<typename T1, typename T2>
693 typename std::enable_if<!IsNullable<T1>::value, bool>::type
694 operator<=(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some <= other.get_Value(); }
695
702 template<
703 typename T1,
704 typename T2,
705 typename = typename std::enable_if<!IsNullable<T1>::value && !IsNullable<T2>::value && !std::is_same<T1, System::String>::value, int>::type
706 > auto operator + (const T1& some, const Nullable<T2> &other) -> System::Nullable<decltype(some + other.get_Value())>
707 {
708 return other.IsNull() ? nullptr : Nullable<decltype(some + other.get_Value())>(some + other.get_Value());
709 }
710
717 template<
718 typename T1,
719 typename T2,
720 typename = typename std::enable_if<!IsNullable<T1>::value && !IsNullable<T2>::value && !std::is_same<T1, System::String>::value, int>::type
721 > auto operator - (const T1& some, const Nullable<T2> &other) -> System::Nullable<decltype(some - other.get_Value())>
722 {
723 return other.IsNull() ? nullptr : Nullable<decltype(some - other.get_Value())>(some - other.get_Value());
724 }
725
728 template <typename T>
729 struct IsBoxable<Nullable<T>> : std::true_type {};
731
733 template <typename T>
734 void PrintTo(const Nullable<T>& value, std::ostream* stream)
735 {
736 if (value == nullptr)
737 *stream << "nullptr";
738 else
739 *stream << value.ToString().ToUtf8String();
740 }
741
746 template <typename T>
747 std::ostream& operator<<(std::ostream& stream, const Nullable<T>& value)
748 {
749 stream << value.ToString();
750 return stream;
751 }
752
757 template <typename T>
758 std::wostream& operator<<(std::wostream& stream, const Nullable<T>& value)
759 {
760 stream << value.ToString();
761 return stream;
762 }
763
767 class NullableUtils final
768 {
769 public:
774 static ASPOSECPP_SHARED_API const System::TypeInfo& GetUnderlyingType(const System::TypeInfo& nullableType);
775 };
776}
777
778#endif
779
Forward declaration.
Definition: nullable.h:75
Nullable< T > operator-=(const Nullable< T1 > &other)
Applies operator-=() to the value represented by the current object using the value represented by th...
Definition: nullable.h:523
Nullable< T > operator+=(std::nullptr_t)
Resets the current object so that it represents a null-value.
Definition: nullable.h:426
std::enable_if<!IsNullable< T1 >::value, bool >::type operator>(const T1 &other) const
Determines if the value represented by the current object is greater than the specified value by appl...
Definition: nullable.h:273
Nullable< T > operator+=(const Nullable< T1 > &other)
Applies operator+=() to the value represented by the current object using the value represented by th...
Definition: nullable.h:450
bool operator>(std::nullptr_t) const
Always returns false.
Definition: nullable.h:266
bool operator!=(const Nullable< T1 > &other) const
Determines if the value represented by the current object is not equal to the value represented by th...
Definition: nullable.h:258
std::enable_if<!IsNullable< T1 >::value, Nullable< T > >::type operator-=(const T1 &other)
Applies operator-=() to the value represented by the current object using the specified value as a ri...
Definition: nullable.h:471
Nullable< T > & operator=(const Nullable< T1 > &x)
Replaces the object's currently represented value with the specified one.
Definition: nullable.h:130
void reset()
Sets the currently represented value to null.
Definition: nullable.h:168
Nullable< T > operator=(std::nullptr_t)
Assigns a null to the current object.
Definition: nullable.h:110
bool operator!=(std::nullptr_t) const
Determines if the value represented by the current object is not null.
Definition: nullable.h:241
Nullable(std::nullptr_t)
Constructs an instance that represents null.
Definition: nullable.h:88
auto operator-(const T1 &other) const -> Nullable< decltype(get_Value() - other)>
Subtracts nullable and non-nullable values.
Definition: nullable.h:406
auto operator-(const Nullable< T1 > &other) const -> System::Nullable< decltype(get_Value() - other.get_Value())>
Subtracts nullable values.
Definition: nullable.h:417
bool operator<(std::nullptr_t) const
Always returns false.
Definition: nullable.h:291
bool operator==(std::nullptr_t) const
Determines if the value represented by the current object is null.
Definition: nullable.h:205
bool NullableBoolHelper(const T1 &other, const std::function< bool()> &f, bool default_if_both_are_null=false) const
Helper function to check if this and other are both not nulls and call a lambda if so....
Definition: nullable.h:181
bool operator>=(const Nullable< T1 > &other) const
Determines if the value represented by the current object is greater or equal to the value represente...
Definition: nullable.h:334
T ValueType
An alias for a type of the value represented by this class.
Definition: nullable.h:81
T get_Value() const
Returns a copy of the value represented by the current object.
Definition: nullable.h:144
std::enable_if<!IsNullable< T1 >::value, bool >::type operator<(const T1 &other) const
Determines if the value represented by the current object is less than the specified value by applyin...
Definition: nullable.h:298
int GetHashCode() const
Returns a hash code for the current object.
Definition: nullable.h:533
std::enable_if< IsNullable< T1 >::value, bool >::type Equals(const T1 &other) const
Determines if the value represented by the current object is equal to the value represented by the sp...
Definition: nullable.h:234
std::enable_if<!IsNullable< T1 >::value, bool >::type operator!=(const T1 &other) const
Determines if the value represented by the current object is not equal to the specified value.
Definition: nullable.h:248
Nullable< T > operator-=(T1)
Returns an instance of Nullable class that represents a null-value.
Definition: nullable.h:461
bool operator>=(std::nullptr_t) const
Always returns false.
Definition: nullable.h:317
std::enable_if< std::is_same< T1, bool >::value, Nullable< T > >::type operator|=(bool other)
Applies operator|=() to the value represented by the current object using the specified value as a ri...
Definition: nullable.h:484
bool operator==(const Nullable< T1 > &other) const
Determines if the value represented by the current object is equal to the value represented by the sp...
Definition: nullable.h:222
Nullable< T > operator+(std::nullptr_t) const
Returns a default constructed instance of Nullable<T> class.
Definition: nullable.h:367
Nullable()
Constructs an instance that represents null-value.
Definition: nullable.h:84
std::enable_if< std::is_same< T1, bool >::value, Nullable< T > >::type operator&=(bool other)
Applies operator&=() to the value represented by the current object using the specified value as a ri...
Definition: nullable.h:503
String ToString() const
Converts the value represented by the current object to string.
Definition: nullable.h:540
bool operator>(const Nullable< T1 > &other) const
Determines if the value represented by the current object is greater than the value represented by th...
Definition: nullable.h:283
bool operator<=(std::nullptr_t) const
Always returns false.
Definition: nullable.h:342
std::enable_if<!IsNullable< T1 >::value, bool >::type operator<=(const T1 &other) const
Determines if the value represented by the current object is less or equal to the specified value by ...
Definition: nullable.h:349
bool IsNull() const
Determines if the current object represents a null-value.
Definition: nullable.h:172
bool operator<(const Nullable< T1 > &other) const
Determines if the value represented by the current object is less than the value represented by the s...
Definition: nullable.h:308
std::enable_if<!IsNullable< T1 >::value, Nullable< T > >::type operator+=(const T1 &other)
Applies operator+=() to the value represented by the current object using the specified value as a ri...
Definition: nullable.h:437
std::enable_if<!IsNullable< T1 >::value &&!std::is_null_pointer< T1 >::value, Nullable< T > & >::type operator=(const T1 &x)
Replaces the object's currently represented value with the specified one.
Definition: nullable.h:117
T GetValueOrDefault()
Definition: nullable.h:553
auto operator+(const Nullable< T1 > &other) const -> System::Nullable< decltype(get_Value()+other.get_Value())>
Sums nullable values.
Definition: nullable.h:385
bool operator<=(const Nullable< T1 > &other) const
Determines if the value represented by the current object is less or equal to the value represented b...
Definition: nullable.h:359
auto operator+(const T1 &other) const -> Nullable< decltype(get_Value()+other)>
Sums nullable and non-nullable values.
Definition: nullable.h:374
std::enable_if<!IsNullable< T1 >::value, bool >::type operator==(const T1 &other) const
Determines if the value represented by the current object is equal to the specified value.
Definition: nullable.h:212
Nullable(const Nullable< T1 > &value)
Constructs an instance that represents a value that is represented by the specified Nullable object....
Definition: nullable.h:104
Nullable< T > operator-(T1) const
Subtracts nullable and null-pointed values.
Definition: nullable.h:396
Nullable(const T1 &value)
Constructs an instance of Nullable class that represents the specified value converted (if necessary)...
Definition: nullable.h:96
T GetValueOrDefault(T default_value)
Returns the value represented by the current object or the specified value if the value represented b...
Definition: nullable.h:548
std::enable_if<!IsNullable< T1 >::value, bool >::type operator>=(const T1 &other) const
Determines if the value represented by the current object is greater or equal to the value represente...
Definition: nullable.h:324
bool get_HasValue() const
Determines whether the current object represents any value.
Definition: nullable.h:154
Represents C# System.Nullable (with no type arguments) static class. Unable to use original name due ...
Definition: nullable.h:768
static const System::TypeInfo & GetUnderlyingType(const System::TypeInfo &nullableType)
Returns the underlying type argument of the specified nullable type.
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:1415
std::string ToUtf8String() const
Converts string to std::string. Uses UTF-8 encoding.
Represents a particular type and provides information about it.
Definition: type_info.h:109
Definition: db_command.h:9
void PrintTo(DateTime value, std::ostream *stream)
Prints value to ostream. Mostly used for debug.
bool operator!=(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:156
Decimal operator+(const T &x, const Decimal &d)
Returns a new instance of Decimal class that represents a value that is a sum of the specified value ...
Definition: decimal.h:542
constexpr bool operator>(std::nullptr_t, DateTime)
Definition: date_time.h:714
auto operator-(DayOfWeek a, DayOfWeek b)
Calculates the number of days between two days of week.
Definition: day_of_week.h:25
std::ostream & operator<<(std::ostream &stream, DateTime date_time)
Insert data into the stream using UTF-8 encoding.
Definition: date_time.h:729
constexpr bool operator<(std::nullptr_t, DateTime)
Definition: date_time.h:712
constexpr bool operator<=(std::nullptr_t, DateTime)
Definition: date_time.h:713
constexpr bool operator>=(std::nullptr_t, DateTime)
Definition: date_time.h:715
bool operator==(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:150
A template predicate that determines if its template argument T in Nullable or its subclass.
Definition: nullable.h:23
Tests if specific type is a specialization of specific template. If it is, inherits std::true_type,...
Definition: detail.h:80