Seastar
High performance C++ framework for concurrent servers
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 
29 namespace seastar {
30 
39 template<typename Func>
40 class lazy_eval {
41 private:
42  Func _func;
43 
44 private:
45  lazy_eval(Func&& f) : _func(std::forward<Func>(f)) {}
46 
47 public:
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 
76 template <typename Func>
77 inline lazy_eval<Func> value_of(Func&& func) {
78  return lazy_eval<Func>(std::forward<Func>(func));
79 }
80 
89 template <typename T>
91  const T& p;
92 
93  constexpr lazy_deref_wrapper(const T& p) : p(p) {
94  }
95 };
96 
106 template <typename T>
108 lazy_deref(const T& p) {
109  return lazy_deref_wrapper<T>(p);
110 }
111 
112 }
113 
114 namespace std {
130 template <typename Func>
131 ostream& operator<<(ostream& os, const seastar::lazy_eval<Func>& lf) {
132  return os << lf();
133 }
134 
135 template <typename Func>
136 ostream& operator<<(ostream& os, seastar::lazy_eval<Func>& lf) {
137  return os << lf();
138 }
139 
140 template <typename Func>
141 ostream& operator<<(ostream& os, seastar::lazy_eval<Func>&& lf) {
142  return os << lf();
143 }
144 
145 template <typename T>
146 ostream& 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 
155 template <typename Func>
156 struct fmt::formatter<seastar::lazy_eval<Func>> : fmt::formatter<std::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 
163 template <typename T>
164 struct fmt::formatter<seastar::lazy_deref_wrapper<T>> : fmt::formatter<std::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_deref_wrapper< T > lazy_deref(const T &p)
Definition: lazy.hh:108
lazy_eval< Func > value_of(Func &&func)
Definition: lazy.hh:77
This struct is a wrapper for lazy dereferencing a pointer.
Definition: lazy.hh:90