shared_resource.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_DETAIL_SHARED_RESOURCE_HPP
  10. #define BOOST_JSON_DETAIL_SHARED_RESOURCE_HPP
  11. #include <boost/container/pmr/memory_resource.hpp>
  12. #include <boost/json/memory_resource.hpp>
  13. #include <atomic>
  14. #include <utility>
  15. namespace boost {
  16. namespace json {
  17. namespace detail {
  18. #ifdef _MSC_VER
  19. #pragma warning(push)
  20. #pragma warning(disable: 4275) // non dll-interface class used as base for dll-interface class
  21. #endif
  22. struct BOOST_SYMBOL_VISIBLE
  23. shared_resource
  24. : container::pmr::memory_resource
  25. {
  26. BOOST_JSON_DECL
  27. shared_resource();
  28. BOOST_JSON_DECL
  29. ~shared_resource();
  30. std::atomic<std::size_t> refs{ 1 };
  31. };
  32. template<class T>
  33. class shared_resource_impl final
  34. : public shared_resource
  35. {
  36. T t;
  37. public:
  38. template<class... Args>
  39. shared_resource_impl(
  40. Args&&... args)
  41. : t(std::forward<Args>(args)...)
  42. {
  43. }
  44. void*
  45. do_allocate(
  46. std::size_t n,
  47. std::size_t align) override
  48. {
  49. return t.allocate(n, align);
  50. }
  51. void
  52. do_deallocate(
  53. void* p,
  54. std::size_t n,
  55. std::size_t align) override
  56. {
  57. return t.deallocate(p, n, align);
  58. }
  59. bool
  60. do_is_equal(
  61. memory_resource const&) const noexcept override
  62. {
  63. // VFALCO Is always false ok?
  64. return false;
  65. }
  66. };
  67. #ifdef _MSC_VER
  68. #pragma warning(pop)
  69. #endif
  70. } // detail
  71. } // namespace json
  72. } // namespace boost
  73. #endif