volatile.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. //[volatile
  8. class u {
  9. public:
  10. static void static_invariant(); // Static invariants.
  11. void invariant() const volatile; // Volatile invariants.
  12. void invariant() const; // Const invariants.
  13. u() { // Check static, volatile, and const invariants.
  14. boost::contract::check c= boost::contract::constructor(this);
  15. }
  16. ~u() { // Check static, volatile, and const invariants.
  17. boost::contract::check c = boost::contract::destructor(this);
  18. }
  19. void nc() { // Check static and const invariants.
  20. boost::contract::check c = boost::contract::public_function(this);
  21. }
  22. void c() const { // Check static and const invariants.
  23. boost::contract::check c = boost::contract::public_function(this);
  24. }
  25. void v() volatile { // Check static and volatile invariants.
  26. boost::contract::check c = boost::contract::public_function(this);
  27. }
  28. void cv() const volatile { // Check static and volatile invariants.
  29. boost::contract::check c = boost::contract::public_function(this);
  30. }
  31. static void s() { // Check static invariants only.
  32. boost::contract::check c = boost::contract::public_function<u>();
  33. }
  34. };
  35. //]
  36. bool static_inv_checked, cv_inv_checked, const_inv_checked;
  37. void u::static_invariant() { static_inv_checked = true; }
  38. void u::invariant() const volatile { cv_inv_checked = true; }
  39. void u::invariant() const { const_inv_checked = true; }
  40. int main() {
  41. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  42. {
  43. static_inv_checked = cv_inv_checked = const_inv_checked = false;
  44. u x;
  45. assert(static_inv_checked);
  46. assert(cv_inv_checked);
  47. assert(const_inv_checked);
  48. static_inv_checked = cv_inv_checked = const_inv_checked = false;
  49. x.nc();
  50. assert(static_inv_checked);
  51. assert(!cv_inv_checked);
  52. assert(const_inv_checked);
  53. static_inv_checked = cv_inv_checked = const_inv_checked = false;
  54. x.c();
  55. assert(static_inv_checked);
  56. assert(!cv_inv_checked);
  57. assert(const_inv_checked);
  58. static_inv_checked = cv_inv_checked = const_inv_checked = false;
  59. x.v();
  60. assert(static_inv_checked);
  61. assert(cv_inv_checked);
  62. assert(!const_inv_checked);
  63. static_inv_checked = cv_inv_checked = const_inv_checked = false;
  64. x.cv();
  65. assert(static_inv_checked);
  66. assert(cv_inv_checked);
  67. assert(!const_inv_checked);
  68. static_inv_checked = cv_inv_checked = const_inv_checked = false;
  69. x.s();
  70. assert(static_inv_checked);
  71. assert(!cv_inv_checked);
  72. assert(!const_inv_checked);
  73. static_inv_checked = cv_inv_checked = const_inv_checked = false;
  74. } // Call destructor.
  75. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  76. assert(static_inv_checked);
  77. assert(cv_inv_checked);
  78. assert(const_inv_checked);
  79. #endif
  80. #endif
  81. return 0;
  82. }