proxygen
opt.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #if __cpp_lib_optional >= 201606
19 #include <optional> // @manual
20 #endif
21 #include <type_traits>
22 
23 namespace folly {
24 namespace pushmi {
25 namespace detail {
26 #if __cpp_lib_optional >= 201606
27 template <class T>
28 struct opt : private std::optional<T> {
29  opt() = default;
30  opt& operator=(T&& t) {
31  this->std::optional<T>::operator=(std::move(t));
32  return *this;
33  }
34  using std::optional<T>::operator*;
35  using std::optional<T>::operator bool;
36 };
37 #else
38 template <class T>
39 struct opt {
40  private:
41  bool empty_ = true;
42  std::aligned_union_t<0, T> data_;
43  T* ptr() {
44  return static_cast<T*>((void*)&data_);
45  }
46  const T* ptr() const {
47  return static_cast<const T*>((const void*)&data_);
48  }
49  void reset() {
50  if (!empty_) {
51  ptr()->~T();
52  empty_ = true;
53  }
54  }
55 
56  public:
57  opt() = default;
58  opt(T&& t) noexcept(std::is_nothrow_move_constructible<T>::value) {
59  ::new (ptr()) T(std::move(t));
60  empty_ = false;
61  }
62  opt(const T& t) {
63  ::new (ptr()) T(t);
64  empty_ = false;
65  }
66  opt(opt&& that) noexcept(std::is_nothrow_move_constructible<T>::value) {
67  if (that) {
68  ::new (ptr()) T(std::move(*that));
69  empty_ = false;
70  that.reset();
71  }
72  }
73  opt(const opt& that) {
74  if (that) {
75  ::new (ptr()) T(*that);
76  empty_ = false;
77  }
78  }
79  ~opt() {
80  reset();
81  }
83  std::is_nothrow_move_constructible<T>::value&&
84  std::is_nothrow_move_assignable<T>::value) {
85  if (*this && that) {
86  **this = std::move(*that);
87  that.reset();
88  } else if (*this) {
89  reset();
90  } else if (that) {
91  ::new (ptr()) T(std::move(*that));
92  empty_ = false;
93  }
94  return *this;
95  }
96  opt& operator=(const opt& that) {
97  if (*this && that) {
98  **this = *that;
99  } else if (*this) {
100  reset();
101  } else if (that) {
102  ::new (ptr()) T(*that);
103  empty_ = false;
104  }
105  return *this;
106  }
108  std::is_nothrow_move_constructible<T>::value&&
109  std::is_nothrow_move_assignable<T>::value) {
110  if (*this)
111  **this = std::move(t);
112  else {
113  ::new (ptr()) T(std::move(t));
114  empty_ = false;
115  }
116  return *this;
117  }
118  opt& operator=(const T& t) {
119  if (*this)
120  **this = t;
121  else {
122  ::new (ptr()) T(t);
123  empty_ = false;
124  }
125  return *this;
126  }
127  explicit operator bool() const noexcept {
128  return !empty_;
129  }
131  return *ptr();
132  }
134  return *ptr();
135  }
136 };
137 #endif
138 
139 } // namespace detail
140 } // namespace pushmi
141 } // namespace folly
opt(const opt &that)
Definition: opt.h:73
opt(const T &t)
Definition: opt.h:62
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
T & operator*() noexcept
Definition: opt.h:130
opt & operator=(T &&t) noexcept(std::is_nothrow_move_constructible< T >::value &&std::is_nothrow_move_assignable< T >::value)
Definition: opt.h:107
opt & operator=(const T &t)
Definition: opt.h:118
const T & operator*() const noexcept
Definition: opt.h:133
opt & operator=(const opt &that)
Definition: opt.h:96
opt(T &&t) noexcept(std::is_nothrow_move_constructible< T >::value)
Definition: opt.h:58
const
Definition: upload.py:398
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
opt & operator=(opt &&that) noexcept(std::is_nothrow_move_constructible< T >::value &&std::is_nothrow_move_assignable< T >::value)
Definition: opt.h:82
opt(opt &&that) noexcept(std::is_nothrow_move_constructible< T >::value)
Definition: opt.h:66
std::aligned_union_t< 0, T > data_
Definition: opt.h:42
const T * ptr() const
Definition: opt.h:46