base_types_no_macro.cpp 5.4 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_no_macro
  96. #include <boost/mpl/vector.hpp>
  97. class chars :
  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. {
  104. public:
  105. // Program `base_types` without macros (list only public bases).
  106. typedef boost::mpl::vector<unique_chars, pushable<char> > base_types;
  107. /* ... */
  108. //]
  109. void invariant() const {
  110. BOOST_CONTRACT_ASSERT(empty() == (size() == 0));
  111. }
  112. chars(char from, char to) : unique_chars(from, to) {
  113. boost::contract::check c = boost::contract::constructor(this);
  114. }
  115. chars(char const* const c_str) :
  116. boost::contract::constructor_precondition<chars>([&] {
  117. BOOST_CONTRACT_ASSERT(c_str[0] != '\0');
  118. })
  119. {
  120. boost::contract::check c = boost::contract::constructor(this);
  121. for(unsigned i = 0; c_str[i] != '\0'; ++i) push_back(c_str[i]);
  122. }
  123. void push_back(char x, boost::contract::virtual_* v = 0) /* override */ {
  124. boost::contract::old_ptr<bool> old_find =
  125. BOOST_CONTRACT_OLDOF(v, find(x));
  126. boost::contract::old_ptr<unsigned> old_size =
  127. BOOST_CONTRACT_OLDOF(v, size());
  128. boost::contract::check c = boost::contract::public_function<
  129. override_push_back>(v, &chars::push_back, this, x)
  130. .precondition([&] {
  131. BOOST_CONTRACT_ASSERT(find(x));
  132. })
  133. .postcondition([&] {
  134. if(*old_find) BOOST_CONTRACT_ASSERT(size() == *old_size);
  135. })
  136. ;
  137. if(!find(x)) unique_chars::push_back(x);
  138. }
  139. BOOST_CONTRACT_OVERRIDE(push_back);
  140. bool empty() const {
  141. boost::contract::check c = boost::contract::public_function(this);
  142. return size() == 0;
  143. }
  144. unsigned size() const { return unique_chars::size(); }
  145. protected:
  146. unsigned max_size() const { return vect().max_size(); }
  147. unsigned capacity() const { return vect().capacity(); }
  148. };
  149. int main() {
  150. chars s("abc");
  151. assert(s.find('a'));
  152. assert(s.find('b'));
  153. assert(!s.find('x'));
  154. return 0;
  155. }