1 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 6 7 //[mitchell02_counter_main 8 #include "counter/counter.hpp" 9 #include "counter/decrement_button.hpp" 10 #include "observer/observer.hpp" 11 #include <cassert> 12 13 int test_counter; 14 15 class view_of_counter 16 #define BASES public observer 17 : BASES 18 { 19 friend class boost::contract::access; 20 21 typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; 22 #undef BASES 23 24 BOOST_CONTRACT_OVERRIDES(up_to_date_with_subject, update) 25 26 public: 27 /* Creation */ 28 29 // Create view associated with given counter. view_of_counter(counter & a_counter)30 explicit view_of_counter(counter& a_counter) : counter_(a_counter) { 31 // Could have omitted contracts here (nothing to check). 32 boost::contract::check c = boost::contract::constructor(this); 33 34 counter_.attach(this); 35 assert(counter_.value() == test_counter); 36 } 37 38 // Destroy view. ~view_of_counter()39 virtual ~view_of_counter() { 40 // Could have omitted contracts here (nothing to check). 41 boost::contract::check c = boost::contract::destructor(this); 42 } 43 44 /* Commands */ 45 up_to_date_with_subject(boost::contract::virtual_ * v=0) const46 virtual bool up_to_date_with_subject(boost::contract::virtual_* v = 0) 47 const /* override */ { 48 bool result; 49 boost::contract::check c = boost::contract::public_function< 50 override_up_to_date_with_subject 51 >(v, result, &view_of_counter::up_to_date_with_subject, this); 52 53 return result = true; // For simplicity, assume always up-to-date. 54 } 55 update(boost::contract::virtual_ * v=0)56 virtual void update(boost::contract::virtual_* v = 0) /* override */ { 57 boost::contract::check c = boost::contract::public_function< 58 override_update>(v, &view_of_counter::update, this); 59 60 assert(counter_.value() == test_counter); 61 } 62 63 private: 64 counter& counter_; 65 }; 66 main()67int main() { 68 counter cnt(test_counter = 1); 69 view_of_counter view(cnt); 70 71 decrement_button dec(cnt); 72 assert(dec.enabled()); 73 74 test_counter--; 75 dec.on_bn_clicked(); 76 assert(!dec.enabled()); 77 78 return 0; 79 } 80 //] 81 82