observer.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_observer
  6. #ifndef OBSERVER_HPP_
  7. #define OBSERVER_HPP_
  8. #include <boost/contract.hpp>
  9. #include <cassert>
  10. // Observer.
  11. class observer {
  12. friend class subject;
  13. public:
  14. // No inv and no bases so contracts optional if no pre, post, and override.
  15. /* Creation */
  16. observer() {
  17. // Could have omitted contracts here (nothing to check).
  18. boost::contract::check c = boost::contract::constructor(this);
  19. }
  20. virtual ~observer() {
  21. // Could have omitted contracts here (nothing to check).
  22. boost::contract::check c = boost::contract::destructor(this);
  23. }
  24. /* Commands */
  25. // If up-to-date with related subject.
  26. virtual bool up_to_date_with_subject(boost::contract::virtual_* v = 0)
  27. const = 0;
  28. // Update this observer.
  29. virtual void update(boost::contract::virtual_* v = 0) = 0;
  30. };
  31. bool observer::up_to_date_with_subject(boost::contract::virtual_* v) const {
  32. boost::contract::check c = boost::contract::public_function(v, this);
  33. assert(false);
  34. return false;
  35. }
  36. void observer::update(boost::contract::virtual_* v) {
  37. boost::contract::check c = boost::contract::public_function(v, this)
  38. .postcondition([&] {
  39. BOOST_CONTRACT_ASSERT(up_to_date_with_subject()); // Up-to-date.
  40. })
  41. ;
  42. assert(false);
  43. }
  44. #endif // #include guard
  45. //]