BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
AbstractAsyncWork.h
1 #pragma once
2 
3 #include <Poco/Exception.h>
4 #include <Poco/Mutex.h>
5 
6 #include "util/AsyncWork.h"
7 
8 namespace BeeeOn {
9 
16 template <typename Result = Poco::Void>
17 class AbstractAsyncWork : public AsyncWork<Result> {
18 public:
19  typedef Poco::SharedPtr<AbstractAsyncWork<Result>> Ptr;
20 
25  void setResult(const Result &result);
26 
30  Poco::Nullable<Result> result() const override;
31 
32 private:
33  mutable Poco::FastMutex m_lock;
34  Poco::Nullable<Result> m_result;
35 };
36 
37 template <>
38 inline void AbstractAsyncWork<Poco::Void>::setResult(const Poco::Void &)
39 {
40  throw Poco::NotImplementedException("cannot set result for Void operation");
41 }
42 
43 template <>
44 inline Poco::Nullable<Poco::Void> AbstractAsyncWork<Poco::Void>::result() const
45 {
46  poco_assert(m_result.isNull());
47  return m_result;
48 }
49 
50 template <typename Result>
51 inline void AbstractAsyncWork<Result>::setResult(const Result &result)
52 {
53  Poco::FastMutex::ScopedLock guard(m_lock);
54 
55  if (!m_result.isNull())
56  throw Poco::IllegalStateException("result is already set");
57 
58  m_result = result;
59 }
60 
61 template <typename Result>
62 inline Poco::Nullable<Result> AbstractAsyncWork<Result>::result() const
63 {
64  Poco::FastMutex::ScopedLock guard(m_lock);
65 
66  return m_result;
67 }
68 
69 }
70 
Poco::Nullable< Result > result() const override
Definition: AbstractAsyncWork.h:62
AbstractAsyncWork provides a generic implementation of method result() and a supplementary method set...
Definition: AbstractAsyncWork.h:17
AsyncWork is an interface allowing to wait until an asynchronous operation finishes. It is also possible to force-stop it by calling cancel().
Definition: AsyncWork.h:21
Definition: Result.h:29
void setResult(const Result &result)
Set result of the operation. The result can be set only once.
Definition: AbstractAsyncWork.h:51