shared_resource.hpp 1.6 KB

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