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 
5 namespace {
6 
7 using namespace wibble;
8 
9 struct TInterface {
10  virtual int value() = 0;
11 };
12 
13 template< typename W >
14 struct 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 
22 struct T : Amorph< T, TInterface >
23 {
24  T( const MorphInterface< TInterface > &i )
25  : Amorph< T, TInterface >( i ) {}
26  T() {}
27 
28  int value() {
29  return this->implementation()->value();
30  }
31 };
32 
33 struct 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 
41 struct T3 : T1 {
42  virtual int value() const { return 3; }
43 };
44 
45 struct T2 : VirtualBase {
46  int value() const { return 2; }
47  bool operator<=( const T2 &o ) const {
48  return value() <= o.value();
49  }
50 };
51 
52 struct ExtractT1Value {
53  typedef int result_type;
54  typedef T1 argument_type;
55  int operator()( const T1 &t ) {
56  return t.value();
57  }
58 };
59 
60 template< typename T >
61 TMorph< T > testMorph( T t ) {
62  return TMorph< T >( t );
63 }
64 
65 struct 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 }