Last active
August 29, 2015 14:09
-
-
Save bekasov/1e30ba3c29388b4f1627 to your computer and use it in GitHub Desktop.
C++ Nullable Type Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef NULLABLETYPE_H | |
#define NULLABLETYPE_H | |
template <class T> | |
class NullableType | |
{ | |
protected: | |
T* value; | |
public: | |
NullableType<T>() : value(NULL) {}; | |
NullableType<T>(T value) | |
{ | |
this->value = new T(value); | |
}; | |
NullableType<T>(T* value) | |
{ | |
if (value != NULL) { | |
this->value = new T(*value); | |
} else { | |
this->value = NULL; | |
} | |
}; | |
NullableType<T>(const NullableType<T>& value) | |
{ | |
T* tmp = (T*)value; | |
if (tmp == NULL) { | |
this->value = NULL; | |
} else { | |
this->value = new T(*tmp); | |
} | |
}; | |
virtual ~NullableType<T>() | |
{ | |
if (this->value != NULL) { | |
delete this->value; | |
} | |
}; | |
/* | |
virtual operator T() | |
{ | |
if (this->value == NULL) { | |
T tmp; | |
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" | |
#pragma GCC diagnostic ignored "-Wuninitialized" | |
return tmp; | |
#pragma GCC diagnostic warning "-Wmaybe-uninitialized" | |
#pragma GCC diagnostic warning "-Wuninitialized" | |
//return 0; | |
} | |
return *this->value; | |
} | |
*/ | |
virtual operator T*() const | |
{ | |
return this->value; | |
} | |
virtual NullableType<T>& operator=(const T* const value) | |
{ | |
if (NULL == value) { | |
if (this->value != NULL) { | |
delete this->value; | |
} | |
this->value = NULL; | |
return *this; | |
} | |
if (this->value == NULL) { | |
this->value = new T(*value); | |
} else { | |
*this->value = *value; | |
} | |
return *this; | |
} | |
virtual NullableType<T>& operator=(const T& value) | |
{ | |
if (NULL == &value) { | |
if (this->value != NULL) { | |
delete this->value; | |
} | |
this->value = NULL; | |
return *this; | |
} | |
if (this->value == NULL) { | |
this->value = new T(value); | |
} else { | |
*this->value = value; | |
} | |
return *this; | |
} | |
virtual NullableType<T>& operator=(const NullableType<T>& value) | |
{ | |
if (this == &value) { | |
return *this; | |
} | |
T* tmp = (T*)value; | |
if (tmp == NULL) { | |
if (this->value != NULL) { | |
delete this->value; | |
} | |
this->value = NULL; | |
} else { | |
if (this->value == NULL) { | |
this->value = new T; | |
} | |
*this->value = *tmp; | |
} | |
return *this; | |
} | |
}; | |
#endif |
There is a issue:
For NullableString:
virtual operator T()
{
if (this->value == NULL) {
return 0;
}
...
Need to return the empty string - "".
If replace "return 0" to:
T tmp;
return tmp;
The compiler gives many warnings about unitialized variable, but all works well.
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#pragma GCC diagnostic ignored "-Wuninitialized"
return tmp;
#pragma GCC diagnostic warning "-Wmaybe-uninitialized"
#pragma GCC diagnostic warning "-Wuninitialized"
Try "return T();"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: