source: mainline/uspace/lib/bithenge/tree.h@ f3a37e28

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f3a37e28 was 0784869, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: improve Doxygen documentation

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 * Copyright (c) 2012 Sean Bartell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup bithenge
30 * @{
31 */
32/**
33 * @file
34 * Trees and nodes.
35 */
36
37#ifndef BITHENGE_TREE_H_
38#define BITHENGE_TREE_H_
39
40#include <assert.h>
41#include <sys/types.h>
42#include "os.h"
43
44/** Indicates the type of a tree node. */
45typedef enum {
46 /** An internal node with labelled edges to other nodes. */
47 BITHENGE_NODE_INTERNAL = 1,
48 /** A leaf node holding a boolean value. */
49 BITHENGE_NODE_BOOLEAN,
50 /** A leaf node holding an integer. */
51 BITHENGE_NODE_INTEGER,
52 /** A leaf node holding a string. */
53 BITHENGE_NODE_STRING,
54 /** A leaf node holding a binary blob. */
55 BITHENGE_NODE_BLOB,
56} bithenge_node_type_t;
57
58/** A tree node. It can have any of the types in @a bithenge_node_type_t. */
59typedef struct bithenge_node_t {
60 /** @privatesection */
61 bithenge_node_type_t type;
62 unsigned int refs;
63 union {
64 /** @privatesection */
65 const struct bithenge_internal_node_ops_t *internal_ops;
66 bool boolean_value;
67 bithenge_int_t integer_value;
68 struct {
69 /** @privatesection */
70 const char *ptr;
71 bool needs_free;
72 } string_value;
73 const struct bithenge_random_access_blob_ops_t *blob_ops;
74 };
75} bithenge_node_t;
76
77/** A callback function used to iterate over a node's children. It takes
78 * ownership of a reference to both the key and the value.
79 * @memberof bithenge_node_t
80 * @param key The key.
81 * @param value The value.
82 * @param data Data provided to @a bithenge_node_t::bithenge_node_for_each.
83 * @return EOK on success or an error code from errno.h. */
84typedef int (*bithenge_for_each_func_t)(bithenge_node_t *key, bithenge_node_t *value, void *data);
85
86/** Operations providing access to an internal node. */
87typedef struct bithenge_internal_node_ops_t {
88 /** @copydoc bithenge_node_t::bithenge_node_for_each */
89 int (*for_each)(bithenge_node_t *self, bithenge_for_each_func_t func, void *data);
90 /** @copydoc bithenge_node_t::bithenge_node_get */
91 int (*get)(bithenge_node_t *self, bithenge_node_t *key,
92 bithenge_node_t **out);
93 /** Destroys the internal node.
94 * @param self The node to destroy. */
95 void (*destroy)(bithenge_node_t *self);
96} bithenge_internal_node_ops_t;
97
98/** Find the type of a node.
99 * @memberof bithenge_node_t
100 * @param node The node.
101 * @return The type of the node. */
102static inline bithenge_node_type_t bithenge_node_type(const bithenge_node_t *node)
103{
104 return node->type;
105}
106
107/** Increment a node's reference count.
108 * @memberof bithenge_node_t
109 * @param node The node to reference. */
110static inline void bithenge_node_inc_ref(bithenge_node_t *node)
111{
112 assert(node);
113 node->refs++;
114}
115
116/** @memberof bithenge_node_t */
117void bithenge_node_dec_ref(bithenge_node_t *node);
118
119/** Iterate over a node's children.
120 * @memberof bithenge_node_t
121 * @param self The internal node to iterate over.
122 * @param func The callback function.
123 * @param data Data to provide to the callback function.
124 * @return EOK on success or an error code from errno.h. */
125static inline int bithenge_node_for_each(bithenge_node_t *self,
126 bithenge_for_each_func_t func, void *data)
127{
128 assert(self->type == BITHENGE_NODE_INTERNAL);
129 return self->internal_ops->for_each(self, func, data);
130}
131
132/** @memberof bithenge_node_t */
133int bithenge_node_get(bithenge_node_t *, bithenge_node_t *,
134 bithenge_node_t **);
135
136/** Get the value of a boolean node.
137 * @memberof bithenge_node_t
138 * @param self The boolean node.
139 * @return The node's value. */
140static inline bool bithenge_boolean_node_value(bithenge_node_t *self)
141{
142 assert(self->type == BITHENGE_NODE_BOOLEAN);
143 return self->boolean_value;
144}
145
146/** Get the value of an integer node.
147 * @memberof bithenge_node_t
148 * @param self The integer node.
149 * @return The node's value. */
150static inline bithenge_int_t bithenge_integer_node_value(bithenge_node_t *self)
151{
152 assert(self->type == BITHENGE_NODE_INTEGER);
153 return self->integer_value;
154}
155
156/** Get the value of an string node.
157 * @memberof bithenge_node_t
158 * @param self The string node.
159 * @return The node's value. */
160static inline const char *bithenge_string_node_value(bithenge_node_t *self)
161{
162 assert(self->type == BITHENGE_NODE_STRING);
163 return self->string_value.ptr;
164}
165
166/** @memberof bithenge_node_t */
167int bithenge_init_internal_node(bithenge_node_t *,
168 const bithenge_internal_node_ops_t *);
169/** @memberof bithenge_node_t */
170int bithenge_new_empty_internal_node(bithenge_node_t **);
171/** @memberof bithenge_node_t */
172int bithenge_new_simple_internal_node(bithenge_node_t **, bithenge_node_t **,
173 bithenge_int_t, bool needs_free);
174/** @memberof bithenge_node_t */
175int bithenge_new_boolean_node(bithenge_node_t **, bool);
176/** @memberof bithenge_node_t */
177int bithenge_new_integer_node(bithenge_node_t **, bithenge_int_t);
178/** @memberof bithenge_node_t */
179int bithenge_new_string_node(bithenge_node_t **, const char *, bool);
180/** @memberof bithenge_node_t */
181int bithenge_node_equal(bool *, bithenge_node_t *, bithenge_node_t *);
182
183#endif
184
185/** @}
186 */
Note: See TracBrowser for help on using the repository browser.