NAP
portalevent.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "portalutils.h"
9 
10 // External Includes
11 #include <nap/core.h>
12 #include <nap/event.h>
13 #include <utility/uniqueptrvectoriterator.h>
14 #include <apievent.h>
15 #include <websocketconnection.h>
16 
17 namespace nap
18 {
26  class NAPAPI PortalEvent : public Event
27  {
28  RTTI_ENABLE(Event)
29 
30  public:
31  using APIEventList = std::vector<APIEventPtr>;
33 
38  PortalEvent(const PortalEventHeader& header) : mHeader(header) { }
39 
45  PortalEvent(const PortalEventHeader& header, const WebSocketConnection& connection) :
46  mHeader(header), mConnection(std::make_unique<WebSocketConnection>(connection)) { }
47 
51  const std::string& getID() const { return mHeader.mID; }
52 
56  const std::string& getPortalID() const { return mHeader.mPortalID; }
57 
61  const EPortalEventType& getType() const { return mHeader.mType; }
62 
66  bool hasConnection() const { return mConnection.get() != nullptr; }
67 
71  const WebSocketConnection& getConnection() const { return *mConnection; }
72 
79  bool toAPIMessageJSON(std::string& outJSON, utility::ErrorState& error);
80 
85  void addAPIEvent(APIEventPtr apiEvent);
86 
90  int getCount() const { return static_cast<int>(mAPIEvents.size()); }
91 
95  const APIEventConstIterator getAPIEvents() const { return APIEventConstIterator(mAPIEvents); }
96 
101  const APIEvent* getAPIEvent(int index) const;
102 
107  APIEvent* getAPIEvent(int index);
108 
113  APIEvent& operator[](std::size_t idx) { return *getAPIEvent(static_cast<int>(idx)); }
114 
119  const APIEvent& operator[](std::size_t idx) const { return *getAPIEvent(static_cast<int>(idx)); }
120 
121  private:
122 
123  PortalEventHeader mHeader;
124  WebSocketConnectionPtr mConnection = nullptr;
125  std::vector<APIEventPtr> mAPIEvents;
126  };
127 
128  using PortalEventPtr = std::unique_ptr<PortalEvent>;
129 }
nap::WebSocketConnection
Definition: websocketconnection.h:27
nap::PortalEvent::hasConnection
bool hasConnection() const
Definition: portalevent.h:66
nap::WebSocketConnectionPtr
std::unique_ptr< WebSocketConnection > WebSocketConnectionPtr
Definition: websocketconnection.h:56
nap::utility::UniquePtrConstVectorWrapper
Definition: uniqueptrvectoriterator.h:70
nap::PortalEventPtr
std::unique_ptr< PortalEvent > PortalEventPtr
Definition: portalevent.h:128
nap::PortalEvent::getCount
int getCount() const
Definition: portalevent.h:90
nap::PortalEvent::APIEventList
std::vector< APIEventPtr > APIEventList
Definition: portalevent.h:31
nap::utility::ErrorState
Definition: errorstate.h:19
nap::Event
Definition: event.h:17
nap::PortalEvent::getAPIEvents
const APIEventConstIterator getAPIEvents() const
Definition: portalevent.h:95
nap::PortalEvent::operator[]
APIEvent & operator[](std::size_t idx)
Definition: portalevent.h:113
nap::EPortalEventType
EPortalEventType
Definition: portalutils.h:29
nap::PortalEvent::getType
const EPortalEventType & getType() const
Definition: portalevent.h:61
nap::APIEventPtr
std::unique_ptr< nap::APIEvent > APIEventPtr
Definition: apievent.h:179
nap::PortalEvent::getPortalID
const std::string & getPortalID() const
Definition: portalevent.h:56
nap::PortalEvent::PortalEvent
PortalEvent(const PortalEventHeader &header, const WebSocketConnection &connection)
Definition: portalevent.h:45
nap::PortalEvent::getID
const std::string & getID() const
Definition: portalevent.h:51
nap::PortalEvent::operator[]
const APIEvent & operator[](std::size_t idx) const
Definition: portalevent.h:119
nap
Definition: templateapp.h:17
nap::PortalEvent::getConnection
const WebSocketConnection & getConnection() const
Definition: portalevent.h:71
nap::PortalEventHeader
Definition: portalutils.h:51
nap::PortalEvent
Definition: portalevent.h:26
nap::APIEvent
Definition: apievent.h:37
nap::PortalEvent::PortalEvent
PortalEvent(const PortalEventHeader &header)
Definition: portalevent.h:38