Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 06:30:39 2018 +0000
Revision:
0:276e7a263c35
Initial commit

Who changed what in which revision?

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