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 void set_Value(const T& value)
155 {
156 m_has_value = true;
157 m_value = value;
158 }
159
162 bool get_HasValue() const { return m_has_value; }
163
167 operator const T&() const
168 {
169 if (!m_has_value)
170 Details::ThrowNullableObjectMustHaveAValue();
171
172 return m_value;
173 }
174
176 void reset() { m_has_value = false; }
177
180 bool IsNull() const { return !m_has_value; }
181
188 template<typename T1>
189 bool NullableBoolHelper(const T1& other, const std::function<bool()>& f, bool default_if_both_are_null = false) const
190 {
191 if (IsNull())
192 {
193 if (other.IsNull())
194 {
195 return default_if_both_are_null;
196 }
197 else
198 {
199 return false;
200 }
201 }
202
203 if (other.IsNull())
204 {
205 return false;
206 }
207
208 return f();
209 }
210
213 bool operator==(std::nullptr_t) const { return IsNull(); }
214
219 template<typename T1>
220 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator==(const T1& other) const
221 {
222 return m_has_value && m_value == other;
223 }
224
229 template<typename T1>
230 bool operator==(const Nullable<T1>& other) const
231 {
232 return NullableBoolHelper<Nullable<T1>>(other,
233 [&] {return m_value == other.get_Value(); },
234 true);
235 }
236
241 template<typename T1>
242 typename std::enable_if<IsNullable<T1>::value, bool>::type Equals(const T1& other) const
243 {
244 return ((!m_has_value) && (!other.m_has_value)) || (m_has_value && other.m_has_value && (m_value == other.get_Value()));
245 }
246
249 bool operator!=(std::nullptr_t) const { return !IsNull(); }
250
255 template<typename T1>
256 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator!=(const T1& other) const
257 {
258 return IsNull() ? true : m_value != other;
259 }
260
265 template<typename T1>
266 bool operator!=(const Nullable<T1>& other) const
267 {
268 return NullableBoolHelper<Nullable<T1>>(other,
269 [&] {return m_value != other.get_Value(); }
270 );
271 }
272
274 bool operator>(std::nullptr_t) const { return false; }
275
280 template<typename T1>
281 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator>(const T1& other) const
282 {
283 return m_has_value && m_value > other;
284 }
285
290 template<typename T1>
291 bool operator>(const Nullable<T1>& other) const
292 {
293 return NullableBoolHelper<Nullable<T1>>(other,
294 [&] {return m_value > other.get_Value(); }
295 );
296 }
297
299 bool operator<(std::nullptr_t) const { return false; }
300
305 template<typename T1>
306 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator<(const T1& other) const
307 {
308 return m_has_value && m_value < other;
309 }
310
315 template<typename T1>
316 bool operator<(const Nullable<T1>& other) const
317 {
318 return NullableBoolHelper<Nullable<T1>>(other,
319 [&] {return m_value < other.get_Value(); }
320 );
321 }
322
325 bool operator>=(std::nullptr_t) const { return false; }
326
331 template<typename T1>
332 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator>=(const T1& other) const
333 {
334 return m_has_value && m_value >= other;
335 }
336
341 template<typename T1>
342 bool operator>=(const Nullable<T1>& other) const
343 {
344 return NullableBoolHelper<Nullable<T1>>(other,
345 [&] {return m_value >= other.get_Value(); }
346 );
347 }
348
350 bool operator<=(std::nullptr_t) const { return false; }
351
356 template<typename T1>
357 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator<=(const T1& other) const
358 {
359 return m_has_value && m_value <= other;
360 }
361
366 template<typename T1>
367 bool operator<=(const Nullable<T1>& other) const
368 {
369 return NullableBoolHelper<Nullable<T1>>(other,
370 [&] {return m_value <= other.get_Value(); }
371 );
372 }
373
375 Nullable<T> operator+(std::nullptr_t) const { return Nullable<T>(); }
376
381 template<typename T1, typename = typename std::enable_if<!IsNullable<T1>::value, int>::type>
382 auto operator+(const T1 &other) const -> Nullable<decltype(get_Value() + other)>
383 {
385 (get_Value() + other);
386 }
387
392 template<typename T1>
393 auto operator+(const Nullable<T1> &other) const -> System::Nullable<decltype(get_Value() + other.get_Value())>
394 {
395 return (IsNull() || other.IsNull()) ? nullptr :
396 Nullable<decltype(get_Value() + other.get_Value())>
397 (get_Value() + other.get_Value());
398 }
399
403 template<typename T1, typename = typename std::enable_if<std::is_null_pointer<T1>::value>::type>
405 static_assert(std::is_null_pointer<T1>::value, "Operator '-' is ambiguous on operands of type 'Nullable<T>' and '<null>'");
406 return Nullable<T>();
407 }
408
413 template<typename T1, typename = typename std::enable_if<!IsNullable<T1>::value, int>::type>
414 auto operator-(const T1 &other) const -> Nullable<decltype(get_Value() - other)>
415 {
416 return IsNull() ? nullptr : Nullable<decltype(get_Value() - other)>
417 (get_Value() - other);
418 }
419
424 template<typename T1>
425 auto operator-(const Nullable<T1> &other) const -> System::Nullable<decltype(get_Value() - other.get_Value())>
426 {
427 return (IsNull() || other.IsNull()) ? nullptr :
428 Nullable<decltype(get_Value() - other.get_Value())>
429 (get_Value() - other.get_Value());
430 }
431
434 Nullable<T> operator+=(std::nullptr_t)
435 {
436 m_has_value = false;
437 return *this;
438 }
439
444 template<typename T1>
445 typename std::enable_if<!IsNullable<T1>::value, Nullable<T>>::type operator+=(const T1& other)
446 {
447 if (m_has_value) {
448 m_value += other;
449 }
450 return *this;
451 }
452
457 template<typename T1>
459 {
460 m_has_value = m_has_value && !other.IsNull();
461 if (m_has_value) {
462 m_value += other.get_Value();
463 }
464 return *this;
465 }
466
468 template<typename T1, typename = typename std::enable_if<std::is_null_pointer<T1>::value>::type>
470 static_assert(std::is_null_pointer<T1>::value, "Operator '-=' is ambiguous on operands of type 'Nullable<T>' and '<null>'");
471 return Nullable<T>();
472 }
473
478 template<typename T1, typename = typename std::enable_if<!std::is_null_pointer<T1>::value, T1>::type>
479 typename std::enable_if<!IsNullable<T1>::value, Nullable<T>>::type operator-=(const T1& other)
480 {
481 if (m_has_value) {
482 m_value -= other;
483 }
484 return *this;
485 }
486
491 template<typename T1 = T>
492 typename std::enable_if<std::is_same<T1, bool>::value, Nullable<T>>::type operator|=(bool other)
493 {
494 if (m_has_value)
495 {
496 m_value |= other;
497 }
498 else if (other)
499 {
500 m_has_value = true;
501 m_value = other;
502 }
503 return *this;
504 }
505
510 template<typename T1 = T>
511 typename std::enable_if<std::is_same<T1, bool>::value, Nullable<T>>::type operator&=(bool other)
512 {
513 if (m_has_value)
514 {
515 m_value &= other;
516 }
517 else if (!other)
518 {
519 m_has_value = true;
520 m_value = other;
521 }
522 return *this;
523 }
524
525
530 template<typename T1>
532 {
533 m_has_value = m_has_value && !other.IsNull();
534 if (m_has_value) {
535 m_value -= other.get_Value();
536 }
537 return *this;
538 }
539
541 int GetHashCode() const
542 {
543 return m_has_value ? System::GetHashCode<T>(m_value) : 0;
544 }
545
549 {
550 return m_has_value ? ToStringHelper(m_value) : u"";
551 }
552
556 T GetValueOrDefault(T default_value)
557 {
558 return m_has_value ? m_value : default_value;
559 }
560
562 {
563 return m_has_value ? m_value : Default<T>();
564 }
565 private:
566
568 bool m_has_value;
569
571 T m_value;
572
576 template<class C>
577 static typename std::enable_if<
578 std::is_arithmetic<C>::value || std::is_enum<C>::value, String>::type
579 ToStringHelper(const C& value)
580 {
581 return String::Format(u"{0}", value);
582 }
583
587 template<typename C>
588 static typename std::enable_if<
589 (!std::is_arithmetic<C>::value && !std::is_enum<C>::value && !IsSmartPtr<C>::value && !IsExceptionWrapper<C>::value), String>::type
590 ToStringHelper(const C& obj)
591 {
592 return obj.ToString();
593 }
594
598 template<typename C>
599 static typename std::enable_if<
600 IsSmartPtr<C>::value || IsExceptionWrapper<C>::value, String>::type
601 ToStringHelper(const C& obj)
602 {
603 return obj->ToString();
604 }
605
609 static String ToStringHelper(bool b)
610 {
611 return b ? u"True" : u"False";
612 }
613 };
614
618 template<typename T>
619 bool operator==(std::nullptr_t, const Nullable<T>& other) { return other.IsNull(); }
620
624 template<typename T>
625 bool operator!=(std::nullptr_t, const Nullable<T>& other) { return !other.IsNull(); }
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
636 template<typename T>
637 bool operator>=(std::nullptr_t, const Nullable<T>&) { return false; }
638
640 template<typename T>
641 bool operator<=(std::nullptr_t, const Nullable<T>&) { return false; }
642
643
650 template<typename T1, typename T2>
651 typename std::enable_if<!IsNullable<T1>::value, bool>::type
652 operator==(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some == other.get_Value(); }
653
660 template<typename T1, typename T2>
661 typename std::enable_if<!IsNullable<T1>::value, bool>::type
662 operator!=(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? true : some != other.get_Value(); }
663
670 template<typename T1, typename T2>
671 typename std::enable_if<!IsNullable<T1>::value, bool>::type
672 operator>(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some > other.get_Value(); }
673
680 template<typename T1, typename T2>
681 typename std::enable_if<!IsNullable<T1>::value, bool>::type
682 operator<(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some < other.get_Value(); }
683
690 template<typename T1, typename T2>
691 typename std::enable_if<!IsNullable<T1>::value, bool>::type
692 operator>=(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some >= other.get_Value(); }
693
700 template<typename T1, typename T2>
701 typename std::enable_if<!IsNullable<T1>::value, bool>::type
702 operator<=(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some <= other.get_Value(); }
703
710 template<
711 typename T1,
712 typename T2,
713 typename = typename std::enable_if<!IsNullable<T1>::value && !IsNullable<T2>::value && !std::is_same<T1, System::String>::value, int>::type
714 > auto operator + (const T1& some, const Nullable<T2> &other) -> System::Nullable<decltype(some + other.get_Value())>
715 {
716 return other.IsNull() ? nullptr : Nullable<decltype(some + other.get_Value())>(some + other.get_Value());
717 }
718
725 template<
726 typename T1,
727 typename T2,
728 typename = typename std::enable_if<!IsNullable<T1>::value && !IsNullable<T2>::value && !std::is_same<T1, System::String>::value, int>::type
729 > auto operator - (const T1& some, const Nullable<T2> &other) -> System::Nullable<decltype(some - other.get_Value())>
730 {
731 return other.IsNull() ? nullptr : Nullable<decltype(some - other.get_Value())>(some - other.get_Value());
732 }
733
736 template <typename T>
737 struct IsBoxable<Nullable<T>> : std::true_type {};
739
741 template <typename T>
742 void PrintTo(const Nullable<T>& value, std::ostream* stream)
743 {
744 if (value == nullptr)
745 *stream << "nullptr";
746 else
747 *stream << value.ToString().ToUtf8String();
748 }
749
754 template <typename T>
755 std::ostream& operator<<(std::ostream& stream, const Nullable<T>& value)
756 {
757 stream << value.ToString();
758 return stream;
759 }
760
765 template <typename T>
766 std::wostream& operator<<(std::wostream& stream, const Nullable<T>& value)
767 {
768 stream << value.ToString();
769 return stream;
770 }
771
775 class NullableUtils final
776 {
777 public:
782 static ASPOSECPP_SHARED_API const System::TypeInfo& GetUnderlyingType(const System::TypeInfo& nullableType);
783 };
784}
785
786#endif
787
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:531
Nullable< T > operator+=(std::nullptr_t)
Resets the current object so that it represents a null-value.
Definition: nullable.h:434
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:281
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:458
bool operator>(std::nullptr_t) const
Always returns false.
Definition: nullable.h:274
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:266
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:479
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:176
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:249
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:414
auto operator-(const Nullable< T1 > &other) const -> System::Nullable< decltype(get_Value() - other.get_Value())>
Subtracts nullable values.
Definition: nullable.h:425
bool operator<(std::nullptr_t) const
Always returns false.
Definition: nullable.h:299
bool operator==(std::nullptr_t) const
Determines if the value represented by the current object is null.
Definition: nullable.h:213
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:189
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:342
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:306
int GetHashCode() const
Returns a hash code for the current object.
Definition: nullable.h:541
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:242
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:256
Nullable< T > operator-=(T1)
Returns an instance of Nullable class that represents a null-value.
Definition: nullable.h:469
bool operator>=(std::nullptr_t) const
Always returns false.
Definition: nullable.h:325
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:492
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:230
Nullable< T > operator+(std::nullptr_t) const
Returns a default constructed instance of Nullable<T> class.
Definition: nullable.h:375
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:511
String ToString() const
Converts the value represented by the current object to string.
Definition: nullable.h:548
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:291
bool operator<=(std::nullptr_t) const
Always returns false.
Definition: nullable.h:350
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:357
bool IsNull() const
Determines if the current object represents a null-value.
Definition: nullable.h:180
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:316
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:445
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:561
auto operator+(const Nullable< T1 > &other) const -> System::Nullable< decltype(get_Value()+other.get_Value())>
Sums nullable values.
Definition: nullable.h:393
void set_Value(const T &value)
Sets a new value to nullable object.
Definition: nullable.h:154
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:367
auto operator+(const T1 &other) const -> Nullable< decltype(get_Value()+other)>
Sums nullable and non-nullable values.
Definition: nullable.h:382
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:220
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:404
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:556
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:332
bool get_HasValue() const
Determines whether the current object represents any value.
Definition: nullable.h:162
Represents C# System.Nullable (with no type arguments) static class. Unable to use original name due ...
Definition: nullable.h:776
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:1419
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