1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___OPTIONAL_COMMON_H
11#define _LIBCPP___OPTIONAL_COMMON_H
12
13#include <__assert>
14#include <__config>
15#include <__exception/exception.h>
16#include <__fwd/format.h>
17#include <__ranges/enable_borrowed_range.h>
18#include <__ranges/enable_view.h>
19#include <__type_traits/is_object.h>
20#include <__type_traits/is_reference.h>
21#include <__verbose_abort>
22
23#include <__fwd/optional.h>
24
25#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26# pragma GCC system_header
27#endif
28
29_LIBCPP_PUSH_MACROS
30#include <__undef_macros>
31
32namespace std // purposefully not using versioning namespace
33{
34
35class _LIBCPP_EXPORTED_FROM_ABI bad_optional_access : public exception {
36public:
37 _LIBCPP_HIDE_FROM_ABI bad_optional_access() _NOEXCEPT = default;
38 _LIBCPP_HIDE_FROM_ABI bad_optional_access(const bad_optional_access&) _NOEXCEPT = default;
39 _LIBCPP_HIDE_FROM_ABI bad_optional_access& operator=(const bad_optional_access&) _NOEXCEPT = default;
40 // Get the key function ~bad_optional_access() into the dylib
41 ~bad_optional_access() _NOEXCEPT override;
42 [[__nodiscard__]] const char* what() const _NOEXCEPT override;
43};
44
45} // namespace std
46
47#if _LIBCPP_STD_VER >= 17
48
49_LIBCPP_BEGIN_NAMESPACE_STD
50
51[[noreturn]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_optional_access() {
52# if _LIBCPP_HAS_EXCEPTIONS
53 throw bad_optional_access();
54# else
55 _LIBCPP_VERBOSE_ABORT("bad_optional_access was thrown in -fno-exceptions mode");
56# endif
57}
58
59struct __optional_construct_from_invoke_tag {};
60
61template <class _Tp>
62inline constexpr bool __is_std_optional_v = false;
63
64template <class _Tp>
65inline constexpr bool __is_std_optional_v<optional<_Tp>> = true;
66
67# if _LIBCPP_STD_VER < 26
68template <class _Tp>
69inline constexpr bool __is_valid_optional_contained_type = is_object_v<_Tp>;
70# else
71template <class _Tp>
72inline constexpr bool __is_valid_optional_contained_type = is_object_v<_Tp> || is_lvalue_reference_v<_Tp>;
73# endif
74
75# if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
76
77template <class _Tp>
78constexpr bool ranges::enable_view<optional<_Tp>> = true;
79
80template <class _Tp>
81constexpr range_format format_kind<optional<_Tp>> = range_format::disabled;
82
83template <class _Tp>
84constexpr bool ranges::enable_borrowed_range<optional<_Tp&>> = true;
85
86# endif // _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
87
88_LIBCPP_END_NAMESPACE_STD
89
90#endif // _LIBCPP_STD_VER >= 17
91
92_LIBCPP_POP_MACROS
93
94#endif // _LIBCPP___OPTIONAL_COMMON_H
95