old_no_macro.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 <cassert>
  7. #include <vector>
  8. //[old_no_macro
  9. template<typename T>
  10. class vector {
  11. public:
  12. virtual void push_back(T const& value, boost::contract::virtual_* v = 0) {
  13. // Program old value instead of using `OLD(size())` macro.
  14. boost::contract::old_ptr<unsigned> old_size =
  15. boost::contract::make_old(v, boost::contract::copy_old(v) ?
  16. size() : boost::contract::null_old())
  17. ;
  18. boost::contract::check c = boost::contract::public_function(v, this)
  19. .postcondition([&] {
  20. BOOST_CONTRACT_ASSERT(size() == *old_size + 1);
  21. })
  22. ;
  23. vect_.push_back(value);
  24. }
  25. /* ... */
  26. //]
  27. unsigned size() const { return vect_.size(); }
  28. private:
  29. std::vector<T> vect_;
  30. };
  31. int main() {
  32. vector<int> vect;
  33. vect.push_back(123);
  34. assert(vect.size() == 1);
  35. return 0;
  36. }