NAP
apicomponent.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 // External Includes
8 #include <string>
9 #include <vector>
10 #include <unordered_map>
11 #include <component.h>
12 #include <nap/signalslot.h>
13 
14 // Local Includes
15 #include "apiservice.h"
16 #include "apievent.h"
17 #include "apisignature.h"
18 
19 namespace nap
20 {
21  class APIComponentInstance;
22  class APICallBack;
23 
29  class NAPAPI APIComponent : public Component
30  {
31  RTTI_ENABLE(Component)
32  DECLARE_COMPONENT(APIComponent, APIComponentInstance)
33  public:
34  std::vector<ResourcePtr<APISignature>> mSignatures;
35  };
36 
37 
48  {
49  friend class APIService;
50  RTTI_ENABLE(ComponentInstance)
51  public:
56  ComponentInstance(entity, resource) { }
57 
61  virtual ~APIComponentInstance() override;
62 
67  virtual bool init(utility::ErrorState& errorState) override;
68 
74  const APISignature* findSignature(const std::string& id) const;
75 
81  bool accepts(const APIEvent& apiEvent) const;
82 
90  void registerCallback(const APISignature& signature, nap::Slot<const APIEvent&>& slot);
91 
93 
94  private:
95  // Pointer to the API service
96  APIService* mAPIService = nullptr;
97 
98  // All accepted names of individual calls
99  std::unordered_map<std::string, APISignature*> mSignatures;
100 
101  // All registered callbacks
102  std::unordered_map<std::string, std::unique_ptr<APICallBack>> mCallbacks;
103 
108  void trigger(const APIEvent& apiEvent);
109 
117  APICallBack& getOrCreateCallback(const APISignature& signature);
118  };
119 
120 
122  // API Callback
124 
131  class NAPAPI APICallBack final
132  {
133  public:
134  // No default constructor
135  APICallBack() = delete;
136 
137  // Removes binding
138  ~APICallBack();
139 
143  APICallBack(APICallBack&) = delete;
144  APICallBack& operator=(const APICallBack&) = delete;
145 
149  APICallBack(APICallBack&&) = delete;
150  APICallBack& operator=(APICallBack&&) = delete;
151 
156  APICallBack(const APISignature& signature) { }
157 
159  };
160 }
nap::APISignature
Definition: apisignature.h:20
nap::APICallBack
Definition: apicomponent.h:131
nap::Slot
Slot.
Definition: signalslot.h:21
nap::APIComponentInstance::messageReceived
Signal< const APIEvent & > messageReceived
Triggered when the component receives an api event.
Definition: apicomponent.h:92
nap::APIComponentInstance::APIComponentInstance
APIComponentInstance(EntityInstance &entity, Component &resource)
Definition: apicomponent.h:55
nap::APICallBack::APICallBack
APICallBack(const APISignature &signature)
Definition: apicomponent.h:156
nap::utility::ErrorState
Definition: errorstate.h:19
nap::APIService
Definition: apiservice.h:47
nap::Signal
Definition: signalslot.h:28
nap::EntityInstance
Definition: entity.h:34
nap::APIComponent::mSignatures
std::vector< ResourcePtr< APISignature > > mSignatures
Property: 'Signatures' all (exact) names of API calls (ids) accepted by this component.
Definition: apicomponent.h:34
nap::ComponentInstance
Definition: component.h:43
nap::Component
Definition: component.h:151
nap
Definition: templateapp.h:17
nap::APIComponent
Definition: apicomponent.h:29
nap::APICallBack::messageReceived
Signal< const APIEvent & > messageReceived
called when this callback receives an API message
Definition: apicomponent.h:158
nap::APIComponentInstance
Definition: apicomponent.h:47
nap::APIEvent
Definition: apievent.h:37