push_button.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_push_button
  6. #ifndef PUSH_BUTTON_HPP_
  7. #define PUSH_BUTTON_HPP_
  8. #include <boost/contract.hpp>
  9. #include <cassert>
  10. class push_button {
  11. public:
  12. // No inv and no bases so contracts optional if no pre, post, and override.
  13. /* Creation */
  14. // Create an enabled button.
  15. push_button() : enabled_(true) {
  16. boost::contract::check c = boost::contract::constructor(this)
  17. .postcondition([&] {
  18. BOOST_CONTRACT_ASSERT(enabled()); // Enabled.
  19. })
  20. ;
  21. }
  22. // Destroy button.
  23. virtual ~push_button() {
  24. // Could have omitted contracts here (nothing to check).
  25. boost::contract::check c = boost::contract::destructor(this);
  26. }
  27. /* Queries */
  28. // If button is enabled.
  29. bool enabled() const {
  30. // Could have omitted contracts here (nothing to check).
  31. boost::contract::check c = boost::contract::public_function(this);
  32. return enabled_;
  33. }
  34. /* Commands */
  35. // Enable button.
  36. void enable() {
  37. boost::contract::check c = boost::contract::public_function(this)
  38. .postcondition([&] {
  39. BOOST_CONTRACT_ASSERT(enabled()); // Enabled.
  40. })
  41. ;
  42. enabled_ = true;
  43. }
  44. // Disable button.
  45. void disable() {
  46. boost::contract::check c = boost::contract::public_function(this)
  47. .postcondition([&] {
  48. BOOST_CONTRACT_ASSERT(!enabled()); // Disabled.
  49. })
  50. ;
  51. enabled_ = false;
  52. }
  53. // Invoke externally when button clicked.
  54. virtual void on_bn_clicked(boost::contract::virtual_* v = 0) = 0;
  55. private:
  56. bool enabled_;
  57. };
  58. void push_button::on_bn_clicked(boost::contract::virtual_* v) {
  59. boost::contract::check c = boost::contract::public_function(v, this)
  60. .precondition([&] {
  61. BOOST_CONTRACT_ASSERT(enabled()); // Enabled.
  62. })
  63. ;
  64. assert(false);
  65. }
  66. #endif // #include guard
  67. //]