Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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/modules.hh>
25#ifndef SEASTAR_MODULE
26#include <iostream>
27#include <optional>
28#include <type_traits>
29#include <fmt/core.h>
30#endif
31
32namespace seastar {
33
34template<typename T>
35concept OptimizableOptional =
36 std::is_default_constructible_v<T>
37 && std::is_nothrow_move_assignable_v<T>
38 && requires(const T& obj) {
39 { bool(obj) } noexcept;
40 };
41
46SEASTAR_MODULE_EXPORT
47template<typename T>
49 T _object;
50public:
51 optimized_optional() = default;
52 optimized_optional(std::nullopt_t) noexcept { }
53 optimized_optional(const T& obj) : _object(obj) { }
54 optimized_optional(T&& obj) noexcept : _object(std::move(obj)) { }
55 optimized_optional(std::optional<T>&& obj) noexcept {
56 if (obj) {
57 _object = std::move(*obj);
58 }
59 }
60 optimized_optional(const optimized_optional&) = default;
62
63 optimized_optional& operator=(std::nullopt_t) noexcept {
64 _object = T();
65 return *this;
66 }
67 template<typename U>
68 std::enable_if_t<std::is_same_v<std::decay_t<U>, T>, optimized_optional&>
69 operator=(U&& obj) noexcept {
70 _object = std::forward<U>(obj);
71 return *this;
72 }
73 optimized_optional& operator=(const optimized_optional&) = default;
74 optimized_optional& operator=(optimized_optional&&) = default;
75
76 explicit operator bool() const noexcept {
77 return bool(_object);
78 }
79
80 T* operator->() noexcept { return &_object; }
81 const T* operator->() const noexcept { return &_object; }
82
83 T& operator*() noexcept { return _object; }
84 const T& operator*() const noexcept { return _object; }
85
86 bool operator==(const optimized_optional& other) const {
87 return _object == other._object;
88 }
89 bool operator!=(const optimized_optional& other) const {
90 return _object != other._object;
91 }
92 friend std::ostream& operator<<(std::ostream& out, const optimized_optional& opt) {
93 if (!opt) {
94 return out << "null";
95 }
96 return out << *opt;
97 }
98};
99
100}
101
102template <typename T>
103struct fmt::formatter<seastar::optimized_optional<T>> : fmt::formatter<string_view> {
104 auto format(const seastar::optimized_optional<T>& opt, fmt::format_context& ctx) const {
105 if (opt) {
106 return fmt::format_to(ctx.out(), "{}", *opt);
107 }
108 return fmt::format_to(ctx.out(), "null");
109 }
110};
Definition: optimized_optional.hh:48
Seastar API namespace.
Definition: abort_on_ebadf.hh:26