leo hendrickson
/
S
simple-mbed-cloud-client/mbed-cloud-client/mbed-client/source/m2mcallbackstorage.cpp@0:25fa8795676b, 2021-04-18 (annotated)
- Committer:
- leothedragon
- Date:
- Sun Apr 18 15:20:23 2021 +0000
- Revision:
- 0:25fa8795676b
DS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:25fa8795676b | 1 | /* |
leothedragon | 0:25fa8795676b | 2 | * Copyright (c) 2017 ARM Limited. All rights reserved. |
leothedragon | 0:25fa8795676b | 3 | * SPDX-License-Identifier: Apache-2.0 |
leothedragon | 0:25fa8795676b | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
leothedragon | 0:25fa8795676b | 5 | * not use this file except in compliance with the License. |
leothedragon | 0:25fa8795676b | 6 | * You may obtain a copy of the License at |
leothedragon | 0:25fa8795676b | 7 | * |
leothedragon | 0:25fa8795676b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:25fa8795676b | 9 | * |
leothedragon | 0:25fa8795676b | 10 | * Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:25fa8795676b | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
leothedragon | 0:25fa8795676b | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:25fa8795676b | 13 | * See the License for the specific language governing permissions and |
leothedragon | 0:25fa8795676b | 14 | * limitations under the License. |
leothedragon | 0:25fa8795676b | 15 | */ |
leothedragon | 0:25fa8795676b | 16 | |
leothedragon | 0:25fa8795676b | 17 | #include "include/m2mcallbackstorage.h" |
leothedragon | 0:25fa8795676b | 18 | |
leothedragon | 0:25fa8795676b | 19 | #include <stddef.h> |
leothedragon | 0:25fa8795676b | 20 | |
leothedragon | 0:25fa8795676b | 21 | // Dummy constructor, which does not init any value to something meaningful but needed for array construction. |
leothedragon | 0:25fa8795676b | 22 | // It is better to leave values unintialized, so the Valgrind will point out if the Vector is used without |
leothedragon | 0:25fa8795676b | 23 | // setting real values in there. |
leothedragon | 0:25fa8795676b | 24 | M2MCallbackAssociation::M2MCallbackAssociation() |
leothedragon | 0:25fa8795676b | 25 | { |
leothedragon | 0:25fa8795676b | 26 | } |
leothedragon | 0:25fa8795676b | 27 | |
leothedragon | 0:25fa8795676b | 28 | M2MCallbackAssociation::M2MCallbackAssociation(const M2MBase *object, void *callback, M2MCallbackType type, void *client_args) |
leothedragon | 0:25fa8795676b | 29 | : _object(object), _callback(callback), _type(type), _client_args(client_args) |
leothedragon | 0:25fa8795676b | 30 | { |
leothedragon | 0:25fa8795676b | 31 | } |
leothedragon | 0:25fa8795676b | 32 | |
leothedragon | 0:25fa8795676b | 33 | M2MCallbackStorage* M2MCallbackStorage::_static_instance = NULL; |
leothedragon | 0:25fa8795676b | 34 | |
leothedragon | 0:25fa8795676b | 35 | M2MCallbackStorage *M2MCallbackStorage::get_instance() |
leothedragon | 0:25fa8795676b | 36 | { |
leothedragon | 0:25fa8795676b | 37 | if (M2MCallbackStorage::_static_instance == NULL) { |
leothedragon | 0:25fa8795676b | 38 | M2MCallbackStorage::_static_instance = new M2MCallbackStorage(); |
leothedragon | 0:25fa8795676b | 39 | } |
leothedragon | 0:25fa8795676b | 40 | return M2MCallbackStorage::_static_instance; |
leothedragon | 0:25fa8795676b | 41 | } |
leothedragon | 0:25fa8795676b | 42 | |
leothedragon | 0:25fa8795676b | 43 | void M2MCallbackStorage::delete_instance() |
leothedragon | 0:25fa8795676b | 44 | { |
leothedragon | 0:25fa8795676b | 45 | delete M2MCallbackStorage::_static_instance; |
leothedragon | 0:25fa8795676b | 46 | M2MCallbackStorage::_static_instance = NULL; |
leothedragon | 0:25fa8795676b | 47 | } |
leothedragon | 0:25fa8795676b | 48 | |
leothedragon | 0:25fa8795676b | 49 | M2MCallbackStorage::~M2MCallbackStorage() |
leothedragon | 0:25fa8795676b | 50 | { |
leothedragon | 0:25fa8795676b | 51 | // TODO: go through the list and delete all the FP<n> objects if there are any. |
leothedragon | 0:25fa8795676b | 52 | // On the other hand, if the system is done properly, each m2mobject should actually |
leothedragon | 0:25fa8795676b | 53 | // remove its callbacks from its destructor so there is nothing here to do |
leothedragon | 0:25fa8795676b | 54 | } |
leothedragon | 0:25fa8795676b | 55 | |
leothedragon | 0:25fa8795676b | 56 | bool M2MCallbackStorage::add_callback(const M2MBase &object, |
leothedragon | 0:25fa8795676b | 57 | void *callback, |
leothedragon | 0:25fa8795676b | 58 | M2MCallbackAssociation::M2MCallbackType type, |
leothedragon | 0:25fa8795676b | 59 | void *client_args) |
leothedragon | 0:25fa8795676b | 60 | { |
leothedragon | 0:25fa8795676b | 61 | bool add_success = false; |
leothedragon | 0:25fa8795676b | 62 | M2MCallbackStorage* instance = get_instance(); |
leothedragon | 0:25fa8795676b | 63 | if (instance) { |
leothedragon | 0:25fa8795676b | 64 | |
leothedragon | 0:25fa8795676b | 65 | add_success = instance->do_add_callback(object, callback, type, client_args); |
leothedragon | 0:25fa8795676b | 66 | } |
leothedragon | 0:25fa8795676b | 67 | return add_success; |
leothedragon | 0:25fa8795676b | 68 | } |
leothedragon | 0:25fa8795676b | 69 | |
leothedragon | 0:25fa8795676b | 70 | bool M2MCallbackStorage::do_add_callback(const M2MBase &object, void *callback, M2MCallbackAssociation::M2MCallbackType type, void *client_args) |
leothedragon | 0:25fa8795676b | 71 | { |
leothedragon | 0:25fa8795676b | 72 | bool add_success = false; |
leothedragon | 0:25fa8795676b | 73 | |
leothedragon | 0:25fa8795676b | 74 | // verify that the same callback is not re-added. |
leothedragon | 0:25fa8795676b | 75 | if (does_callback_exist(object, callback, type) == false) { |
leothedragon | 0:25fa8795676b | 76 | |
leothedragon | 0:25fa8795676b | 77 | const M2MCallbackAssociation association(&object, callback, type, client_args); |
leothedragon | 0:25fa8795676b | 78 | _callbacks.push_back(association); |
leothedragon | 0:25fa8795676b | 79 | add_success = true; |
leothedragon | 0:25fa8795676b | 80 | } |
leothedragon | 0:25fa8795676b | 81 | |
leothedragon | 0:25fa8795676b | 82 | return add_success; |
leothedragon | 0:25fa8795676b | 83 | } |
leothedragon | 0:25fa8795676b | 84 | |
leothedragon | 0:25fa8795676b | 85 | #if 0 // Functions not used currently |
leothedragon | 0:25fa8795676b | 86 | void M2MCallbackStorage::remove_callbacks(const M2MBase &object) |
leothedragon | 0:25fa8795676b | 87 | { |
leothedragon | 0:25fa8795676b | 88 | // do not use the get_instance() here as it might create the instance |
leothedragon | 0:25fa8795676b | 89 | M2MCallbackStorage* instance = M2MCallbackStorage::_static_instance; |
leothedragon | 0:25fa8795676b | 90 | if (instance) { |
leothedragon | 0:25fa8795676b | 91 | |
leothedragon | 0:25fa8795676b | 92 | instance->do_remove_callbacks(object); |
leothedragon | 0:25fa8795676b | 93 | } |
leothedragon | 0:25fa8795676b | 94 | } |
leothedragon | 0:25fa8795676b | 95 | |
leothedragon | 0:25fa8795676b | 96 | void M2MCallbackStorage::do_remove_callbacks(const M2MBase &object) |
leothedragon | 0:25fa8795676b | 97 | { |
leothedragon | 0:25fa8795676b | 98 | // find any association to given object and delete them from the vector |
leothedragon | 0:25fa8795676b | 99 | for (int index = 0; index < _callbacks.size();) { |
leothedragon | 0:25fa8795676b | 100 | if (_callbacks[index]._object == &object) { |
leothedragon | 0:25fa8795676b | 101 | _callbacks.erase(index); |
leothedragon | 0:25fa8795676b | 102 | } else { |
leothedragon | 0:25fa8795676b | 103 | index++; |
leothedragon | 0:25fa8795676b | 104 | } |
leothedragon | 0:25fa8795676b | 105 | } |
leothedragon | 0:25fa8795676b | 106 | } |
leothedragon | 0:25fa8795676b | 107 | #endif |
leothedragon | 0:25fa8795676b | 108 | |
leothedragon | 0:25fa8795676b | 109 | void* M2MCallbackStorage::remove_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) |
leothedragon | 0:25fa8795676b | 110 | { |
leothedragon | 0:25fa8795676b | 111 | void* callback = NULL; |
leothedragon | 0:25fa8795676b | 112 | |
leothedragon | 0:25fa8795676b | 113 | // do not use the get_instance() here as it might create the instance |
leothedragon | 0:25fa8795676b | 114 | M2MCallbackStorage* instance = M2MCallbackStorage::_static_instance; |
leothedragon | 0:25fa8795676b | 115 | if (instance) { |
leothedragon | 0:25fa8795676b | 116 | |
leothedragon | 0:25fa8795676b | 117 | callback = instance->do_remove_callback(object, type); |
leothedragon | 0:25fa8795676b | 118 | } |
leothedragon | 0:25fa8795676b | 119 | return callback; |
leothedragon | 0:25fa8795676b | 120 | } |
leothedragon | 0:25fa8795676b | 121 | |
leothedragon | 0:25fa8795676b | 122 | void* M2MCallbackStorage::do_remove_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) |
leothedragon | 0:25fa8795676b | 123 | { |
leothedragon | 0:25fa8795676b | 124 | void* callback = NULL; |
leothedragon | 0:25fa8795676b | 125 | for (int index = 0; index < _callbacks.size(); index++) { |
leothedragon | 0:25fa8795676b | 126 | |
leothedragon | 0:25fa8795676b | 127 | if ((_callbacks[index]._object == &object) && (_callbacks[index]._type == type)) { |
leothedragon | 0:25fa8795676b | 128 | callback = _callbacks[index]._callback; |
leothedragon | 0:25fa8795676b | 129 | _callbacks.erase(index); |
leothedragon | 0:25fa8795676b | 130 | break; |
leothedragon | 0:25fa8795676b | 131 | } |
leothedragon | 0:25fa8795676b | 132 | } |
leothedragon | 0:25fa8795676b | 133 | return callback; |
leothedragon | 0:25fa8795676b | 134 | } |
leothedragon | 0:25fa8795676b | 135 | |
leothedragon | 0:25fa8795676b | 136 | void* M2MCallbackStorage::get_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) |
leothedragon | 0:25fa8795676b | 137 | { |
leothedragon | 0:25fa8795676b | 138 | void* callback = NULL; |
leothedragon | 0:25fa8795676b | 139 | const M2MCallbackStorage* instance = get_instance(); |
leothedragon | 0:25fa8795676b | 140 | if (instance) { |
leothedragon | 0:25fa8795676b | 141 | |
leothedragon | 0:25fa8795676b | 142 | callback = instance->do_get_callback(object, type); |
leothedragon | 0:25fa8795676b | 143 | } |
leothedragon | 0:25fa8795676b | 144 | return callback; |
leothedragon | 0:25fa8795676b | 145 | } |
leothedragon | 0:25fa8795676b | 146 | |
leothedragon | 0:25fa8795676b | 147 | void* M2MCallbackStorage::do_get_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) const |
leothedragon | 0:25fa8795676b | 148 | { |
leothedragon | 0:25fa8795676b | 149 | void* callback = NULL; |
leothedragon | 0:25fa8795676b | 150 | if (!_callbacks.empty()) { |
leothedragon | 0:25fa8795676b | 151 | M2MCallbackAssociationList::const_iterator it = _callbacks.begin(); |
leothedragon | 0:25fa8795676b | 152 | |
leothedragon | 0:25fa8795676b | 153 | for ( ; it != _callbacks.end(); it++ ) { |
leothedragon | 0:25fa8795676b | 154 | |
leothedragon | 0:25fa8795676b | 155 | if ((it->_object == &object) && (it->_type == type)) { |
leothedragon | 0:25fa8795676b | 156 | callback = it->_callback; |
leothedragon | 0:25fa8795676b | 157 | break; |
leothedragon | 0:25fa8795676b | 158 | } |
leothedragon | 0:25fa8795676b | 159 | } |
leothedragon | 0:25fa8795676b | 160 | } |
leothedragon | 0:25fa8795676b | 161 | return callback; |
leothedragon | 0:25fa8795676b | 162 | } |
leothedragon | 0:25fa8795676b | 163 | |
leothedragon | 0:25fa8795676b | 164 | M2MCallbackAssociation* M2MCallbackStorage::get_association_item(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) |
leothedragon | 0:25fa8795676b | 165 | { |
leothedragon | 0:25fa8795676b | 166 | M2MCallbackAssociation* callback_association = NULL; |
leothedragon | 0:25fa8795676b | 167 | const M2MCallbackStorage* instance = get_instance(); |
leothedragon | 0:25fa8795676b | 168 | if (instance) { |
leothedragon | 0:25fa8795676b | 169 | |
leothedragon | 0:25fa8795676b | 170 | callback_association = instance->do_get_association_item(object, type); |
leothedragon | 0:25fa8795676b | 171 | } |
leothedragon | 0:25fa8795676b | 172 | return callback_association; |
leothedragon | 0:25fa8795676b | 173 | } |
leothedragon | 0:25fa8795676b | 174 | |
leothedragon | 0:25fa8795676b | 175 | M2MCallbackAssociation* M2MCallbackStorage::do_get_association_item(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) const |
leothedragon | 0:25fa8795676b | 176 | { |
leothedragon | 0:25fa8795676b | 177 | M2MCallbackAssociation* callback_association = NULL; |
leothedragon | 0:25fa8795676b | 178 | if (!_callbacks.empty()) { |
leothedragon | 0:25fa8795676b | 179 | M2MCallbackAssociationList::const_iterator it = _callbacks.begin(); |
leothedragon | 0:25fa8795676b | 180 | |
leothedragon | 0:25fa8795676b | 181 | for ( ; it != _callbacks.end(); it++ ) { |
leothedragon | 0:25fa8795676b | 182 | |
leothedragon | 0:25fa8795676b | 183 | if ((it->_object == &object) && (it->_type == type)) { |
leothedragon | 0:25fa8795676b | 184 | callback_association = (M2MCallbackAssociation*)it; |
leothedragon | 0:25fa8795676b | 185 | break; |
leothedragon | 0:25fa8795676b | 186 | } |
leothedragon | 0:25fa8795676b | 187 | } |
leothedragon | 0:25fa8795676b | 188 | } |
leothedragon | 0:25fa8795676b | 189 | return callback_association; |
leothedragon | 0:25fa8795676b | 190 | } |
leothedragon | 0:25fa8795676b | 191 | |
leothedragon | 0:25fa8795676b | 192 | bool M2MCallbackStorage::does_callback_exist(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) |
leothedragon | 0:25fa8795676b | 193 | { |
leothedragon | 0:25fa8795676b | 194 | bool found = false; |
leothedragon | 0:25fa8795676b | 195 | if (get_callback(object, type) != NULL) { |
leothedragon | 0:25fa8795676b | 196 | found = true; |
leothedragon | 0:25fa8795676b | 197 | } |
leothedragon | 0:25fa8795676b | 198 | return found; |
leothedragon | 0:25fa8795676b | 199 | } |
leothedragon | 0:25fa8795676b | 200 | |
leothedragon | 0:25fa8795676b | 201 | bool M2MCallbackStorage::does_callback_exist(const M2MBase &object, void *callback, M2MCallbackAssociation::M2MCallbackType type) const |
leothedragon | 0:25fa8795676b | 202 | { |
leothedragon | 0:25fa8795676b | 203 | bool match_found = false; |
leothedragon | 0:25fa8795676b | 204 | |
leothedragon | 0:25fa8795676b | 205 | if (!_callbacks.empty()) { |
leothedragon | 0:25fa8795676b | 206 | M2MCallbackAssociationList::const_iterator it = _callbacks.begin(); |
leothedragon | 0:25fa8795676b | 207 | |
leothedragon | 0:25fa8795676b | 208 | for ( ; it != _callbacks.end(); it++ ) { |
leothedragon | 0:25fa8795676b | 209 | |
leothedragon | 0:25fa8795676b | 210 | if ((it->_object == &object) && (it->_callback == callback) && (it->_type == type)) { |
leothedragon | 0:25fa8795676b | 211 | match_found = true; |
leothedragon | 0:25fa8795676b | 212 | break; |
leothedragon | 0:25fa8795676b | 213 | } |
leothedragon | 0:25fa8795676b | 214 | } |
leothedragon | 0:25fa8795676b | 215 | } |
leothedragon | 0:25fa8795676b | 216 | |
leothedragon | 0:25fa8795676b | 217 | return match_found; |
leothedragon | 0:25fa8795676b | 218 | } |