union.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <boost/config.hpp>
  7. #include <cassert>
  8. #ifdef BOOST_GCC // G++ does not support static union members yet.
  9. int instances_ = 0;
  10. #endif
  11. //[union
  12. union positive {
  13. public:
  14. static void static_invariant() { // Static class invariants (as usual).
  15. BOOST_CONTRACT_ASSERT(instances() >= 0);
  16. }
  17. void invariant() const { // Class invariants (as usual).
  18. BOOST_CONTRACT_ASSERT(i_ > 0);
  19. BOOST_CONTRACT_ASSERT(d_ > 0);
  20. }
  21. // Contracts for constructor, as usual but...
  22. explicit positive(int x) : d_(0) {
  23. // ...unions cannot have bases so constructor preconditions here.
  24. boost::contract::constructor_precondition<positive> pre([&] {
  25. BOOST_CONTRACT_ASSERT(x > 0);
  26. });
  27. boost::contract::old_ptr<int> old_instances =
  28. BOOST_CONTRACT_OLDOF(instances());
  29. boost::contract::check c = boost::contract::constructor(this)
  30. .postcondition([&] {
  31. { int y; get(y); BOOST_CONTRACT_ASSERT(y == x); }
  32. BOOST_CONTRACT_ASSERT(instances() == *old_instances + 1);
  33. })
  34. ;
  35. i_ = x;
  36. ++instances_;
  37. }
  38. // Contracts for destructor (as usual).
  39. ~positive() {
  40. boost::contract::old_ptr<int> old_instances =
  41. BOOST_CONTRACT_OLDOF(instances());
  42. boost::contract::check c = boost::contract::destructor(this)
  43. .postcondition([&] {
  44. BOOST_CONTRACT_ASSERT(instances() == *old_instances - 1);
  45. })
  46. ;
  47. --instances_;
  48. }
  49. // Contracts for public function (as usual, but no virtual or override).
  50. void get(int& x) const {
  51. boost::contract::check c = boost::contract::public_function(this)
  52. .postcondition([&] {
  53. BOOST_CONTRACT_ASSERT(x > 0);
  54. })
  55. ;
  56. x = i_;
  57. }
  58. // Contracts for static public function (as usual).
  59. static int instances() {
  60. boost::contract::check c = boost::contract::public_function<positive>();
  61. return instances_;
  62. }
  63. private:
  64. int i_;
  65. double d_;
  66. /* ... */
  67. //]
  68. public:
  69. explicit positive(double x) : d_(0) {
  70. // Unions cannot have bases so constructor preconditions here.
  71. boost::contract::constructor_precondition<positive> pre([&] {
  72. BOOST_CONTRACT_ASSERT(x > 0);
  73. });
  74. boost::contract::old_ptr<int> old_instances =
  75. BOOST_CONTRACT_OLDOF(instances());
  76. boost::contract::check c = boost::contract::constructor(this)
  77. .postcondition([&] {
  78. { double y; get(y); BOOST_CONTRACT_ASSERT(y == x); }
  79. BOOST_CONTRACT_ASSERT(instances() == *old_instances + 1);
  80. })
  81. ;
  82. d_ = x;
  83. ++instances_;
  84. }
  85. void get(double& x) const {
  86. boost::contract::check c = boost::contract::public_function(this)
  87. .postcondition([&] {
  88. BOOST_CONTRACT_ASSERT(x > 0);
  89. })
  90. ;
  91. x = d_;
  92. }
  93. #ifndef BOOST_GCC // G++ does not support static union members yet.
  94. static int instances_;
  95. #endif
  96. };
  97. #ifndef BOOST_GCC // G++ does not support static union members yet.
  98. int positive::instances_ = 0;
  99. #endif
  100. int main() {
  101. {
  102. positive p(123);
  103. assert(p.instances() == 1);
  104. { int y = -456; p.get(y); assert(y == 123); }
  105. positive q(1.23);
  106. assert(q.instances() == 2);
  107. { double y = -4.56; q.get(y); assert(y == 1.23); }
  108. }
  109. assert(positive::instances() == 0);
  110. return 0;
  111. }