added a test and a method

This commit is contained in:
LPTK 2013-06-12 15:44:59 +02:00
commit 95a6e7a231

View file

@ -32,26 +32,26 @@ For example, this is not valid C++98 code:
\code \code
struct MyClass { struct MyClass {
MyClass(T& my_T = default_T) MyClass(T& my_T = default_T)
: actual_T(my_T) : actual_T(my_T)
{ } { }
private: private:
T default_T; T default_T;
T& actual_T; T& actual_T;
} };
\endcode \endcode
This is the same code using eoOptional, which is valid: This is the same code using eoOptional, which is valid:
\code \code
struct MyClass { struct MyClass {
MyClass(eoOptional<T> my_T = NULL) MyClass(eoOptional<T> my_T = NULL)
: actual_T(my_T.getOr(default_T)) : actual_T(my_T.getOr(default_T))
{ } { }
private: private:
T default_T; T default_T;
T& actual_T; T& actual_T;
} };
\endcode \endcode
And from the point of view of the user, it is transparent: And from the point of view of the user, it is transparent:
@ -78,36 +78,41 @@ MyClass mc3(t);
template< class T > template< class T >
class eoOptional { class eoOptional {
public: public:
static const eoOptional<T> null; // = eoOptional<T>(); static const eoOptional<T> null; // = eoOptional<T>();
eoOptional (T& init) eoOptional (T& init)
: _val(&init) : _val(&init)
{ } { }
// used mainly for converting NULL to this // used mainly for converting NULL to this class
eoOptional (T* init) eoOptional (T* init)
: _val(init) : _val(init)
{ } { }
bool hasValue() const bool hasValue() const
{ {
return _val != NULL; return _val != NULL;
} }
T& get () const T& get () const
{ {
if (!hasValue()) if (!hasValue())
throw std::runtime_error("Cannot get a reference from a eoOptional wrapper with no value"); throw std::runtime_error("Cannot get a reference from a eoOptional wrapper with no value");
return *_val; return *_val;
} }
T& getOr (T& default) const
{
return hasValue()? *_val: default;
}
protected: protected:
eoOptional () eoOptional ()
: _val(NULL) : _val(NULL)
{ } { }
private: private:
T* _val; T* _val;
}; };
template< class T > template< class T >