virtual.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef BOOST_CONTRACT_VIRTUAL_HPP_
  2. #define BOOST_CONTRACT_VIRTUAL_HPP_
  3. // Copyright (C) 2008-2018 Lorenzo Caminiti
  4. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  5. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  6. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  7. /** @file
  8. Handle virtual public functions with contracts (for subcontracting).
  9. */
  10. // IMPORTANT: Included by contract_macro.hpp so must #if-guard all its includes.
  11. #include <boost/contract/core/config.hpp>
  12. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  13. #include <boost/contract/detail/decl.hpp>
  14. #endif
  15. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  16. #include <boost/any.hpp>
  17. #endif
  18. #ifndef BOOST_CONTRACT_NO_OLDS
  19. #include <boost/shared_ptr.hpp>
  20. #include <queue>
  21. #endif
  22. namespace boost { namespace contract {
  23. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  24. namespace detail {
  25. BOOST_CONTRACT_DETAIL_DECL_DETAIL_COND_SUBCONTRACTING_Z(1,
  26. /* is_friend = */ 0, OO, RR, FF, CC, AArgs);
  27. }
  28. #endif
  29. /**
  30. Type of extra function parameter to handle contracts for virtual public
  31. functions (for subcontracting).
  32. Virtual public functions (and therefore also public function overrides)
  33. declaring contracts using this library must specify an extra function parameter
  34. at the very end of the parameter list.
  35. This parameter must be a pointer to this class and it must have default value
  36. @c 0 (i.e., @c nullptr).
  37. (This extra parameter is often named @c v in this documentation, but any name
  38. can be used.)
  39. In practice this extra parameter does not alter the calling interface of the
  40. enclosing function declaring the contract because it is always the very last
  41. parameter and it has a default value (so it can always be omitted when users
  42. call the function).
  43. This extra parameter must be passed to
  44. @RefFunc{boost::contract::public_function}, @RefMacro{BOOST_CONTRACT_OLDOF}, and
  45. all other operations of this library that accept a pointer to
  46. @RefClass{boost::contract::virtual_}.
  47. A part from that, this class is not intended to be directly used by programmers
  48. (and that is why this class does not have any public member and it is not
  49. copyable).
  50. @see @RefSect{tutorial.virtual_public_functions, Virtual Public Functions},
  51. @RefSect{tutorial.public_function_overrides__subcontracting_,
  52. Public Function Overrides}
  53. */
  54. class virtual_ { // Non-copyable (see below) to avoid copy queue, stack, etc.
  55. /** @cond */
  56. private: // No public API (so users cannot use it directly by mistake).
  57. // No boost::noncopyable to avoid its overhead when contracts disabled.
  58. virtual_(virtual_&);
  59. virtual_& operator=(virtual_&);
  60. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  61. enum action_enum {
  62. // virtual_ always held/passed as ptr so nullptr used for user call.
  63. no_action,
  64. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  65. check_entry_inv,
  66. #endif
  67. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  68. check_pre,
  69. #endif
  70. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  71. check_exit_inv,
  72. #endif
  73. #ifndef BOOST_CONTRACT_NO_OLDS
  74. // For outside .old(...).
  75. push_old_init_copy,
  76. // pop_old_init_copy as static function below.
  77. // For inside .old(...).
  78. call_old_ftor,
  79. push_old_ftor_copy,
  80. pop_old_ftor_copy,
  81. #endif
  82. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  83. check_post,
  84. #endif
  85. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  86. check_except,
  87. #endif
  88. };
  89. #endif
  90. #ifndef BOOST_CONTRACT_NO_OLDS
  91. // Not just an enum value because the logical combination of two values.
  92. inline static bool pop_old_init_copy(action_enum a) {
  93. return
  94. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  95. a == check_post
  96. #endif
  97. #if !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) && \
  98. !defined(BOOST_CONTRACT_NO_EXCEPTS)
  99. ||
  100. #endif
  101. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  102. a == check_except
  103. #endif
  104. ;
  105. }
  106. #endif
  107. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  108. explicit virtual_(action_enum a) :
  109. action_(a)
  110. , failed_(false)
  111. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  112. , result_type_name_()
  113. , result_optional_()
  114. #endif
  115. {}
  116. #endif
  117. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  118. action_enum action_;
  119. bool failed_;
  120. #endif
  121. #ifndef BOOST_CONTRACT_NO_OLDS
  122. std::queue<boost::shared_ptr<void> > old_init_copies_;
  123. std::queue<boost::shared_ptr<void> > old_ftor_copies_;
  124. #endif
  125. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  126. boost::any result_ptr_; // Result for virtual and overriding functions.
  127. char const* result_type_name_;
  128. bool result_optional_;
  129. #endif
  130. // Friends (used to limit library's public API).
  131. #ifndef BOOST_CONTRACT_NO_OLDS
  132. friend bool copy_old(virtual_*);
  133. friend class old_pointer;
  134. #endif
  135. #ifndef BOOST_CONTRACT_NO_CONDITIONS
  136. BOOST_CONTRACT_DETAIL_DECL_DETAIL_COND_SUBCONTRACTING_Z(1,
  137. /* is_friend = */ 1, OO, RR, FF, CC, AArgs);
  138. #endif
  139. /** @endcond */
  140. };
  141. } } // namespace
  142. #endif // #include guard