amorph.test.h
Go to the documentation of this file.
// -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
#include <wibble/amorph.h>
namespace {
using namespace wibble;
struct TInterface {
virtual int value() = 0;
};
template< typename W >
struct TMorph : Morph< TMorph< W >, W, TInterface >
{
TMorph() {}
TMorph( const W &w ) : Morph< TMorph, W, TInterface >( w ) {}
virtual int value() { return this->wrapped().value(); }
};
struct T : Amorph< T, TInterface >
{
T( const MorphInterface< TInterface > &i )
: Amorph< T, TInterface >( i ) {}
T() {}
int value() {
return this->implementation()->value();
}
};
struct T1 : VirtualBase {
virtual int value() const { return 1; }
bool operator<=( const T1 &o ) const {
return value() <= o.value();
}
};
struct T3 : T1 {
virtual int value() const { return 3; }
};
struct T2 : VirtualBase {
int value() const { return 2; }
bool operator<=( const T2 &o ) const {
return value() <= o.value();
}
};
struct ExtractT1Value {
typedef int result_type;
typedef T1 argument_type;
int operator()( const T1 &t ) {
return t.value();
}
};
template< typename T >
TMorph< T > testMorph( T t ) {
return TMorph< T >( t );
}
struct TestAmorph {
Test basic()
{
T1 t1;
T2 t2;
T3 t3;
T t = testMorph( t1 );
assert_eq( t.value(), 1 );
assert_eq( t.ifType( ExtractT1Value() ), Maybe< int >::Just( 1 ) );
t = testMorph( t2 );
assert_eq( t.value(), 2 );
assert_eq( t.ifType( ExtractT1Value() ), Maybe< int >::Nothing() );
t = testMorph( t3 );
assert_eq( t.value(), 3 );
assert_eq( t.ifType( ExtractT1Value() ), Maybe< int >::Just( 3 ) );
}
};
}
Test
void Test
Definition: test.h:178
wibble::Maybe::Just
static Maybe Just(const T &t)
Definition: maybe.h:36
amorph.h
-*- C++ -*-
assert_eq
#define assert_eq(x, y)
Definition: test.h:33
wibble::Amorph
Definition: amorph.h:272
wibble::MorphInterface
An interface implemented by all morph classes.
Definition: amorph.h:91
wibble::operators::operator<=
bool operator<=(const T &a, const std::set< T > &b)
Definition: operators.h:220
wibble::Maybe::Nothing
static Maybe Nothing(const T &df=T())
Definition: maybe.h:37
wibble
Definition: amorph.h:17
wibble::VirtualBase
Definition: amorph.h:83
wibble::Morph
Definition: amorph.h:141