Hyper API for C++ 0.0.20746
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 // NOLINTNEXTLINE(modernize-type-traits)
65 typename = typename std::enable_if<!std::is_same<typename std::decay<U>::type, optional<T>>::value>::type>
67
69 optional& operator=(const optional& other);
70
72 optional& operator=(optional&& other);
73
75 template <typename Other>
76 bool operator==(const optional<Other>& other) const noexcept {
77 return (exists_ == other.exists_) && ((!exists_) || (**this == *other));
78 }
80 template <typename Other>
81 bool operator!=(const optional<Other>& other) const noexcept { return !(*this == other); }
82
85
87 bool has_value() const noexcept { return exists_; }
89 explicit operator bool() const noexcept { return exists_; }
90
92 T& value() & {
93 if (!*this) {
94 throw bad_optional_access{};
95 }
96 return **this;
97 }
99 const T& value() const& {
100 if (!*this) {
101 throw bad_optional_access{};
102 }
103 return **this;
104 }
106 T&& value() && {
107 if (!*this) {
108 throw bad_optional_access{};
109 }
110 return **this;
111 }
113 const T&& value() const&& {
114 if (!*this) {
115 throw bad_optional_access{};
116 }
117 return **this;
118 }
119
121 template <typename U>
122 T value_or(U&& default_value) const& {
123 return bool(*this) ? **this : static_cast<T>(std::forward<U>(default_value));
124 }
125
127 template <typename U>
128 T value_or(U&& default_value) && {
129 return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(default_value));
130 }
131
133 T* operator->() noexcept { return ptr(); }
135 const T* operator->() const noexcept { return ptr(); }
136
138 const T& operator*() const& { return *ptr(); }
140 T& operator*() & { return *ptr(); }
142 const T&& operator*() const&& { return *ptr(); }
144 T&& operator*() && { return std::move(*ptr()); }
145
147 void reset() noexcept;
149 void swap(optional& other);
151 template <typename... Args>
152 void emplace(Args&&... args);
153
154 private:
156 template <typename... Args>
157 void create(Args&&... args);
158
160 T* ptr() noexcept { return exists_ ? reinterpret_cast<T*>(data_) : nullptr; }
162 const T* ptr() const noexcept { return exists_ ? reinterpret_cast<const T*>(data_) : nullptr; }
163};
164
165#endif
166}
167#include <hyperapi/impl/optional.impl.hpp>
168#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:140
const T && operator*() const &&
Value access.
Definition optional.hpp:142
const T && value() const &&
Value access.
Definition optional.hpp:113
T value_or(U &&default_value) &&
Value or default.
Definition optional.hpp:128
void reset() noexcept
Reset.
T && operator*() &&
Value access.
Definition optional.hpp:144
T value_type
The value type.
Definition optional.hpp:50
const T & value() const &
Value access.
Definition optional.hpp:99
~optional()
Destructor.
Definition optional.hpp:84
optional() noexcept=default
Constructor.
T value_or(U &&default_value) const &
Value or default.
Definition optional.hpp:122
T && value() &&
Value access.
Definition optional.hpp:106
bool operator!=(const optional< Other > &other) const noexcept
Comparison.
Definition optional.hpp:81
bool has_value() const noexcept
Checks whether *this contains a value.
Definition optional.hpp:87
void swap(optional &other)
Swap.
const T * operator->() const noexcept
Value access.
Definition optional.hpp:135
T & value() &
Value access.
Definition optional.hpp:92
const T & operator*() const &
Value access.
Definition optional.hpp:138
void emplace(Args &&... args)
Emplace.
T * operator->() noexcept
Value access.
Definition optional.hpp:133
The primary namespace of the Hyper API for C++.
Definition ByteSpan.hpp:15