no_lambdas.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "no_lambdas.hpp"
  6. #include <boost/bind.hpp>
  7. #include <cassert>
  8. //[no_lambdas_cpp
  9. iarray::iarray(unsigned max, unsigned count) :
  10. boost::contract::constructor_precondition<iarray>(boost::bind(
  11. &iarray::constructor_precondition, max, count)),
  12. values_(new int[max]), // Member initializations can be here.
  13. capacity_(max)
  14. {
  15. boost::contract::old_ptr<int> old_instances;
  16. boost::contract::check c = boost::contract::constructor(this)
  17. .old(boost::bind(&iarray::constructor_old, boost::ref(old_instances)))
  18. .postcondition(boost::bind(
  19. &iarray::constructor_postcondition,
  20. this,
  21. boost::cref(max),
  22. boost::cref(count),
  23. boost::cref(old_instances)
  24. ))
  25. ;
  26. for(unsigned i = 0; i < count; ++i) values_[i] = int();
  27. size_ = count;
  28. ++instances_;
  29. }
  30. iarray::~iarray() {
  31. boost::contract::old_ptr<int> old_instances;
  32. boost::contract::check c = boost::contract::destructor(this)
  33. .old(boost::bind(&iarray::destructor_old, this,
  34. boost::ref(old_instances)))
  35. .postcondition(boost::bind(&iarray::destructor_postcondition,
  36. boost::cref(old_instances)))
  37. ;
  38. delete[] values_;
  39. --instances_;
  40. }
  41. void iarray::push_back(int value, boost::contract::virtual_* v) {
  42. boost::contract::old_ptr<unsigned> old_size;
  43. boost::contract::check c = boost::contract::public_function(v, this)
  44. .precondition(boost::bind(&iarray::push_back_precondition, this))
  45. .old(boost::bind(&iarray::push_back_old, this, boost::cref(v),
  46. boost::ref(old_size)))
  47. .postcondition(boost::bind(&iarray::push_back_postcondition, this,
  48. boost::cref(old_size)))
  49. ;
  50. values_[size_++] = value;
  51. }
  52. unsigned iarray::capacity() const {
  53. // Check invariants.
  54. boost::contract::check c = boost::contract::public_function(this);
  55. return capacity_;
  56. }
  57. unsigned iarray::size() const {
  58. // Check invariants.
  59. boost::contract::check c = boost::contract::public_function(this);
  60. return size_;
  61. }
  62. int iarray::instances() {
  63. // Check static invariants.
  64. boost::contract::check c = boost::contract::public_function<iarray>();
  65. return instances_;
  66. }
  67. int iarray::instances_ = 0;
  68. //]
  69. int main() {
  70. iarray a(3, 2);
  71. assert(a.capacity() == 3);
  72. assert(a.size() == 2);
  73. a.push_back(-123);
  74. assert(a.size() == 3);
  75. return 0;
  76. }