NAP
apimessage.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 "apivalue.h"
9 #include "apievent.h"
10 
11 // External Includes
12 #include <nap/resource.h>
13 #include <nap/resourceptr.h>
14 
15 namespace nap
16 {
28  class NAPAPI APIMessage : public Resource
29  {
30  RTTI_ENABLE(Resource)
31  public:
32  // Default constructor
33  APIMessage() = default;
34 
35  // Destructor
36  virtual ~APIMessage();
37 
43  APIMessage(const APIEvent& apiEvent);
44 
49  APIMessage(const std::string& name);
50 
57  template<typename T, typename... Args>
58  std::unique_ptr<T> toEvent(Args&&... args);
59 
66  bool toJSON(std::string& outString, utility::ErrorState& error);
67 
68  std::vector<APIBaseValue*> mArguments;
69  std::string mName;
70 
71  private:
72  // When constructing this message from an api event the values are owned by this object.
73  // This ensures that when the message is destructed the arguments are deleted.
74  std::vector<std::unique_ptr<APIBaseValue>> mOwningArguments;
75 
79  void copyArguments(APIEvent& apiEvent);
80  };
81 
82  using APIMessagePtr = std::unique_ptr<nap::APIMessage>;
83 
84 
86  // Template Definitions
88 
89  template<typename T, typename... Args>
90  std::unique_ptr<T> nap::APIMessage::toEvent(Args&&... args)
91  {
92  assert(RTTI_OF(T).is_derived_from(RTTI_OF(APIEvent)));
93  std::unique_ptr<T> ptr = std::make_unique<T>(mName, mID, std::forward<Args>(args)...);
94  copyArguments(*ptr);
95  return ptr;
96  }
97 }
nap::APIMessage::mArguments
std::vector< APIBaseValue * > mArguments
Property: 'Arguments': All input arguments associated with this message.
Definition: apimessage.h:68
nap::utility::ErrorState
Definition: errorstate.h:19
nap::APIMessagePtr
std::unique_ptr< nap::APIMessage > APIMessagePtr
Definition: apimessage.h:82
nap::APIMessage::toEvent
std::unique_ptr< T > toEvent(Args &&... args)
Definition: apimessage.h:90
nap::APIMessage::mName
std::string mName
Property: 'Name': action associated with the message.
Definition: apimessage.h:69
nap::APIMessage
Definition: apimessage.h:28
nap
Definition: templateapp.h:17
nap::Resource
Definition: resource.h:19
nap::APIEvent
Definition: apievent.h:37