Added utility method getOr(T&) in eoOptional
This commit is contained in:
parent
0e08d2f5bb
commit
290ff1cfd1
1 changed files with 45 additions and 40 deletions
|
|
@ -38,7 +38,7 @@ struct MyClass {
|
||||||
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:
|
||||||
|
|
@ -51,7 +51,7 @@ struct MyClass {
|
||||||
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:
|
||||||
|
|
@ -84,7 +84,7 @@ public:
|
||||||
: _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)
|
||||||
{ }
|
{ }
|
||||||
|
|
@ -101,6 +101,11 @@ public:
|
||||||
return *_val;
|
return *_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T& getOr (T& default) const
|
||||||
|
{
|
||||||
|
return hasValue()? *_val: default;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
eoOptional ()
|
eoOptional ()
|
||||||
: _val(NULL)
|
: _val(NULL)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue