Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
lazy.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) 2016 Cloudius Systems, Ltd.
20 */
21#pragma once
22
23#include <ostream>
24#include <fmt/core.h>
25
28
29namespace seastar {
30
39template<typename Func>
40class lazy_eval {
41private:
42 Func _func;
43
44private:
45 lazy_eval(Func&& f) : _func(std::forward<Func>(f)) {}
46
47public:
51 auto operator()() {
52 return _func();
53 }
54
58 auto operator()() const {
59 return _func();
60 }
61
62 template <typename F>
63 friend lazy_eval<F> value_of(F&& func);
64};
65
66
76template <typename Func>
77inline lazy_eval<Func> value_of(Func&& func) {
78 return lazy_eval<Func>(std::forward<Func>(func));
79}
80
89template <typename T>
91 const T& p;
92
93 constexpr lazy_deref_wrapper(const T& p) : p(p) {
94 }
95};
96
106template <typename T>
108lazy_deref(const T& p) {
109 return lazy_deref_wrapper<T>(p);
110}
111
112}
113
114namespace std {
130template <typename Func>
131ostream& operator<<(ostream& os, const seastar::lazy_eval<Func>& lf) {
132 return os << lf();
133}
134
135template <typename Func>
136ostream& operator<<(ostream& os, seastar::lazy_eval<Func>& lf) {
137 return os << lf();
138}
139
140template <typename Func>
141ostream& operator<<(ostream& os, seastar::lazy_eval<Func>&& lf) {
142 return os << lf();
143}
144
145template <typename T>
146ostream& operator<<(ostream& os, seastar::lazy_deref_wrapper<T> ld) {
147 if (ld.p) {
148 return os << *ld.p;
149 }
150
151 return os << "null";
152}
153}
154
155template <typename Func>
156struct fmt::formatter<seastar::lazy_eval<Func>> : fmt::formatter<string_view> {
157 template <typename FormatContext>
158 auto format(const seastar::lazy_eval<Func>& lf, FormatContext& ctx) const {
159 return fmt::format_to(ctx.out(), "{}", lf());
160 }
161};
162
163template <typename T>
164struct fmt::formatter<seastar::lazy_deref_wrapper<T>> : fmt::formatter<string_view> {
165 template <typename FormatContext>
166 auto format(const seastar::lazy_deref_wrapper<T>& ld, FormatContext& ctx) const {
167 if (ld.p) {
168 return fmt::format_to(ctx.out(), "{}", *ld.p);
169 } else {
170 return fmt::format_to(ctx.out(), "null");
171 }
172 }
173};
174
This class is a wrapper for a lazy evaluation of a value.
Definition: lazy.hh:40
auto operator()() const
Evaluate a value (const version).
Definition: lazy.hh:58
auto operator()()
Evaluate a value.
Definition: lazy.hh:51
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
lazy_eval< Func > value_of(Func &&func)
Definition: lazy.hh:77
lazy_deref_wrapper< T > lazy_deref(const T &p)
Definition: lazy.hh:108
STL namespace.
ostream & operator<<(ostream &os, const seastar::lazy_eval< Func > &lf)
Definition: lazy.hh:131
This struct is a wrapper for lazy dereferencing a pointer.
Definition: lazy.hh:90