#ifndef PLATFORM_API_SETTABLE_FUTURE_H_ #define PLATFORM_API_SETTABLE_FUTURE_H_ #include "platform/api/platform.h" #include "platform/api/settable_future_def.h" #include "platform/exception.h" #include "platform/ptr.h" #include "platform/runnable.h" #include "absl/types/any.h" namespace location { namespace nearby { // "Common" part of implementation. // Placed here for textual compatibility to minimize scope of changes. // Can be (and should be) moved to a separate file outside "api" folder. // TODO(apolyudov): for API v2.0 namespace platform { namespace impl { template class SettableFutureImpl : public SettableFuture { public: SettableFutureImpl() { future_ = platform::ImplementationPlatform::createSettableFutureAny(); } ~SettableFutureImpl() override = default; bool set(T value) override { return future_->set(absl::any(value)); } bool setException(Exception exception) override { return future_->setException(exception); } void addListener(Ptr runnable, Executor* executor) override { future_->addListener(runnable, executor); } ExceptionOr get() override { return CommonGet(future_->get()); } ExceptionOr get(std::int64_t timeout_ms) override { return CommonGet(future_->get(timeout_ms)); } private: ExceptionOr CommonGet(ExceptionOr ret_val) { if (ret_val.exception() != Exception::kSuccess) { return ExceptionOr{ret_val.exception()}; } return ExceptionOr{absl::any_cast(ret_val.result())}; } Ptr> future_; }; } // namespace impl template Ptr> ImplementationPlatform::createSettableFuture() { return Ptr>(new impl::SettableFutureImpl{}); } } // namespace platform } // namespace nearby } // namespace location #endif // PLATFORM_API_SETTABLE_FUTURE_H_