iberbox  2.1.1
events.h
Go to the documentation of this file.
1 
10 #ifndef EVENTS_H
11 #define EVENTS_H
12 
13 #include "iberbox/config.h"
14 #include "iberbox/ownership.h"
15 #include "iberbox/node.h"
16 #include "iberbox/account.h"
17 
18 #include <memory>
19 #include <functional>
20 
21 #ifdef USE_SCAPIX
22 #include <scapix/bridge/object.h>
23 #endif
24 
25 namespace iberbox {
26 
30 class EventData
31 #ifdef USE_SCAPIX
32  : public scapix::bridge::object<EventData>
33 #endif
34 {
35 public:
36 
40  enum EventType
41  {
54  };
55 
62  static const char *getTypeName(unsigned type)
63  {
64  static const char *sEventNames[] =
65  {
66  "NEW_NODES",
67  "MOVED_NODE",
68  "DELETED_NODE",
69  "UPDATED_NODE",
70  "UPDATED_META",
71  "UPDATED_KEYS",
72  "LOGOUT",
73  "FULL_RELOAD",
74  "ACCOUNT_READY",
75  "CONNECTIVITY",
76  "ACCOUNT_INFO",
77  };
78  static_assert((sizeof(sEventNames) / sizeof(sEventNames[0]) == EventData::LAST), "Invalid number of event names");
79  return type < EventData::LAST ? sEventNames[type] : "INVALID";
80  }
81 
88  static const std::string getTypeNameString(unsigned type)
89  {
90  return getTypeName(type);
91  }
92 
98  int getType() const;
104  AccountInfo getAccountInfo() const;
110  Node getNode() const;
116  Node getPreviousNode() const;
122  NodeList getNodeList() const;
128  uint64_t getNodeId() const;
134  const char *getCloudState() const;
140  uint64_t getClientId() const;
146  const char *getPath() const;
152  const std::string getPathString() const;
158  const char *getName() const;
164  const std::string getNameString() const;
171  bool isExternal() const;
172 
178  int getNumber() const;
179 
180  virtual ~EventData();
181 
182 #ifndef EMSCRIPTEN
183 protected:
184 #endif
185 
186  EventData(int type);
187 
188  int mType;
189  AccountInfo mAccountInfo;
190  Node mNode;
191  Node mPreviousNode;
192  NodeList mNodeList;
193  uint64_t mNodeId;
194  char *mCloudState;
195  uint64_t mClientId;
196  char *mPath;
197  char *mName;
198  std::string mHandle;
199  bool mIsExternal;
200  int mNumber;
201 };
202 
203 
204 typedef std::shared_ptr<EventData> Event;
205 typedef std::shared_ptr<std::vector<Event>> EventList;
206 
207 class EventListener : public SharedOwnership
208 #ifdef USE_SCAPIX
209  , public scapix::bridge::object<EventListener>
210 #endif
211 {
212 public:
213  EventListener();
214  EventListener(std::function<void(Event)> eventCallback);
215  virtual ~EventListener();
216 
217  void setEventCallback(std::function<void(Event)> eventCallback);
218 
219  virtual void onEvent(Event);
220 
221 protected:
222  std::function<void(Event)> mEventCallback;
223 };
224 
225 
226 #ifdef USE_SCAPIX
227 typedef std::shared_ptr<EventListener> EventListenerType;
228 #else
230 #endif
231 
232 }
233 
234 #endif // EVENTS_H
Definition: events.h:49
AccountInfo getAccountInfo() const
Gets the account info after the event occurred.
Definition: events.h:43
Definition: events.h:53
Node getPreviousNode() const
Gets the Previous Node object.
uint64_t getNodeId() const
Gets the Node Id object.
Node getNode() const
Gets the node that caused the event.
The iberbox namespace.
Definition: task.h:21
bool isExternal() const
Definition: events.h:42
Definition: events.h:47
virtual ~EventData()
Definition: events.h:46
std::shared_ptr< AccountInfoData > AccountInfo
Definition: account.h:221
std::shared_ptr< NodeData > Node
Definition: node.h:666
Definition: events.h:51
The event data class.
Definition: events.h:30
Definition: events.h:52
static const char * getTypeName(unsigned type)
Gets the event type name.
Definition: events.h:62
std::shared_ptr< NodeDataList > NodeList
Definition: node.h:668
uint64_t getClientId() const
Gets the Client Id object.
const char * getName() const
Gets the Name object.
const std::string getNameString() const
Gets the Name String object.
EventType
The event type enum.
Definition: events.h:40
std::shared_ptr< EventData > Event
Definition: events.h:204
static const std::string getTypeNameString(unsigned type)
Gets the event type name.
Definition: events.h:88
std::shared_ptr< std::vector< Event > > EventList
Definition: events.h:205
int getType() const
Gets the event type.
Definition: events.h:44
Definition: events.h:50
const char * getPath() const
Gets the Path object.
EventListener * EventListenerType
Definition: events.h:229
Definition: events.h:45
Definition: events.h:207
const std::string getPathString() const
Gets the Path String object.
int getNumber() const
Gets the number associated to the event.
Definition: events.h:48
const char * getCloudState() const
Gets the Cloud State object.
NodeList getNodeList() const
Gets the Node List object.