olp-cpp-sdk  1.22.0
make_unique.h
1 /*
2  * Copyright (C) 2019-2021 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <memory>
23 
24 #if ((__cplusplus >= 201304L) || (defined(_MSC_VER) && _MSC_VER >= 1800))
25 #define HAVE_STD_MAKE_UNIQUE
26 #endif
27 
28 #if !defined(HAVE_STD_MAKE_UNIQUE)
29 
30 #include <cstdint>
31 #include <type_traits>
32 #include <utility>
33 
34 // Implementation is taken from here https://isocpp.org/files/papers/N3656.txt
35 
36 namespace std {
37 namespace {
38 template <class T>
39 struct unique_if {
40  typedef unique_ptr<T> single_object;
41 };
42 
43 template <class T>
44 struct unique_if<T[]> {
45  typedef unique_ptr<T[]> unknown_bound;
46 };
47 
48 template <class T, size_t N>
49 struct unique_if<T[N]> {
50  typedef void known_bound;
51 };
52 } // namespace
53 
61 template <class T, class... Args>
62 typename unique_if<T>::single_object make_unique(Args&&... args) {
63  return unique_ptr<T>(new T(std::forward<Args>(args)...));
64 }
65 
72 template <class T>
73 typename unique_if<T>::unknown_bound make_unique(size_t n) {
74  typedef typename remove_extent<T>::type U;
75  return unique_ptr<T>(new U[n]());
76 }
77 
78 template <class T, class... Args>
79 typename unique_if<T>::known_bound make_unique(Args&&...) = delete;
80 
81 } // namespace std
82 
83 #endif // HAVE_STD_MAKE_UNIQUE