| 1 | /* |
| 2 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define BT_LOG_TAG "LIB/FIELD-WRAPPER" |
| 24 | #include "lib/logging.h" |
| 25 | |
| 26 | #include "lib/object-pool.h" |
| 27 | #include "lib/object.h" |
| 28 | #include <glib.h> |
| 29 | |
| 30 | #include "field-wrapper.h" |
| 31 | #include "field.h" |
| 32 | |
| 33 | BT_HIDDEN |
| 34 | struct bt_field_wrapper *bt_field_wrapper_new(void *data) |
| 35 | { |
| 36 | struct bt_field_wrapper *field_wrapper = |
| 37 | g_new0(struct bt_field_wrapper, 1); |
| 38 | |
| 39 | BT_LOGD_STR("Creating empty field wrapper object."); |
| 40 | |
| 41 | if (!field_wrapper) { |
| 42 | BT_LIB_LOGE_APPEND_CAUSE( |
| 43 | "Failed to allocate one field wrapper."); |
| 44 | goto end; |
| 45 | } |
| 46 | |
| 47 | bt_object_init_unique(&field_wrapper->base); |
| 48 | BT_LOGD("Created empty field wrapper object: addr=%p", |
| 49 | field_wrapper); |
| 50 | |
| 51 | end: |
| 52 | return field_wrapper; |
| 53 | } |
| 54 | |
| 55 | BT_HIDDEN |
| 56 | void bt_field_wrapper_destroy(struct bt_field_wrapper *field_wrapper) |
| 57 | { |
| 58 | BT_LOGD("Destroying field wrapper: addr=%p", field_wrapper); |
| 59 | |
| 60 | if (field_wrapper->field) { |
| 61 | BT_LOGD_STR("Destroying field."); |
| 62 | bt_field_destroy((void *) field_wrapper->field); |
| 63 | field_wrapper->field = NULL; |
| 64 | } |
| 65 | |
| 66 | BT_LOGD_STR("Putting stream class."); |
| 67 | g_free(field_wrapper); |
| 68 | } |
| 69 | |
| 70 | BT_HIDDEN |
| 71 | struct bt_field_wrapper *bt_field_wrapper_create( |
| 72 | struct bt_object_pool *pool, struct bt_field_class *fc) |
| 73 | { |
| 74 | struct bt_field_wrapper *field_wrapper = NULL; |
| 75 | |
| 76 | BT_ASSERT(pool); |
| 77 | BT_ASSERT(fc); |
| 78 | field_wrapper = bt_object_pool_create_object(pool); |
| 79 | if (!field_wrapper) { |
| 80 | BT_LIB_LOGE_APPEND_CAUSE( |
| 81 | "Cannot allocate one field wrapper from field wrapper pool: " |
| 82 | "%![pool-]+o", pool); |
| 83 | goto error; |
| 84 | } |
| 85 | |
| 86 | if (!field_wrapper->field) { |
| 87 | field_wrapper->field = (void *) bt_field_create(fc); |
| 88 | if (!field_wrapper->field) { |
| 89 | BT_LIB_LOGE_APPEND_CAUSE( |
| 90 | "Cannot create field wrapper from field class: " |
| 91 | "%![fc-]+F", fc); |
| 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | BT_LIB_LOGD("Created initial field wrapper object: " |
| 96 | "wrapper-addr=%p, %![field-]+f", field_wrapper, |
| 97 | field_wrapper->field); |
| 98 | } |
| 99 | |
| 100 | goto end; |
| 101 | |
| 102 | error: |
| 103 | if (field_wrapper) { |
| 104 | bt_field_wrapper_destroy(field_wrapper); |
| 105 | field_wrapper = NULL; |
| 106 | } |
| 107 | |
| 108 | end: |
| 109 | return field_wrapper; |
| 110 | } |