base_types.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include <boost/contract.hpp>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <cassert>
  9. template<typename T>
  10. class pushable {
  11. public:
  12. void invariant() const {
  13. BOOST_CONTRACT_ASSERT(capacity() <= max_size());
  14. }
  15. virtual void push_back(T x, boost::contract::virtual_* v = 0) = 0;
  16. protected:
  17. virtual unsigned capacity() const = 0;
  18. virtual unsigned max_size() const = 0;
  19. };
  20. template<typename T>
  21. void pushable<T>::push_back(T x, boost::contract::virtual_* v) {
  22. boost::contract::old_ptr<unsigned> old_capacity =
  23. BOOST_CONTRACT_OLDOF(v, capacity());
  24. boost::contract::check c = boost::contract::public_function(v, this)
  25. .precondition([&] {
  26. BOOST_CONTRACT_ASSERT(capacity() < max_size());
  27. })
  28. .postcondition([&] {
  29. BOOST_CONTRACT_ASSERT(capacity() >= *old_capacity);
  30. })
  31. ;
  32. assert(false); // Shall never execute this body.
  33. }
  34. struct has_size { virtual unsigned size() const = 0; };
  35. struct has_empty { virtual bool empty() const = 0; };
  36. class unique_chars
  37. : private boost::contract::constructor_precondition<unique_chars>
  38. {
  39. public:
  40. void invariant() const {
  41. BOOST_CONTRACT_ASSERT(size() >= 0);
  42. }
  43. unique_chars(char from, char to) :
  44. boost::contract::constructor_precondition<unique_chars>([&] {
  45. BOOST_CONTRACT_ASSERT(from <= to);
  46. })
  47. {
  48. boost::contract::check c = boost::contract::constructor(this)
  49. .postcondition([&] {
  50. BOOST_CONTRACT_ASSERT(int(size()) == (to - from + 1));
  51. })
  52. ;
  53. for(char x = from; x <= to; ++x) vect_.push_back(x);
  54. }
  55. virtual ~unique_chars() {
  56. boost::contract::check c = boost::contract::destructor(this);
  57. }
  58. unsigned size() const {
  59. boost::contract::check c = boost::contract::public_function(this);
  60. return vect_.size();
  61. }
  62. bool find(char x) const {
  63. bool result;
  64. boost::contract::check c = boost::contract::public_function(this)
  65. .postcondition([&] {
  66. if(size() == 0) BOOST_CONTRACT_ASSERT(!result);
  67. })
  68. ;
  69. return result = std::find(vect_.begin(), vect_.end(), x) != vect_.end();
  70. }
  71. virtual void push_back(char x, boost::contract::virtual_* v = 0) {
  72. boost::contract::old_ptr<bool> old_find =
  73. BOOST_CONTRACT_OLDOF(v, find(x));
  74. boost::contract::old_ptr<unsigned> old_size =
  75. BOOST_CONTRACT_OLDOF(v, size());
  76. boost::contract::check c = boost::contract::public_function(v, this)
  77. .precondition([&] {
  78. BOOST_CONTRACT_ASSERT(!find(x));
  79. })
  80. .postcondition([&] {
  81. if(!*old_find) {
  82. BOOST_CONTRACT_ASSERT(find(x));
  83. BOOST_CONTRACT_ASSERT(size() == *old_size + 1);
  84. }
  85. })
  86. ;
  87. vect_.push_back(x);
  88. }
  89. protected:
  90. unique_chars() {}
  91. std::vector<char> const& vect() const { return vect_; }
  92. private:
  93. std::vector<char> vect_;
  94. };
  95. //[base_types
  96. class chars
  97. #define BASES /* local macro (for convenience) */ \
  98. private boost::contract::constructor_precondition<chars>, \
  99. public unique_chars, \
  100. public virtual pushable<char>, \
  101. virtual protected has_size, \
  102. private has_empty
  103. : BASES // Bases of this class.
  104. {
  105. public:
  106. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; // Bases typedef.
  107. #undef BASES // Undefine local macro.
  108. /* ... */
  109. //]
  110. void invariant() const {
  111. BOOST_CONTRACT_ASSERT(empty() == (size() == 0));
  112. }
  113. chars(char from, char to) : unique_chars(from, to) {
  114. boost::contract::check c = boost::contract::constructor(this);
  115. }
  116. chars(char const* const c_str) :
  117. boost::contract::constructor_precondition<chars>([&] {
  118. BOOST_CONTRACT_ASSERT(c_str[0] != '\0');
  119. })
  120. {
  121. boost::contract::check c = boost::contract::constructor(this);
  122. for(unsigned i = 0; c_str[i] != '\0'; ++i) push_back(c_str[i]);
  123. }
  124. void push_back(char x, boost::contract::virtual_* v = 0) /* override */ {
  125. boost::contract::old_ptr<bool> old_find =
  126. BOOST_CONTRACT_OLDOF(v, find(x));
  127. boost::contract::old_ptr<unsigned> old_size =
  128. BOOST_CONTRACT_OLDOF(v, size());
  129. boost::contract::check c = boost::contract::public_function<
  130. override_push_back>(v, &chars::push_back, this, x)
  131. .precondition([&] {
  132. BOOST_CONTRACT_ASSERT(find(x));
  133. })
  134. .postcondition([&] {
  135. if(*old_find) BOOST_CONTRACT_ASSERT(size() == *old_size);
  136. })
  137. ;
  138. if(!find(x)) unique_chars::push_back(x);
  139. }
  140. BOOST_CONTRACT_OVERRIDE(push_back);
  141. bool empty() const {
  142. boost::contract::check c = boost::contract::public_function(this);
  143. return size() == 0;
  144. }
  145. unsigned size() const { return unique_chars::size(); }
  146. protected:
  147. unsigned max_size() const { return vect().max_size(); }
  148. unsigned capacity() const { return vect().capacity(); }
  149. };
  150. int main() {
  151. chars s("abc");
  152. assert(s.find('a'));
  153. assert(s.find('b'));
  154. assert(!s.find('x'));
  155. return 0;
  156. }