operator_safe_bool.hpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #ifndef BOOST_CONTRACT_DETAIL_OPERATOR_SAFE_BOOL_HPP_
  2. #define BOOST_CONTRACT_DETAIL_OPERATOR_SAFE_BOOL_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/detail/name.hpp>
  8. #include <boost/config.hpp>
  9. #include <boost/detail/workaround.hpp>
  10. // NOTE: This code is inspired by <boost/shared_ptr/detail/operator_bool.hpp>.
  11. /* PRIVATE */
  12. // operator! is redundant, but some compilers need it.
  13. #define BOOST_CONTRACT_OPERATOR_SAFE_BOOL_NOT_(bool_expr) \
  14. bool operator!() const BOOST_NOEXCEPT { return !(bool_expr); }
  15. /* PUBLIC */
  16. #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) && \
  17. !defined(BOOST_NO_CXX11_NULLPTR)
  18. #define BOOST_CONTRACT_DETAIL_OPERATOR_SAFE_BOOL(this_type, bool_expr) \
  19. explicit operator bool() const BOOST_NOEXCEPT { return (bool_expr); } \
  20. BOOST_CONTRACT_OPERATOR_SAFE_BOOL_NOT_(bool_expr)
  21. #elif (defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, < 0x570) ) || \
  22. defined(__CINT__)
  23. #define BOOST_CONTRACT_DETAIL_OPERATOR_SAFE_BOOL(this_type, bool_expr) \
  24. operator bool() const BOOST_NOEXCEPT { return (bool_expr); } \
  25. BOOST_CONTRACT_OPERATOR_SAFE_BOOL_NOT_(bool_expr)
  26. #elif defined(_MANAGED)
  27. #define BOOST_CONTRACT_DETAIL_OPERATOR_SAFE_BOOL(this_type, bool_expr) \
  28. static void BOOST_CONTRACT_DETAIL_NAME1(operator_safe_bool_func)( \
  29. this_type***) {} \
  30. typedef void (*BOOST_CONTRACT_DETAIL_NAME1(operator_safe_bool_type))( \
  31. this_type***); \
  32. operator BOOST_CONTRACT_DETAIL_NANE(operator_safe_bool_type)() \
  33. const BOOST_NOEXCEPT { \
  34. return (bool_expr) ? \
  35. &BOOST_CONTRACT_DETAIL_NAME1(operator_safe_bool_func) : 0; \
  36. } \
  37. BOOST_CONTRACT_OPERATOR_SAFE_BOOL_NOT_(bool_expr)
  38. #elif (defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, < 0x3200)) || \
  39. (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 304)) || \
  40. (defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x590))
  41. #define BOOST_CONTRACT_DETAIL_OPERATOR_SAFE_BOOL(this_type, bool_expr) \
  42. void BOOST_CONTRACT_DETAIL_NAME1(operator_safe_bool_func)() const {} \
  43. typedef void (this_type::*BOOST_CONTRACT_DETAIL_NAME1( \
  44. operator_safe_bool_type))() const; \
  45. operator BOOST_CONTRACT_DETAIL_NAME1(operator_safe_bool_type)() \
  46. const BOOST_NOEXCEPT { \
  47. return (bool_expr) ? &this_type:: \
  48. BOOST_CONTRACT_DETAIL_NAME1(operator_safe_bool_func) : 0; \
  49. } \
  50. BOOST_CONTRACT_OPERATOR_SAFE_BOOL_NOT_(bool_expr)
  51. #else
  52. #define BOOST_CONTRACT_DETAIL_OPERATOR_SAFE_BOOL(this_type, bool_expr) \
  53. void* BOOST_CONTRACT_DETAIL_NAME1(operator_safe_bool_data); \
  54. typedef void* this_type::*BOOST_CONTRACT_DETAIL_NAME1( \
  55. operator_safe_bool_type);\
  56. operator BOOST_CONTRACT_DETAIL_NAME1(operator_safe_bool_type)() \
  57. const BOOST_NOEXCEPT { \
  58. return (bool_expr) ? &this_type:: \
  59. BOOST_CONTRACT_DETAIL_NAME1(operator_safe_bool_data) : 0; \
  60. } \
  61. BOOST_CONTRACT_OPERATOR_SAFE_BOOL_NOT_(bool_expr)
  62. #endif
  63. #endif // #include guard