2 // Copyright (C) 2008-2018 Lorenzo Caminiti
3 // Distributed under the Boost Software License, Version 1.0 (see accompanying
4 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
5 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
7 //[mitchell02_observer_main
8 #include "observer/observer.hpp"
9 #include "observer/subject.hpp"
10 #include <boost/contract.hpp>
13 int test_state
; // For testing only.
15 // Implement an actual subject.
16 class concrete_subject
17 #define BASES public subject
20 friend class boost::contract::access
;
22 typedef BOOST_CONTRACT_BASE_TYPES(BASES
) base_types
; // Subcontracting.
26 typedef int state
; // Some state being observed.
28 concrete_subject() : state_() {
29 // Could have omitted contracts here (nothing to check).
30 boost::contract::check c
= boost::contract::constructor(this);
33 virtual ~concrete_subject() {
34 // Could have omitted contracts here (nothing to check).
35 boost::contract::check c
= boost::contract::destructor(this);
38 void set_state(state
const& new_state
) {
39 // Could have omitted contracts here (nothing to check).
40 boost::contract::check c
= boost::contract::public_function(this);
43 assert(state_
== test_state
);
44 notify(); // Notify all observers.
47 state
get_state() const {
48 // Could have omitted contracts here (nothing to check).
49 boost::contract::check c
= boost::contract::public_function(this);
57 // Implement an actual observer.
58 class concrete_observer
59 #define BASES public observer
62 friend class boost::contract::access
;
64 typedef BOOST_CONTRACT_BASE_TYPES(BASES
) base_types
; // Subcontracting.
67 BOOST_CONTRACT_OVERRIDES(up_to_date_with_subject
, update
)
70 // Create concrete observer.
71 explicit concrete_observer(concrete_subject
const& subj
) :
72 subject_(subj
), observed_state_() {
73 // Could have omitted contracts here (nothing to check).
74 boost::contract::check c
= boost::contract::constructor(this);
77 virtual ~concrete_observer() {
78 // Could have omitted contracts here (nothing to check).
79 boost::contract::check c
= boost::contract::destructor(this);
82 // Implement base virtual functions.
84 bool up_to_date_with_subject(boost::contract::virtual_
* v
= 0)
85 const /* override */ {
87 boost::contract::check c
= boost::contract::public_function
<
88 override_up_to_date_with_subject
89 >(v
, result
, &concrete_observer::up_to_date_with_subject
, this);
91 return result
= true; // For simplicity, assume always up-to-date.
94 void update(boost::contract::virtual_
* v
= 0) /* override */ {
95 boost::contract::check c
= boost::contract::public_function
<
96 override_update
>(v
, &concrete_observer::update
, this);
98 observed_state_
= subject_
.get_state();
99 assert(observed_state_
== test_state
);
103 concrete_subject
const& subject_
;
104 concrete_subject::state observed_state_
;
108 concrete_subject subj
;
109 concrete_observer
ob(subj
);
112 subj
.set_state(test_state
= 123);
113 subj
.set_state(test_state
= 456);