pure_virtual_public.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <cassert>
  8. //[pure_virtual_public_base_begin
  9. template<typename Iterator>
  10. class range {
  11. public:
  12. // Pure virtual function declaration (contract in definition below).
  13. virtual Iterator begin(boost::contract::virtual_* v = 0) = 0;
  14. //]
  15. // Could program class invariants and contracts for the following too.
  16. virtual Iterator end() = 0;
  17. virtual bool empty() const = 0;
  18. //[pure_virtual_public_base_end
  19. /* ... */
  20. };
  21. //]
  22. //[pure_virtual_public_base_impl
  23. // Pure virtual function default implementation (out-of-line in C++).
  24. template<typename Iterator>
  25. Iterator range<Iterator>::begin(boost::contract::virtual_* v) {
  26. Iterator result; // As usual, virtual pass `result` right after `v`...
  27. boost::contract::check c = boost::contract::public_function(v, result, this)
  28. .postcondition([&] (Iterator const& result) {
  29. if(empty()) BOOST_CONTRACT_ASSERT(result == end());
  30. })
  31. ;
  32. // Pure function body (never executed by this library).
  33. assert(false);
  34. return result;
  35. }
  36. //]
  37. template<typename T>
  38. class vector
  39. #define BASES public range<typename std::vector<T>::iterator>
  40. : BASES
  41. {
  42. public:
  43. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  44. #undef BASES
  45. typedef typename std::vector<T>::iterator iterator;
  46. iterator begin(boost::contract::virtual_* v = 0) /* override */ {
  47. iterator result;
  48. // Again, pass result right after `v`...
  49. boost::contract::check c = boost::contract::public_function<
  50. override_begin>(v, result, &vector::begin, this)
  51. // ...plus postconditions take `result` as parameter (not capture).
  52. .postcondition([&] (iterator const& result) {
  53. if(!empty()) BOOST_CONTRACT_ASSERT(*result == front());
  54. })
  55. ;
  56. return result = vect_.begin();
  57. }
  58. BOOST_CONTRACT_OVERRIDE(begin)
  59. // Could program class invariants and contracts for the following too.
  60. iterator end() { return vect_.end(); }
  61. bool empty() const { return vect_.empty(); }
  62. T const& front() const { return vect_.front(); }
  63. void push_back(T const& value) { vect_.push_back(value); }
  64. private:
  65. std::vector<T> vect_;
  66. };
  67. int main() {
  68. vector<int> v;
  69. v.push_back(1);
  70. v.push_back(2);
  71. v.push_back(3);
  72. range<std::vector<int>::iterator>& r = v;
  73. assert(*(r.begin()) == 1);
  74. return 0;
  75. }