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 #include <seastar/util/modules.hh>
27 #ifndef SEASTAR_MODULE
28 #include <iostream>
29 #include <memory>
30 #include <type_traits>
31 #endif
32 
33 namespace seastar {
34 
35 SEASTAR_CONCEPT(
36 
37 template<typename T>
38 concept OptimizableOptional =
39  std::is_default_constructible_v<T>
40  && std::is_nothrow_move_assignable_v<T>
41  && requires(const T& obj) {
42  { bool(obj) } noexcept;
43  };
44 
45 )
46 
51 SEASTAR_MODULE_EXPORT
52 template<typename T>
54  T _object;
55 public:
56  optimized_optional() = default;
57  optimized_optional(std::nullopt_t) noexcept { }
58  optimized_optional(const T& obj) : _object(obj) { }
59  optimized_optional(T&& obj) noexcept : _object(std::move(obj)) { }
60  optimized_optional(std::optional<T>&& obj) noexcept {
61  if (obj) {
62  _object = std::move(*obj);
63  }
64  }
65  optimized_optional(const optimized_optional&) = default;
67 
68  optimized_optional& operator=(std::nullopt_t) noexcept {
69  _object = T();
70  return *this;
71  }
72  template<typename U>
73  std::enable_if_t<std::is_same_v<std::decay_t<U>, T>, optimized_optional&>
74  operator=(U&& obj) noexcept {
75  _object = std::forward<U>(obj);
76  return *this;
77  }
78  optimized_optional& operator=(const optimized_optional&) = default;
79  optimized_optional& operator=(optimized_optional&&) = default;
80 
81  explicit operator bool() const noexcept {
82  return bool(_object);
83  }
84 
85  T* operator->() noexcept { return &_object; }
86  const T* operator->() const noexcept { return &_object; }
87 
88  T& operator*() noexcept { return _object; }
89  const T& operator*() const noexcept { return _object; }
90 
91  bool operator==(const optimized_optional& other) const {
92  return _object == other._object;
93  }
94  bool operator!=(const optimized_optional& other) const {
95  return _object != other._object;
96  }
97  friend std::ostream& operator<<(std::ostream& out, const optimized_optional& opt) {
98  if (!opt) {
99  return out << "null";
100  }
101  return out << *opt;
102  }
103 };
104 
105 }
Definition: optimized_optional.hh:53
Seastar API namespace.
Definition: abort_on_ebadf.hh:26