Zigbee Protocol Controller 1.6.0
multi_invoke.hpp
Go to the documentation of this file.
1/******************************************************************************
2 * # License
3 * <b>Copyright 2021 Silicon Laboratories Inc. www.silabs.com</b>
4 ******************************************************************************
5 * The licensor of this software is Silicon Laboratories Inc. Your use of this
6 * software is governed by the terms of Silicon Labs Master Software License
7 * Agreement (MSLA) available at
8 * www.silabs.com/about-us/legal/master-software-license-agreement. This
9 * software is distributed to you in Source Code format and is governed by the
10 * sections of the MSLA applicable to Source Code.
11 *
12 *****************************************************************************/
13
44#ifndef MULTI_INVOKE_HPP
45#define MULTI_INVOKE_HPP
46#ifdef __cplusplus
47#include <map>
48#include <vector>
55template<typename key, typename... function_args> class multi_invoke
56{
57 public:
58 typedef void (*callback_type_t)(function_args...);
65 void operator()(key k, function_args... args) const
66 {
67 std::vector<callback_type_t> funcs;
68 // find all entries that match the key
69 auto range = callbacks.equal_range(k);
70 // to protect against the case where the callback modifies the list
71 // cache the result
72 for (auto i = range.first; i != range.second; i++) {
73 funcs.push_back((*i).second);
74 }
75 for (auto cb: funcs) {
76 cb(args...);
77 }
78 }
79
86 void add(key k, callback_type_t cb)
87 {
88 // Prevent inserting same node/callback pair several times
89 auto range = callbacks.equal_range(k);
90 for (auto it = range.first; it != range.second; ++it) {
91 if (it->second == cb) {
92 return;
93 }
94 }
95
96 callbacks.insert(std::make_pair(k, cb));
97 }
98
105 void remove(key k, callback_type_t cb)
106 {
107 auto range = callbacks.equal_range(k);
108 for (auto it = range.first; it != range.second; ++it) {
109 if (it->second == cb) {
110 callbacks.erase(it);
111 break;
112 }
113 }
114 }
115
123 bool contains(key k) const
124 {
125 return callbacks.count(k) > 0;
126 }
127
133 void erase(key k)
134 {
135 callbacks.erase(k);
136 }
137
141 void clear()
142 {
143 callbacks.clear();
144 }
145
150 bool empty() const
151 {
152 return callbacks.empty();
153 }
154
155 private:
156 std::multimap<key, callback_type_t> callbacks;
157};
158
159#endif // __cplusplus
160
161#endif //MULTI_INVOKE_HPP
void clear(void)
Clear all registered callbacks.
sl_status_t remove(zigbee_eui64_uint_t eui64, const zigpc_discovery_status_callback_t callback)
Remove a registered callback for a specific EUI64 (or WILDCARD_EUI64 for all devices).
sl_status_t add(zigbee_eui64_uint_t eui64, const zigpc_discovery_status_callback_t callback)
Add a callback to be invoked for a specific EUI64 (or WILDCARD_EUI64 for all devices).