olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
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
36namespace std {
37namespace {
38template <class T>
39struct unique_if {
40 typedef unique_ptr<T> single_object;
41};
42
43template <class T>
44struct unique_if<T[]> {
45 typedef unique_ptr<T[]> unknown_bound;
46};
47
48template <class T, size_t N>
49struct unique_if<T[N]> {
50 typedef void known_bound;
51};
52} // namespace
53
61template <class T, class... Args>
62typename unique_if<T>::single_object make_unique(Args&&... args) {
63 return unique_ptr<T>(new T(std::forward<Args>(args)...));
64}
65
72template <class T>
73typename 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
78template <class T, class... Args>
79typename unique_if<T>::known_bound make_unique(Args&&...) = delete;
80
81} // namespace std
82
83#endif // HAVE_STD_MAKE_UNIQUE