cond_post.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef BOOST_CONTRACT_DETAIL_COND_POST_HPP_
  2. #define BOOST_CONTRACT_DETAIL_COND_POST_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. #include <boost/contract/core/exception.hpp>
  8. #include <boost/contract/core/config.hpp>
  9. #include <boost/contract/detail/condition/cond_base.hpp>
  10. #include <boost/contract/detail/none.hpp>
  11. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  12. #include <boost/contract/detail/type_traits/optional.hpp>
  13. #include <boost/optional.hpp>
  14. #include <boost/function.hpp>
  15. #include <boost/type_traits/remove_reference.hpp>
  16. #include <boost/mpl/if.hpp>
  17. #endif
  18. /* PRIVATE */
  19. #define BOOST_CONTRACT_DETAIL_COND_POST_DEF_( \
  20. result_type, result_param, ftor_type, ftor_var, ftor_call) \
  21. public: \
  22. template<typename F> \
  23. void set_post(F const& f) { ftor_var = f; } \
  24. \
  25. protected: \
  26. void check_post(result_type const& result_param) { \
  27. if(failed()) return; \
  28. try { if(ftor_var) { ftor_call; } } \
  29. catch(...) { fail(&boost::contract::postcondition_failure); } \
  30. } \
  31. \
  32. private: \
  33. boost::function<ftor_type> ftor_var; /* Boost.Func for lambdas, etc. */
  34. /* CODE */
  35. namespace boost { namespace contract { namespace detail {
  36. template<typename VR>
  37. class cond_post : public cond_base { // Non-copyable base.
  38. public:
  39. explicit cond_post(boost::contract::from from) : cond_base(from) {}
  40. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  41. private: typedef typename boost::mpl::if_<is_optional<VR>,
  42. boost::optional<typename boost::remove_reference<typename
  43. optional_value_type<VR>::type>::type const&> const&
  44. ,
  45. VR const&
  46. >::type r_type;
  47. BOOST_CONTRACT_DETAIL_COND_POST_DEF_(
  48. r_type,
  49. r,
  50. void (r_type),
  51. // Won't raise this error if NO_POST (for optimization).
  52. BOOST_CONTRACT_ERROR_postcondition_result_parameter_required,
  53. BOOST_CONTRACT_ERROR_postcondition_result_parameter_required(r)
  54. )
  55. #endif
  56. };
  57. template<>
  58. class cond_post<none> : public cond_base { // Non-copyable base.
  59. public:
  60. explicit cond_post(boost::contract::from from) : cond_base(from) {}
  61. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  62. BOOST_CONTRACT_DETAIL_COND_POST_DEF_(
  63. none,
  64. unused,
  65. void (),
  66. // Won't raise this error if NO_POST (for optimization).
  67. BOOST_CONTRACT_ERROR_postcondition_result_parameter_not_allowed,
  68. BOOST_CONTRACT_ERROR_postcondition_result_parameter_not_allowed()
  69. )
  70. #endif
  71. };
  72. } } } // namespace
  73. #endif // #include guard