System::Nullable< T > Class Template Reference

Forward declaration. More...

Public Types

typedef T ValueType
 An alias for a type of the value represented by this class. More...
 

Public Member Functions

 Nullable ()
 Constructs an instance that represents null-value. More...
 
 Nullable (std::nullptr_t)
 Constructs an instance that represents null. More...
 
template<typename T1 >
 Nullable (const T1 &value)
 
template<typename T1 >
 Nullable (const Nullable< T1 > &value)
 
template<typename T1 , typename = typename std::enable_if<std::is_null_pointer<T1>::value>>
Nullable< T > operator= (std::nullptr_t)
 
template<typename T1 >
std::enable_if<!IsNullable< T1 >::value &&!std::is_null_pointer< T1 >::value, Nullable< T > & >::type operator= (const T1 &x)
 
template<typename T1 >
Nullable< T > & operator= (const Nullable< T1 > &x)
 
get_Value () const
 
bool get_HasValue () const
 
 operator const T & () const
 
void reset ()
 Sets the currently represented value to null. More...
 
bool IsNull () const
 
template<typename T1 >
bool NullableBoolHelper (const T1 &other, const std::function< bool()> &f, bool default_if_both_are_null=false) const
 
bool operator== (std::nullptr_t) const
 
template<typename T1 >
std::enable_if<!IsNullable< T1 >::value, bool >::type operator== (const T1 &other) const
 
template<typename T1 >
bool operator== (const Nullable< T1 > &other) const
 
template<typename T1 >
std::enable_if< IsNullable< T1 >::value, bool >::type Equals (const T1 &other) const
 
bool operator!= (std::nullptr_t) const
 
template<typename T1 >
std::enable_if<!IsNullable< T1 >::value, bool >::type operator!= (const T1 &other) const
 
template<typename T1 >
bool operator!= (const Nullable< T1 > &other) const
 
bool operator> (std::nullptr_t) const
 Always returns false. More...
 
template<typename T1 >
std::enable_if<!IsNullable< T1 >::value, bool >::type operator> (const T1 &other) const
 
template<typename T1 >
bool operator> (const Nullable< T1 > &other) const
 
bool operator< (std::nullptr_t) const
 Always returns false. More...
 
template<typename T1 >
std::enable_if<!IsNullable< T1 >::value, bool >::type operator< (const T1 &other) const
 
template<typename T1 >
bool operator< (const Nullable< T1 > &other) const
 
bool operator>= (std::nullptr_t) const
 
template<typename T1 >
std::enable_if<!IsNullable< T1 >::value, bool >::type operator>= (const T1 &other) const
 
template<typename T1 >
bool operator>= (const Nullable< T1 > &other) const
 
bool operator<= (std::nullptr_t) const
 Always returns false. More...
 
template<typename T1 >
std::enable_if<!IsNullable< T1 >::value, bool >::type operator<= (const T1 &other) const
 
template<typename T1 >
bool operator<= (const Nullable< T1 > &other) const
 
Nullable< T > operator+ (std::nullptr_t) const
 Returns a default constructed instance of Nullable<T> class. More...
 
template<typename T1 , typename = typename std::enable_if<!IsNullable<T1>::value, int>::type>
auto operator+ (const T1 &other) const -> Nullable< decltype(get_Value()+other)>
 
template<typename T1 >
auto operator+ (const Nullable< T1 > &other) const -> System::Nullable< decltype(get_Value()+other.get_Value())>
 
template<typename T1 , typename = typename std::enable_if<std::is_null_pointer<T1>::value>::type>
Nullable< T > operator- (T1) const
 
template<typename T1 , typename = typename std::enable_if<!IsNullable<T1>::value, int>::type>
auto operator- (const T1 &other) const -> Nullable< decltype(get_Value() - other)>
 
template<typename T1 >
auto operator- (const Nullable< T1 > &other) const -> System::Nullable< decltype(get_Value() - other.get_Value())>
 
Nullable< T > operator+= (std::nullptr_t)
 
template<typename T1 >
std::enable_if<!IsNullable< T1 >::value, Nullable< T > >::type operator+= (const T1 &other)
 
template<typename T1 >
Nullable< T > operator+= (const Nullable< T1 > &other)
 
template<typename T1 , typename = typename std::enable_if<std::is_null_pointer<T1>::value>::type>
Nullable< T > operator-= (T1)
 Returns an instance of Nullable class that represents a null-value. More...
 
template<typename T1 , typename = typename std::enable_if<!std::is_null_pointer<T1>::value, T1>::type>
std::enable_if<!IsNullable< T1 >::value, Nullable< T > >::type operator-= (const T1 &other)
 
template<typename T1 = T>
std::enable_if< std::is_same< T1, bool >::value, Nullable< T > >::type operator|= (bool other)
 
template<typename T1 = T>
std::enable_if< std::is_same< T1, bool >::value, Nullable< T > >::type operator &= (bool other)
 
template<typename T1 >
Nullable< T > operator-= (const Nullable< T1 > &other)
 
int GetHashCode () const
 Returns a hash code for the current object. More...
 
String ToString () const
 
GetValueOrDefault (T default_value)
 
GetValueOrDefault ()
 

Detailed Description

template<typename T>
class System::Nullable< T >

Forward declaration.

Represents a value of the specified type that can be assigned null. This type should be allocated on stack and passed to functions by value or by reference. Never use System::SmartPtr class to manage objects of this type.

Template Parameters
TThe underlying value type which is extended by the Nullable class
#include "system/nullable.h"
#include <iostream>
int main()
{
using NullableInt = System::Nullable<int>;
// Create a lambda function that is used to print a value.
const auto printLambda = [](const NullableInt &nullable) -> void
{
if (nullable.get_HasValue())
{
std::cout << nullable.get_Value() << std::endl;
}
else
{
std::cout << "null" << std::endl;
}
};
// Create an instance of Nullable that doesn't contain any value and try to print it using the created lambda.
NullableInt nullable;
printLambda(nullable);
// Assign the value to the instance of Nullable and print it using the created lambda.
nullable = 15;
printLambda(nullable);
return 0;
}
/*
* This code example produces the following output:
* null
* 15
*/

Member Typedef Documentation

◆ ValueType

template<typename T>
typedef T System::Nullable< T >::ValueType

An alias for a type of the value represented by this class.

Constructor & Destructor Documentation

◆ Nullable() [1/4]

template<typename T>
System::Nullable< T >::Nullable ( )
inline

Constructs an instance that represents null-value.

◆ Nullable() [2/4]

template<typename T>
System::Nullable< T >::Nullable ( std::nullptr_t  )
inline

Constructs an instance that represents null.

◆ Nullable() [3/4]

template<typename T>
template<typename T1 >
System::Nullable< T >::Nullable ( const T1 &  value)
inline

Constructs an instance of Nullable class that represents the specified value converted (if necessary) to the value of the underlying type T.

Parameters
valueA constant reference to the value to be represented by the newly constructed Nullable object
Template Parameters
T1The type of the specified value

◆ Nullable() [4/4]

template<typename T>
template<typename T1 >
System::Nullable< T >::Nullable ( const Nullable< T1 > &  value)
inline

Constructs an instance that represents a value that is represented by the specified Nullable object. The specified nullable object may represent a value of different type than the underlying type of the constructed instance in which case the represented value is converted to a value of type T.

Template Parameters
T1The type of the value represented by the specified Nullable object

Member Function Documentation

◆ Equals()

template<typename T>
template<typename T1 >
std::enable_if<IsNullable<T1>::value, bool>::type System::Nullable< T >::Equals ( const T1 &  other) const
inline

Determines if the value represented by the current object is equal to the value represented by the specified Nullable object.

Parameters
otherA constant reference to the Nullable object to compare with
Returns
True if the value represented by the current object is equal to the value represented by the specified Nullable object, otherwise - false
Template Parameters
T1The underlying type of the Nullable object to compare with

◆ get_HasValue()

template<typename T>
bool System::Nullable< T >::get_HasValue ( ) const
inline

Determines whether the current object represents any value.

Returns
True if the current object represents a value, otherwise - false

◆ get_Value()

template<typename T>
T System::Nullable< T >::get_Value ( ) const
inline

Returns a copy of the value represented by the current object.

Returns
The value represented by the current object
Exceptions
InvalidOperationExceptionIf the current object does not represent any value

◆ GetHashCode()

template<typename T>
int System::Nullable< T >::GetHashCode ( ) const
inline

Returns a hash code for the current object.

◆ GetValueOrDefault() [1/2]

template<typename T>
T System::Nullable< T >::GetValueOrDefault ( default_value)
inline

Returns the value represented by the current object or the specified value if the value represented by the current object is null.

Parameters
default_valueThe value returned by the method if the value represented by the current object is null
Returns
A copy of the value represented by the current object is it is not null, otherwise the specified value is returned

◆ GetValueOrDefault() [2/2]

template<typename T>
T System::Nullable< T >::GetValueOrDefault ( )
inline

◆ IsNull()

template<typename T>
bool System::Nullable< T >::IsNull ( ) const
inline

Determines if the current object represents a null-value.

Returns
True if the current object represents a null-value, otherwise - false

◆ NullableBoolHelper()

template<typename T>
template<typename T1 >
bool System::Nullable< T >::NullableBoolHelper ( const T1 &  other,
const std::function< bool()> &  f,
bool  default_if_both_are_null = false 
) const
inline

Helper function to check if this and other are both not nulls and call a lambda if so. Used in implementation.s

Template Parameters
T1Other nullable type.
Parameters
otherOther nullable value to compare to.
fLambda to call if both this and other are not nulls.
default_if_both_are_nullReturn value if both values are nulls.
Returns
false if either this or other is null; default_if_both_are_null if both are null; result of f call if both are not null.

◆ operator &=()

template<typename T>
template<typename T1 = T>
std::enable_if<std::is_same<T1, bool>::value, Nullable<T> >::type System::Nullable< T >::operator&= ( bool  other)
inline

Applies operator&=() to the value represented by the current object using the specified value as a right-side argument.

Parameters
otherA boolean value that is used as a right-side value of the operator&=() applied to the value represented by the current object.
Returns
A reference to the self.
Template Parameters
T1The template parameter to make SFINAE work.

◆ operator const T &()

template<typename T>
System::Nullable< T >::operator const T & ( ) const
inline

Returns a constant reference to the value represented by the current object.

Returns
A constant reference to the value represented by the current object
Exceptions
InvalidOperationExceptionIf the current object does not represent any value

◆ operator!=() [1/3]

template<typename T>
bool System::Nullable< T >::operator!= ( std::nullptr_t  ) const
inline

Determines if the value represented by the current object is not null.

Returns
True if the value represented by the current object is not null, otherwise - false

◆ operator!=() [2/3]

template<typename T>
template<typename T1 >
std::enable_if<!IsNullable<T1>::value, bool>::type System::Nullable< T >::operator!= ( const T1 &  other) const
inline

Determines if the value represented by the current object is not equal to the specified value.

Parameters
otherA constant reference to the value to compare with
Returns
True if the value represented by the current object is not equal to the specified value, otherwise - false
Template Parameters
T1The type of the value to compare with

◆ operator!=() [3/3]

template<typename T>
template<typename T1 >
bool System::Nullable< T >::operator!= ( const Nullable< T1 > &  other) const
inline

Determines if the value represented by the current object is not equal to the value represented by the specified Nullable object.

Parameters
otherA constant reference to the Nullable object to compare with
Returns
True if the value represented by the current object is not equal to the value represented by the specified Nullable object, otherwise - false
Template Parameters
T1The underlying type of the Nullable object to compare with

◆ operator+() [1/3]

template<typename T>
Nullable<T> System::Nullable< T >::operator+ ( std::nullptr_t  ) const
inline

Returns a default constructed instance of Nullable<T> class.

◆ operator+() [2/3]

template<typename T>
template<typename T1 , typename = typename std::enable_if<!IsNullable<T1>::value, int>::type>
auto System::Nullable< T >::operator+ ( const T1 &  other) const -> Nullable<decltype(get_Value() + other)>
inline

Sums nullable and non-nullable values.

Template Parameters
T1Right operand type.
Parameters
othervalue to add.
Returns
Summing result.

◆ operator+() [3/3]

template<typename T>
template<typename T1 >
auto System::Nullable< T >::operator+ ( const Nullable< T1 > &  other) const -> System::Nullable<decltype(get_Value() + other.get_Value())>
inline

Sums nullable values.

Template Parameters
T1Right operand type.
Parameters
othervalue to add.
Returns
Summing result.

◆ operator+=() [1/3]

template<typename T>
Nullable<T> System::Nullable< T >::operator+= ( std::nullptr_t  )
inline

Resets the current object so that it represents a null-value.

Returns
A copy of the self

◆ operator+=() [2/3]

template<typename T>
template<typename T1 >
std::enable_if<!IsNullable<T1>::value, Nullable<T> >::type System::Nullable< T >::operator+= ( const T1 &  other)
inline

Applies operator+=() to the value represented by the current object using the specified value as a right-side argument.

Parameters
otherA constant reference to the value that is used as a right-side value of the operator+=() applied to the value represented by the current object.
Returns
A reference to the self
Template Parameters
T1The type of the value used as the right-side value of operator+=()

◆ operator+=() [3/3]

template<typename T>
template<typename T1 >
Nullable<T> System::Nullable< T >::operator+= ( const Nullable< T1 > &  other)
inline

Applies operator+=() to the value represented by the current object using the value represented by the specified Nullable object as the right-side argument.

Parameters
otherA constant reference to Nullable object the value represented by which is used as a right-side argument of the operator+=() applied to the value represented by the current object.
Returns
A reference to the self
Template Parameters
T1The underlying type of a Nullable object the value represented by which is used as the right-side argument of operator+=()

◆ operator-() [1/3]

template<typename T>
template<typename T1 , typename = typename std::enable_if<std::is_null_pointer<T1>::value>::type>
Nullable<T> System::Nullable< T >::operator- ( T1  ) const
inline

Subtracts nullable and null-pointed values.

Template Parameters
T1Right operand type, should be nullptr_t.
Returns
Empty Nullable object.

◆ operator-() [2/3]

template<typename T>
template<typename T1 , typename = typename std::enable_if<!IsNullable<T1>::value, int>::type>
auto System::Nullable< T >::operator- ( const T1 &  other) const -> Nullable<decltype(get_Value() - other)>
inline

Subtracts nullable and non-nullable values.

Template Parameters
T1Right operand type.
Parameters
othervalue to subtract.
Returns
Subtraction result.

◆ operator-() [3/3]

template<typename T>
template<typename T1 >
auto System::Nullable< T >::operator- ( const Nullable< T1 > &  other) const -> System::Nullable<decltype(get_Value() - other.get_Value())>
inline

Subtracts nullable values.

Template Parameters
T1Right operand type.
Parameters
othervalue to subtract.
Returns
Subtraction result.

◆ operator-=() [1/3]

template<typename T>
template<typename T1 , typename = typename std::enable_if<std::is_null_pointer<T1>::value>::type>
Nullable<T> System::Nullable< T >::operator-= ( T1  )
inline

Returns an instance of Nullable class that represents a null-value.

◆ operator-=() [2/3]

template<typename T>
template<typename T1 , typename = typename std::enable_if<!std::is_null_pointer<T1>::value, T1>::type>
std::enable_if<!IsNullable<T1>::value, Nullable<T> >::type System::Nullable< T >::operator-= ( const T1 &  other)
inline

Applies operator-=() to the value represented by the current object using the specified value as a right-side argument.

Parameters
otherA constant reference to the value that is used as a right-side value of the operator-=() applied to the value represented by the current object.
Returns
A reference to the self
Template Parameters
T1The type of the value used as the right-side value of operator-=()

◆ operator-=() [3/3]

template<typename T>
template<typename T1 >
Nullable<T> System::Nullable< T >::operator-= ( const Nullable< T1 > &  other)
inline

Applies operator-=() to the value represented by the current object using the value represented by the specified Nullable object as the right-side argument.

Parameters
otherA constant reference to Nullable object the value represented by which is used as a right-side argument of the operator-=() applied to the value represented by the current object.
Returns
A reference to the self
Template Parameters
T1The underlying type of a Nullable object the value represented by which is used as the right-side argument of operator-=()

◆ operator<() [1/3]

template<typename T>
bool System::Nullable< T >::operator< ( std::nullptr_t  ) const
inline

Always returns false.

◆ operator<() [2/3]

template<typename T>
template<typename T1 >
std::enable_if<!IsNullable<T1>::value, bool>::type System::Nullable< T >::operator< ( const T1 &  other) const
inline

Determines if the value represented by the current object is less than the specified value by applying operator<() to these values.

Parameters
otherA constant reference to the value to compare with
Returns
True if the value represented by the current object is less than the specified value, otherwise - false
Template Parameters
T1The type of the value to compare with

◆ operator<() [3/3]

template<typename T>
template<typename T1 >
bool System::Nullable< T >::operator< ( const Nullable< T1 > &  other) const
inline

Determines if the value represented by the current object is less than the value represented by the specified Nullable object by applying operator<() to these values.

Parameters
otherA constant reference to the Nullable object to compare with
Returns
True if the value represented by the current object is less than the value represented by the specified Nullable object, otherwise - false
Template Parameters
T1The underlying type of the Nullable object to compare with

◆ operator<=() [1/3]

template<typename T>
bool System::Nullable< T >::operator<= ( std::nullptr_t  ) const
inline

Always returns false.

◆ operator<=() [2/3]

template<typename T>
template<typename T1 >
std::enable_if<!IsNullable<T1>::value, bool>::type System::Nullable< T >::operator<= ( const T1 &  other) const
inline

Determines if the value represented by the current object is less or equal to the specified value by applying operator<=() to these values.

Parameters
otherA constant reference to the value to compare with
Returns
True if the value represented by the current object is less or equal to the specified value, otherwise - false
Template Parameters
T1The type of the value to compare with

◆ operator<=() [3/3]

template<typename T>
template<typename T1 >
bool System::Nullable< T >::operator<= ( const Nullable< T1 > &  other) const
inline

Determines if the value represented by the current object is less or equal to the value represented by the specified Nullable object by applying operator<=() to these values.

Parameters
otherA constant reference to the Nullable object to compare with
Returns
True if the value represented by the current object is less or equal to the value represented by the specified Nullable object, otherwise - false
Template Parameters
T1The underlying type of the Nullable object to compare with

◆ operator=() [1/3]

template<typename T>
template<typename T1 , typename = typename std::enable_if<std::is_null_pointer<T1>::value>>
Nullable<T> System::Nullable< T >::operator= ( std::nullptr_t  )
inline

Assigns a null to the current object.

Returns
A Nullable object that represents null-value.

◆ operator=() [2/3]

template<typename T>
template<typename T1 >
std::enable_if<!IsNullable<T1>::value && !std::is_null_pointer<T1>::value, Nullable<T>&>::type System::Nullable< T >::operator= ( const T1 &  x)
inline

Replaces the object's currently represented value with the specified one.

Parameters
xThe new value to be represented by the current object
Returns
A reference to the self
Template Parameters
Thetype of the new value to be represented by the current object

◆ operator=() [3/3]

template<typename T>
template<typename T1 >
Nullable<T>& System::Nullable< T >::operator= ( const Nullable< T1 > &  x)
inline

Replaces the object's currently represented value with the specified one.

Parameters
xThe new value to be represented by the current object
Returns
A reference to the self
Template Parameters
Thetype of the new value to be represented by the current object

◆ operator==() [1/3]

template<typename T>
bool System::Nullable< T >::operator== ( std::nullptr_t  ) const
inline

Determines if the value represented by the current object is null.

Returns
True if the value represented by the current object is null, otherwise - false

◆ operator==() [2/3]

template<typename T>
template<typename T1 >
std::enable_if<!IsNullable<T1>::value, bool>::type System::Nullable< T >::operator== ( const T1 &  other) const
inline

Determines if the value represented by the current object is equal to the specified value.

Parameters
otherA constant reference to the value to compare with
Returns
True if the value represented by the current object is equal to the specified value, otherwise - false
Template Parameters
T1The type of the value to compare with

◆ operator==() [3/3]

template<typename T>
template<typename T1 >
bool System::Nullable< T >::operator== ( const Nullable< T1 > &  other) const
inline

Determines if the value represented by the current object is equal to the value represented by the specified Nullable object.

Parameters
otherA constant reference to the Nullable object to compare with
Returns
True if the value represented by the current object is equal to the value represented by the specified Nullable object, otherwise - false
Template Parameters
T1The underlying type of the Nullable object to compare with

◆ operator>() [1/3]

template<typename T>
bool System::Nullable< T >::operator> ( std::nullptr_t  ) const
inline

Always returns false.

◆ operator>() [2/3]

template<typename T>
template<typename T1 >
std::enable_if<!IsNullable<T1>::value, bool>::type System::Nullable< T >::operator> ( const T1 &  other) const
inline

Determines if the value represented by the current object is greater than the specified value by applying operator>() to these values.

Parameters
otherA constant reference to the value to compare with
Returns
True if the value represented by the current object is greater than the specified value, otherwise - false
Template Parameters
T1The type of the value to compare with

◆ operator>() [3/3]

template<typename T>
template<typename T1 >
bool System::Nullable< T >::operator> ( const Nullable< T1 > &  other) const
inline

Determines if the value represented by the current object is greater than the value represented by the specified Nullable object by applying operator>() to these values.

Parameters
otherA constant reference to the Nullable object to compare with
Returns
True if the value represented by the current object is greater than the value represented by the specified Nullable object, otherwise - false
Template Parameters
T1The underlying type of the Nullable object to compare with

◆ operator>=() [1/3]

template<typename T>
bool System::Nullable< T >::operator>= ( std::nullptr_t  ) const
inline

Always returns false.

Returns
Always - false

◆ operator>=() [2/3]

template<typename T>
template<typename T1 >
std::enable_if<!IsNullable<T1>::value, bool>::type System::Nullable< T >::operator>= ( const T1 &  other) const
inline

Determines if the value represented by the current object is greater or equal to the value represented by the specified object by applying operator>=() to these values.

Parameters
otherA constant reference to an object to compare the current object with
Returns
True if the value represented by the current object is greater or equal to the value represented by the specified object, otherwise - false
Template Parameters
T1The underlying type of the value to compare the value represented by the current object with

◆ operator>=() [3/3]

template<typename T>
template<typename T1 >
bool System::Nullable< T >::operator>= ( const Nullable< T1 > &  other) const
inline

Determines if the value represented by the current object is greater or equal to the value represented by the specified Nullable object by applying operator>=() to these values.

Parameters
otherA constant reference to the Nullable object to compare with
Returns
True if the value represented by the current object is greater or equal to the value represented by the specified Nullable object, otherwise - false
Template Parameters
T1The underlying type of the Nullable object to compare with

◆ operator|=()

template<typename T>
template<typename T1 = T>
std::enable_if<std::is_same<T1, bool>::value, Nullable<T> >::type System::Nullable< T >::operator|= ( bool  other)
inline

Applies operator|=() to the value represented by the current object using the specified value as a right-side argument.

Parameters
otherA boolean value that is used as a right-side value of the operator|=() applied to the value represented by the current object.
Returns
A reference to the self.
Template Parameters
T1The template parameter to make SFINAE work.

◆ reset()

template<typename T>
void System::Nullable< T >::reset ( )
inline

Sets the currently represented value to null.

◆ ToString()

template<typename T>
String System::Nullable< T >::ToString ( ) const
inline

Converts the value represented by the current object to string.

Returns
A string representation of the value represented by the current object or an empty string if the value represented by the current object is null