counter_main.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (C) 2008-2018 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  3. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  4. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  5. //[mitchell02_counter_main
  6. #include "counter/counter.hpp"
  7. #include "counter/decrement_button.hpp"
  8. #include "observer/observer.hpp"
  9. #include <cassert>
  10. int test_counter;
  11. class view_of_counter
  12. #define BASES public observer
  13. : BASES
  14. {
  15. friend class boost::contract::access;
  16. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  17. #undef BASES
  18. BOOST_CONTRACT_OVERRIDES(up_to_date_with_subject, update)
  19. public:
  20. /* Creation */
  21. // Create view associated with given counter.
  22. explicit view_of_counter(counter& a_counter) : counter_(a_counter) {
  23. // Could have omitted contracts here (nothing to check).
  24. boost::contract::check c = boost::contract::constructor(this);
  25. counter_.attach(this);
  26. assert(counter_.value() == test_counter);
  27. }
  28. // Destroy view.
  29. virtual ~view_of_counter() {
  30. // Could have omitted contracts here (nothing to check).
  31. boost::contract::check c = boost::contract::destructor(this);
  32. }
  33. /* Commands */
  34. virtual bool up_to_date_with_subject(boost::contract::virtual_* v = 0)
  35. const /* override */ {
  36. bool result;
  37. boost::contract::check c = boost::contract::public_function<
  38. override_up_to_date_with_subject
  39. >(v, result, &view_of_counter::up_to_date_with_subject, this);
  40. return result = true; // For simplicity, assume always up-to-date.
  41. }
  42. virtual void update(boost::contract::virtual_* v = 0) /* override */ {
  43. boost::contract::check c = boost::contract::public_function<
  44. override_update>(v, &view_of_counter::update, this);
  45. assert(counter_.value() == test_counter);
  46. }
  47. private:
  48. counter& counter_;
  49. };
  50. int main() {
  51. counter cnt(test_counter = 1);
  52. view_of_counter view(cnt);
  53. decrement_button dec(cnt);
  54. assert(dec.enabled());
  55. test_counter--;
  56. dec.on_bn_clicked();
  57. assert(!dec.enabled());
  58. return 0;
  59. }
  60. //]