PdCom  5.1
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 "pdcom5_export.h"
27 
28 #include <functional>
29 #include <memory>
30 
31 namespace PdCom {
32 namespace impl {
33 template <class Exception, class... Result>
34 struct Promise;
35 }
36 
49 template <class Exception, class... Result>
50 class PDCOM5_PUBLIC Future
51 {
52  public:
53  using ResolveFn = std::function<void(Result...)>;
54  using RejectFn = std::function<void(Exception)>;
55 
57  Future() = default;
58  Future(std::shared_ptr<impl::Promise<Exception, Result...>> impl) noexcept :
59  impl_(std::move(impl))
60  {}
61  Future(Future &&) = default;
62  Future(const Future &) = delete;
63  Future &operator=(Future &&) = default;
64  Future &operator=(const Future &) = delete;
65 
71  const Future &then(ResolveFn resolve) const &;
77  Future then(ResolveFn resolve) &&;
83  const Future &handle_exception(RejectFn reject) const &;
89  Future handle_exception(RejectFn reject) &&;
90 
94  bool empty() const noexcept { return !impl_.operator bool(); }
95 
97  struct Hash
98  {
99  std::size_t operator()(Future const &future) const
100  {
101  return std::hash<
102  std::shared_ptr<impl::Promise<Exception, Result...>>>()(
103  future.impl_);
104  }
105  };
106 
108  struct Less
109  {
110  bool operator()(const Future &lhs, const Future &rhs) const noexcept
111  {
112  return std::owner_less<
113  std::shared_ptr<impl::Promise<Exception, Result...>>>()(
114  lhs.impl_, rhs.impl_);
115  }
116  };
117 
119  bool operator==(const Future &other) const noexcept
120  {
121  return other.impl_ == impl_;
122  }
123 
124  private:
125  std::shared_ptr<impl::Promise<Exception, Result...>> impl_;
126 };
127 } // namespace PdCom
128 
129 namespace std {
130 template <class Exception, class... Result>
131 struct hash<PdCom::Future<Exception, Result...>> :
132  PdCom::Future<Exception, Result...>::Hash
133 {};
134 
135 template <class Exception, class... Result>
136 struct less<PdCom::Future<Exception, Result...>> :
137  PdCom::Future<Exception, Result...>::Less
138 {};
139 
140 } // namespace std
141 
142 
143 #endif // PDCOM5_FUTURE_H
Callback management handle.
Definition: Future.h:51
const Future & handle_exception(RejectFn reject) const &
Set error handling callback.
Future handle_exception(RejectFn reject) &&
Set error handling callback.
Future then(ResolveFn resolve) &&
Set continuation callback.
bool operator==(const Future &other) const noexcept
Equal comparsion.
Definition: Future.h:119
const Future & then(ResolveFn resolve) const &
Set continuation callback.
bool empty() const noexcept
Definition: Future.h:94
Future()=default
Default Constructor.
library version string as "major.minor.patch"
Definition: ClientStatistics.h:31
Definition: Exception.h:35
Hash support, e.g. for std::unordered_set.
Definition: Future.h:98
Less compare support, e.g. for std::set.
Definition: Future.h:109
Definition: Future.h:34