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