NAP
portalitemsimple.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 "portalitem.h"
9 #include "portalutils.h"
10 
11 // External Includes
12 #include <apivalue.h>
13 #include <apievent.h>
14 #include <parametersimple.h>
15 #include <nap/signalslot.h>
16 
17 namespace nap
18 {
22  template<typename T>
24  {
25  RTTI_ENABLE(PortalItem)
26  public:
27 
33  virtual bool init(utility::ErrorState& error) override;
34 
38  virtual void onDestroy() override;
39 
46  virtual bool processUpdate(const APIEvent& event, utility::ErrorState& error) override;
47 
51  virtual APIEventPtr getDescriptor() const override;
52 
56  virtual APIEventPtr getValue() const override;
57 
62  virtual void onParameterUpdate(T value);
63 
68 
70  };
71 
72 
74  // Portal Item Simple Type Definitions
76 
79  class PortalItemTextArea : public PortalItemSimple<std::string>
80  {
82  };
83 
84 
86  // Template Definitions
88 
89  template<typename T>
91  {
92  mParameter->valueChanged.connect(mParameterUpdateSlot);
93  return true;
94  }
95 
96  template<typename T>
98  {
99  mParameter->valueChanged.disconnect(mParameterUpdateSlot);
100  }
101 
102  template<typename T>
104  {
105  updateSignal(*this);
106  }
107 
108  template<typename T>
110  {
111  // Check for the portal item value argument
112  const APIArgument* arg = event.getArgumentByName(nap::portal::itemValueArgName);
113  if (!error.check(arg != nullptr, "%s: update event missing argument %s", mID.c_str(), nap::portal::itemValueArgName))
114  return false;
115 
116  // Check the portal item value type
117  const rtti::TypeInfo type = arg->getValueType();
118  if (!error.check(type == RTTI_OF(T), "%s: cannot process value type %s", mID.c_str(), type.get_name().data()))
119  return false;
120 
121  // Cast and set the value on the parameter
122  T value = static_cast<const APIValue<T>*>(&arg->getValue())->mValue;
123  mParameter->valueChanged.disconnect(mParameterUpdateSlot);
124  mParameter->setValue(value);
125  mParameter->valueChanged.connect(mParameterUpdateSlot);
126  return true;
127  }
128 
129  template<typename T>
131  {
132  APIEventPtr event = std::make_unique<APIEvent>(mParameter->getDisplayName(), mID);
133  event->addArgument<APIString>(nap::portal::itemTypeArgName, get_type().get_name().data());
134  event->addArgument<APIValue<T>>(nap::portal::itemValueArgName, mParameter->mValue);
135  return event;
136  }
137 
138  template<typename T>
140  {
141  APIEventPtr event = std::make_unique<APIEvent>(mParameter->getDisplayName(), mID);
142  event->addArgument<APIValue<T>>(nap::portal::itemValueArgName, mParameter->mValue);
143  return event;
144  }
145 }
146 
150 #define DEFINE_PORTAL_ITEM_SIMPLE(Type) \
151  RTTI_BEGIN_CLASS(Type) \
152  RTTI_PROPERTY("Parameter", &Type::mParameter, nap::rtti::EPropertyMetaData::Required, "Parameter resource") \
153  RTTI_END_CLASS
nap::PortalItemSimple::getDescriptor
virtual APIEventPtr getDescriptor() const override
Definition: portalitemsimple.h:130
nap::APIValue
Definition: apivalue.h:56
nap::portal::itemValueArgName
constexpr const char * itemValueArgName
Name of the argument containing the portal item value in the portal item message.
Definition: portalutils.h:19
nap::portal::itemTypeArgName
constexpr const char * itemTypeArgName
Name of the argument containing the portal item type in the portal item message.
Definition: portalutils.h:18
nap::PortalItemSimple::getValue
virtual APIEventPtr getValue() const override
Definition: portalitemsimple.h:139
nap::APIArgument::getValueType
const rtti::TypeInfo getValueType() const
nap::PortalItemSimple::onDestroy
virtual void onDestroy() override
Definition: portalitemsimple.h:97
nap::Slot< T >
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::PortalItemSimple::init
virtual bool init(utility::ErrorState &error) override
Definition: portalitemsimple.h:90
nap::utility::ErrorState
Definition: errorstate.h:19
nap::PortalItemSimple::mParameter
ResourcePtr< ParameterSimple< T > > mParameter
Property: 'Parameter' the parameter linked to this portal item.
Definition: portalitemsimple.h:69
nap::PortalItemSimple::mParameterUpdateSlot
Slot< T > mParameterUpdateSlot
Definition: portalitemsimple.h:67
nap::PortalItem
Definition: portalitem.h:19
nap::PortalItemSimple::onParameterUpdate
virtual void onParameterUpdate(T value)
Definition: portalitemsimple.h:103
nap::APIEventPtr
std::unique_ptr< nap::APIEvent > APIEventPtr
Definition: apievent.h:179
nap::APIArgument::getValue
const APIBaseValue & getValue() const
Definition: apiargument.h:51
nap::PortalItemTextArea
Definition: portalitemsimple.h:79
nap::PortalItemSimple
Definition: portalitemsimple.h:23
nap
Definition: templateapp.h:17
nap::APIArgument
Definition: apiargument.h:22
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:140
nap::APIEvent
Definition: apievent.h:37
nap::PortalItemSimple::processUpdate
virtual bool processUpdate(const APIEvent &event, utility::ErrorState &error) override
Definition: portalitemsimple.h:109