Spicy
deferred-expression.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <functional>
6 #include <string>
7 #include <utility>
8 
9 #include <hilti/rt/extension-points.h>
10 #include <hilti/rt/types/result.h>
11 
12 namespace hilti::rt {
13 
22 template<typename Result>
24 public:
25  DeferredExpression(std::function<Result()> expr) : _expr(std::move(expr)) {}
26  DeferredExpression() = delete;
27  DeferredExpression(const DeferredExpression&) = default;
28  DeferredExpression(DeferredExpression&&) noexcept = default;
29 
30  ~DeferredExpression() = default;
31 
32  DeferredExpression& operator=(const DeferredExpression&) = default;
33  DeferredExpression& operator=(DeferredExpression&&) noexcept = default;
34 
35  Result operator()() const { return _expr(); }
36 
37 private:
38  std::function<Result()> _expr;
39 };
40 
41 namespace detail::adl {
42 template<typename Result>
43 inline std::string to_string(const DeferredExpression<Result>& x, adl::tag /*unused*/) {
44  return hilti::rt::to_string(x());
45 }
46 } // namespace detail::adl
47 
48 // This function is declared as an overload since we cannot partially specialize
49 // `hilti::detail::to_string_for_print` for `DeferredExpression<T>`.
50 template<typename Result>
51 inline std::string to_string_for_print(const DeferredExpression<Result>& x) {
53 }
54 
55 template<typename Result>
56 inline std::ostream& operator<<(std::ostream& out, const DeferredExpression<Result>& x) {
57  return out << to_string_for_print(x);
58 }
59 
60 } // namespace hilti::rt
std::string to_string(T &&x)
Definition: extension-points.h:26
std::string to_string_for_print(const T &x)
Definition: extension-points.h:45
Definition: any.h:7
Definition: deferred-expression.h:23
Definition: result.h:67