checking.hpp 2.1 KB

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