File: amorph.test.h

package info (click to toggle)
libwibble 0.1.19
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 832 kB
  • ctags: 1,940
  • sloc: cpp: 9,798; makefile: 163; perl: 84; sh: 11
file content (83 lines) | stat: -rw-r--r-- 1,753 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// -*- 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 ) );
    }
};

}