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 
27 
28 namespace seastar {
29 
38 template<typename Func>
39 class lazy_eval {
40 private:
41  Func _func;
42 
43 private:
44  lazy_eval(Func&& f) : _func(std::forward<Func>(f)) {}
45 
46 public:
50  auto operator()() {
51  return _func();
52  }
53 
57  auto operator()() const {
58  return _func();
59  }
60 
61  template <typename F>
62  friend lazy_eval<F> value_of(F&& func);
63 };
64 
65 
75 template <typename Func>
76 inline lazy_eval<Func> value_of(Func&& func) {
77  return lazy_eval<Func>(std::forward<Func>(func));
78 }
79 
88 template <typename T>
90  const T& p;
91 
92  constexpr lazy_deref_wrapper(const T& p) : p(p) {
93  }
94 };
95 
105 template <typename T>
107 lazy_deref(const T& p) {
108  return lazy_deref_wrapper<T>(p);
109 }
110 
111 }
112 
113 namespace std {
129 template <typename Func>
130 ostream& operator<<(ostream& os, const seastar::lazy_eval<Func>& lf) {
131  return os << lf();
132 }
133 
134 template <typename Func>
135 ostream& operator<<(ostream& os, seastar::lazy_eval<Func>& lf) {
136  return os << lf();
137 }
138 
139 template <typename Func>
140 ostream& operator<<(ostream& os, seastar::lazy_eval<Func>&& lf) {
141  return os << lf();
142 }
143 
144 template <typename T>
145 ostream& operator<<(ostream& os, seastar::lazy_deref_wrapper<T> ld) {
146  if (ld.p) {
147  return os << *ld.p;
148  }
149 
150  return os << "null";
151 }
152 }
This class is a wrapper for a lazy evaluation of a value.
Definition: lazy.hh:39
auto operator()() const
Evaluate a value (const version).
Definition: lazy.hh:57
auto operator()()
Evaluate a value.
Definition: lazy.hh:50
Seastar API namespace.
Definition: abort_on_ebadf.hh:24
lazy_deref_wrapper< T > lazy_deref(const T &p)
Definition: lazy.hh:107
lazy_eval< Func > value_of(Func &&func)
Definition: lazy.hh:76
This struct is a wrapper for lazy dereferencing a pointer.
Definition: lazy.hh:89