wibble 1.1
amorph.test.h
Go to the documentation of this file.
1// -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
2
3#include <wibble/amorph.h>
4
5namespace {
6
7using namespace wibble;
8
9struct TInterface {
10 virtual int value() = 0;
11};
12
13template< typename W >
14struct TMorph : Morph< TMorph< W >, W, TInterface >
15{
16 TMorph() {}
17 TMorph( const W &w ) : Morph< TMorph, W, TInterface >( w ) {}
18
19 virtual int value() { return this->wrapped().value(); }
20};
21
22struct T : Amorph< T, TInterface >
23{
25 : Amorph< T, TInterface >( i ) {}
26 T() {}
27
28 int value() {
29 return this->implementation()->value();
30 }
31};
32
33struct T1 : VirtualBase {
34 virtual int value() const { return 1; }
35 bool operator<=( const T1 &o ) const {
36 return value() <= o.value();
37 }
38
39};
40
41struct T3 : T1 {
42 virtual int value() const { return 3; }
43};
44
45struct T2 : VirtualBase {
46 int value() const { return 2; }
47 bool operator<=( const T2 &o ) const {
48 return value() <= o.value();
49 }
50};
51
52struct ExtractT1Value {
53 typedef int result_type;
54 typedef T1 argument_type;
55 int operator()( const T1 &t ) {
56 return t.value();
57 }
58};
59
60template< typename T >
62 return TMorph< T >( t );
63}
64
65struct TestAmorph {
66 Test basic()
67 {
68 T1 t1;
69 T2 t2;
70 T3 t3;
71 T t = testMorph( t1 );
72 assert_eq( t.value(), 1 );
73 assert_eq( t.ifType( ExtractT1Value() ), Maybe< int >::Just( 1 ) );
74 t = testMorph( t2 );
75 assert_eq( t.value(), 2 );
76 assert_eq( t.ifType( ExtractT1Value() ), Maybe< int >::Nothing() );
77 t = testMorph( t3 );
78 assert_eq( t.value(), 3 );
79 assert_eq( t.ifType( ExtractT1Value() ), Maybe< int >::Just( 3 ) );
80 }
81};
82
83}
-*- C++ -*-
Definition amorph.h:17
Definition amorph.h:272
const Interface * implementation() const
Definition amorph.h:361
Definition maybe.h:29
Definition amorph.h:144
const Wrapped & wrapped() const
Definition amorph.h:181
Definition amorph.h:30
Definition amorph.h:83
#define assert_eq(x, y)
Definition test.h:33