summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/contract/example/mitchell02/observer_main.cpp
blob: 0f6c1a5b8441dee68ebf90f8b13ae3f577aaa524 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (C) 2008-2018 Lorenzo Caminiti
// Distributed under the Boost Software License, Version 1.0 (see accompanying
// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html

//[mitchell02_observer_main
#include "observer/observer.hpp"
#include "observer/subject.hpp"
#include <boost/contract.hpp>
#include <cassert>

int test_state; // For testing only.

// Implement an actual subject.
class concrete_subject
    #define BASES public subject
    : BASES
{
    friend class boost::contract::access;

    typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; // Subcontracting.
    #undef BASES

public:
    typedef int state; // Some state being observed.

    concrete_subject() : state_() {
        // Could have omitted contracts here (nothing to check).
        boost::contract::check c = boost::contract::constructor(this);
    }

    virtual ~concrete_subject() {
        // Could have omitted contracts here (nothing to check).
        boost::contract::check c = boost::contract::destructor(this);
    }

    void set_state(state const& new_state) {
        // Could have omitted contracts here (nothing to check).
        boost::contract::check c = boost::contract::public_function(this);

        state_ = new_state;
        assert(state_ == test_state);
        notify(); // Notify all observers.
    }

    state get_state() const {
        // Could have omitted contracts here (nothing to check).
        boost::contract::check c = boost::contract::public_function(this);
        return state_;
    }

private:
    state state_;
};

// Implement an actual observer.
class concrete_observer
    #define BASES public observer
    : BASES
{
    friend class boost::contract::access;

    typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; // Subcontracting.
    #undef BASES

    BOOST_CONTRACT_OVERRIDES(up_to_date_with_subject, update)

public:
    // Create concrete observer.
    explicit concrete_observer(concrete_subject const& subj) :
            subject_(subj), observed_state_() {
        // Could have omitted contracts here (nothing to check).
        boost::contract::check c = boost::contract::constructor(this);
    }

    virtual ~concrete_observer() {
        // Could have omitted contracts here (nothing to check).
        boost::contract::check c = boost::contract::destructor(this);
    }

    // Implement base virtual functions.

    bool up_to_date_with_subject(boost::contract::virtual_* v = 0)
            const /* override */ {
        bool result;
        boost::contract::check c = boost::contract::public_function<
            override_up_to_date_with_subject
        >(v, result, &concrete_observer::up_to_date_with_subject, this);

        return result = true; // For simplicity, assume always up-to-date.
    }

    void update(boost::contract::virtual_* v = 0) /* override */ {
        boost::contract::check c = boost::contract::public_function<
                override_update>(v, &concrete_observer::update, this);

        observed_state_ = subject_.get_state();
        assert(observed_state_ == test_state);
    }

private:
    concrete_subject const& subject_;
    concrete_subject::state observed_state_;
};

int main() {
    concrete_subject subj;
    concrete_observer ob(subj);
    subj.attach(&ob);

    subj.set_state(test_state = 123);
    subj.set_state(test_state = 456);

    return 0;
}
//]