1 | /* |
2 | * SPDX-License-Identifier: Apache-2.0 |
3 | */ |
4 | |
5 | #pragma once |
6 | |
7 | // This file contains backports of STL features for newer C++. |
8 | |
9 | #include <memory> |
10 | #include <type_traits> |
11 | |
12 | /* |
13 | * Use MOVE_CAPTURE_IF_CPP14 in a lambda capture so it gets |
14 | * copied in C++11 and moved in C++14. |
15 | * Example: |
16 | * std::string mystring; |
17 | * auto lambda = [MOVE_CAPTURE_IF_CPP14(mystring)] { |
18 | * std::cout << mystring; |
19 | * } |
20 | */ |
21 | #ifdef __cpp_init_captures |
22 | #define MOVE_CAPTURE_IF_CPP14(variable) variable = std::move(variable) |
23 | #else |
24 | #define MOVE_CAPTURE_IF_CPP14(variable) variable |
25 | #endif |
26 | |
27 | /** |
28 | * For exception safety and consistency with make_shared. Erase me when |
29 | * we have std::make_unique(). |
30 | * |
31 | * @author Louis Brandy (ldbrandy@fb.com) |
32 | * @author Xu Ning (xning@fb.com) |
33 | * @author Sebastian Messmer (messmer@fb.com) |
34 | */ |
35 | |
36 | #if __cplusplus >= 201402L || __cpp_lib_make_unique >= 201304L || (__ANDROID__ && __cplusplus >= 201300L) || \ |
37 | _MSC_VER >= 1900 |
38 | |
39 | namespace ONNX_NAMESPACE { |
40 | using std::make_unique; |
41 | } |
42 | |
43 | #else |
44 | |
45 | namespace ONNX_NAMESPACE { |
46 | |
47 | template <typename T, typename... Args> |
48 | typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type make_unique(Args&&... args) { |
49 | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
50 | } |
51 | |
52 | // Allows 'make_unique<T[]>(10)'. (N3690 s20.9.1.4 p3-4) |
53 | template <typename T> |
54 | typename std::enable_if<std::is_array<T>::value, std::unique_ptr<T>>::type make_unique(const size_t n) { |
55 | return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]()); |
56 | } |
57 | |
58 | // Disallows 'make_unique<T[10]>()'. (N3690 s20.9.1.4 p5) |
59 | template <typename T, typename... Args> |
60 | typename std::enable_if<std::extent<T>::value != 0, std::unique_ptr<T>>::type make_unique(Args&&...) = delete; |
61 | |
62 | } // namespace ONNX_NAMESPACE |
63 | |
64 | #endif |
65 | |