| 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or |
| 3 | * modify it under the terms of the GNU General Public License |
| 4 | * as published by the Free Software Foundation; either version 2 |
| 5 | * of the License, or (at your option) any later version. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 15 | */ |
| 16 | |
| 17 | #ifndef MAKE_UNIQUE_H |
| 18 | #define MAKE_UNIQUE_H |
| 19 | |
| 20 | #include "common/setup_before.h" |
| 21 | |
| 22 | #ifndef HAVE_MAKE_UNIQUE |
| 23 | |
| 24 | #include <memory> |
| 25 | #include <type_traits> |
| 26 | #include <utility> |
| 27 | |
| 28 | namespace pvpgn |
| 29 | { |
| 30 | //https://isocpp.org/files/papers/N3656.txt |
| 31 | |
| 32 | template<class T> struct _Unique_if { |
| 33 | typedef std::unique_ptr<T> _Single_object; |
| 34 | }; |
| 35 | |
| 36 | template<class T> struct _Unique_if<T[]> { |
| 37 | typedef std::unique_ptr<T[]> _Unknown_bound; |
| 38 | }; |
| 39 | |
| 40 | template<class T, size_t N> struct _Unique_if<T[N]> { |
| 41 | typedef void _Known_bound; |
| 42 | }; |
| 43 | |
| 44 | template<class T, class... Args> |
| 45 | typename _Unique_if<T>::_Single_object |
| 46 | make_unique(Args&&... args) { |
| 47 | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| 48 | } |
| 49 | |
| 50 | template<class T> |
| 51 | typename _Unique_if<T>::_Unknown_bound |
| 52 | make_unique(size_t n) { |
| 53 | typedef typename remove_extent<T>::type U; |
| 54 | return std::unique_ptr<T>(new U[n]()); |
| 55 | } |
| 56 | |
| 57 | template<class T, class... Args> |
| 58 | typename _Unique_if<T>::_Known_bound |
| 59 | make_unique(Args&&...) = delete; |
| 60 | } |
| 61 | #else |
| 62 | #define make_unique std::make_unique |
| 63 | #endif //HAVE_MAKE_UNIQUE |
| 64 | #endif //MAKE_UNIQUE_H |