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 }
30
72 template<typename T>
74 {
75 RTTI_INFO_VALUE_TYPE(Nullable<T>);
77 template<class T2> friend class System::Nullable;
78 public:
80 typedef T ValueType;
81
83 Nullable() : m_has_value(false)
84 {}
85
87 Nullable(std::nullptr_t) : m_has_value(false)
88 {}
89
94 template<typename T1>
95 Nullable(const T1& value) : m_has_value(true), m_value(value)
96 {}
97
102 template<typename T1>
103 Nullable(const Nullable<T1>& value) : m_has_value(value.m_has_value), m_value(value.m_value)
104 {}
105
108 template<typename T1, typename = typename std::enable_if<std::is_null_pointer<T1>::value>>
109 Nullable<T> operator=(std::nullptr_t) { return Nullable<T>(); }
110
115 template<typename T1>
116 typename std::enable_if<!IsNullable<T1>::value && !std::is_null_pointer<T1>::value, Nullable<T>&>::type operator=(const T1& x)
117 {
118 this->m_has_value = true;
119 this->m_value = static_cast<T>(x);
120
121 return *this;
122 }
123
128 template<typename T1>
130 {
131 this->m_has_value = x.m_has_value;
132 if (x.m_has_value)
133 {
134 this->m_value = x.m_value;
135 }
136
137 return *this;
138 }
139
143 T get_Value() const
144 {
145 if (!m_has_value)
146 Details::ThrowNullableObjectMustHaveAValue();
147
148 return m_value;
149 }
150
153 bool get_HasValue() const { return m_has_value; }
154
158 operator const T&() const
159 {
160 if (!m_has_value)
161 Details::ThrowNullableObjectMustHaveAValue();
162
163 return m_value;
164 }
165
167 void reset() { m_has_value = false; }
168
171 bool IsNull() const { return !m_has_value; }
172
179 template<typename T1>
180 bool NullableBoolHelper(const T1& other, const std::function<bool()>& f, bool default_if_both_are_null = false) const
181 {
182 if (IsNull())
183 {
184 if (other.IsNull())
185 {
186 return default_if_both_are_null;
187 }
188 else
189 {
190 return false;
191 }
192 }
193
194 if (other.IsNull())
195 {
196 return false;
197 }
198
199 return f();
200 }
201
204 bool operator==(std::nullptr_t) const { return IsNull(); }
205
210 template<typename T1>
211 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator==(const T1& other) const
212 {
213 return m_has_value && m_value == other;
214 }
215
220 template<typename T1>
221 bool operator==(const Nullable<T1>& other) const
222 {
223 return NullableBoolHelper<Nullable<T1>>(other,
224 [&] {return m_value == other.get_Value(); },
225 true);
226 }
227
232 template<typename T1>
233 typename std::enable_if<IsNullable<T1>::value, bool>::type Equals(const T1& other) const
234 {
235 return ((!m_has_value) && (!other.m_has_value)) || (m_has_value && other.m_has_value && (m_value == other.get_Value()));
236 }
237
240 bool operator!=(std::nullptr_t) const { return !IsNull(); }
241
246 template<typename T1>
247 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator!=(const T1& other) const
248 {
249 return IsNull() ? true : m_value != other;
250 }
251
256 template<typename T1>
257 bool operator!=(const Nullable<T1>& other) const
258 {
259 return NullableBoolHelper<Nullable<T1>>(other,
260 [&] {return m_value != other.get_Value(); }
261 );
262 }
263
265 bool operator>(std::nullptr_t) const { return false; }
266
271 template<typename T1>
272 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator>(const T1& other) const
273 {
274 return m_has_value && m_value > other;
275 }
276
281 template<typename T1>
282 bool operator>(const Nullable<T1>& other) const
283 {
284 return NullableBoolHelper<Nullable<T1>>(other,
285 [&] {return m_value > other.get_Value(); }
286 );
287 }
288
290 bool operator<(std::nullptr_t) const { return false; }
291
296 template<typename T1>
297 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator<(const T1& other) const
298 {
299 return m_has_value && m_value < other;
300 }
301
306 template<typename T1>
307 bool operator<(const Nullable<T1>& other) const
308 {
309 return NullableBoolHelper<Nullable<T1>>(other,
310 [&] {return m_value < other.get_Value(); }
311 );
312 }
313
316 bool operator>=(std::nullptr_t) const { return false; }
317
322 template<typename T1>
323 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator>=(const T1& other) const
324 {
325 return m_has_value && m_value >= other;
326 }
327
332 template<typename T1>
333 bool operator>=(const Nullable<T1>& other) const
334 {
335 return NullableBoolHelper<Nullable<T1>>(other,
336 [&] {return m_value >= other.get_Value(); }
337 );
338 }
339
341 bool operator<=(std::nullptr_t) const { return false; }
342
347 template<typename T1>
348 typename std::enable_if<!IsNullable<T1>::value, bool>::type operator<=(const T1& other) const
349 {
350 return m_has_value && m_value <= other;
351 }
352
357 template<typename T1>
358 bool operator<=(const Nullable<T1>& other) const
359 {
360 return NullableBoolHelper<Nullable<T1>>(other,
361 [&] {return m_value <= other.get_Value(); }
362 );
363 }
364
366 Nullable<T> operator+(std::nullptr_t) const { return Nullable<T>(); }
367
372 template<typename T1, typename = typename std::enable_if<!IsNullable<T1>::value, int>::type>
373 auto operator+(const T1 &other) const -> Nullable<decltype(get_Value() + other)>
374 {
376 (get_Value() + other);
377 }
378
383 template<typename T1>
384 auto operator+(const Nullable<T1> &other) const -> System::Nullable<decltype(get_Value() + other.get_Value())>
385 {
386 return (IsNull() || other.IsNull()) ? nullptr :
387 Nullable<decltype(get_Value() + other.get_Value())>
388 (get_Value() + other.get_Value());
389 }
390
394 template<typename T1, typename = typename std::enable_if<std::is_null_pointer<T1>::value>::type>
396 static_assert(std::is_null_pointer<T1>::value, "Operator '-' is ambiguous on operands of type 'Nullable<T>' and '<null>'");
397 return Nullable<T>();
398 }
399
404 template<typename T1, typename = typename std::enable_if<!IsNullable<T1>::value, int>::type>
405 auto operator-(const T1 &other) const -> Nullable<decltype(get_Value() - other)>
406 {
407 return IsNull() ? nullptr : Nullable<decltype(get_Value() - other)>
408 (get_Value() - other);
409 }
410
415 template<typename T1>
416 auto operator-(const Nullable<T1> &other) const -> System::Nullable<decltype(get_Value() - other.get_Value())>
417 {
418 return (IsNull() || other.IsNull()) ? nullptr :
419 Nullable<decltype(get_Value() - other.get_Value())>
420 (get_Value() - other.get_Value());
421 }
422
425 Nullable<T> operator+=(std::nullptr_t)
426 {
427 m_has_value = false;
428 return *this;
429 }
430
435 template<typename T1>
436 typename std::enable_if<!IsNullable<T1>::value, Nullable<T>>::type operator+=(const T1& other)
437 {
438 if (m_has_value) {
439 m_value += other;
440 }
441 return *this;
442 }
443
448 template<typename T1>
450 {
451 m_has_value = m_has_value && !other.IsNull();
452 if (m_has_value) {
453 m_value += other.get_Value();
454 }
455 return *this;
456 }
457
459 template<typename T1, typename = typename std::enable_if<std::is_null_pointer<T1>::value>::type>
461 static_assert(std::is_null_pointer<T1>::value, "Operator '-=' is ambiguous on operands of type 'Nullable<T>' and '<null>'");
462 return Nullable<T>();
463 }
464
469 template<typename T1, typename = typename std::enable_if<!std::is_null_pointer<T1>::value, T1>::type>
470 typename std::enable_if<!IsNullable<T1>::value, Nullable<T>>::type operator-=(const T1& other)
471 {
472 if (m_has_value) {
473 m_value -= other;
474 }
475 return *this;
476 }
477
482 template<typename T1 = T>
483 typename std::enable_if<std::is_same<T1, bool>::value, Nullable<T>>::type operator|=(bool other)
484 {
485 if (m_has_value)
486 {
487 m_value |= other;
488 }
489 else if (other)
490 {
491 m_has_value = true;
492 m_value = other;
493 }
494 return *this;
495 }
496
501 template<typename T1 = T>
502 typename std::enable_if<std::is_same<T1, bool>::value, Nullable<T>>::type operator&=(bool other)
503 {
504 if (m_has_value)
505 {
506 m_value &= other;
507 }
508 else if (!other)
509 {
510 m_has_value = true;
511 m_value = other;
512 }
513 return *this;
514 }
515
516
521 template<typename T1>
523 {
524 m_has_value = m_has_value && !other.IsNull();
525 if (m_has_value) {
526 m_value -= other.get_Value();
527 }
528 return *this;
529 }
530
532 int GetHashCode() const
533 {
534 return m_has_value ? System::GetHashCode<T>(m_value) : 0;
535 }
536
540 {
541 return m_has_value ? ToStringHelper(m_value) : u"";
542 }
543
547 T GetValueOrDefault(T default_value)
548 {
549 return m_has_value ? m_value : default_value;
550 }
551
553 {
554 return m_has_value ? m_value : Default<T>();
555 }
556 private:
557
559 bool m_has_value;
560
562 T m_value;
563
567 template<class C>
568 static typename std::enable_if<
569 std::is_arithmetic<C>::value || std::is_enum<C>::value, String>::type
570 ToStringHelper(const C& value)
571 {
572 return String::Format(u"{0}", value);
573 }
574
578 template<typename C>
579 static typename std::enable_if<
580 (!std::is_arithmetic<C>::value && !std::is_enum<C>::value && !IsSmartPtr<C>::value && !IsExceptionWrapper<C>::value), String>::type
581 ToStringHelper(const C& obj)
582 {
583 return obj.ToString();
584 }
585
589 template<typename C>
590 static typename std::enable_if<
591 IsSmartPtr<C>::value || IsExceptionWrapper<C>::value, String>::type
592 ToStringHelper(const C& obj)
593 {
594 return obj->ToString();
595 }
596
600 static String ToStringHelper(bool b)
601 {
602 return b ? u"True" : u"False";
603 }
604 };
605
609 template<typename T>
610 bool operator==(std::nullptr_t, const Nullable<T>& other) { return other.IsNull(); }
611
615 template<typename T>
616 bool operator!=(std::nullptr_t, const Nullable<T>& other) { return !other.IsNull(); }
617
619 template<typename T>
620 bool operator>(std::nullptr_t, const Nullable<T>&) { return false; }
621
623 template<typename T>
624 bool operator<(std::nullptr_t, const Nullable<T>&) { return false; }
625
627 template<typename T>
628 bool operator>=(std::nullptr_t, const Nullable<T>&) { return false; }
629
631 template<typename T>
632 bool operator<=(std::nullptr_t, const Nullable<T>&) { return false; }
633
634
641 template<typename T1, typename T2>
642 typename std::enable_if<!IsNullable<T1>::value, bool>::type
643 operator==(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some == other.get_Value(); }
644
651 template<typename T1, typename T2>
652 typename std::enable_if<!IsNullable<T1>::value, bool>::type
653 operator!=(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? true : some != other.get_Value(); }
654
661 template<typename T1, typename T2>
662 typename std::enable_if<!IsNullable<T1>::value, bool>::type
663 operator>(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some > other.get_Value(); }
664
671 template<typename T1, typename T2>
672 typename std::enable_if<!IsNullable<T1>::value, bool>::type
673 operator<(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some < other.get_Value(); }
674
681 template<typename T1, typename T2>
682 typename std::enable_if<!IsNullable<T1>::value, bool>::type
683 operator>=(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some >= other.get_Value(); }
684
691 template<typename T1, typename T2>
692 typename std::enable_if<!IsNullable<T1>::value, bool>::type
693 operator<=(const T1& some, const Nullable<T2>& other) { return other.IsNull() ? false : some <= other.get_Value(); }
694
701 template<
702 typename T1,
703 typename T2,
704 typename = typename std::enable_if<!IsNullable<T1>::value && !IsNullable<T2>::value && !std::is_same<T1, System::String>::value, int>::type
705 > auto operator + (const T1& some, const Nullable<T2> &other) -> System::Nullable<decltype(some + other.get_Value())>
706 {
707 return other.IsNull() ? nullptr : Nullable<decltype(some + other.get_Value())>(some + other.get_Value());
708 }
709
716 template<
717 typename T1,
718 typename T2,
719 typename = typename std::enable_if<!IsNullable<T1>::value && !IsNullable<T2>::value && !std::is_same<T1, System::String>::value, int>::type
720 > auto operator - (const T1& some, const Nullable<T2> &other) -> System::Nullable<decltype(some - other.get_Value())>
721 {
722 return other.IsNull() ? nullptr : Nullable<decltype(some - other.get_Value())>(some - other.get_Value());
723 }
724
727 template <typename T>
728 struct IsBoxable<Nullable<T>> : std::true_type {};
730
732 template <typename T>
733 void PrintTo(const Nullable<T>& value, std::ostream* stream)
734 {
735 if (value == nullptr)
736 *stream << "nullptr";
737 else
738 *stream << value.ToString().ToUtf8String();
739 }
740
745 template <typename T>
746 std::ostream& operator<<(std::ostream& stream, const Nullable<T>& value)
747 {
748 stream << value.ToString();
749 return stream;
750 }
751
756 template <typename T>
757 std::wostream& operator<<(std::wostream& stream, const Nullable<T>& value)
758 {
759 stream << value.ToString();
760 return stream;
761 }
762
766 class NullableUtils final
767 {
768 public:
773 static ASPOSECPP_SHARED_API const System::TypeInfo& GetUnderlyingType(const System::TypeInfo& nullableType);
774 };
775}
776
777#endif
778
Forward declaration.
Definition: nullable.h:74
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:522
Nullable< T > operator+=(std::nullptr_t)
Resets the current object so that it represents a null-value.
Definition: nullable.h:425
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:272
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:449
bool operator>(std::nullptr_t) const
Always returns false.
Definition: nullable.h:265
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:257
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:470
Nullable< T > & operator=(const Nullable< T1 > &x)
Replaces the object's currently represented value with the specified one.
Definition: nullable.h:129
void reset()
Sets the currently represented value to null.
Definition: nullable.h:167
Nullable< T > operator=(std::nullptr_t)
Assigns a null to the current object.
Definition: nullable.h:109
bool operator!=(std::nullptr_t) const
Determines if the value represented by the current object is not null.
Definition: nullable.h:240
Nullable(std::nullptr_t)
Constructs an instance that represents null.
Definition: nullable.h:87
auto operator-(const T1 &other) const -> Nullable< decltype(get_Value() - other)>
Subtracts nullable and non-nullable values.
Definition: nullable.h:405
auto operator-(const Nullable< T1 > &other) const -> System::Nullable< decltype(get_Value() - other.get_Value())>
Subtracts nullable values.
Definition: nullable.h:416
bool operator<(std::nullptr_t) const
Always returns false.
Definition: nullable.h:290
bool operator==(std::nullptr_t) const
Determines if the value represented by the current object is null.
Definition: nullable.h:204
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:180
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:333
T ValueType
An alias for a type of the value represented by this class.
Definition: nullable.h:80
T get_Value() const
Returns a copy of the value represented by the current object.
Definition: nullable.h:143
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:297
int GetHashCode() const
Returns a hash code for the current object.
Definition: nullable.h:532
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:233
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:247
Nullable< T > operator-=(T1)
Returns an instance of Nullable class that represents a null-value.
Definition: nullable.h:460
bool operator>=(std::nullptr_t) const
Always returns false.
Definition: nullable.h:316
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:483
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:221
Nullable< T > operator+(std::nullptr_t) const
Returns a default constructed instance of Nullable<T> class.
Definition: nullable.h:366
Nullable()
Constructs an instance that represents null-value.
Definition: nullable.h:83
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:502
String ToString() const
Converts the value represented by the current object to string.
Definition: nullable.h:539
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:282
bool operator<=(std::nullptr_t) const
Always returns false.
Definition: nullable.h:341
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:348
bool IsNull() const
Determines if the current object represents a null-value.
Definition: nullable.h:171
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:307
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:436
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:116
T GetValueOrDefault()
Definition: nullable.h:552
auto operator+(const Nullable< T1 > &other) const -> System::Nullable< decltype(get_Value()+other.get_Value())>
Sums nullable values.
Definition: nullable.h:384
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:358
auto operator+(const T1 &other) const -> Nullable< decltype(get_Value()+other)>
Sums nullable and non-nullable values.
Definition: nullable.h:373
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:211
Nullable(const Nullable< T1 > &value)
Constructs an instance that represents a value that is represented by the specified Nullable object....
Definition: nullable.h:103
Nullable< T > operator-(T1) const
Subtracts nullable and null-pointed values.
Definition: nullable.h:395
Nullable(const T1 &value)
Constructs an instance of Nullable class that represents the specified value converted (if necessary)...
Definition: nullable.h:95
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:547
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:323
bool get_HasValue() const
Determines whether the current object represents any value.
Definition: nullable.h:153
Represents C# System.Nullable (with no type arguments) static class. Unable to use original name due ...
Definition: nullable.h:767
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:1405
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:157
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:151
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