// Copyright Takatoshi Kondo 2019 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #if !defined(MQTT_SHARED_PTR_ARRAY_HPP) #define MQTT_SHARED_PTR_ARRAY_HPP #if defined(_DOXYGEN_) /** * @brief Type alias of shared_ptr char array. * You can choose the target type. * - If MQTT_STD_SHARED_PTR_ARRAY is defined, `std::shared_ptr` is used. * - std::shared_ptr is supported since C++17. * - If MQTT_STD_SHARED_PTR_ARRAY is not defined (default), `boost::shared_ptr` is used. * - `boost::shared_ptr` can be used on C++14. */ using shared_ptr_array = std::shared_ptr; using const_shared_ptr_array = std::shared_ptr; /** * @brief shared_ptr_array creating function. * You can choose the target type. * - If MQTT_STD_SHARED_PTR_ARRAY is defined, * - and if your compiler setting is C++20 or later, then `std::make_shared(size)` is used. * - It can allocate an array of characters and the control block in a single allocation. * - otherwise `std::shared_ptr(new char[size])` is used. * - It requires two times allocations. * - If MQTT_STD_SHARED_PTR_ARRAY is not defined (default), then `boost::make_shared(size)` is used. * - It can allocate an array of characters and the control block in a single allocation. */ inline shared_ptr_array make_shared_ptr_array(std::size_t size); #else // defined(_DOXYGEN_) #include #ifdef MQTT_STD_SHARED_PTR_ARRAY #include namespace MQTT_NS { using shared_ptr_array = std::shared_ptr; using const_shared_ptr_array = std::shared_ptr; inline shared_ptr_array make_shared_ptr_array(std::size_t size) { #if __cpp_lib_shared_ptr_arrays >= 201707L return std::make_shared(size); #else // __cpp_lib_shared_ptr_arrays >= 201707L return std::shared_ptr(new char[size]); #endif // __cpp_lib_shared_ptr_arrays >= 201707L } } // namespace MQTT_NS #else // MQTT_STD_SHARED_PTR_ARRAY #include #include namespace MQTT_NS { using shared_ptr_array = boost::shared_ptr; using const_shared_ptr_array = boost::shared_ptr; inline shared_ptr_array make_shared_ptr_array(std::size_t size) { return boost::make_shared(size); } } // namespace MQTT_NS #endif // MQTT_STD_SHARED_PTR_ARRAY #endif // defined(_DOXYGEN_) #endif // MQTT_SHARED_PTR_ARRAY_HPP