PdCom  5.3
Process data communication client
Future.h
1 /*****************************************************************************
2  * vim:tw=78
3  *
4  * Copyright (C) 2022 Bjarne von Horn (vh at igh dot de).
5  *
6  * This file is part of the PdCom library.
7  *
8  * The PdCom library is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * The PdCom library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>.
20  *
21  *****************************************************************************/
22 
23 #ifndef PDCOM5_FUTURE_H
24 #define PDCOM5_FUTURE_H
25 
26 #include <functional>
27 #include <memory>
28 #include <pdcom5_export.h>
29 
30 namespace PdCom {
31 namespace impl {
32 template <class Exception, class... Result>
33 struct Promise;
34 }
35 
48 template <class Exception, class... Result>
49 class PDCOM5_PUBLIC Future
50 {
51  public:
52  using ResolveFn = std::function<void(Result...)>;
53  using RejectFn = std::function<void(Exception)>;
54 
56  Future() = default;
57  Future(std::shared_ptr<impl::Promise<Exception, Result...>> impl) noexcept :
58  impl_(std::move(impl))
59  {}
60  Future(Future &&) = default;
61  Future(const Future &) = delete;
62  Future &operator=(Future &&) = default;
63  Future &operator=(const Future &) = delete;
64 
70  const Future &then(ResolveFn resolve) const &;
76  Future then(ResolveFn resolve) &&;
82  const Future &handle_exception(RejectFn reject) const &;
88  Future handle_exception(RejectFn reject) &&;
89 
93  bool empty() const noexcept { return !impl_.operator bool(); }
94 
96  struct Hash
97  {
98  std::size_t operator()(Future const &future) const
99  {
100  return std::hash<
101  std::shared_ptr<impl::Promise<Exception, Result...>>>()(
102  future.impl_);
103  }
104  };
105 
107  struct Less
108  {
109  bool operator()(const Future &lhs, const Future &rhs) const noexcept
110  {
111  return std::owner_less<
112  std::shared_ptr<impl::Promise<Exception, Result...>>>()(
113  lhs.impl_, rhs.impl_);
114  }
115  };
116 
118  bool operator==(const Future &other) const noexcept
119  {
120  return other.impl_ == impl_;
121  }
122 
123  private:
124  std::shared_ptr<impl::Promise<Exception, Result...>> impl_;
125 };
126 } // namespace PdCom
127 
128 namespace std {
129 template <class Exception, class... Result>
130 struct hash<PdCom::Future<Exception, Result...>> :
131  PdCom::Future<Exception, Result...>::Hash
132 {};
133 
134 template <class Exception, class... Result>
135 struct less<PdCom::Future<Exception, Result...>> :
136  PdCom::Future<Exception, Result...>::Less
137 {};
138 
139 } // namespace std
140 
141 
142 #endif // PDCOM5_FUTURE_H
Definition: Exception.h:33
bool empty() const noexcept
Definition: Future.h:93
Callback management handle.
Definition: Future.h:49
Definition: Future.h:128
Hash support, e.g. for std::unordered_set.
Definition: Future.h:96
Less compare support, e.g. for std::set.
Definition: Future.h:107
bool operator==(const Future &other) const noexcept
Equal comparsion.
Definition: Future.h:118
Definition: Future.h:33
Definition: ClientStatistics.h:31