From b03f4d54e73e5c8906df260a7501ccca62a54124 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 15 Aug 2024 20:42:21 +0300 Subject: [PATCH] Add non-template RequestPromiseBase. --- td/telegram/Requests.h | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/td/telegram/Requests.h b/td/telegram/Requests.h index 0df4cbec0..01851500b 100644 --- a/td/telegram/Requests.h +++ b/td/telegram/Requests.h @@ -49,41 +49,58 @@ class Requests { std::shared_ptr download_file_callback_; - template - class RequestPromise : public PromiseInterface { + class RequestPromiseBase { enum class State : int32 { Empty, Ready, Complete }; ActorId td_actor_; uint64 request_id_; MovableValue state_{State::Empty}; public: - void set_value(T &&value) override { + void set_value(td_api::object_ptr value) { CHECK(state_.get() == State::Ready); send_closure(td_actor_, &Td::send_result, request_id_, std::move(value)); state_ = State::Complete; } - void set_error(Status &&error) override { + void set_error(Status &&error) { if (state_.get() == State::Ready) { send_closure(td_actor_, &Td::send_error, request_id_, std::move(error)); state_ = State::Complete; } } - RequestPromise(const RequestPromise &) = delete; - RequestPromise &operator=(const RequestPromise &) = delete; - RequestPromise(RequestPromise &&) = default; - RequestPromise &operator=(RequestPromise &&) = default; - ~RequestPromise() override { + + RequestPromiseBase(const RequestPromiseBase &) = delete; + RequestPromiseBase &operator=(const RequestPromiseBase &) = delete; + RequestPromiseBase(RequestPromiseBase &&) = default; + RequestPromiseBase &operator=(RequestPromiseBase &&) = default; + virtual ~RequestPromiseBase() { if (state_.get() == State::Ready) { send_closure(td_actor_, &Td::send_error, request_id_, Status::Error("Lost promise")); } } - RequestPromise(ActorId td_actor, uint64 request_id) + RequestPromiseBase(ActorId td_actor, uint64 request_id) : td_actor_(std::move(td_actor)), request_id_(request_id), state_(State::Ready) { } }; + template + class RequestPromise + : public PromiseInterface + , RequestPromiseBase { + public: + void set_value(T &&value) override { + RequestPromiseBase::set_value(std::move(value)); + } + + void set_error(Status &&error) override { + RequestPromiseBase::set_error(std::move(error)); + } + + RequestPromise(ActorId td_actor, uint64 request_id) : RequestPromiseBase(std::move(td_actor), request_id) { + } + }; + template Promise create_request_promise(uint64 request_id) const { return Promise(td::make_unique>(td_actor_, request_id));