NAP
Public Types | Public Member Functions | List of all members
APIEvent Class Reference

#include <apievent.h>

Public Types

using ArgumentConstIterator = utility::UniquePtrConstVectorWrapper< APIArgumentList, APIArgument * >
 

Public Member Functions

 APIEvent (const std::string &name)
 
 APIEvent (std::string &&name)
 
 APIEvent (const std::string &name, const std::string &id)
 
 APIEvent (std::string &&name, std::string &&id)
 
const std::string & getName () const
 
const std::string & getID () const
 
template<typename T , typename... Args>
APIArgumentaddArgument (const std::string &name, Args &&... args)
 
APIArgumentaddArgument (std::unique_ptr< APIBaseValue > value)
 
int getCount () const
 
const ArgumentConstIterator getArguments () const
 
const APIArgumentgetArgument (int index) const
 
APIArgumentgetArgument (int index)
 
const APIArgumentgetArgumentByName (std::string &&name) const
 
APIArgumentgetArgumentByName (std::string &&name)
 
bool matches (const nap::APISignature &signature) const
 
template<typename T >
const T & to () const
 
template<typename T >
T & to ()
 
APIArgumentoperator[] (std::size_t idx)
 
const APIArgumentoperator[] (std::size_t idx) const
 
- Public Member Functions inherited from Event
virtual ~Event ()=default
 
 Event ()=default
 
 Event (const Event &)=delete
 
Eventoperator= (const Event &)=delete
 

Description

Input / Output message that is used at run-time to describe an api message. Contains a list of API arguments. Every api argument carries a single value or list of values. This event is created and given to the application by the APIService when an external request is made. To dispatch an event to an external environment call APIService::dispatchEvent after construction of this event.

Example:

// Create the event
APIEventPtr progress_event = std::make_unique<APIEvent>("CacheProgress");

// Add an argument
progress_event->addArgument<APIInt>("Percentage", ++curr_pro);

// Dispatch to possible listeners
mAPIService->dispatchEvent(std::move(progress_event));
Inheritance diagram for APIEvent:
[legend]
Collaboration diagram for APIEvent:
[legend]

Member Typedef Documentation

◆ ArgumentConstIterator

Constructor & Destructor Documentation

◆ APIEvent() [1/4]

APIEvent ( const std::string &  name)

Construct a new api event with the given name. A unique id is generated.

Parameters
namename of this call

◆ APIEvent() [2/4]

APIEvent ( std::string &&  name)

Construct a new api event with the given name. A unique id is generated.

Parameters
namename of this call

◆ APIEvent() [3/4]

APIEvent ( const std::string &  name,
const std::string &  id 
)

Construct a new api event with the given name and unique id. Use this constructor to form a reply based on a previously received client request. The uuid should match the uuid of the request. This allows the client to match call id's.

Parameters
namename of this call
idunique identifier of this call

◆ APIEvent() [4/4]

APIEvent ( std::string &&  name,
std::string &&  id 
)

Construct a new api event with the given name and unique id. Use this constructor to form a reply based on a previously received client request. The uuid should match the uuid of the request. This allows the client to match call id's.

Parameters
nameidentifier of this call
idunique identifier of this call

Member Function Documentation

◆ addArgument() [1/2]

APIArgument * addArgument ( const std::string &  name,
Args &&...  args 
)

Adds an api argument with an id to this event where T is of type APIValue and 'args' the actual value, for example: 0.0f etc. To add a float as an argument call: addArgument<APIFloat>("drag", 1.0f).

Parameters
namethe name of the newly created api value.
argsthe template arguments used for constructing the argument. In case of an APIFloat the argument could be 1.0f etc.
Returns
the newly created and added argument

◆ addArgument() [2/2]

APIArgument* addArgument ( std::unique_ptr< APIBaseValue value)

Adds an api argument to this event based on the given api value.

Parameters
valuethe api value to add as an argument.
Returns
the added value as api argument.

◆ getArgument() [1/2]

APIArgument* getArgument ( int  index)
Parameters
indexthe index of the argument
Returns
an argument based on the given index, throws an exception when out of bounds

◆ getArgument() [2/2]

const APIArgument* getArgument ( int  index) const
Parameters
indexthe index of the argument
Returns
an argument based on the given index, throws an exception when out of bounds

◆ getArgumentByName() [1/2]

APIArgument* getArgumentByName ( std::string &&  name)
Parameters
namethe name of the argument
Returns
an argument based on the given name, returns nullptr if not found

◆ getArgumentByName() [2/2]

const APIArgument* getArgumentByName ( std::string &&  name) const
Parameters
namethe name of the argument
Returns
an argument based on the given name, returns nullptr if not found

◆ getArguments()

const ArgumentConstIterator getArguments ( ) const
Returns
the arguments of this api event

◆ getCount()

int getCount ( ) const
Returns
the number of arguments associated with this event

◆ getID()

const std::string& getID ( ) const
Returns
id of this call

◆ getName()

const std::string& getName ( ) const
Returns
name (action) of this call

◆ matches()

bool matches ( const nap::APISignature signature) const

If the api arguments and order of arguments matches the given api signature.

Parameters
signaturethe method signature to validate
Returns
if this event matches the given api signature.

◆ operator[]() [1/2]

APIArgument& operator[] ( std::size_t  idx)

Array [] subscript operator

Returns
the api argument at index

◆ operator[]() [2/2]

const APIArgument& operator[] ( std::size_t  idx) const

Array [] subscript operator

Returns
the api argument at index

◆ to() [1/2]

T & to

Returns this event as an event of type T. Example: ws_event = api_event.to<APIWebSocketEvent>(); For this to work this event must be an event of type T. Asserts if the event isn't of type T.

Returns
This event as T.

◆ to() [2/2]

const T & to

Returns this event as an event of type T. Example: ws_event = api_event.to<APIWebSocketEvent>(); For this to work this event must be an event of type T. Asserts if the event isn't of type T.

Returns
This event as T.