Seastar
High performance C++ framework for concurrent servers
optimized_optional.hh
1 /*
2  * This file is open source software, licensed to you under the terms
3  * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4  * distributed with this work for additional information regarding copyright
5  * ownership. You may not use this file except in compliance with the License.
6  *
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  * KIND, either express or implied. See the License for the
15  * specific language governing permissions and limitations
16  * under the License.
17  */
18 /*
19  * Copyright (C) 2017 ScyllaDB
20  */
21 
22 #pragma once
23 
24 #include <seastar/util/concepts.hh>
25 #include <seastar/util/std-compat.hh>
26 
27 #include <type_traits>
28 #include <iostream>
29 
30 namespace seastar {
31 
32 SEASTAR_CONCEPT(
33 
34 template<typename T>
35 concept OptimizableOptional =
36  std::is_default_constructible<T>::value
37  && std::is_nothrow_move_assignable<T>::value
38  && requires(const T& obj) {
39  { bool(obj) } noexcept;
40  };
41 
42 )
43 
48 template<typename T>
50  T _object;
51 public:
52  optimized_optional() = default;
53  optimized_optional(std::nullopt_t) noexcept { }
54  optimized_optional(const T& obj) : _object(obj) { }
55  optimized_optional(T&& obj) noexcept : _object(std::move(obj)) { }
56  optimized_optional(std::optional<T>&& obj) noexcept {
57  if (obj) {
58  _object = std::move(*obj);
59  }
60  }
61  optimized_optional(const optimized_optional&) = default;
63 
64  optimized_optional& operator=(std::nullopt_t) noexcept {
65  _object = T();
66  return *this;
67  }
68  template<typename U>
69  std::enable_if_t<std::is_same<std::decay_t<U>, T>::value, optimized_optional&>
70  operator=(U&& obj) noexcept {
71  _object = std::forward<U>(obj);
72  return *this;
73  }
74  optimized_optional& operator=(const optimized_optional&) = default;
75  optimized_optional& operator=(optimized_optional&&) = default;
76 
77  explicit operator bool() const noexcept {
78  return bool(_object);
79  }
80 
81  T* operator->() noexcept { return &_object; }
82  const T* operator->() const noexcept { return &_object; }
83 
84  T& operator*() noexcept { return _object; }
85  const T& operator*() const noexcept { return _object; }
86 
87  bool operator==(const optimized_optional& other) const {
88  return _object == other._object;
89  }
90  bool operator!=(const optimized_optional& other) const {
91  return _object != other._object;
92  }
93  friend std::ostream& operator<<(std::ostream& out, const optimized_optional& opt) {
94  if (!opt) {
95  return out << "null";
96  }
97  return out << *opt;
98  }
99 };
100 
101 }
seastar
Seastar API namespace.
Definition: abort_on_ebadf.hh:24
seastar::optimized_optional
Definition: optimized_optional.hh:49