NAP
portalitemnumeric.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 <parameternumeric.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 Numeric Type Definitions
76 
82 
83 
85  // Template Definitions
87 
88  template<typename T>
90  {
91  mParameter->valueChanged.connect(mParameterUpdateSlot);
92  return true;
93  }
94 
95  template<typename T>
97  {
98  mParameter->valueChanged.disconnect(mParameterUpdateSlot);
99  }
100 
101  template<typename T>
103  {
104  updateSignal(*this);
105  }
106 
107  template<typename T>
109  {
110  // Check for the portal item value argument
111  const APIArgument* arg = event.getArgumentByName(nap::portal::itemValueArgName);
112  if (!error.check(arg != nullptr, "%s: update event missing argument %s", mID.c_str(), nap::portal::itemValueArgName))
113  return false;
114 
115  // Check the portal item value type
116  const rtti::TypeInfo type = arg->getValueType();
117  if (!error.check(type == RTTI_OF(T), "%s: cannot process value type %s", mID.c_str(), type.get_name().data()))
118  return false;
119 
120  // Cast and set the value on the parameter
121  T value = static_cast<const APIValue<T>*>(&arg->getValue())->mValue;
122  mParameter->valueChanged.disconnect(mParameterUpdateSlot);
123  mParameter->setValue(value);
124  mParameter->valueChanged.connect(mParameterUpdateSlot);
125  return true;
126  }
127 
128  template<typename T>
130  {
131  APIEventPtr event = std::make_unique<APIEvent>(mParameter->getDisplayName(), mID);
132  event->addArgument<APIString>(nap::portal::itemTypeArgName, get_type().get_name().data());
133  event->addArgument<APIValue<T>>(nap::portal::itemValueArgName, mParameter->mValue);
134  event->addArgument<APIValue<T>>(nap::portal::itemMinArgName, mParameter->mMinimum);
135  event->addArgument<APIValue<T>>(nap::portal::itemMaxArgName, mParameter->mMaximum);
136  return event;
137  }
138 
139  template<typename T>
141  {
142  APIEventPtr event = std::make_unique<APIEvent>(mParameter->getDisplayName(), mID);
143  event->addArgument<APIValue<T>>(nap::portal::itemValueArgName, mParameter->mValue);
144  return event;
145  }
146 }
147 
151 #define DEFINE_PORTAL_ITEM_NUMERIC(Type) \
152  RTTI_BEGIN_CLASS(Type, "Numeric portal item") \
153  RTTI_PROPERTY("Parameter", &Type::mParameter, nap::rtti::EPropertyMetaData::Required, "Numeric parameter") \
154  RTTI_END_CLASS
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::APIArgument::getValueType
const rtti::TypeInfo getValueType() const
nap::PortalItemNumeric::mParameterUpdateSlot
Slot< T > mParameterUpdateSlot
Definition: portalitemnumeric.h:67
nap::PortalItemNumeric::getDescriptor
virtual APIEventPtr getDescriptor() const override
Definition: portalitemnumeric.h:129
nap::PortalItemNumeric
Definition: portalitemnumeric.h:23
nap::portal::itemMinArgName
constexpr const char * itemMinArgName
Name of the argument containing the minimum portal item value in the portal item message.
Definition: portalutils.h:20
nap::Slot< T >
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::PortalItemNumeric::onDestroy
virtual void onDestroy() override
Definition: portalitemnumeric.h:96
nap::utility::ErrorState
Definition: errorstate.h:19
nap::PortalItemNumeric::processUpdate
virtual bool processUpdate(const APIEvent &event, utility::ErrorState &error) override
Definition: portalitemnumeric.h:108
nap::PortalItemNumeric::getValue
virtual APIEventPtr getValue() const override
Definition: portalitemnumeric.h:140
nap::PortalItem
Definition: portalitem.h:19
nap::portal::itemMaxArgName
constexpr const char * itemMaxArgName
Name of the argument containing the maximum portal item value in the portal item message.
Definition: portalutils.h:21
nap::APIEventPtr
std::unique_ptr< nap::APIEvent > APIEventPtr
Definition: apievent.h:179
nap::APIArgument::getValue
const APIBaseValue & getValue() const
Definition: apiargument.h:51
nap
Definition: templateapp.h:17
nap::PortalItemNumeric::onParameterUpdate
virtual void onParameterUpdate(T value)
Definition: portalitemnumeric.h:102
nap::PortalItemNumeric::mParameter
ResourcePtr< ParameterNumeric< T > > mParameter
Property: 'Parameter' the parameter linked to this portal item.
Definition: portalitemnumeric.h:69
nap::APIArgument
Definition: apiargument.h:22
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:140
nap::APIEvent
Definition: apievent.h:37
nap::PortalItemNumeric::init
virtual bool init(utility::ErrorState &error) override
Definition: portalitemnumeric.h:89