shared_grids_std.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Boost.Geometry
  2. // Copyright (c) 2018-2019, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_GEOMETRY_SRS_SHARED_GRIDS_STD_HPP
  8. #define BOOST_GEOMETRY_SRS_SHARED_GRIDS_STD_HPP
  9. #include <boost/config.hpp>
  10. #ifdef BOOST_NO_CXX14_HDR_SHARED_MUTEX
  11. #error "C++14 <shared_mutex> header required."
  12. #endif
  13. #include <boost/geometry/srs/projections/grids.hpp>
  14. #include <mutex>
  15. #include <shared_mutex>
  16. namespace boost { namespace geometry
  17. {
  18. namespace srs
  19. {
  20. class shared_grids_std
  21. {
  22. // VS 2015 Update 2
  23. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023918)
  24. typedef std::shared_mutex mutex_type;
  25. // Other C++17
  26. #elif !defined(BOOST_NO_CXX14_HDR_SHARED_MUTEX) && (__cplusplus > 201402L)
  27. typedef std::shared_mutex mutex_type;
  28. #else
  29. typedef std::shared_timed_mutex mutex_type;
  30. #endif
  31. public:
  32. std::size_t size() const
  33. {
  34. std::shared_lock<mutex_type> lock(mutex);
  35. return gridinfo.size();
  36. }
  37. bool empty() const
  38. {
  39. std::shared_lock<mutex_type> lock(mutex);
  40. return gridinfo.empty();
  41. }
  42. typedef projections::detail::shared_grids_tag tag;
  43. struct read_locked
  44. {
  45. read_locked(shared_grids_std & g)
  46. : gridinfo(g.gridinfo)
  47. , lock(g.mutex)
  48. {}
  49. // should be const&
  50. projections::detail::pj_gridinfo & gridinfo;
  51. private:
  52. std::shared_lock<mutex_type> lock;
  53. };
  54. struct write_locked
  55. {
  56. write_locked(shared_grids_std & g)
  57. : gridinfo(g.gridinfo)
  58. , lock(g.mutex)
  59. {}
  60. projections::detail::pj_gridinfo & gridinfo;
  61. private:
  62. std::unique_lock<mutex_type> lock;
  63. };
  64. private:
  65. projections::detail::pj_gridinfo gridinfo;
  66. mutable mutex_type mutex;
  67. };
  68. } // namespace srs
  69. }} // namespace boost::geometry
  70. #endif // BOOST_GEOMETRY_SRS_SHARED_GRIDS_STD_HPP