decrement_button.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_decrement_button
  6. #ifndef DECREMENT_BUTTON_HPP_
  7. #define DECREMENT_BUTTON_HPP_
  8. #include "push_button.hpp"
  9. #include "counter.hpp"
  10. #include "../observer/observer.hpp"
  11. #include <boost/contract.hpp>
  12. #include <boost/noncopyable.hpp>
  13. class decrement_button
  14. #define BASES public push_button, public observer, \
  15. private boost::noncopyable
  16. : BASES
  17. {
  18. friend class boost::contract::access;
  19. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  20. #undef BASES
  21. BOOST_CONTRACT_OVERRIDES(on_bn_clicked, up_to_date_with_subject, update);
  22. public:
  23. /* Creation */
  24. explicit decrement_button(counter& a_counter) : counter_(a_counter) {
  25. boost::contract::check c = boost::contract::constructor(this)
  26. .postcondition([&] {
  27. // Enable iff positive value.
  28. BOOST_CONTRACT_ASSERT(enabled() == (a_counter.value() > 0));
  29. })
  30. ;
  31. counter_.attach(this);
  32. }
  33. // Destroy button.
  34. virtual ~decrement_button() {
  35. // Could have omitted contracts here (nothing to check).
  36. boost::contract::check c = boost::contract::destructor(this);
  37. }
  38. /* Commands */
  39. virtual void on_bn_clicked(boost::contract::virtual_* v = 0)
  40. /* override */ {
  41. boost::contract::old_ptr<int> old_value =
  42. BOOST_CONTRACT_OLDOF(v, counter_.value());
  43. boost::contract::check c = boost::contract::public_function<
  44. override_on_bn_clicked
  45. >(v, &decrement_button::on_bn_clicked, this)
  46. .postcondition([&] {
  47. // Counter decremented.
  48. BOOST_CONTRACT_ASSERT(counter_.value() == *old_value - 1);
  49. })
  50. ;
  51. counter_.decrement();
  52. }
  53. virtual bool up_to_date_with_subject(boost::contract::virtual_* v = 0)
  54. const /* override */ {
  55. bool result;
  56. boost::contract::check c = boost::contract::public_function<
  57. override_up_to_date_with_subject
  58. >(v, result, &decrement_button::up_to_date_with_subject, this);
  59. return result = true; // For simplicity, assume always up-to-date.
  60. }
  61. virtual void update(boost::contract::virtual_* v = 0) /* override */ {
  62. boost::contract::check c = boost::contract::public_function<
  63. override_update>(v, &decrement_button::update, this)
  64. .postcondition([&] {
  65. // Enabled iff positive value.
  66. BOOST_CONTRACT_ASSERT(enabled() == (counter_.value() > 0));
  67. })
  68. ;
  69. if(counter_.value() == 0) disable();
  70. else enable();
  71. }
  72. private:
  73. counter& counter_;
  74. };
  75. #endif // #include guard
  76. //]