]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/leaf/detail/optional.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / leaf / detail / optional.hpp
1 #ifndef BOOST_LEAF_DETAIL_OPTIONAL_HPP_INCLUDED
2 #define BOOST_LEAF_DETAIL_OPTIONAL_HPP_INCLUDED
3
4 // Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc.
5
6 // Distributed under the Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9 #include <boost/leaf/config.hpp>
10
11 #include <utility>
12 #include <new>
13
14 namespace boost { namespace leaf {
15
16 namespace leaf_detail
17 {
18 template <class T>
19 class optional
20 {
21 int key_;
22 union { T value_; };
23
24 public:
25
26 typedef T value_type;
27
28 BOOST_LEAF_CONSTEXPR optional() noexcept:
29 key_(0)
30 {
31 }
32
33 BOOST_LEAF_CONSTEXPR optional( optional const & x ):
34 key_(x.key_)
35 {
36 if( x.key_ )
37 (void) new (&value_) T( x.value_ );
38 }
39
40 BOOST_LEAF_CONSTEXPR optional( optional && x ) noexcept:
41 key_(x.key_)
42 {
43 if( x.key_ )
44 {
45 (void) new (&value_) T( std::move(x.value_) );
46 x.reset();
47 }
48 }
49
50 BOOST_LEAF_CONSTEXPR optional( int key, T const & v ):
51 key_(key),
52 value_(v)
53 {
54 BOOST_LEAF_ASSERT(!empty());
55 }
56
57 BOOST_LEAF_CONSTEXPR optional( int key, T && v ) noexcept:
58 key_(key),
59 value_(std::move(v))
60 {
61 BOOST_LEAF_ASSERT(!empty());
62 }
63
64 BOOST_LEAF_CONSTEXPR optional & operator=( optional const & x )
65 {
66 reset();
67 if( int key = x.key() )
68 {
69 put(key, x.value_);
70 key_ = key;
71 }
72 return *this;
73 }
74
75 BOOST_LEAF_CONSTEXPR optional & operator=( optional && x ) noexcept
76 {
77 reset();
78 if( int key = x.key() )
79 {
80 put(key, std::move(x.value_));
81 x.reset();
82 }
83 return *this;
84 }
85
86 ~optional() noexcept
87 {
88 reset();
89 }
90
91 BOOST_LEAF_CONSTEXPR bool empty() const noexcept
92 {
93 return key_==0;
94 }
95
96 BOOST_LEAF_CONSTEXPR int key() const noexcept
97 {
98 return key_;
99 }
100
101 BOOST_LEAF_CONSTEXPR void reset() noexcept
102 {
103 if( key_ )
104 {
105 value_.~T();
106 key_=0;
107 }
108 }
109
110 BOOST_LEAF_CONSTEXPR T & put( int key, T const & v )
111 {
112 BOOST_LEAF_ASSERT(key);
113 reset();
114 (void) new(&value_) T(v);
115 key_=key;
116 return value_;
117 }
118
119 BOOST_LEAF_CONSTEXPR T & put( int key, T && v ) noexcept
120 {
121 BOOST_LEAF_ASSERT(key);
122 reset();
123 (void) new(&value_) T(std::move(v));
124 key_=key;
125 return value_;
126 }
127
128 BOOST_LEAF_CONSTEXPR T const * has_value(int key) const noexcept
129 {
130 BOOST_LEAF_ASSERT(key);
131 return key_==key ? &value_ : 0;
132 }
133
134 BOOST_LEAF_CONSTEXPR T * has_value(int key) noexcept
135 {
136 BOOST_LEAF_ASSERT(key);
137 return key_==key ? &value_ : 0;
138 }
139
140 BOOST_LEAF_CONSTEXPR T const & value(int key) const & noexcept
141 {
142 BOOST_LEAF_ASSERT(has_value(key) != 0);
143 return value_;
144 }
145
146 BOOST_LEAF_CONSTEXPR T & value(int key) & noexcept
147 {
148 BOOST_LEAF_ASSERT(has_value(key) != 0);
149 return value_;
150 }
151
152 BOOST_LEAF_CONSTEXPR T const && value(int key) const && noexcept
153 {
154 BOOST_LEAF_ASSERT(has_value(key) != 0);
155 return value_;
156 }
157
158 BOOST_LEAF_CONSTEXPR T value(int key) && noexcept
159 {
160 BOOST_LEAF_ASSERT(has_value(key) != 0);
161 T tmp(std::move(value_));
162 reset();
163 return tmp;
164 }
165 };
166
167 }
168
169 } }
170
171 #endif