shared_resource.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/json/memory_resource.hpp>
  12. #include <atomic>
  13. #include <utility>
  14. namespace boost {
  15. namespace json {
  16. namespace detail {
  17. #ifdef _MSC_VER
  18. #pragma warning(push)
  19. #pragma warning(disable: 4275) // non dll-interface class used as base for dll-interface class
  20. #endif
  21. struct BOOST_SYMBOL_VISIBLE
  22. shared_resource
  23. : memory_resource
  24. {
  25. BOOST_JSON_DECL
  26. shared_resource();
  27. BOOST_JSON_DECL
  28. ~shared_resource();
  29. std::atomic<std::size_t> refs{ 1 };
  30. };
  31. template<class T>
  32. class shared_resource_impl final
  33. : public shared_resource
  34. {
  35. T t;
  36. public:
  37. template<class... Args>
  38. shared_resource_impl(
  39. Args&&... args)
  40. : t(std::forward<Args>(args)...)
  41. {
  42. }
  43. void*
  44. do_allocate(
  45. std::size_t n,
  46. std::size_t align) override
  47. {
  48. return t.allocate(n, align);
  49. }
  50. void
  51. do_deallocate(
  52. void* p,
  53. std::size_t n,
  54. std::size_t align) override
  55. {
  56. return t.deallocate(p, n, align);
  57. }
  58. bool
  59. do_is_equal(
  60. memory_resource const&) const noexcept override
  61. {
  62. // VFALCO Is always false ok?
  63. return false;
  64. }
  65. };
  66. #ifdef _MSC_VER
  67. #pragma warning(pop)
  68. #endif
  69. } // detail
  70. } // namespace json
  71. } // namespace boost
  72. #endif