shared_ptr_array.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright Takatoshi Kondo 2019
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(MQTT_SHARED_PTR_ARRAY_HPP)
  7. #define MQTT_SHARED_PTR_ARRAY_HPP
  8. #if defined(_DOXYGEN_)
  9. /**
  10. * @brief Type alias of shared_ptr char array.
  11. * You can choose the target type.
  12. * - If MQTT_STD_SHARED_PTR_ARRAY is defined, `std::shared_ptr<char []>` is used.
  13. * - std::shared_ptr<char []> is supported since C++17.
  14. * - If MQTT_STD_SHARED_PTR_ARRAY is not defined (default), `boost::shared_ptr<char []>` is used.
  15. * - `boost::shared_ptr<char []>` can be used on C++14.
  16. */
  17. using shared_ptr_array = std::shared_ptr<char []>;
  18. using const_shared_ptr_array = std::shared_ptr<char const []>;
  19. /**
  20. * @brief shared_ptr_array creating function.
  21. * You can choose the target type.
  22. * - If MQTT_STD_SHARED_PTR_ARRAY is defined,
  23. * - and if your compiler setting is C++20 or later, then `std::make_shared<char[]>(size)` is used.
  24. * - It can allocate an array of characters and the control block in a single allocation.
  25. * - otherwise `std::shared_ptr<char[]>(new char[size])` is used.
  26. * - It requires two times allocations.
  27. * - If MQTT_STD_SHARED_PTR_ARRAY is not defined (default), then `boost::make_shared<char[]>(size)` is used.
  28. * - It can allocate an array of characters and the control block in a single allocation.
  29. */
  30. inline shared_ptr_array make_shared_ptr_array(std::size_t size);
  31. #else // defined(_DOXYGEN_)
  32. #include <mqtt/namespace.hpp>
  33. #ifdef MQTT_STD_SHARED_PTR_ARRAY
  34. #include <memory>
  35. namespace MQTT_NS {
  36. using shared_ptr_array = std::shared_ptr<char []>;
  37. using const_shared_ptr_array = std::shared_ptr<char const []>;
  38. inline shared_ptr_array make_shared_ptr_array(std::size_t size) {
  39. #if __cpp_lib_shared_ptr_arrays >= 201707L
  40. return std::make_shared<char[]>(size);
  41. #else // __cpp_lib_shared_ptr_arrays >= 201707L
  42. return std::shared_ptr<char[]>(new char[size]);
  43. #endif // __cpp_lib_shared_ptr_arrays >= 201707L
  44. }
  45. } // namespace MQTT_NS
  46. #else // MQTT_STD_SHARED_PTR_ARRAY
  47. #include <boost/shared_ptr.hpp>
  48. #include <boost/smart_ptr/make_shared.hpp>
  49. namespace MQTT_NS {
  50. using shared_ptr_array = boost::shared_ptr<char []>;
  51. using const_shared_ptr_array = boost::shared_ptr<char const []>;
  52. inline shared_ptr_array make_shared_ptr_array(std::size_t size) {
  53. return boost::make_shared<char[]>(size);
  54. }
  55. } // namespace MQTT_NS
  56. #endif // MQTT_STD_SHARED_PTR_ARRAY
  57. #endif // defined(_DOXYGEN_)
  58. #endif // MQTT_SHARED_PTR_ARRAY_HPP