checking.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef BOOST_CONTRACT_DETAIL_CHECKING_HPP_
  2. #define BOOST_CONTRACT_DETAIL_CHECKING_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/config.hpp>
  8. #include <boost/contract/detail/static_local_var.hpp>
  9. #include <boost/contract/detail/declspec.hpp>
  10. #include <boost/thread/mutex.hpp>
  11. #include <boost/noncopyable.hpp>
  12. namespace boost { namespace contract { namespace detail {
  13. #ifdef BOOST_MSVC
  14. #pragma warning(push)
  15. #pragma warning(disable: 4251) // Member w/o DLL spec (mutex_ type).
  16. #endif
  17. // RAII facility to disable assertions while checking other assertions.
  18. class BOOST_CONTRACT_DETAIL_DECLSPEC checking :
  19. private boost::noncopyable // Non-copyable resource (might use mutex, etc.).
  20. {
  21. public:
  22. explicit checking() {
  23. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  24. init_locked();
  25. #else
  26. init_unlocked();
  27. #endif
  28. }
  29. ~checking() {
  30. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  31. done_locked();
  32. #else
  33. done_unlocked();
  34. #endif
  35. }
  36. static bool already() {
  37. #ifndef BOOST_CONTRACT_DISABLE_THREADS
  38. return already_locked();
  39. #else
  40. return already_unlocked();
  41. #endif
  42. }
  43. private:
  44. void init_unlocked();
  45. void init_locked();
  46. void done_unlocked();
  47. void done_locked();
  48. static bool already_unlocked();
  49. static bool already_locked();
  50. struct mutex_tag;
  51. typedef static_local_var<mutex_tag, boost::mutex> mutex;
  52. struct checking_tag;
  53. typedef static_local_var_init<checking_tag, bool, bool, false> flag;
  54. };
  55. #ifdef BOOST_MSVC
  56. #pragma warning(pop)
  57. #endif
  58. } } } // namespace
  59. #ifdef BOOST_CONTRACT_HEADER_ONLY
  60. #include <boost/contract/detail/inlined/detail/checking.hpp>
  61. #endif
  62. #endif // #include guard