Hyper API for C++ 0.0.18161
Hyper client library for C++ applications
Loading...
Searching...
No Matches
optional.hpp
Go to the documentation of this file.
1
5#ifndef TABLEAU_HYPER_OPTIONAL_HPP
6#define TABLEAU_HYPER_OPTIONAL_HPP
7
8#if !defined(__cplusplus) || (__cplusplus < 201703L) /* Use own optional if C++ is older than C++17 */
9#define hyper_use_own_optional
10#endif
11
12#ifdef hyper_use_own_optional
13#include <exception>
14#include <type_traits>
15#include <utility>
16#else
17#include <optional>
18#endif
19
20#ifdef hyper_use_own_optional
21// Our optional surrogate does not support `constexpr`
22#define CONSTEXPR_OPTIONAL const
23#else
24#define CONSTEXPR_OPTIONAL constexpr
25#endif
26
27namespace hyperapi {
28#ifndef hyper_use_own_optional /* C++17 or greater */
29template <typename T>
30using optional = std::optional<T>;
31using bad_optional_access = std::bad_optional_access;
32#else
34class bad_optional_access : public std::exception {
35 using std::exception::exception;
36};
37
39template <typename T>
40class optional {
41 private:
43 alignas(T) char data[sizeof(T)];
44
46 bool exists = false;
47
48 public:
50 using value_type = T;
51
53 optional() noexcept = default;
54
56 optional(const optional& other);
57
60
62 template <
63 typename U = value_type,
64 typename = typename std::enable_if<!std::is_same<typename std::decay<U>::type, optional<T>>::value>::type>
66
68 optional& operator=(const optional& other);
69
71 optional& operator=(optional&& other);
72
74 template <typename Other>
75 bool operator==(const optional<Other>& other) const noexcept {
76 return (exists == other.exists) && ((!exists) || (**this == *other));
77 }
79 template <typename Other>
80 bool operator!=(const optional<Other>& other) const noexcept { return !(*this == other); }
81
84
86 bool has_value() const noexcept { return exists; }
88 explicit operator bool() const noexcept { return exists; }
89
91 T& value() & {
92 if (!*this) {
93 throw bad_optional_access{};
94 }
95 return **this;
96 }
98 const T& value() const& {
99 if (!*this) {
100 throw bad_optional_access{};
101 }
102 return **this;
103 }
105 T&& value() && {
106 if (!*this) {
107 throw bad_optional_access{};
108 }
109 return **this;
110 }
112 const T&& value() const&& {
113 if (!*this) {
114 throw bad_optional_access{};
115 }
116 return **this;
117 }
118
120 template <typename U>
121 T value_or(U&& default_value) const& {
122 return bool(*this) ? **this : static_cast<T>(std::forward<U>(default_value));
123 }
124
126 template <typename U>
127 T value_or(U&& default_value) && {
128 return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(default_value));
129 }
130
132 T* operator->() noexcept { return ptr(); }
134 const T* operator->() const noexcept { return ptr(); }
135
137 const T& operator*() const& { return *ptr(); }
139 T& operator*() & { return *ptr(); }
141 const T&& operator*() const&& { return *ptr(); }
143 T&& operator*() && { return std::move(*ptr()); }
144
146 void reset() noexcept;
148 void swap(optional& other);
150 template <typename... Args>
151 void emplace(Args&&... args);
152
153 private:
155 template <typename... Args>
156 void create(Args&&... args);
157
159 T* ptr() noexcept { return exists ? reinterpret_cast<T*>(data) : nullptr; }
161 const T* ptr() const noexcept { return exists ? reinterpret_cast<const T*>(data) : nullptr; }
162};
163
164#endif
165}
166#include <hyperapi/impl/optional.impl.hpp>
167#endif
Surrogate for C++17 std::bad_optional_access
Definition optional.hpp:34
Surrogate for C++17 std::optional
Definition optional.hpp:40
T & operator*() &
Value access.
Definition optional.hpp:139
const T && operator*() const &&
Value access.
Definition optional.hpp:141
const T && value() const &&
Value access.
Definition optional.hpp:112
T value_or(U &&default_value) &&
Value or default.
Definition optional.hpp:127
void reset() noexcept
Reset.
T && operator*() &&
Value access.
Definition optional.hpp:143
T value_type
The value type.
Definition optional.hpp:50
const T & value() const &
Value access.
Definition optional.hpp:98
~optional()
Destructor.
Definition optional.hpp:83
optional() noexcept=default
Constructor.
T value_or(U &&default_value) const &
Value or default.
Definition optional.hpp:121
T && value() &&
Value access.
Definition optional.hpp:105
bool operator!=(const optional< Other > &other) const noexcept
Comparison.
Definition optional.hpp:80
bool has_value() const noexcept
Checks whether *this contains a value.
Definition optional.hpp:86
void swap(optional &other)
Swap.
const T * operator->() const noexcept
Value access.
Definition optional.hpp:134
T & value() &
Value access.
Definition optional.hpp:91
const T & operator*() const &
Value access.
Definition optional.hpp:137
void emplace(Args &&... args)
Emplace.
T * operator->() noexcept
Value access.
Definition optional.hpp:132
The primary namespace of the Hyper API for C++.
Definition ByteSpan.hpp:15