Optional.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#ifndef SQUID__SRC_BASE_OPTIONAL_H
10#define SQUID__SRC_BASE_OPTIONAL_H
11
12#include <exception>
13#include <ostream>
14#include <type_traits>
15#include <utility>
16
18class BadOptionalAccess: public std::exception
19{
20public:
22 /* std::exception API */
23 virtual const char* what() const noexcept override { return "bad-optional-access"; }
24 virtual ~BadOptionalAccess() noexcept = default;
25};
26
28template <typename Value>
30{
31public:
32 constexpr Optional() noexcept: dummy_() {}
33 constexpr explicit Optional(const Value &v): value_(v), hasValue_(true) {}
34
36 {
37 // XXX: This simplified implementation does not keep the destructor
38 // trivial for trivial Value types, but optimizing compilers still
39 // optimize such destruction away, and that is sufficient for our
40 // current needs.
41 reset();
42 }
43
44 Optional(const Optional &other): Optional()
45 {
46 if (other.hasValue_)
47 *this = other.value_;
48 }
49
50 Optional &operator =(const Optional &other)
51 {
52 if (this != &other) {
53 if (other.hasValue_)
54 *this = other.value_;
55 else
56 reset();
57 }
58 return *this;
59 }
60
62 {
63 if (other.hasValue_) {
64 *this = std::move(other.value_);
65 // no other.reset() per std::optional move semantics
66 }
67 }
68
69 Optional &operator =(Optional<Value> &&other)
70 {
71 if (this != &other) {
72 if (other.hasValue_) {
73 *this = std::move(other.value_);
74 // no other.reset() per std::optional move semantics
75 } else {
76 reset();
77 }
78 }
79 return *this;
80 }
81
82 constexpr explicit operator bool() const noexcept { return hasValue_; }
83 constexpr bool has_value() const noexcept { return hasValue_; }
84
85 const Value &value() const &
86 {
87 if (!hasValue_)
88 throw BadOptionalAccess();
89 return value_;
90 }
91
92 template <class Other>
93 constexpr Value value_or(Other &&defaultValue) const &
94 {
95 return hasValue_ ? value_ : static_cast<Value>(std::forward<Other>(defaultValue));
96 }
97
98 template <class Other = Value>
99 Optional &operator =(Other &&otherValue)
100 {
101 value_ = std::forward<Other>(otherValue);
102 hasValue_ = true;
103 return *this;
104 }
105
106 void reset() {
107 if (hasValue_) {
108 hasValue_ = false;
109 value_.~Value();
110 }
111 }
112
113private:
114 union {
116 struct {} dummy_;
117
119 Value value_;
120 };
121
122 bool hasValue_ = false;
123};
124
125template <typename Value>
126inline
127std::ostream &operator <<(std::ostream &os, const Optional<Value> &opt)
128{
129 if (opt.has_value())
130 os << opt.value();
131 else
132 os << "[no value]";
133 return os;
134}
135
136#endif /* SQUID__SRC_BASE_OPTIONAL_H */
137
#define true
Definition: GnuRegex.c:241
std::ostream & operator<<(std::ostream &os, const Optional< Value > &opt)
Definition: Optional.h:127
std::bad_optional_access replacement (until we upgrade to C++17)
Definition: Optional.h:19
virtual const char * what() const noexcept override
Definition: Optional.h:23
virtual ~BadOptionalAccess() noexcept=default
(limited) std::optional replacement (until we upgrade to C++17)
Definition: Optional.h:30
Value value_
stored value; inaccessible/uninitialized unless hasValue_
Definition: Optional.h:119
constexpr Optional() noexcept
Definition: Optional.h:32
constexpr Value value_or(Other &&defaultValue) const &
Definition: Optional.h:93
void reset()
Definition: Optional.h:106
const Value & value() const &
Definition: Optional.h:85
Optional(Optional< Value > &&other)
Definition: Optional.h:61
Optional(const Optional &other)
Definition: Optional.h:44
bool hasValue_
Definition: Optional.h:122
constexpr bool has_value() const noexcept
Definition: Optional.h:83
~Optional()
Definition: Optional.h:35
constexpr Optional(const Value &v)
Definition: Optional.h:33
pid_t Other()
Definition: Instance.cc:128
char * class
Definition: smb-errors.c:146

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors