| 1 | // |
| 2 | // MakeUnique.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Core |
| 6 | // Module: MakeUnique |
| 7 | // |
| 8 | // Definition of the MakeUnique template class. This is essentially std::make_unique |
| 9 | // for pre-C++14 compilers. |
| 10 | // |
| 11 | // Code adapted for naming convention from https://isocpp.org/files/papers/N3656.txt |
| 12 | // |
| 13 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 14 | // and Contributors. |
| 15 | // |
| 16 | // SPDX-License-Identifier: BSL-1.0 |
| 17 | // |
| 18 | |
| 19 | |
| 20 | #ifndef Foundation_MakeUnique_INCLUDED |
| 21 | #define Foundation_MakeUnique_INCLUDED |
| 22 | |
| 23 | |
| 24 | #include "Poco/Foundation.h" |
| 25 | #include <cstddef> |
| 26 | #include <memory> |
| 27 | #include <type_traits> |
| 28 | #include <utility> |
| 29 | |
| 30 | |
| 31 | namespace Poco { |
| 32 | |
| 33 | |
| 34 | template<class T> struct UniqueIf |
| 35 | { |
| 36 | typedef std::unique_ptr<T> SingleObject; |
| 37 | }; |
| 38 | |
| 39 | template<class T> struct UniqueIf<T[]> |
| 40 | { |
| 41 | typedef std::unique_ptr<T[]> UnknownBound; |
| 42 | }; |
| 43 | |
| 44 | template<class T, size_t N> struct UniqueIf<T[N]> |
| 45 | { |
| 46 | typedef void KnownBound; |
| 47 | }; |
| 48 | |
| 49 | template<class T, class... Args> |
| 50 | typename UniqueIf<T>::SingleObject makeUnique(Args&&... args) |
| 51 | { |
| 52 | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| 53 | } |
| 54 | |
| 55 | template<class T> |
| 56 | typename UniqueIf<T>::UnknownBound makeUnique(size_t n) |
| 57 | { |
| 58 | typedef typename std::remove_extent<T>::type U; |
| 59 | return std::unique_ptr<T>(new U[n]()); |
| 60 | } |
| 61 | |
| 62 | template<class T, class... Args> |
| 63 | typename UniqueIf<T>::KnownBound makeUnique(Args&&...) = delete; |
| 64 | |
| 65 | |
| 66 | } // namespace Poco |
| 67 | |
| 68 | |
| 69 | #endif // Foundation_MakeUnique_INCLUDED |
| 70 | |