1BSON_STEAL(3)                       libbson                      BSON_STEAL(3)
2
3
4

SYNOPSIS

6          bool
7          bson_steal (bson_t *dst, bson_t *src);
8

PARAMETERS

10dst: An uninitialized bson_t.
11
12src: A bson_t.
13

DESCRIPTION

15       Efficiently transfer the contents of src to dst and destroy src.
16
17       Before  calling  this function, src must be initialized and dst must be
18       uninitialized. After this function returns  successfully,  src  is  de‐
19       stroyed, and dst is initialized and must be freed with bson_destroy().
20
21       For  example, if you have a higher-level structure that wraps a bson_t,
22       use bson_steal to transfer BSON data into it:
23
24          typedef struct {
25             bson_t bson;
26          } bson_wrapper_t;
27
28
29          bson_wrapper_t *
30          wrap_bson (bson_t *b)
31          {
32             bson_wrapper_t *wrapper = bson_malloc (sizeof (bson_wrapper_t));
33
34             if (bson_steal (&wrapper->bson, b)) {
35                return wrapper;
36             }
37
38             bson_free (wrapper);
39             return NULL;
40          }
41
42
43          void
44          bson_wrapper_destroy (bson_wrapper_t *wrapper)
45          {
46             bson_destroy (&wrapper->bson);
47             bson_free (wrapper);
48          }
49
50
51          int
52          main (int argc, char *argv[])
53          {
54             bson_t bson = BSON_INITIALIZER;
55             bson_wrapper_t *wrapper;
56
57             BSON_APPEND_UTF8 (&bson, "key", "value");
58
59             /* now "bson" is destroyed */
60             wrapper = wrap_bson (&bson);
61
62             /* clean up */
63             bson_wrapper_destroy (wrapper);
64          }
65

RETURNS

67       Returns true if src was successfully moved to dst, false if src is  in‐
68       valid, or was statically initialized, or another error occurred.
69
70       SEE ALSO:
71          bson_destroy_with_steal(), a lower-level function that returns the raw contents of a bson_t.
72
73

AUTHOR

75       MongoDB, Inc
76
78       2017-present, MongoDB, Inc
79
80
81
82
831.25.1                           Nov 08, 2023                    BSON_STEAL(3)
Impressum