courier.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_courier
  6. #include <boost/contract.hpp>
  7. #include <string>
  8. #include <cassert>
  9. struct package {
  10. double weight_kg;
  11. std::string location;
  12. double accepted_hour;
  13. double delivered_hour;
  14. explicit package(
  15. double _weight_kg,
  16. std::string const& _location = "",
  17. double _accepted_hour = 0.0,
  18. double _delivered_hour = 0.0
  19. ) :
  20. weight_kg(_weight_kg),
  21. location(_location),
  22. accepted_hour(_accepted_hour),
  23. delivered_hour(_delivered_hour)
  24. {}
  25. };
  26. // Courier for package delivery.
  27. class courier
  28. #define BASES private boost::contract::constructor_precondition<courier>
  29. : BASES
  30. {
  31. friend class boost::contract::access;
  32. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  33. #undef BASES
  34. static void static_invariant() {
  35. // Positive min. insurance.
  36. BOOST_CONTRACT_ASSERT(min_insurance_usd >= 0.0);
  37. }
  38. void invariant() const {
  39. // Above min. insurance.
  40. BOOST_CONTRACT_ASSERT(insurance_cover_usd() >= min_insurance_usd);
  41. }
  42. public:
  43. static double min_insurance_usd;
  44. /* Creation */
  45. // Create courier with specified insurance value.
  46. explicit courier(double _insurance_cover_usd = min_insurance_usd) :
  47. boost::contract::constructor_precondition<courier>([&] {
  48. // Positive insurance.
  49. BOOST_CONTRACT_ASSERT(_insurance_cover_usd >= 0.0);
  50. }),
  51. insurance_cover_usd_(_insurance_cover_usd)
  52. {
  53. // Check invariants.
  54. boost::contract::check c = boost::contract::constructor(this);
  55. }
  56. // Destroy courier.
  57. virtual ~courier() {
  58. // Check invariants.
  59. boost::contract::check c = boost::contract::destructor(this);
  60. }
  61. /* Queries */
  62. // Return insurance cover.
  63. double insurance_cover_usd() const {
  64. // Check invariants.
  65. boost::contract::check c = boost::contract::public_function(this);
  66. return insurance_cover_usd_;
  67. }
  68. /* Commands */
  69. // Deliver package to destination.
  70. virtual void deliver(
  71. package& package_delivery,
  72. std::string const& destination,
  73. boost::contract::virtual_* v = 0
  74. ) {
  75. boost::contract::check c = boost::contract::public_function(v, this)
  76. .precondition([&] {
  77. // Within max weight of this delivery.
  78. BOOST_CONTRACT_ASSERT(package_delivery.weight_kg < 5.0);
  79. })
  80. .postcondition([&] {
  81. // Within max delivery type.
  82. BOOST_CONTRACT_ASSERT(double(package_delivery.delivered_hour -
  83. package_delivery.accepted_hour) <= 3.0);
  84. // Delivered at destination.
  85. BOOST_CONTRACT_ASSERT(package_delivery.location == destination);
  86. })
  87. ;
  88. package_delivery.location = destination;
  89. // Delivery takes 2.5 hours.
  90. package_delivery.delivered_hour = package_delivery.accepted_hour + 2.5;
  91. }
  92. private:
  93. double insurance_cover_usd_;
  94. };
  95. double courier::min_insurance_usd = 10.0e+6;
  96. // Different courier for package delivery.
  97. class different_courier
  98. #define BASES private boost::contract::constructor_precondition< \
  99. different_courier>, public courier
  100. : BASES
  101. {
  102. friend class boost::contract::access;
  103. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; // Subcontracting.
  104. #undef BASES
  105. static void static_invariant() {
  106. BOOST_CONTRACT_ASSERT( // Better insurance amount.
  107. different_insurance_usd >= courier::min_insurance_usd);
  108. }
  109. void invariant() const {
  110. // Above different insurance value.
  111. BOOST_CONTRACT_ASSERT(insurance_cover_usd() >= different_insurance_usd);
  112. }
  113. BOOST_CONTRACT_OVERRIDE(deliver)
  114. public:
  115. static double different_insurance_usd;
  116. /* Creation */
  117. // Create courier with specified insurance value.
  118. explicit different_courier(
  119. double insurance_cover_usd = different_insurance_usd) :
  120. boost::contract::constructor_precondition<different_courier>([&] {
  121. // Positive insurance value.
  122. BOOST_CONTRACT_ASSERT(insurance_cover_usd > 0.0);
  123. }),
  124. courier(insurance_cover_usd)
  125. {
  126. // Check invariants.
  127. boost::contract::check c = boost::contract::constructor(this);
  128. }
  129. // Destroy courier.
  130. virtual ~different_courier() {
  131. // Check invariants.
  132. boost::contract::check c = boost::contract::destructor(this);
  133. }
  134. /* Commands */
  135. virtual void deliver(
  136. package& package_delivery,
  137. std::string const& destination,
  138. boost::contract::virtual_* v = 0
  139. ) /* override */ {
  140. boost::contract::check c = boost::contract::public_function<
  141. override_deliver
  142. >(v, &different_courier::deliver, this, package_delivery, destination)
  143. .precondition([&] {
  144. // Package can weight more (weaker precondition).
  145. BOOST_CONTRACT_ASSERT(package_delivery.weight_kg <= 8.0);
  146. })
  147. .postcondition([&] {
  148. // Faster delivery (stronger postcondition).
  149. BOOST_CONTRACT_ASSERT(double(package_delivery.delivered_hour -
  150. package_delivery.accepted_hour) <= 2.0);
  151. // Inherited "delivery at destination" postcondition.
  152. })
  153. ;
  154. package_delivery.location = destination;
  155. // Delivery takes 0.5 hours.
  156. package_delivery.delivered_hour = package_delivery.accepted_hour + 0.5;
  157. }
  158. };
  159. double different_courier::different_insurance_usd = 20.0e+6;
  160. int main() {
  161. package cups(3.6, "store");
  162. courier c;
  163. c.deliver(cups, "home");
  164. assert(cups.location == "home");
  165. package desk(7.2, "store");
  166. different_courier dc;
  167. dc.deliver(desk, "office");
  168. assert(desk.location == "office");
  169. return 0;
  170. }
  171. //]