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_decrement_button
8 #ifndef DECREMENT_BUTTON_HPP_
9 #define DECREMENT_BUTTON_HPP_
11 #include "push_button.hpp"
12 #include "counter.hpp"
13 #include "../observer/observer.hpp"
14 #include <boost/contract.hpp>
15 #include <boost/noncopyable.hpp>
17 class decrement_button
18 #define BASES public push_button, public observer, \
19 private boost::noncopyable
22 friend class boost::contract::access;
24 typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
27 BOOST_CONTRACT_OVERRIDES(on_bn_clicked, up_to_date_with_subject, update);
32 explicit decrement_button(counter& a_counter) : counter_(a_counter) {
33 boost::contract::check c = boost::contract::constructor(this)
35 // Enable iff positive value.
36 BOOST_CONTRACT_ASSERT(enabled() == (a_counter.value() > 0));
39 counter_.attach(this);
43 virtual ~decrement_button() {
44 // Could have omitted contracts here (nothing to check).
45 boost::contract::check c = boost::contract::destructor(this);
50 virtual void on_bn_clicked(boost::contract::virtual_* v = 0)
52 boost::contract::old_ptr<int> old_value =
53 BOOST_CONTRACT_OLDOF(v, counter_.value());
54 boost::contract::check c = boost::contract::public_function<
55 override_on_bn_clicked
56 >(v, &decrement_button::on_bn_clicked, this)
58 // Counter decremented.
59 BOOST_CONTRACT_ASSERT(counter_.value() == *old_value - 1);
65 virtual bool up_to_date_with_subject(boost::contract::virtual_* v = 0)
66 const /* override */ {
68 boost::contract::check c = boost::contract::public_function<
69 override_up_to_date_with_subject
70 >(v, result, &decrement_button::up_to_date_with_subject, this);
72 return result = true; // For simplicity, assume always up-to-date.
75 virtual void update(boost::contract::virtual_* v = 0) /* override */ {
76 boost::contract::check c = boost::contract::public_function<
77 override_update>(v, &decrement_button::update, this)
79 // Enabled iff positive value.
80 BOOST_CONTRACT_ASSERT(enabled() == (counter_.value() > 0));
84 if(counter_.value() == 0) disable();
92 #endif // #include guard