BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Result.h
1 #pragma once
2 
3 #include <Poco/AutoPtr.h>
4 #include <Poco/Event.h>
5 #include <Poco/RefCountedObject.h>
6 
7 #include "util/Castable.h"
8 #include "util/Enum.h"
9 
10 namespace BeeeOn {
11 
12 class Answer;
13 
14 /*
15  * Representation of the result that is created and set in
16  * CommandHandler::handle(). Status of the Result is set to
17  * PENDING after the result creation.
18  *
19  * The notification about the changed status is sent using notifyUpdated()
20  * after the status is changed. It is automatically called in the
21  * setStatus() method and it is processed by Answer. The setDirty(true)
22  * is set after the notification setting. It means that the new result is
23  * available in the Answer.
24  *
25  * The Answer and the Result share the common mutex. The operations that
26  * change/get the state in the Answer and in the Result MUST be locked.
27  * The methods in the Results are created for this purpose.
28  */
29 class Result : public Poco::RefCountedObject, public Castable {
30 public:
31  typedef Poco::AutoPtr<Result> Ptr;
32  typedef Poco::ScopedLock<Result> ScopedLock;
33 
34  struct StatusEnum
35  {
36  enum Raw
37  {
38  PENDING,
39  SUCCESS,
40  FAILED,
41  };
42 
43  static EnumHelper<Raw>::ValueMap &valueMap();
44  };
45 
46  typedef Enum<StatusEnum> Status;
47 
48  Result(Poco::AutoPtr<Answer> answer);
49 
50  /*
51  * It internally calls the notifyUpdated().
52  */
53  void setStatus(const Status status);
54  Status status() const;
55 
56  /*
57  * Notifies the waiting threads that this Result (and its Answer) were
58  * changed. The call sets Answer::setDirty(true).
59  */
60  void notifyUpdated();
61 
62  void lock();
63  void unlock();
64 
65 protected:
66  ~Result();
67 
68 private:
69  Status m_status;
70  Answer &m_answer;
71 };
72 
73 }
Definition: Answer.h:30
Definition: Result.h:34
Definition: Castable.h:7
Definition: Result.h:29
std::map< Raw, std::string > ValueMap
Definition: Enum.h:20