stack.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_stack
  6. #include <boost/contract.hpp>
  7. #include <boost/optional.hpp>
  8. #include <vector>
  9. #include <cassert>
  10. template<typename T>
  11. class stack {
  12. friend class boost::contract::access;
  13. void invariant() const {
  14. BOOST_CONTRACT_ASSERT(count() >= 0); // Non-negative count.
  15. }
  16. public:
  17. /* Creation */
  18. // Create empty stack.
  19. stack() {
  20. boost::contract::check c = boost::contract::constructor(this)
  21. .postcondition([&] {
  22. BOOST_CONTRACT_ASSERT(count() == 0); // Empty.
  23. })
  24. ;
  25. }
  26. // Destroy stack.
  27. virtual ~stack() {
  28. // Check invariants.
  29. boost::contract::check c = boost::contract::destructor(this);
  30. }
  31. /* Basic Queries */
  32. // Number of items.
  33. int count() const {
  34. // Check invariants.
  35. boost::contract::check c = boost::contract::public_function(this);
  36. return items_.size();
  37. }
  38. // Item at index in [1, count()] (as in Eiffel).
  39. T const& item_at(int index) const {
  40. boost::contract::check c = boost::contract::public_function(this)
  41. .precondition([&] {
  42. BOOST_CONTRACT_ASSERT(index > 0); // Positive index.
  43. BOOST_CONTRACT_ASSERT(index <= count()); // Index within count.
  44. })
  45. ;
  46. return items_[index - 1];
  47. }
  48. /* Derived Queries */
  49. // If no items.
  50. bool is_empty() const {
  51. bool result;
  52. boost::contract::check c = boost::contract::public_function(this)
  53. .postcondition([&] {
  54. // Consistent with count.
  55. BOOST_CONTRACT_ASSERT(result == (count() == 0));
  56. })
  57. ;
  58. return result = (count() == 0);
  59. }
  60. // Top item.
  61. T const& item() const {
  62. boost::optional<T const&> result; // Avoid extra construction of T.
  63. boost::contract::check c = boost::contract::public_function(this)
  64. .precondition([&] {
  65. BOOST_CONTRACT_ASSERT(count() > 0); // Not empty.
  66. })
  67. .postcondition([&] {
  68. // Item on top.
  69. BOOST_CONTRACT_ASSERT(*result == item_at(count()));
  70. })
  71. ;
  72. return *(result = item_at(count()));
  73. }
  74. /* Commands */
  75. // Push item to the top.
  76. void put(T const& new_item) {
  77. boost::contract::old_ptr<int> old_count = BOOST_CONTRACT_OLDOF(count());
  78. boost::contract::check c = boost::contract::public_function(this)
  79. .postcondition([&] {
  80. BOOST_CONTRACT_ASSERT(count() == *old_count + 1); // Count inc.
  81. BOOST_CONTRACT_ASSERT(item() == new_item); // Item set.
  82. })
  83. ;
  84. items_.push_back(new_item);
  85. }
  86. // Pop top item.
  87. void remove() {
  88. boost::contract::old_ptr<int> old_count = BOOST_CONTRACT_OLDOF(count());
  89. boost::contract::check c = boost::contract::public_function(this)
  90. .precondition([&] {
  91. BOOST_CONTRACT_ASSERT(count() > 0); // Not empty.
  92. })
  93. .postcondition([&] {
  94. BOOST_CONTRACT_ASSERT(count() == *old_count - 1); // Count dec.
  95. })
  96. ;
  97. items_.pop_back();
  98. }
  99. private:
  100. std::vector<T> items_;
  101. };
  102. int main() {
  103. stack<int> s;
  104. assert(s.count() == 0);
  105. s.put(123);
  106. assert(s.item() == 123);
  107. s.remove();
  108. assert(s.is_empty());
  109. return 0;
  110. }
  111. //]