objects: add bt_object_freeze() and more statuses
[babeltrace.git] / include / babeltrace / objects.h
CommitLineData
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);
a40a567e
PP
84 *
85 * // ...
347829f5
PP
86 * \endcode
87 *
88 * Another common manipulation is to move the ownership of an object
89 * from one variable to another: since the reference count is not
90 * incremented, and since, to avoid errors, two variables should not
91 * point to same object without each of them having their own reference,
92 * it is best practice to set the original variable to \c NULL. This
93 * too can be accomplished in a single step using the BT_OBJECT_MOVE()
94 * macro:
95 *
96 * \code{.c}
97 * struct bt_object *int_obj2 = NULL;
98 * struct bt_object *int_obj = bt_object_integer_create_init(-23);
99 *
100 * if (!int_obj) {
101 * goto error;
102 * }
103 *
104 * // stuff, which could jump to error
105 *
106 * BT_OBJECT_MOVE(int_obj2, int_obj);
107 *
108 * // stuff, which could jump to error
109 *
110 * return 0;
111 *
112 * error:
113 * // safe, since only one of int_obj/int_obj2 (or none)
114 * // points to the object
115 * BT_OBJECT_PUT(int_obj);
116 * BT_OBJECT_PUT(int_obj2);
a40a567e
PP
117 *
118 * // ...
347829f5
PP
119 * \endcode
120 *
a40a567e
PP
121 * Most functions return a status code, one of the values in
122 * #bt_object_status.
123 *
347829f5
PP
124 * You can create a deep copy of any object using the bt_object_copy()
125 * function. You can compare two given objects using
126 * bt_object_compare().
127 *
a40a567e
PP
128 * Any object may be frozen using bt_object_freeze(). You may get the
129 * value of a frozen object, but you cannot modify it. Reference
130 * counting still works on frozen objects. You may also copy and compare
131 * frozen objects.
132 *
347829f5
PP
133 * @author Philippe Proulx <pproulx@efficios.com>
134 * @bug No known bugs
135 */
136
137#include <stdint.h>
138#include <stdbool.h>
139#include <stddef.h>
140
141#ifdef __cplusplus
142extern "C" {
143#endif
144
145/**
146 * Object type.
147 */
148enum bt_object_type {
149 /** Unknown object, used as an error code. */
150 BT_OBJECT_TYPE_UNKNOWN = -1,
151
152 /** Null object. */
153 BT_OBJECT_TYPE_NULL = 0,
154
155 /** Boolean object (holds \c true or \c false). */
156 BT_OBJECT_TYPE_BOOL = 1,
157
158 /** Integer (holds a signed 64-bit integer value). */
159 BT_OBJECT_TYPE_INTEGER = 2,
160
161 /**
162 * Floating point number object (holds a \c double value).
163 */
164 BT_OBJECT_TYPE_FLOAT = 3,
165
166 /** String object. */
167 BT_OBJECT_TYPE_STRING = 4,
168
169 /** Array object. */
170 BT_OBJECT_TYPE_ARRAY = 5,
171
172 /** Map object. */
173 BT_OBJECT_TYPE_MAP = 6,
174};
175
4a512e75 176/**
a40a567e 177 * Status code.
4a512e75
PP
178 */
179enum bt_object_status {
a40a567e
PP
180 /** Object cannot be altered because it's frozen. */
181 BT_OBJECT_STATUS_FROZEN = -4,
182
4a512e75 183 /** Operation cancelled. */
a40a567e
PP
184 BT_OBJECT_STATUS_CANCELLED = -3,
185
186 /** Invalid arguments. */
187 /* -22 for compatibility with -EINVAL */
188 BT_OBJECT_STATUS_INVAL = -22,
4a512e75 189
a40a567e 190 /** General error. */
4a512e75
PP
191 BT_OBJECT_STATUS_ERROR = -1,
192
193 /** Okay, no error. */
194 BT_OBJECT_STATUS_OK = 0,
195};
196
347829f5
PP
197/**
198 * Object.
199 */
200struct bt_object;
201
202/**
203 * The null object singleton.
204 *
a40a567e
PP
205 * Use this everytime you need a null object. The null object singleton
206 * has no reference count; there's only one. You may directly compare
207 * any object to the null singleton to find out if it's a null object,
208 * or otherwise use bt_object_is_null().
347829f5
PP
209 *
210 * Functions of this API return this when the object is actually a
a40a567e
PP
211 * null object (of type #BT_OBJECT_TYPE_NULL), whereas \c NULL means an
212 * error of some sort.
347829f5
PP
213 */
214extern struct bt_object *bt_object_null;
215
216/**
217 * User function type for bt_object_map_foreach().
218 *
4e2f2908 219 * \p object is a \em weak reference; you must pass it to
347829f5
PP
220 * bt_object_get() to get your own reference.
221 *
222 * Return \c true to continue the loop, or \c false to break it.
223 *
224 * @param key Key of map entry
225 * @param object Object of map entry (weak reference)
226 * @param data User data
227 * @returns \c true to continue the loop
228 */
229typedef bool (* bt_object_map_foreach_cb)(const char *key,
230 struct bt_object *object, void *data);
231
232/**
233 * Puts the object \p _object (calls bt_object_put() on it), and resets
234 * the variable to \c NULL.
235 *
236 * This is something that is often done when putting and object;
237 * resetting the variable to \c NULL makes sure it cannot be put a
238 * second time later.
239 *
240 * @param _object Object to put
241 *
242 * @see BT_OBJECT_MOVE() (moves an object from one variable to the other
243 * without putting it)
244 */
245#define BT_OBJECT_PUT(_object) \
246 do { \
247 bt_object_put(_object); \
248 (_object) = NULL; \
249 } while (0)
250
251/**
252 * Moves the object referenced by the variable \p _src_object to the
253 * \p _dst_object variable, then resets \p _src_object to \c NULL.
254 *
255 * The object's reference count is <b>not changed</b>. Resetting
256 * \p _src_object to \c NULL ensures the object will not be put
4e2f2908 257 * twice later; its ownership is indeed \em moved from the source
347829f5
PP
258 * variable to the destination variable.
259 *
260 * @param _src_object Source object variable
261 * @param _dst_object Destination object variable
262 */
263#define BT_OBJECT_MOVE(_dst_object, _src_object) \
264 do { \
265 (_dst_object) = (_src_object); \
266 (_src_object) = NULL; \
267 } while (0)
268
269/**
270 * Increments the reference count of \p object.
271 *
272 * @param object Object of which to increment the reference count
273 */
274extern void bt_object_get(struct bt_object *object);
275
276/**
277 * Decrements the reference count of \p object, destroying it when this
278 * count reaches 0.
279 *
280 * @param object Object of which to decrement the reference count
281 *
282 * @see BT_OBJECT_PUT() (puts an object and resets the variable to
283 * \c NULL)
284 */
285extern void bt_object_put(struct bt_object *object);
286
a40a567e
PP
287/**
288 * Recursively freezes the object \p object.
289 *
290 * A frozen object cannot be modified; it is considered immutable.
291 * Reference counting still works on a frozen object though: you may
292 * pass a frozen object to bt_object_get() and bt_object_put().
293 *
294 * @param object Object to freeze
295 * @returns One of #bt_object_status values; if \p object
296 * is already frozen, though, #BT_OBJECT_STATUS_OK
297 * is returned anyway (i.e. this function never
298 * returns #BT_OBJECT_STATUS_FROZEN)
299 */
300extern enum bt_object_status bt_object_freeze(struct bt_object *object);
301
302/**
303 * Checks whether \p object is frozen or not.
304 *
305 * @param object Object to check
306 * @returns \c true if \p object is frozen
307 */
308extern bool bt_object_is_frozen(const struct bt_object *object);
309
347829f5
PP
310/**
311 * Returns the type of \p object.
312 *
313 * @param object Object of which to get the type
a40a567e 314 * @returns Object's type, or #BT_OBJECT_TYPE_UNKNOWN
347829f5
PP
315 * on error
316 *
a40a567e 317 * @see #bt_object_type (object types)
347829f5
PP
318 */
319extern enum bt_object_type bt_object_get_type(const struct bt_object *object);
320
321/**
322 * Checks whether \p object is a null object. The only valid null
323 * object is \ref bt_object_null.
324 *
325 * @param object Object to check
326 * @returns \c true if \p object is a null object
327 */
eadb143f
PP
328static inline
329bool bt_object_is_null(const struct bt_object *object)
347829f5
PP
330{
331 return bt_object_get_type(object) == BT_OBJECT_TYPE_NULL;
332}
333
334/**
335 * Checks whether \p object is a boolean object.
336 *
337 * @param object Object to check
338 * @returns \c true if \p object is a boolean object
339 */
eadb143f
PP
340static inline
341bool bt_object_is_bool(const struct bt_object *object)
347829f5
PP
342{
343 return bt_object_get_type(object) == BT_OBJECT_TYPE_BOOL;
344}
345
346/**
347 * Checks whether \p object is an integer object.
348 *
349 * @param object Object to check
350 * @returns \c true if \p object is an integer object
351 */
eadb143f
PP
352static inline
353bool bt_object_is_integer(const struct bt_object *object)
347829f5
PP
354{
355 return bt_object_get_type(object) == BT_OBJECT_TYPE_INTEGER;
356}
357
358/**
359 * Checks whether \p object is a floating point number object.
360 *
361 * @param object Object to check
362 * @returns \c true if \p object is a floating point number object
363 */
eadb143f
PP
364static inline
365bool bt_object_is_float(const struct bt_object *object)
347829f5
PP
366{
367 return bt_object_get_type(object) == BT_OBJECT_TYPE_FLOAT;
368}
369
370/**
371 * Checks whether \p object is a string object.
372 *
373 * @param object Object to check
374 * @returns \c true if \p object is a string object
375 */
eadb143f
PP
376static inline
377bool bt_object_is_string(const struct bt_object *object)
347829f5
PP
378{
379 return bt_object_get_type(object) == BT_OBJECT_TYPE_STRING;
380}
381
382/**
383 * Checks whether \p object is an array object.
384 *
385 * @param object Object to check
386 * @returns \c true if \p object is an array object
387 */
eadb143f
PP
388static inline
389bool bt_object_is_array(const struct bt_object *object)
347829f5
PP
390{
391 return bt_object_get_type(object) == BT_OBJECT_TYPE_ARRAY;
392}
393
394/**
395 * Checks whether \p object is a map object.
396 *
397 * @param object Object to check
398 * @returns \c true if \p object is a map object
399 */
eadb143f
PP
400static inline
401bool bt_object_is_map(const struct bt_object *object)
347829f5
PP
402{
403 return bt_object_get_type(object) == BT_OBJECT_TYPE_MAP;
404}
405
406/**
407 * Creates a boolean object. The created boolean object's initial value
408 * is \c false.
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 */
414extern struct bt_object *bt_object_bool_create(void);
415
416/**
417 * Creates a boolean object with its initial value set to \p val.
418 *
419 * The created object's reference count is set to 1.
420 *
421 * @param val Initial value
422 * @returns Created object on success, or \c NULL on error
423 */
424extern struct bt_object *bt_object_bool_create_init(bool val);
425
426/**
427 * Creates an integer object. The created integer object's initial value
428 * is 0.
429 *
430 * The created object's reference count is set to 1.
431 *
432 * @returns Created object on success, or \c NULL on error
433 */
434extern struct bt_object *bt_object_integer_create(void);
435
436/**
437 * Creates an integer object with its initial value set to \p val.
438 *
439 * The created object's reference count is set to 1.
440 *
441 * @param val Initial value
442 * @returns Created object on success, or \c NULL on error
443 */
444extern struct bt_object *bt_object_integer_create_init(int64_t val);
445
446/**
447 * Creates a floating point number object. The created floating point
448 * number object's initial value is 0.
449 *
450 * The created object's reference count is set to 1.
451 *
452 * @returns Created object on success, or \c NULL on error
453 */
454extern struct bt_object *bt_object_float_create(void);
455
456/**
457 * Creates a floating point number object with its initial value set
458 * to \p val.
459 *
460 * The created object's reference count is set to 1.
461 *
462 * @param val Initial value
463 * @returns Created object on success, or \c NULL on error
464 */
465extern struct bt_object *bt_object_float_create_init(double val);
466
467/**
468 * Creates a string object. The string object is initially empty.
469 *
470 * The created object's reference count is set to 1.
471 *
472 * @returns Created object on success, or \c NULL on error
473 */
474extern struct bt_object *bt_object_string_create(void);
475
476/**
477 * Creates a string object with its initial value set to \p val.
478 *
479 * \p val is copied.
480 *
481 * The created object's reference count is set to 1.
482 *
483 * @param val Initial value (will be copied)
484 * @returns Created object on success, or \c NULL on error
485 */
486extern struct bt_object *bt_object_string_create_init(const char *val);
487
488/**
489 * Creates an empty array object.
490 *
491 * The created object's reference count is set to 1.
492 *
493 * @returns Created object on success, or \c NULL on error
494 */
495extern struct bt_object *bt_object_array_create(void);
496
497/**
498 * Creates an empty map object.
499 *
500 * The created object's reference count is set to 1.
501 *
502 * @returns Created object on success, or \c NULL on error
503 */
504extern struct bt_object *bt_object_map_create(void);
505
506/**
a40a567e 507 * Gets the boolean value of the boolean object \p bool_obj.
347829f5
PP
508 *
509 * @param bool_obj Boolean object
510 * @param val Returned boolean value
a40a567e 511 * @returns One of #bt_object_status values
347829f5 512 */
a40a567e
PP
513extern enum bt_object_status bt_object_bool_get(
514 const struct bt_object *bool_obj, bool *val);
347829f5
PP
515
516/**
517 * Sets the boolean value of the boolean object \p bool_obj to \p val.
518 *
519 * @param bool_obj Boolean object
520 * @param val New boolean value
a40a567e 521 * @returns One of #bt_object_status values
347829f5 522 */
a40a567e
PP
523extern enum bt_object_status bt_object_bool_set(struct bt_object *bool_obj,
524 bool val);
347829f5
PP
525
526/**
a40a567e 527 * Gets the integer value of the integer object \p integer_obj.
347829f5
PP
528 *
529 * @param integer_obj Integer object
530 * @param val Returned integer value
a40a567e 531 * @returns One of #bt_object_status values
347829f5 532 */
a40a567e
PP
533extern enum bt_object_status bt_object_integer_get(
534 const struct bt_object *integer_obj, int64_t *val);
347829f5
PP
535
536/**
537 * Sets the integer value of the integer object \p integer_obj to
538 * \p val.
539 *
540 * @param integer_obj Integer object
541 * @param val New integer value
a40a567e 542 * @returns One of #bt_object_status values
347829f5 543 */
a40a567e
PP
544extern enum bt_object_status bt_object_integer_set(
545 struct bt_object *integer_obj, int64_t val);
347829f5
PP
546
547/**
548 * Gets the floating point number value of the floating point number
a40a567e 549 * object \p float_obj.
347829f5
PP
550 *
551 * @param float_obj Floating point number object
552 * @param val Returned floating point number value
a40a567e 553 * @returns One of #bt_object_status values
347829f5 554 */
a40a567e
PP
555extern enum bt_object_status bt_object_float_get(
556 const struct bt_object *float_obj, double *val);
347829f5
PP
557
558/**
559 * Sets the floating point number value of the floating point number
560 * object \p float_obj to \p val.
561 *
562 * @param float_obj Floating point number object
563 * @param val New floating point number value
a40a567e 564 * @returns One of #bt_object_status values
347829f5 565 */
a40a567e
PP
566extern enum bt_object_status bt_object_float_set(
567 struct bt_object *float_obj, double val);
347829f5
PP
568
569/**
a40a567e 570 * Gets the string value of the string object \p string_obj. The
347829f5 571 * returned string is valid as long as this object exists and is not
a40a567e
PP
572 * modified. The ownership of the returned string is \em not
573 * transferred to the caller.
347829f5
PP
574 *
575 * @param string_obj String object
a40a567e
PP
576 * @param val Returned string value
577 * @returns One of #bt_object_status values
347829f5 578 */
a40a567e
PP
579extern enum bt_object_status bt_object_string_get(
580 const struct bt_object *string_obj, const char **val);
347829f5
PP
581
582/**
583 * Sets the string value of the string object \p string_obj to
584 * \p val.
585 *
586 * \p val is copied.
587 *
588 * @param string_obj String object
589 * @param val New string value (copied)
a40a567e 590 * @returns One of #bt_object_status values
347829f5 591 */
a40a567e
PP
592extern enum bt_object_status bt_object_string_set(struct bt_object *string_obj,
593 const char *val);
347829f5
PP
594
595/**
596 * Gets the size of the array object \p array_obj, that is, the number
597 * of elements contained in \p array_obj.
598 *
599 * @param array_obj Array object
a40a567e
PP
600 * @returns Array size if the return value is 0 (empty) or a
601 * positive value, or one of
602 * #bt_object_status negative values otherwise
347829f5
PP
603 */
604extern int bt_object_array_size(const struct bt_object *array_obj);
605
606/**
607 * Returns \c true if the array object \p array_obj.
608 *
609 * @param array_obj Array object
610 * @returns \c true if \p array_obj is empty
611 */
612extern bool bt_object_array_is_empty(const struct bt_object *array_obj);
613
614/**
615 * Gets the element object of the array object \p array_obj at the
616 * index \p index.
617 *
618 * The returned object's reference count is incremented, unless it's
619 * a null object.
620 *
621 * @param array_obj Array object
622 * @param index Index of element to get
623 * @returns Element object at index \p index on success,
624 * or \c NULL on error
625 */
626extern struct bt_object *bt_object_array_get(const struct bt_object *array_obj,
627 size_t index);
628
629/**
630 * Appends the element object \p element_obj to the array object
631 * \p array_obj.
632 *
633 * The appended object's reference count is incremented, unless it's
634 * a null object.
635 *
636 * @param array_obj Array object
637 * @param element_obj Element object to append
a40a567e 638 * @returns One of #bt_object_status values
347829f5 639 */
a40a567e 640extern enum bt_object_status bt_object_array_append(struct bt_object *array_obj,
347829f5
PP
641 struct bt_object *element_obj);
642
643/**
644 * Appends the boolean value \p val to the array object \p array_obj.
645 * This is a convenience function which creates the underlying boolean
646 * object before appending it.
647 *
648 * The created boolean object's reference count is set to 1.
649 *
650 * @param array_obj Array object
651 * @param val Boolean value to append
a40a567e 652 * @returns One of #bt_object_status values
347829f5 653 */
a40a567e
PP
654extern enum bt_object_status bt_object_array_append_bool(
655 struct bt_object *array_obj, bool val);
347829f5
PP
656
657/**
658 * Appends the integer value \p val to the array object \p array_obj.
659 * This is a convenience function which creates the underlying integer
660 * object before appending it.
661 *
662 * The created integer object's reference count is set to 1.
663 *
664 * @param array_obj Array object
665 * @param val Integer value to append
a40a567e 666 * @returns One of #bt_object_status values
347829f5 667 */
a40a567e
PP
668extern enum bt_object_status bt_object_array_append_integer(
669 struct bt_object *array_obj, int64_t val);
347829f5
PP
670
671/**
672 * Appends the floating point number value \p val to the array object
673 * \p array_obj. This is a convenience function which creates the
674 * underlying floating point number object before appending it.
675 *
676 * The created floating point number object's reference count is
677 * set to 1.
678 *
679 * @param array_obj Array object
680 * @param val Floating point number value to append
a40a567e 681 * @returns One of #bt_object_status values
347829f5 682 */
a40a567e
PP
683extern enum bt_object_status bt_object_array_append_float(
684 struct bt_object *array_obj, double val);
347829f5
PP
685
686/**
687 * Appends the string value \p val to the array object \p array_obj.
688 * This is a convenience function which creates the underlying string
689 * object before appending it.
690 *
691 * \p val is copied.
692 *
693 * The created string object's reference count is set to 1.
694 *
695 * @param array_obj Array object
696 * @param val String value to append (copied)
a40a567e 697 * @returns One of #bt_object_status values
347829f5 698 */
a40a567e
PP
699extern enum bt_object_status bt_object_array_append_string(
700 struct bt_object *array_obj, const char *val);
347829f5
PP
701
702/**
703 * Appends an empty array object to the array object \p array_obj.
704 * This is a convenience function which creates the underlying array
705 * object before appending it.
706 *
707 * The created array object's reference count is set to 1.
708 *
709 * @param array_obj Array object
a40a567e 710 * @returns One of #bt_object_status values
347829f5 711 */
a40a567e
PP
712extern enum bt_object_status bt_object_array_append_array(
713 struct bt_object *array_obj);
347829f5
PP
714
715/**
716 * Appends an empty map object to the array object \p array_obj. This
717 * is a convenience function which creates the underlying map object
718 * before appending it.
719 *
720 * The created map object's reference count is set to 1.
721 *
722 * @param array_obj Array object
a40a567e 723 * @returns One of #bt_object_status values
347829f5 724 */
a40a567e
PP
725extern enum bt_object_status bt_object_array_append_map(
726 struct bt_object *array_obj);
347829f5 727
3695540c
PP
728/**
729 * Replaces the element object at index \p index of the array object
730 * \p array_obj by \p element_obj.
731 *
732 * The replaced object's reference count is decremented, unless it's
733 * a null object. The reference count of \p element_obj is incremented,
734 * unless it's a null object.
735 *
736 * @param array_obj Array object
737 * @param index Index of element object to replace
738 * @param element_obj New element object at position \p index of
739 * \p array_obj
a40a567e 740 * @returns One of #bt_object_status values
3695540c 741 */
a40a567e
PP
742extern enum bt_object_status bt_object_array_set(struct bt_object *array_obj,
743 size_t index, struct bt_object *element_obj);
3695540c 744
347829f5
PP
745/**
746 * Gets the size of a map object, that is, the number of elements
747 * contained in a map object.
748 *
749 * @param map_obj Map object
a40a567e
PP
750 * @returns Map size if the return value is 0 (empty) or a
751 * positive value, or one of
752 * #bt_object_status negative values otherwise
347829f5
PP
753 */
754extern int bt_object_map_size(const struct bt_object *map_obj);
755
756/**
757 * Returns \c true if the map object \p map_obj.
758 *
759 * @param map_obj Map object
760 * @returns \c true if \p map_obj is empty
761 */
762extern bool bt_object_map_is_empty(const struct bt_object *map_obj);
763
764/**
765 * Gets the element object associated with the key \p key within the
766 * map object \p map_obj.
767 *
768 * The returned object's reference count is incremented, unless it's
769 * a null object.
770 *
771 * @param map_obj Map object
772 * @param key Key of the element to get
773 * @returns Element object associated with the key \p key
774 * on success, or \c NULL on error
775 */
776extern struct bt_object *bt_object_map_get(const struct bt_object *map_obj,
777 const char *key);
778
779/**
780 * Calls a provided user function \p cb for each element of the map
781 * object \p map_obj.
782 *
783 * The object passed to the user function is a <b>weak reference</b>:
784 * you must call bt_object_get() on it to obtain your own reference.
785 *
786 * The key passed to the user function is only valid in the scope of
787 * this user function.
788 *
789 * The user function must return \c true to continue the loop, or
790 * \c false to break it.
791 *
792 * @param map_obj Map object
793 * @param cb User function to call back
794 * @param data User data passed to the user function
a40a567e
PP
795 * @returns One of #bt_object_status values; more
796 * specifically, #BT_OBJECT_STATUS_CANCELLED is
797 * returned if the loop was cancelled by the user
798 * function
4a512e75
PP
799 */
800extern enum bt_object_status bt_object_map_foreach(
801 const struct bt_object *map_obj, bt_object_map_foreach_cb cb,
802 void *data);
347829f5
PP
803
804/**
805 * Returns whether or not the map object \p map_obj contains the
806 * key \p key.
807 *
808 * @param map_obj Map object
809 * @param key Key to check
810 * @returns \c true if \p map_obj contains the key \p key,
811 * or \c false if it doesn't have \p key or
812 * on error
813 */
814extern bool bt_object_map_has_key(const struct bt_object *map_obj,
815 const char *key);
816
817/**
818 * Inserts the element object \p element_obj associated with the key
819 * \p key into the map object \p map_obj. If \p key exists in
820 * \p map_obj, the associated element object is first put, and then
821 * replaced by \p element_obj.
822 *
823 * \p key is copied.
824 *
825 * The inserted object's reference count is incremented, unless it's
826 * a null object.
827 *
828 * @param map_obj Map object
829 * @param key Key (copied) of object to insert
830 * @param element_obj Element object to insert, associated with the
831 * key \p key
a40a567e 832 * @returns One of #bt_object_status values
347829f5 833 */
a40a567e
PP
834extern enum bt_object_status bt_object_map_insert(
835 struct bt_object *map_obj, const char *key,
836 struct bt_object *element_obj);
347829f5
PP
837
838/**
839 * Inserts the boolean value \p val associated with the key \p key into
840 * the map object \p map_obj. This is a convenience function which
841 * creates the underlying boolean object before inserting it.
842 *
843 * \p key is copied.
844 *
845 * The created boolean object's reference count is set to 1.
846 *
847 * @param map_obj Map object
848 * @param key Key (copied) of boolean value to insert
849 * @param val Boolean value to insert, associated with the
850 * key \p key
a40a567e 851 * @returns One of #bt_object_status values
347829f5 852 */
a40a567e
PP
853extern enum bt_object_status bt_object_map_insert_bool(
854 struct bt_object *map_obj, const char *key, bool val);
347829f5
PP
855
856/**
857 * Inserts the integer value \p val associated with the key \p key into
858 * the map object \p map_obj. This is a convenience function which
859 * creates the underlying integer object before inserting it.
860 *
861 * \p key is copied.
862 *
863 * The created integer object's reference count is set to 1.
864 *
865 * @param map_obj Map object
866 * @param key Key (copied) of integer value to insert
867 * @param val Integer value to insert, associated with the
868 * key \p key
a40a567e 869 * @returns One of #bt_object_status values
347829f5 870 */
a40a567e
PP
871extern enum bt_object_status bt_object_map_insert_integer(
872 struct bt_object *map_obj, const char *key, int64_t val);
347829f5
PP
873
874/**
875 * Inserts the floating point number value \p val associated with the
876 * key \p key into the map object \p map_obj. This is a convenience
877 * function which creates the underlying floating point number object
878 * before inserting it.
879 *
880 * \p key is copied.
881 *
882 * The created floating point number object's reference count is
883 * set to 1.
884 *
885 * @param map_obj Map object
886 * @param key Key (copied) of floating point number value to
887 * insert
888 * @param val Floating point number value to insert,
889 * associated with the key \p key
a40a567e 890 * @returns One of #bt_object_status values
347829f5 891 */
a40a567e
PP
892extern enum bt_object_status bt_object_map_insert_float(
893 struct bt_object *map_obj, const char *key, double val);
347829f5
PP
894
895/**
896 * Inserts the string value \p val associated with the key \p key into
897 * the map object \p map_obj. This is a convenience function which
898 * creates the underlying string object before inserting it.
899 *
900 * \p val and \p key are copied.
901 *
902 * The created string object's reference count is set to 1.
903 *
904 * @param map_obj Map object
905 * @param key Key (copied) of string value to insert
906 * @param val String value to insert, associated with the
907 * key \p key
a40a567e 908 * @returns One of #bt_object_status values
347829f5 909 */
a40a567e
PP
910extern enum bt_object_status bt_object_map_insert_string(
911 struct bt_object *map_obj, const char *key, const char *val);
347829f5
PP
912
913/**
914 * Inserts an empty array object associated with the key \p key into
915 * the map object \p map_obj. This is a convenience function which
916 * creates the underlying array object before inserting it.
917 *
918 * \p key is copied.
919 *
920 * The created array object's reference count is set to 1.
921 *
922 * @param map_obj Map object
923 * @param key Key (copied) of empty array to insert
a40a567e 924 * @returns One of #bt_object_status values
347829f5 925 */
a40a567e
PP
926extern enum bt_object_status bt_object_map_insert_array(
927 struct bt_object *map_obj, const char *key);
347829f5
PP
928
929/**
930 * Inserts an empty map object associated with the key \p key into
931 * the map object \p map_obj. This is a convenience function which
932 * creates the underlying map object before inserting it.
933 *
934 * \p key is copied.
935 *
936 * The created map object's reference count is set to 1.
937 *
938 * @param map_obj Map object
939 * @param key Key (copied) of empty map to insert
a40a567e 940 * @returns One of #bt_object_status values
347829f5 941 */
a40a567e
PP
942extern enum bt_object_status bt_object_map_insert_map(
943 struct bt_object *map_obj, const char *key);
347829f5
PP
944
945/**
946 * Creates a deep copy of the object \p object.
947 *
948 * The created object's reference count is set to 1, unless
949 * \p object is a null object.
950 *
951 * @param object Object to copy
952 * @returns Deep copy of \p object on success, or \c NULL
953 * on error
954 */
955extern struct bt_object *bt_object_copy(const struct bt_object *object);
956
957/**
958 * Compares the objects \p object_a and \p object_b and returns \c true
959 * if they have the same content.
960 *
961 * @param object_a Object A
962 * @param object_B Object B
963 * @returns \c true if \p object_a and \p object_b have the
964 * same content, or \c false if they differ or on
965 * error
966 */
967extern bool bt_object_compare(const struct bt_object *object_a,
968 const struct bt_object *object_b);
969
970#ifdef __cplusplus
971}
972#endif
973
974#endif /* _BABELTRACE_OBJECTS_H */
This page took 0.061871 seconds and 4 git commands to generate.