2013-05-05 18:18:41 +02:00
|
|
|
|
|
|
|
namespace System
|
|
|
|
{
|
|
|
|
// Represents an object whose underlying type is a value type that can also be assigned null like a reference type.
|
|
|
|
template <typename T>
|
|
|
|
class Nullable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
T* data;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static const Nullable<T> Null;
|
|
|
|
|
|
|
|
Nullable()
|
|
|
|
: data(null)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Nullable(const T& newData)
|
|
|
|
: data(const_cast<T*>(&newData))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-03 22:44:44 +02:00
|
|
|
Nullable(T const * const newData)
|
|
|
|
: data(const_cast<T*>(newData))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-05 18:18:41 +02:00
|
|
|
Nullable(const Nullable<T> &obj)
|
|
|
|
: data(obj.data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasValue() const { return (data != null); }
|
|
|
|
T getValue() const { return *data; }
|
|
|
|
|
|
|
|
operator T() const { return *data; }
|
2013-06-01 16:04:30 +02:00
|
|
|
|
|
|
|
Nullable<T>& operator =(const T * newVal)
|
|
|
|
{
|
|
|
|
data = newVal;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Nullable<T>& operator =(const Nullable<T>& right)
|
|
|
|
{
|
|
|
|
if (right == *this)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
*data = *right.data;
|
|
|
|
end:
|
|
|
|
return *this;
|
|
|
|
}
|
2013-05-05 18:18:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
const Nullable<T> Nullable<T>::Null = Nullable<T>();
|
|
|
|
}
|