Commit | Line | Data |
---|---|---|
a3f0c7db SM |
1 | /* |
2 | * Copyright 2019 EfficiOS, Inc. | |
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/MESSAGE-ITERATOR-CLASS" | |
24 | #include "lib/logging.h" | |
25 | ||
26 | #include "message-iterator-class.h" | |
27 | ||
28 | #include "compat/compiler.h" | |
29 | #include "lib/assert-pre.h" | |
30 | #include "lib/func-status.h" | |
31 | ||
32 | #define BT_ASSERT_PRE_DEV_MSG_ITER_CLS_HOT(_msg_iter_cls) \ | |
33 | BT_ASSERT_PRE_DEV_HOT((_msg_iter_cls), \ | |
34 | "Message iterator class", ": %!+I", (_msg_iter_cls)) | |
35 | ||
36 | BT_HIDDEN | |
37 | void _bt_message_iterator_class_freeze( | |
38 | const struct bt_message_iterator_class *msg_iter_cls) | |
39 | { | |
40 | BT_ASSERT(msg_iter_cls); | |
41 | BT_LIB_LOGD("Freezing message iterator class: %!+I", msg_iter_cls); | |
42 | ((struct bt_message_iterator_class *) msg_iter_cls)->frozen = true; | |
43 | } | |
44 | ||
45 | void bt_message_iterator_class_get_ref( | |
46 | const bt_message_iterator_class *message_iterator_class) | |
47 | { | |
48 | bt_object_get_ref(message_iterator_class); | |
49 | } | |
50 | ||
51 | void bt_message_iterator_class_put_ref( | |
52 | const bt_message_iterator_class *message_iterator_class) | |
53 | { | |
54 | bt_object_put_ref(message_iterator_class); | |
55 | } | |
56 | ||
57 | static | |
58 | void destroy_iterator_class(struct bt_object *obj) | |
59 | { | |
60 | struct bt_message_iterator_class *class; | |
61 | ||
62 | BT_ASSERT(obj); | |
63 | class = container_of(obj, struct bt_message_iterator_class, base); | |
64 | ||
65 | BT_LIB_LOGI("Destroying message iterator class: %!+I", class); | |
66 | ||
67 | g_free(class); | |
68 | } | |
69 | ||
70 | struct bt_message_iterator_class *bt_message_iterator_class_create( | |
71 | bt_message_iterator_class_next_method next_method) | |
72 | { | |
73 | struct bt_message_iterator_class *message_iterator_class; | |
74 | ||
75 | BT_ASSERT_PRE_NO_ERROR(); | |
76 | BT_ASSERT_PRE_NON_NULL(next_method, "Next method"); | |
77 | BT_LOGI("Creating message iterator class: next-method-addr=%p", | |
78 | next_method); | |
79 | ||
80 | message_iterator_class = g_new0(struct bt_message_iterator_class, 1); | |
81 | if (!message_iterator_class) { | |
82 | BT_LIB_LOGE_APPEND_CAUSE( | |
83 | "Failed to allocate one message iterator class."); | |
84 | goto end; | |
85 | } | |
86 | ||
87 | bt_object_init_shared(&message_iterator_class->base, destroy_iterator_class); | |
88 | ||
89 | message_iterator_class->methods.next = next_method; | |
90 | ||
91 | end: | |
92 | return message_iterator_class; | |
93 | } | |
94 | ||
95 | bt_message_iterator_class_set_method_status | |
96 | bt_message_iterator_class_set_initialize_method( | |
97 | bt_message_iterator_class *message_iterator_class, | |
98 | bt_message_iterator_class_initialize_method method) | |
99 | { | |
100 | BT_ASSERT_PRE_NO_ERROR(); | |
101 | BT_ASSERT_PRE_NON_NULL(message_iterator_class, "Message iterator class"); | |
102 | BT_ASSERT_PRE_NON_NULL(method, "Method"); | |
103 | BT_ASSERT_PRE_DEV_MSG_ITER_CLS_HOT(message_iterator_class); | |
104 | message_iterator_class->methods.initialize = method; | |
105 | BT_LIB_LOGD("Set message iterator class's iterator initialization method" | |
106 | ": %!+I", message_iterator_class); | |
107 | return BT_FUNC_STATUS_OK; | |
108 | } | |
109 | ||
110 | bt_message_iterator_class_set_method_status | |
111 | bt_message_iterator_class_set_finalize_method( | |
112 | bt_message_iterator_class *message_iterator_class, | |
113 | bt_message_iterator_class_finalize_method method) | |
114 | { | |
115 | BT_ASSERT_PRE_NO_ERROR(); | |
116 | BT_ASSERT_PRE_NON_NULL(message_iterator_class, "Message iterator class"); | |
117 | BT_ASSERT_PRE_NON_NULL(method, "Method"); | |
118 | BT_ASSERT_PRE_DEV_MSG_ITER_CLS_HOT(message_iterator_class); | |
119 | message_iterator_class->methods.finalize = method; | |
120 | BT_LIB_LOGD("Set message iterator class's finalization method" | |
121 | ": %!+I", message_iterator_class); | |
122 | return BT_FUNC_STATUS_OK; | |
123 | } | |
124 | ||
125 | bt_message_iterator_class_set_method_status | |
126 | bt_message_iterator_class_set_seek_ns_from_origin_methods( | |
127 | bt_message_iterator_class *message_iterator_class, | |
128 | bt_message_iterator_class_seek_ns_from_origin_method seek_method, | |
129 | bt_message_iterator_class_can_seek_ns_from_origin_method can_seek_method) | |
130 | { | |
131 | BT_ASSERT_PRE_NO_ERROR(); | |
132 | BT_ASSERT_PRE_NON_NULL(message_iterator_class, "Message iterator class"); | |
133 | BT_ASSERT_PRE_NON_NULL(seek_method, "Seek method"); | |
134 | BT_ASSERT_PRE_DEV_MSG_ITER_CLS_HOT(message_iterator_class); | |
135 | message_iterator_class->methods.seek_ns_from_origin = seek_method; | |
136 | message_iterator_class->methods.can_seek_ns_from_origin = can_seek_method; | |
137 | BT_LIB_LOGD("Set message iterator class's \"seek nanoseconds from origin\" method" | |
138 | ": %!+I", message_iterator_class); | |
139 | return BT_FUNC_STATUS_OK; | |
140 | } | |
141 | ||
142 | bt_message_iterator_class_set_method_status | |
143 | bt_message_iterator_class_set_seek_beginning_methods( | |
144 | bt_message_iterator_class *message_iterator_class, | |
145 | bt_message_iterator_class_seek_beginning_method seek_method, | |
146 | bt_message_iterator_class_can_seek_beginning_method can_seek_method) | |
147 | { | |
148 | BT_ASSERT_PRE_NO_ERROR(); | |
149 | BT_ASSERT_PRE_NON_NULL(message_iterator_class, "Message iterator class"); | |
150 | BT_ASSERT_PRE_NON_NULL(seek_method, "Seek method"); | |
151 | BT_ASSERT_PRE_DEV_MSG_ITER_CLS_HOT(message_iterator_class); | |
152 | message_iterator_class->methods.seek_beginning = seek_method; | |
153 | message_iterator_class->methods.can_seek_beginning = can_seek_method; | |
154 | BT_LIB_LOGD("Set message iterator class's \"seek beginning\" methods" | |
155 | ": %!+C", message_iterator_class); | |
156 | return BT_FUNC_STATUS_OK; | |
157 | } |