Commit | Line | Data |
---|---|---|
347829f5 PP |
1 | #ifndef _BABELTRACE_OBJECTS_H |
2 | #define _BABELTRACE_OBJECTS_H | |
3 | ||
4 | /* | |
5 | * Babeltrace | |
6 | * | |
7 | * Basic object system | |
8 | * | |
9 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation | |
10 | * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com> | |
11 | * | |
12 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
13 | * of this software and associated documentation files (the "Software"), to deal | |
14 | * in the Software without restriction, including without limitation the rights | |
15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
16 | * copies of the Software, and to permit persons to whom the Software is | |
17 | * furnished to do so, subject to the following conditions: | |
18 | * | |
19 | * The above copyright notice and this permission notice shall be included in | |
20 | * all copies or substantial portions of the Software. | |
21 | * | |
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
28 | * SOFTWARE. | |
29 | */ | |
30 | ||
31 | /** | |
32 | * @file objects.h | |
33 | * @brief Basic object system | |
34 | * | |
35 | * This is a basic object system API. The following functions allow you | |
36 | * to create, modify, and destroy: | |
37 | * | |
38 | * - \link bt_object_null null objects\endlink | |
39 | * - \link bt_object_bool_create() boolean objects\endlink | |
40 | * - \link bt_object_integer_create() integer objects\endlink | |
41 | * - \link bt_object_float_create() floating point number | |
42 | * objects\endlink | |
43 | * - \link bt_object_string_create() string objects\endlink | |
44 | * - \link bt_object_array_create() array objects\endlink, | |
45 | * containing zero or more objects | |
46 | * - \link bt_object_map_create() map objects\endlink, mapping | |
47 | * string keys to objects | |
48 | * | |
49 | * All the object types above, except for null objects (which always | |
50 | * point to the same \link bt_object_null singleton\endlink), have a | |
51 | * reference count property. Once an object is created, its reference | |
52 | * count is set to 1. When \link bt_object_array_append() appending an | |
53 | * object to an array object\endlink, or \link bt_object_map_insert() | |
54 | * inserting an object into a map object\endlink, its reference count | |
55 | * is incremented, as well as when getting an object back from those | |
56 | * structures. The bt_object_get() and bt_object_put() functions exist | |
57 | * to deal with reference counting. Once you are done with an object, | |
58 | * pass it to bt_object_put(). | |
59 | * | |
60 | * A common action with objects is to create or get one, do something | |
61 | * with it, and then put it. To avoid putting it a second time later | |
62 | * (if an error occurs, for example), the variable is often reset to | |
63 | * \c NULL after putting the object it points to. Since this is so | |
64 | * common, you can use the BT_OBJECT_PUT() macro, which does just that: | |
65 | * | |
66 | * \code{.c} | |
67 | * struct bt_object *int_obj = bt_object_integer_create_init(34); | |
68 | * | |
69 | * if (!int_obj) { | |
70 | * goto error; | |
71 | * } | |
72 | * | |
73 | * // stuff, which could jump to error | |
74 | * | |
75 | * BT_OBJECT_PUT(int_obj); | |
76 | * | |
77 | * // stuff, which could jump to error | |
78 | * | |
79 | * return 0; | |
80 | * | |
81 | * error: | |
82 | * // safe, even if int_obj is NULL | |
83 | * BT_OBJECT_PUT(int_obj); | |
84 | * \endcode | |
85 | * | |
86 | * Another common manipulation is to move the ownership of an object | |
87 | * from one variable to another: since the reference count is not | |
88 | * incremented, and since, to avoid errors, two variables should not | |
89 | * point to same object without each of them having their own reference, | |
90 | * it is best practice to set the original variable to \c NULL. This | |
91 | * too can be accomplished in a single step using the BT_OBJECT_MOVE() | |
92 | * macro: | |
93 | * | |
94 | * \code{.c} | |
95 | * struct bt_object *int_obj2 = NULL; | |
96 | * struct bt_object *int_obj = bt_object_integer_create_init(-23); | |
97 | * | |
98 | * if (!int_obj) { | |
99 | * goto error; | |
100 | * } | |
101 | * | |
102 | * // stuff, which could jump to error | |
103 | * | |
104 | * BT_OBJECT_MOVE(int_obj2, int_obj); | |
105 | * | |
106 | * // stuff, which could jump to error | |
107 | * | |
108 | * return 0; | |
109 | * | |
110 | * error: | |
111 | * // safe, since only one of int_obj/int_obj2 (or none) | |
112 | * // points to the object | |
113 | * BT_OBJECT_PUT(int_obj); | |
114 | * BT_OBJECT_PUT(int_obj2); | |
115 | * \endcode | |
116 | * | |
117 | * You can create a deep copy of any object using the bt_object_copy() | |
118 | * function. You can compare two given objects using | |
119 | * bt_object_compare(). | |
120 | * | |
121 | * @author Philippe Proulx <pproulx@efficios.com> | |
122 | * @bug No known bugs | |
123 | */ | |
124 | ||
125 | #include <stdint.h> | |
126 | #include <stdbool.h> | |
127 | #include <stddef.h> | |
128 | ||
129 | #ifdef __cplusplus | |
130 | extern "C" { | |
131 | #endif | |
132 | ||
133 | /** | |
134 | * Object type. | |
135 | */ | |
136 | enum bt_object_type { | |
137 | /** Unknown object, used as an error code. */ | |
138 | BT_OBJECT_TYPE_UNKNOWN = -1, | |
139 | ||
140 | /** Null object. */ | |
141 | BT_OBJECT_TYPE_NULL = 0, | |
142 | ||
143 | /** Boolean object (holds \c true or \c false). */ | |
144 | BT_OBJECT_TYPE_BOOL = 1, | |
145 | ||
146 | /** Integer (holds a signed 64-bit integer value). */ | |
147 | BT_OBJECT_TYPE_INTEGER = 2, | |
148 | ||
149 | /** | |
150 | * Floating point number object (holds a \c double value). | |
151 | */ | |
152 | BT_OBJECT_TYPE_FLOAT = 3, | |
153 | ||
154 | /** String object. */ | |
155 | BT_OBJECT_TYPE_STRING = 4, | |
156 | ||
157 | /** Array object. */ | |
158 | BT_OBJECT_TYPE_ARRAY = 5, | |
159 | ||
160 | /** Map object. */ | |
161 | BT_OBJECT_TYPE_MAP = 6, | |
162 | }; | |
163 | ||
164 | /** | |
165 | * Object. | |
166 | */ | |
167 | struct bt_object; | |
168 | ||
169 | /** | |
170 | * The null object singleton. | |
171 | * | |
172 | * Use this everytime you need a null objet. The null objet singleton | |
173 | * has no reference count; there's only one. You may compare any object | |
174 | * to the null singleton to find out if it's a null object, or otherwise | |
175 | * use bt_object_is_null(). | |
176 | * | |
177 | * Functions of this API return this when the object is actually a | |
178 | * null object (of type \link bt_object_type::BT_OBJECT_TYPE_NULL | |
179 | * <code>BT_OBJECT_TYPE_NULL</code>\endlink), whereas \c NULL means an error | |
180 | * of some sort. | |
181 | */ | |
182 | extern struct bt_object *bt_object_null; | |
183 | ||
184 | /** | |
185 | * User function type for bt_object_map_foreach(). | |
186 | * | |
187 | * \p object is a \i weak reference; you must pass it to | |
188 | * bt_object_get() to get your own reference. | |
189 | * | |
190 | * Return \c true to continue the loop, or \c false to break it. | |
191 | * | |
192 | * @param key Key of map entry | |
193 | * @param object Object of map entry (weak reference) | |
194 | * @param data User data | |
195 | * @returns \c true to continue the loop | |
196 | */ | |
197 | typedef bool (* bt_object_map_foreach_cb)(const char *key, | |
198 | struct bt_object *object, void *data); | |
199 | ||
200 | /** | |
201 | * Puts the object \p _object (calls bt_object_put() on it), and resets | |
202 | * the variable to \c NULL. | |
203 | * | |
204 | * This is something that is often done when putting and object; | |
205 | * resetting the variable to \c NULL makes sure it cannot be put a | |
206 | * second time later. | |
207 | * | |
208 | * @param _object Object to put | |
209 | * | |
210 | * @see BT_OBJECT_MOVE() (moves an object from one variable to the other | |
211 | * without putting it) | |
212 | */ | |
213 | #define BT_OBJECT_PUT(_object) \ | |
214 | do { \ | |
215 | bt_object_put(_object); \ | |
216 | (_object) = NULL; \ | |
217 | } while (0) | |
218 | ||
219 | /** | |
220 | * Moves the object referenced by the variable \p _src_object to the | |
221 | * \p _dst_object variable, then resets \p _src_object to \c NULL. | |
222 | * | |
223 | * The object's reference count is <b>not changed</b>. Resetting | |
224 | * \p _src_object to \c NULL ensures the object will not be put | |
225 | * twice later; its ownership is indeed \i moved from the source | |
226 | * variable to the destination variable. | |
227 | * | |
228 | * @param _src_object Source object variable | |
229 | * @param _dst_object Destination object variable | |
230 | */ | |
231 | #define BT_OBJECT_MOVE(_dst_object, _src_object) \ | |
232 | do { \ | |
233 | (_dst_object) = (_src_object); \ | |
234 | (_src_object) = NULL; \ | |
235 | } while (0) | |
236 | ||
237 | /** | |
238 | * Increments the reference count of \p object. | |
239 | * | |
240 | * @param object Object of which to increment the reference count | |
241 | */ | |
242 | extern void bt_object_get(struct bt_object *object); | |
243 | ||
244 | /** | |
245 | * Decrements the reference count of \p object, destroying it when this | |
246 | * count reaches 0. | |
247 | * | |
248 | * @param object Object of which to decrement the reference count | |
249 | * | |
250 | * @see BT_OBJECT_PUT() (puts an object and resets the variable to | |
251 | * \c NULL) | |
252 | */ | |
253 | extern void bt_object_put(struct bt_object *object); | |
254 | ||
255 | /** | |
256 | * Returns the type of \p object. | |
257 | * | |
258 | * @param object Object of which to get the type | |
259 | * @returns Object's type, or | |
260 | * \link bt_object_type::BT_OBJECT_TYPE_NULL | |
261 | * <code>BT_OBJECT_TYPE_UNKNOWN</code>\endlink | |
262 | * on error | |
263 | * | |
264 | * @see enum bt_object_type (object types) | |
265 | */ | |
266 | extern enum bt_object_type bt_object_get_type(const struct bt_object *object); | |
267 | ||
268 | /** | |
269 | * Checks whether \p object is a null object. The only valid null | |
270 | * object is \ref bt_object_null. | |
271 | * | |
272 | * @param object Object to check | |
273 | * @returns \c true if \p object is a null object | |
274 | */ | |
e6631f7d | 275 | static bool bt_object_is_null(const struct bt_object *object) |
347829f5 PP |
276 | { |
277 | return bt_object_get_type(object) == BT_OBJECT_TYPE_NULL; | |
278 | } | |
279 | ||
280 | /** | |
281 | * Checks whether \p object is a boolean object. | |
282 | * | |
283 | * @param object Object to check | |
284 | * @returns \c true if \p object is a boolean object | |
285 | */ | |
e6631f7d | 286 | static bool bt_object_is_bool(const struct bt_object *object) |
347829f5 PP |
287 | { |
288 | return bt_object_get_type(object) == BT_OBJECT_TYPE_BOOL; | |
289 | } | |
290 | ||
291 | /** | |
292 | * Checks whether \p object is an integer object. | |
293 | * | |
294 | * @param object Object to check | |
295 | * @returns \c true if \p object is an integer object | |
296 | */ | |
e6631f7d | 297 | static bool bt_object_is_integer(const struct bt_object *object) |
347829f5 PP |
298 | { |
299 | return bt_object_get_type(object) == BT_OBJECT_TYPE_INTEGER; | |
300 | } | |
301 | ||
302 | /** | |
303 | * Checks whether \p object is a floating point number object. | |
304 | * | |
305 | * @param object Object to check | |
306 | * @returns \c true if \p object is a floating point number object | |
307 | */ | |
e6631f7d | 308 | static bool bt_object_is_float(const struct bt_object *object) |
347829f5 PP |
309 | { |
310 | return bt_object_get_type(object) == BT_OBJECT_TYPE_FLOAT; | |
311 | } | |
312 | ||
313 | /** | |
314 | * Checks whether \p object is a string object. | |
315 | * | |
316 | * @param object Object to check | |
317 | * @returns \c true if \p object is a string object | |
318 | */ | |
e6631f7d | 319 | static bool bt_object_is_string(const struct bt_object *object) |
347829f5 PP |
320 | { |
321 | return bt_object_get_type(object) == BT_OBJECT_TYPE_STRING; | |
322 | } | |
323 | ||
324 | /** | |
325 | * Checks whether \p object is an array object. | |
326 | * | |
327 | * @param object Object to check | |
328 | * @returns \c true if \p object is an array object | |
329 | */ | |
e6631f7d | 330 | static bool bt_object_is_array(const struct bt_object *object) |
347829f5 PP |
331 | { |
332 | return bt_object_get_type(object) == BT_OBJECT_TYPE_ARRAY; | |
333 | } | |
334 | ||
335 | /** | |
336 | * Checks whether \p object is a map object. | |
337 | * | |
338 | * @param object Object to check | |
339 | * @returns \c true if \p object is a map object | |
340 | */ | |
e6631f7d | 341 | static bool bt_object_is_map(const struct bt_object *object) |
347829f5 PP |
342 | { |
343 | return bt_object_get_type(object) == BT_OBJECT_TYPE_MAP; | |
344 | } | |
345 | ||
346 | /** | |
347 | * Creates a boolean object. The created boolean object's initial value | |
348 | * is \c false. | |
349 | * | |
350 | * The created object's reference count is set to 1. | |
351 | * | |
352 | * @returns Created object on success, or \c NULL on error | |
353 | */ | |
354 | extern struct bt_object *bt_object_bool_create(void); | |
355 | ||
356 | /** | |
357 | * Creates a boolean object with its initial value set to \p val. | |
358 | * | |
359 | * The created object's reference count is set to 1. | |
360 | * | |
361 | * @param val Initial value | |
362 | * @returns Created object on success, or \c NULL on error | |
363 | */ | |
364 | extern struct bt_object *bt_object_bool_create_init(bool val); | |
365 | ||
366 | /** | |
367 | * Creates an integer object. The created integer object's initial value | |
368 | * is 0. | |
369 | * | |
370 | * The created object's reference count is set to 1. | |
371 | * | |
372 | * @returns Created object on success, or \c NULL on error | |
373 | */ | |
374 | extern struct bt_object *bt_object_integer_create(void); | |
375 | ||
376 | /** | |
377 | * Creates an integer object with its initial value set to \p val. | |
378 | * | |
379 | * The created object's reference count is set to 1. | |
380 | * | |
381 | * @param val Initial value | |
382 | * @returns Created object on success, or \c NULL on error | |
383 | */ | |
384 | extern struct bt_object *bt_object_integer_create_init(int64_t val); | |
385 | ||
386 | /** | |
387 | * Creates a floating point number object. The created floating point | |
388 | * number object's initial value is 0. | |
389 | * | |
390 | * The created object's reference count is set to 1. | |
391 | * | |
392 | * @returns Created object on success, or \c NULL on error | |
393 | */ | |
394 | extern struct bt_object *bt_object_float_create(void); | |
395 | ||
396 | /** | |
397 | * Creates a floating point number object with its initial value set | |
398 | * to \p val. | |
399 | * | |
400 | * The created object's reference count is set to 1. | |
401 | * | |
402 | * @param val Initial value | |
403 | * @returns Created object on success, or \c NULL on error | |
404 | */ | |
405 | extern struct bt_object *bt_object_float_create_init(double val); | |
406 | ||
407 | /** | |
408 | * Creates a string object. The string object is initially empty. | |
409 | * | |
410 | * The created object's reference count is set to 1. | |
411 | * | |
412 | * @returns Created object on success, or \c NULL on error | |
413 | */ | |
414 | extern struct bt_object *bt_object_string_create(void); | |
415 | ||
416 | /** | |
417 | * Creates a string object with its initial value set to \p val. | |
418 | * | |
419 | * \p val is copied. | |
420 | * | |
421 | * The created object's reference count is set to 1. | |
422 | * | |
423 | * @param val Initial value (will be copied) | |
424 | * @returns Created object on success, or \c NULL on error | |
425 | */ | |
426 | extern struct bt_object *bt_object_string_create_init(const char *val); | |
427 | ||
428 | /** | |
429 | * Creates an empty array object. | |
430 | * | |
431 | * The created object's reference count is set to 1. | |
432 | * | |
433 | * @returns Created object on success, or \c NULL on error | |
434 | */ | |
435 | extern struct bt_object *bt_object_array_create(void); | |
436 | ||
437 | /** | |
438 | * Creates an empty map object. | |
439 | * | |
440 | * The created object's reference count is set to 1. | |
441 | * | |
442 | * @returns Created object on success, or \c NULL on error | |
443 | */ | |
444 | extern struct bt_object *bt_object_map_create(void); | |
445 | ||
446 | /** | |
447 | * Gets the boolean value of the boolean objet \p bool_obj. | |
448 | * | |
449 | * @param bool_obj Boolean object | |
450 | * @param val Returned boolean value | |
451 | * @returns 0 on success, negative value on error | |
452 | */ | |
453 | extern int bt_object_bool_get(const struct bt_object *bool_obj, bool *val); | |
454 | ||
455 | /** | |
456 | * Sets the boolean value of the boolean object \p bool_obj to \p val. | |
457 | * | |
458 | * @param bool_obj Boolean object | |
459 | * @param val New boolean value | |
460 | * @returns 0 on success, negative value on error | |
461 | */ | |
462 | extern int bt_object_bool_set(struct bt_object *bool_obj, bool val); | |
463 | ||
464 | /** | |
465 | * Gets the integer value of the integer objet \p integer_obj. | |
466 | * | |
467 | * @param integer_obj Integer object | |
468 | * @param val Returned integer value | |
469 | * @returns 0 on success, negative value on error | |
470 | */ | |
471 | extern int bt_object_integer_get(const struct bt_object *integer_obj, | |
472 | int64_t *val); | |
473 | ||
474 | /** | |
475 | * Sets the integer value of the integer object \p integer_obj to | |
476 | * \p val. | |
477 | * | |
478 | * @param integer_obj Integer object | |
479 | * @param val New integer value | |
480 | * @returns 0 on success, negative value on error | |
481 | */ | |
482 | extern int bt_object_integer_set(struct bt_object *integer_obj, int64_t val); | |
483 | ||
484 | /** | |
485 | * Gets the floating point number value of the floating point number | |
486 | * objet \p float_obj. | |
487 | * | |
488 | * @param float_obj Floating point number object | |
489 | * @param val Returned floating point number value | |
490 | * @returns 0 on success, negative value on error | |
491 | */ | |
492 | extern int bt_object_float_get(const struct bt_object *float_obj, double *val); | |
493 | ||
494 | /** | |
495 | * Sets the floating point number value of the floating point number | |
496 | * object \p float_obj to \p val. | |
497 | * | |
498 | * @param float_obj Floating point number object | |
499 | * @param val New floating point number value | |
500 | * @returns 0 on success, negative value on error | |
501 | */ | |
502 | extern int bt_object_float_set(struct bt_object *float_obj, double val); | |
503 | ||
504 | /** | |
505 | * Gets the string value of the string objet \p string_obj. The | |
506 | * returned string is valid as long as this object exists and is not | |
507 | * modified. | |
508 | * | |
509 | * @param string_obj String object | |
510 | * @returns String value, or \c NULL on error | |
511 | */ | |
512 | extern const char *bt_object_string_get(const struct bt_object *string_obj); | |
513 | ||
514 | /** | |
515 | * Sets the string value of the string object \p string_obj to | |
516 | * \p val. | |
517 | * | |
518 | * \p val is copied. | |
519 | * | |
520 | * @param string_obj String object | |
521 | * @param val New string value (copied) | |
522 | * @returns 0 on success, negative value on error | |
523 | */ | |
524 | extern int bt_object_string_set(struct bt_object *string_obj, const char *val); | |
525 | ||
526 | /** | |
527 | * Gets the size of the array object \p array_obj, that is, the number | |
528 | * of elements contained in \p array_obj. | |
529 | * | |
530 | * @param array_obj Array object | |
531 | * @returns Array size, or a negative value on error | |
532 | */ | |
533 | extern int bt_object_array_size(const struct bt_object *array_obj); | |
534 | ||
535 | /** | |
536 | * Returns \c true if the array object \p array_obj. | |
537 | * | |
538 | * @param array_obj Array object | |
539 | * @returns \c true if \p array_obj is empty | |
540 | */ | |
541 | extern bool bt_object_array_is_empty(const struct bt_object *array_obj); | |
542 | ||
543 | /** | |
544 | * Gets the element object of the array object \p array_obj at the | |
545 | * index \p index. | |
546 | * | |
547 | * The returned object's reference count is incremented, unless it's | |
548 | * a null object. | |
549 | * | |
550 | * @param array_obj Array object | |
551 | * @param index Index of element to get | |
552 | * @returns Element object at index \p index on success, | |
553 | * or \c NULL on error | |
554 | */ | |
555 | extern struct bt_object *bt_object_array_get(const struct bt_object *array_obj, | |
556 | size_t index); | |
557 | ||
558 | /** | |
559 | * Appends the element object \p element_obj to the array object | |
560 | * \p array_obj. | |
561 | * | |
562 | * The appended object's reference count is incremented, unless it's | |
563 | * a null object. | |
564 | * | |
565 | * @param array_obj Array object | |
566 | * @param element_obj Element object to append | |
567 | * @returns 0 on success, or a negative value on error | |
568 | */ | |
569 | extern int bt_object_array_append(struct bt_object *array_obj, | |
570 | struct bt_object *element_obj); | |
571 | ||
572 | /** | |
573 | * Appends the boolean value \p val to the array object \p array_obj. | |
574 | * This is a convenience function which creates the underlying boolean | |
575 | * object before appending it. | |
576 | * | |
577 | * The created boolean object's reference count is set to 1. | |
578 | * | |
579 | * @param array_obj Array object | |
580 | * @param val Boolean value to append | |
581 | * @returns 0 on success, or a negative value on error | |
582 | */ | |
583 | extern int bt_object_array_append_bool(struct bt_object *array_obj, bool val); | |
584 | ||
585 | /** | |
586 | * Appends the integer value \p val to the array object \p array_obj. | |
587 | * This is a convenience function which creates the underlying integer | |
588 | * object before appending it. | |
589 | * | |
590 | * The created integer object's reference count is set to 1. | |
591 | * | |
592 | * @param array_obj Array object | |
593 | * @param val Integer value to append | |
594 | * @returns 0 on success, or a negative value on error | |
595 | */ | |
596 | extern int bt_object_array_append_integer(struct bt_object *array_obj, | |
597 | int64_t val); | |
598 | ||
599 | /** | |
600 | * Appends the floating point number value \p val to the array object | |
601 | * \p array_obj. This is a convenience function which creates the | |
602 | * underlying floating point number object before appending it. | |
603 | * | |
604 | * The created floating point number object's reference count is | |
605 | * set to 1. | |
606 | * | |
607 | * @param array_obj Array object | |
608 | * @param val Floating point number value to append | |
609 | * @returns 0 on success, or a negative value on error | |
610 | */ | |
611 | extern int bt_object_array_append_float(struct bt_object *array_obj, | |
612 | double val); | |
613 | ||
614 | /** | |
615 | * Appends the string value \p val to the array object \p array_obj. | |
616 | * This is a convenience function which creates the underlying string | |
617 | * object before appending it. | |
618 | * | |
619 | * \p val is copied. | |
620 | * | |
621 | * The created string object's reference count is set to 1. | |
622 | * | |
623 | * @param array_obj Array object | |
624 | * @param val String value to append (copied) | |
625 | * @returns 0 on success, or a negative value on error | |
626 | */ | |
627 | extern int bt_object_array_append_string(struct bt_object *array_obj, | |
628 | const char *val); | |
629 | ||
630 | /** | |
631 | * Appends an empty array object to the array object \p array_obj. | |
632 | * This is a convenience function which creates the underlying array | |
633 | * object before appending it. | |
634 | * | |
635 | * The created array object's reference count is set to 1. | |
636 | * | |
637 | * @param array_obj Array object | |
638 | * @returns 0 on success, or a negative value on error | |
639 | */ | |
640 | extern int bt_object_array_append_array(struct bt_object *array_obj); | |
641 | ||
642 | /** | |
643 | * Appends an empty map object to the array object \p array_obj. This | |
644 | * is a convenience function which creates the underlying map object | |
645 | * before appending it. | |
646 | * | |
647 | * The created map object's reference count is set to 1. | |
648 | * | |
649 | * @param array_obj Array object | |
650 | * @returns 0 on success, or a negative value on error | |
651 | */ | |
652 | extern int bt_object_array_append_map(struct bt_object *array_obj); | |
653 | ||
3695540c PP |
654 | /** |
655 | * Replaces the element object at index \p index of the array object | |
656 | * \p array_obj by \p element_obj. | |
657 | * | |
658 | * The replaced object's reference count is decremented, unless it's | |
659 | * a null object. The reference count of \p element_obj is incremented, | |
660 | * unless it's a null object. | |
661 | * | |
662 | * @param array_obj Array object | |
663 | * @param index Index of element object to replace | |
664 | * @param element_obj New element object at position \p index of | |
665 | * \p array_obj | |
666 | * @returns 0 on success, or a negative value on error | |
667 | */ | |
668 | extern int bt_object_array_set(struct bt_object *array_obj, size_t index, | |
669 | struct bt_object *element_obj); | |
670 | ||
347829f5 PP |
671 | /** |
672 | * Gets the size of a map object, that is, the number of elements | |
673 | * contained in a map object. | |
674 | * | |
675 | * @param map_obj Map object | |
676 | * @returns Map size, or a negative value on error | |
677 | */ | |
678 | extern int bt_object_map_size(const struct bt_object *map_obj); | |
679 | ||
680 | /** | |
681 | * Returns \c true if the map object \p map_obj. | |
682 | * | |
683 | * @param map_obj Map object | |
684 | * @returns \c true if \p map_obj is empty | |
685 | */ | |
686 | extern bool bt_object_map_is_empty(const struct bt_object *map_obj); | |
687 | ||
688 | /** | |
689 | * Gets the element object associated with the key \p key within the | |
690 | * map object \p map_obj. | |
691 | * | |
692 | * The returned object's reference count is incremented, unless it's | |
693 | * a null object. | |
694 | * | |
695 | * @param map_obj Map object | |
696 | * @param key Key of the element to get | |
697 | * @returns Element object associated with the key \p key | |
698 | * on success, or \c NULL on error | |
699 | */ | |
700 | extern struct bt_object *bt_object_map_get(const struct bt_object *map_obj, | |
701 | const char *key); | |
702 | ||
703 | /** | |
704 | * Calls a provided user function \p cb for each element of the map | |
705 | * object \p map_obj. | |
706 | * | |
707 | * The object passed to the user function is a <b>weak reference</b>: | |
708 | * you must call bt_object_get() on it to obtain your own reference. | |
709 | * | |
710 | * The key passed to the user function is only valid in the scope of | |
711 | * this user function. | |
712 | * | |
713 | * The user function must return \c true to continue the loop, or | |
714 | * \c false to break it. | |
715 | * | |
716 | * @param map_obj Map object | |
717 | * @param cb User function to call back | |
718 | * @param data User data passed to the user function | |
719 | * @returns 0 on success, or a negative value on error | |
720 | * (the user function breaking the loop is \b not | |
721 | * considered an error here) | |
722 | */ | |
723 | extern int bt_object_map_foreach(const struct bt_object *map_obj, | |
724 | bt_object_map_foreach_cb cb, void *data); | |
725 | ||
726 | /** | |
727 | * Returns whether or not the map object \p map_obj contains the | |
728 | * key \p key. | |
729 | * | |
730 | * @param map_obj Map object | |
731 | * @param key Key to check | |
732 | * @returns \c true if \p map_obj contains the key \p key, | |
733 | * or \c false if it doesn't have \p key or | |
734 | * on error | |
735 | */ | |
736 | extern bool bt_object_map_has_key(const struct bt_object *map_obj, | |
737 | const char *key); | |
738 | ||
739 | /** | |
740 | * Inserts the element object \p element_obj associated with the key | |
741 | * \p key into the map object \p map_obj. If \p key exists in | |
742 | * \p map_obj, the associated element object is first put, and then | |
743 | * replaced by \p element_obj. | |
744 | * | |
745 | * \p key is copied. | |
746 | * | |
747 | * The inserted object's reference count is incremented, unless it's | |
748 | * a null object. | |
749 | * | |
750 | * @param map_obj Map object | |
751 | * @param key Key (copied) of object to insert | |
752 | * @param element_obj Element object to insert, associated with the | |
753 | * key \p key | |
754 | * @returns 0 on success, or a negative value on error | |
755 | */ | |
756 | extern int bt_object_map_insert(struct bt_object *map_obj, | |
757 | const char *key, struct bt_object *element_obj); | |
758 | ||
759 | /** | |
760 | * Inserts the boolean value \p val associated with the key \p key into | |
761 | * the map object \p map_obj. This is a convenience function which | |
762 | * creates the underlying boolean object before inserting it. | |
763 | * | |
764 | * \p key is copied. | |
765 | * | |
766 | * The created boolean object's reference count is set to 1. | |
767 | * | |
768 | * @param map_obj Map object | |
769 | * @param key Key (copied) of boolean value to insert | |
770 | * @param val Boolean value to insert, associated with the | |
771 | * key \p key | |
772 | * @returns 0 on success, or a negative value on error | |
773 | */ | |
774 | extern int bt_object_map_insert_bool(struct bt_object *map_obj, | |
775 | const char *key, bool val); | |
776 | ||
777 | /** | |
778 | * Inserts the integer value \p val associated with the key \p key into | |
779 | * the map object \p map_obj. This is a convenience function which | |
780 | * creates the underlying integer object before inserting it. | |
781 | * | |
782 | * \p key is copied. | |
783 | * | |
784 | * The created integer object's reference count is set to 1. | |
785 | * | |
786 | * @param map_obj Map object | |
787 | * @param key Key (copied) of integer value to insert | |
788 | * @param val Integer value to insert, associated with the | |
789 | * key \p key | |
790 | * @returns 0 on success, or a negative value on error | |
791 | */ | |
792 | extern int bt_object_map_insert_integer(struct bt_object *map_obj, | |
793 | const char *key, int64_t val); | |
794 | ||
795 | /** | |
796 | * Inserts the floating point number value \p val associated with the | |
797 | * key \p key into the map object \p map_obj. This is a convenience | |
798 | * function which creates the underlying floating point number object | |
799 | * before inserting it. | |
800 | * | |
801 | * \p key is copied. | |
802 | * | |
803 | * The created floating point number object's reference count is | |
804 | * set to 1. | |
805 | * | |
806 | * @param map_obj Map object | |
807 | * @param key Key (copied) of floating point number value to | |
808 | * insert | |
809 | * @param val Floating point number value to insert, | |
810 | * associated with the key \p key | |
811 | * @returns 0 on success, or a negative value on error | |
812 | */ | |
813 | extern int bt_object_map_insert_float(struct bt_object *map_obj, | |
814 | const char *key, double val); | |
815 | ||
816 | /** | |
817 | * Inserts the string value \p val associated with the key \p key into | |
818 | * the map object \p map_obj. This is a convenience function which | |
819 | * creates the underlying string object before inserting it. | |
820 | * | |
821 | * \p val and \p key are copied. | |
822 | * | |
823 | * The created string object's reference count is set to 1. | |
824 | * | |
825 | * @param map_obj Map object | |
826 | * @param key Key (copied) of string value to insert | |
827 | * @param val String value to insert, associated with the | |
828 | * key \p key | |
829 | * @returns 0 on success, or a negative value on error | |
830 | */ | |
831 | extern int bt_object_map_insert_string(struct bt_object *map_obj, | |
832 | const char *key, const char *val); | |
833 | ||
834 | /** | |
835 | * Inserts an empty array object associated with the key \p key into | |
836 | * the map object \p map_obj. This is a convenience function which | |
837 | * creates the underlying array object before inserting it. | |
838 | * | |
839 | * \p key is copied. | |
840 | * | |
841 | * The created array object's reference count is set to 1. | |
842 | * | |
843 | * @param map_obj Map object | |
844 | * @param key Key (copied) of empty array to insert | |
845 | * @returns 0 on success, or a negative value on error | |
846 | */ | |
847 | extern int bt_object_map_insert_array(struct bt_object *map_obj, | |
848 | const char *key); | |
849 | ||
850 | /** | |
851 | * Inserts an empty map object associated with the key \p key into | |
852 | * the map object \p map_obj. This is a convenience function which | |
853 | * creates the underlying map object before inserting it. | |
854 | * | |
855 | * \p key is copied. | |
856 | * | |
857 | * The created map object's reference count is set to 1. | |
858 | * | |
859 | * @param map_obj Map object | |
860 | * @param key Key (copied) of empty map to insert | |
861 | * @returns 0 on success, or a negative value on error | |
862 | */ | |
863 | extern int bt_object_map_insert_map(struct bt_object *map_obj, | |
864 | const char *key); | |
865 | ||
866 | /** | |
867 | * Creates a deep copy of the object \p object. | |
868 | * | |
869 | * The created object's reference count is set to 1, unless | |
870 | * \p object is a null object. | |
871 | * | |
872 | * @param object Object to copy | |
873 | * @returns Deep copy of \p object on success, or \c NULL | |
874 | * on error | |
875 | */ | |
876 | extern struct bt_object *bt_object_copy(const struct bt_object *object); | |
877 | ||
878 | /** | |
879 | * Compares the objects \p object_a and \p object_b and returns \c true | |
880 | * if they have the same content. | |
881 | * | |
882 | * @param object_a Object A | |
883 | * @param object_B Object B | |
884 | * @returns \c true if \p object_a and \p object_b have the | |
885 | * same content, or \c false if they differ or on | |
886 | * error | |
887 | */ | |
888 | extern bool bt_object_compare(const struct bt_object *object_a, | |
889 | const struct bt_object *object_b); | |
890 | ||
891 | #ifdef __cplusplus | |
892 | } | |
893 | #endif | |
894 | ||
895 | #endif /* _BABELTRACE_OBJECTS_H */ |