diff options
Diffstat (limited to 'src/common/callback_handler.c')
-rwxr-xr-x | src/common/callback_handler.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/common/callback_handler.c b/src/common/callback_handler.c new file mode 100755 index 0000000..8c23b7a --- /dev/null +++ b/src/common/callback_handler.c @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2013 - 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common/log.h" +#include "common/callback_handler.h" + +#include <stdlib.h> + +callback_h *callback_handler_new(const char *key) +{ + callback_h *cb_h = (callback_h *)malloc(sizeof(callback_h)); + if (cb_h == NULL) { + _E("failed to malloc"); + return NULL; + } + + cb_h->key = key == NULL ? NULL : strdup(key); + cb_h->cb_count = 0; + + return cb_h; +} + +void callback_handler_free(callback_h *cb_h) +{ + if (cb_h == NULL) { + return ; + } + + free(cb_h->key); + free(cb_h); +} + +bool callback_handler_add_cb(callback_h *cb_h, void(*cb)(void *data)) +{ + if (cb_h == NULL) { + _E("callback handler is null"); + return false; + } + + if (cb_h->cb_count >= MAX_CB_CNT) { + _E("[%s] callback handler is full", cb_h->key); + return false; + } + + int idx = 0; + for (idx = 0; idx < cb_h->cb_count; idx++) { + if (cb == cb_h->cb_list[idx]) { + _E("[%s] %p callback is already registered", cb_h->key, cb); + return false; + } + } + cb_h->cb_list[cb_h->cb_count] = cb; + cb_h->cb_count += 1; + + _D("[%s] current callback count is %d", cb_h->key, cb_h->cb_count); + + return true; +} + +void callback_handler_del_cb(callback_h *cb_h, void(*cb)(void *data)) +{ + if (cb_h == NULL) { + _E("callback handler is null"); + return ; + } + + if (cb_h->cb_count == MAX_CB_CNT) { + _E("[%s] callback handler is empty", cb_h->key); + return ; + } + + int idx = 0; + for (idx = 0; idx < cb_h->cb_count; idx++) { + if (cb == cb_h->cb_list[idx]) { + break; + } + } + + if (idx == cb_h->cb_count) { + _E("[%s] %p cb is not found", cb_h->key, cb); + return ; + } + + int cur = 0; + for (cur = idx + 1; cur < cb_h->cb_count; cur++) { + cb_h->cb_list[cur - 1] = cb_h->cb_list[cur]; + cb_h->cb_list[cur] = NULL; + } + cb_h->cb_count -= 1; + + _D("[%s] current callback count is %d", cb_h->key, cb_h->cb_count); +} + +void callback_handler_execute_cb(callback_h *cb_h, void *data) +{ + if (cb_h == NULL) { + _D("callback handler is null"); + return ; + } + + int idx = 0; + for (idx = 0; idx < cb_h->cb_count; idx++) { + if (cb_h->cb_list[idx] != NULL) { + (*cb_h->cb_list[idx])(data); + } + } +} |