olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
optional.h
1/*
2 * Copyright (C) 2025 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#if (__cplusplus >= 201703L) && defined(OLP_CPP_SDK_USE_STD_OPTIONAL)
23#include <optional>
24
25namespace olp {
26namespace porting {
27
28template <typename T>
29using optional = std::optional<T>;
30constexpr auto none = std::nullopt;
31
32template <typename T, typename U = T>
33constexpr auto value_or(const optional<T>& opt, U&& def) {
34 return opt.value_or(std::forward<U>(def));
35}
36
37template <typename T>
38constexpr const T* get_ptr(const optional<T>& opt) {
39 return opt ? &*opt : nullptr;
40}
41
42template <typename T>
43constexpr T* get_ptr(optional<T>& opt) {
44 return opt ? &*opt : nullptr;
45}
46
47template <typename T>
48optional<std::decay_t<T>> make_optional(T&& v) {
49 return optional<std::decay_t<T>>(std::forward<T>(v));
50}
51
52} // namespace porting
53} // namespace olp
54
55#else
56#include <boost/optional.hpp>
57
58namespace olp {
59namespace porting {
60
61template <typename T>
62using optional = boost::optional<T>;
63
64constexpr auto none = boost::none;
65
66template <typename T>
67constexpr typename optional<T>::reference_const_type value_or(
68 const optional<T>& opt, typename optional<T>::reference_const_type def) {
69 return opt.get_value_or(def);
70}
71
72template <typename T>
73constexpr typename optional<T>::reference_type value_or(
74 optional<T>& opt, typename optional<T>::reference_type def) {
75 return opt.get_value_or(def);
76}
77
78template <typename T>
79constexpr typename optional<T>::pointer_const_type get_ptr(
80 const optional<T>& opt) {
81 return opt.get_ptr();
82}
83
84template <typename T>
85constexpr typename optional<T>::pointer_type get_ptr(optional<T>& opt) {
86 return opt.get_ptr();
87}
88
89template <typename T>
90optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional(T&& v) {
91 return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(
92 boost::forward<T>(v));
93}
94
95} // namespace porting
96} // namespace olp
97
98#endif
Rules all the other namespaces.
Definition AppleSignInProperties.h:24