ThingsBoard Client SDK 0.16.0
Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
Loading...
Searching...
No Matches
Callback.h
Go to the documentation of this file.
1#ifndef Callback_h
2#define Callback_h
3
4// Local includes.
5#include "Configuration.h"
6
7// Library includes.
8#include <ArduinoJson.h>
9#if THINGSBOARD_ENABLE_STL
10#include <functional>
11#include <vector>
12#endif // THINGSBOARD_ENABLE_STL
13
14
15#if THINGSBOARD_ENABLE_STL && THINGSBOARD_ENABLE_DYNAMIC
17template<typename T>
18using Container = std::vector<T>;
19#else
20#include "Container.h"
21#endif // THINGSBOARD_ENABLE_STL && THINGSBOARD_ENABLE_DYNAMIC
22
23
29template<typename return_type, typename... argument_types>
30class Callback {
31 public:
33#if THINGSBOARD_ENABLE_STL
34 using function = std::function<return_type(argument_types... arguments)>;
35#else
36 using function = return_type (*)(argument_types... arguments);
37#endif // THINGSBOARD_ENABLE_STL
38
40 Callback() = default;
41
45 explicit Callback(function callback)
46 : m_callback(callback)
47 {
48 // Nothing to do
49 }
50
54 virtual ~Callback() = default;
55
62 return_type Call_Callback(argument_types const &... arguments) const {
63 if (!m_callback) {
64 return return_type();
65 }
66 return m_callback(arguments...);
67 }
68
72 void Set_Callback(function callback) {
73 m_callback = callback;
74 }
75
76 private:
77 function m_callback = {}; // Callback to call
78};
79
80#endif // Callback_h
General purpose safe callback wrapper. Expects either c-style or c++ style function pointer,...
Definition: Callback.h:30
virtual ~Callback()=default
Virtual default destructor, created to ensure that if a pointer to this class is used and deleted,...
Callback(function callback)
Constructor.
Definition: Callback.h:45
std::function< return_type(argument_types... arguments)> function
Callback signature.
Definition: Callback.h:34
void Set_Callback(function callback)
Sets the callback method that will be called upon data arrival with the given data that was received....
Definition: Callback.h:72
return_type Call_Callback(argument_types const &... arguments) const
Calls the callback that was subscribed, when this class instance was initally created.
Definition: Callback.h:62
Callback()=default
Constructs empty callback, will result in never being called. Internals are simply default constructe...
Custom std::array or std::vector implementation that contains a partial vector-like interface impleme...
Definition: Container.h:29