MillicastSDK 2.5.0
Loading...
Searching...
No Matches
promise.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef __cplusplus
4
5#include <functional>
6
7#include "error.h"
8#include "exports.h"
9
10namespace millicast {
11
12namespace detail {
13template <typename T>
14struct PromiseResultCbType {
15 using Type = std::function<void(T&&)>;
16};
17
18template <>
19struct PromiseResultCbType<void> {
20 using Type = std::function<void()>;
21};
22
23template <typename T>
24class PromiseImpl {
25 public:
26 virtual ~PromiseImpl() = default;
27 virtual void on_result(
28 const typename detail::PromiseResultCbType<T>::Type& cb) = 0;
29 virtual void on_error(const std::function<void(const Error&)>& cb) = 0;
30};
31} // namespace detail
32
46template <typename T>
47class MILLICAST_TEMPLATE_API Promise {
48 public:
49 Promise(std::unique_ptr<detail::PromiseImpl<T>> impl)
50 : _impl(std::move(impl)){};
51 Promise(const Promise&) = delete;
52 Promise(Promise&&) = delete;
53 Promise& operator=(const Promise&) = delete;
54 Promise& operator=(Promise&&) = delete;
55
63 Promise<T>& on_result(
64 const typename detail::PromiseResultCbType<T>::Type& cb) {
65 _impl->on_result(cb);
66 return *this;
67 }
68
75 Promise<T>& on_error(const std::function<void(const Error&)>& cb) {
76 _impl->on_error(cb);
77 return *this;
78 }
79
88 void done() { _impl = {}; }
89
90 private:
91 std::unique_ptr<detail::PromiseImpl<T>> _impl;
92};
93} // namespace millicast
94
95#endif // __cplusplus
#define MILLICAST_TEMPLATE_API
Definition exports.h:46