Added a test for eoOptional

This commit is contained in:
LPTK 2013-06-12 15:45:10 +02:00
commit 7a0b889c3d
3 changed files with 36 additions and 2 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.
This especially is useful for avoiding constructor multiplication.
For example, this is not valid C++98 code:
@ -72,7 +73,9 @@ MyClass mc3(t);
#ifndef _EOOPTIONAL_H
#define _EOOPTIONAL_H
#include <stdexcept>
#include <eoObject.h>
//#include <eoExceptions.h>
template< class T >
@ -98,12 +101,13 @@ public:
{
if (!hasValue())
throw std::runtime_error("Cannot get a reference from a eoOptional wrapper with no value");
//throw eoEx;
return *_val;
}
T& getOr (T& default) const
T& getOr (T& defaultValue) const
{
return hasValue()? *_val: default;
return hasValue()? *_val: defaultValue;
}
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-eoDualFitness
t-eoParser
t-eoOptional
)

29
eo/test/t-eoOptional.cpp Normal file
View file

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