fixed test & impl

This commit is contained in:
LPTK 2013-06-12 15:59:07 +02:00
commit d317cc2ea7
3 changed files with 13 additions and 6 deletions

View file

@ -27,6 +27,7 @@ Lionel Parreaux <lionel.parreaux@gmail.com>
/** /**
A utility class for wrapping non-const references and use them as default arguments in functions. A utility class for wrapping non-const references and use them as default arguments in functions.
This especially is useful for avoiding constructor multiplication.
For example, this is not valid C++98 code: For example, this is not valid C++98 code:
@ -72,7 +73,9 @@ MyClass mc3(t);
#ifndef _EOOPTIONAL_H #ifndef _EOOPTIONAL_H
#define _EOOPTIONAL_H #define _EOOPTIONAL_H
#include <stdexcept>
#include <eoObject.h> #include <eoObject.h>
//#include <eoExceptions.h>
template< class T > template< class T >
@ -98,12 +101,13 @@ public:
{ {
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");
//throw eoEx;
return *_val; return *_val;
} }
T& getOr (T& default) const T& getOr (T& defaultValue) const
{ {
return hasValue()? *_val: default; return hasValue()? *_val: defaultValue;
} }
protected: protected:

View file

@ -69,6 +69,7 @@ set (TEST_LIST
#t-openmp # does not work anymore since functions used in this test were removed from EO #t-openmp # does not work anymore since functions used in this test were removed from EO
#t-eoDualFitness #t-eoDualFitness
t-eoParser t-eoParser
t-eoOptional
) )

View file

@ -6,12 +6,14 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
typedef T int; typedef int T;
struct MyClass { struct MyClass {
MyClass(eoOptional<T> my_T = NULL) MyClass(eoOptional<T> my_T = NULL)
: actual_T(my_T.getOr(default_T)) : default_T(42), actual_T(my_T.getOr(default_T))
{ } {
std::cout << "Value " << actual_T << " was used for construction" << std::endl;
}
private: private:
T default_T; T default_T;
T& actual_T; T& actual_T;
@ -22,6 +24,6 @@ int main(int ac, char** av)
// Three ways of using MyClass: // Three ways of using MyClass:
MyClass mc1; MyClass mc1;
MyClass mc2(NULL); MyClass mc2(NULL);
T t; T t(666);
MyClass mc3(t); MyClass mc3(t);
} }