blob: d1313b7f842da041cf76ca5d1af8caac60797f07 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang geenric macros and functions to work with YANG schema or data trees.
5 *
6 * Copyright (c) 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#ifndef LY_TREE_H_
16#define LY_TREE_H_
17
Radek Krejci7eb54ba2020-05-18 16:30:04 +020018#include "inttypes.h"
19
Michal Vasko9f96a052020-03-10 09:41:45 +010020#include "tree_data.h"
21
Radek Krejcie7b95092019-05-15 11:03:07 +020022#ifdef __cplusplus
23extern "C" {
24#endif
25
26/**
Radek Krejcic4fa0292020-05-14 10:54:49 +020027 * @brief Type (i.e. size) of the [sized array](@ref sizedarrays)'s size counter.
Radek Krejci7eb54ba2020-05-18 16:30:04 +020028 *
29 * To print the value via a print format, use LY_PRI_ARRAY_SIZE_TYPE specifier.
Radek Krejcic4fa0292020-05-14 10:54:49 +020030 */
31#define LY_ARRAY_SIZE_TYPE uint64_t
32
33/**
Radek Krejci7eb54ba2020-05-18 16:30:04 +020034 * @brief Printing format specifier macro for LY_ARRAY_SIZE_TYPE values.
35 */
36#define LY_PRI_ARRAY_SIZE_TYPE PRIu64
37
38/**
Radek Krejcie7b95092019-05-15 11:03:07 +020039 * @brief Macro selector for other LY_ARRAY_* macros, do not use directly!
40 */
41#define LY_ARRAY_SELECT(_1, _2, NAME, ...) NAME
42
43/**
44 * @brief Helper macro to go through sized-arrays with a pointer iterator.
45 *
46 * Use with opening curly bracket (`{`).
47 *
48 * @param[in] ARRAY Array to go through
49 * @param[in] TYPE Type of the records in the ARRAY
50 * @param[out] ITER Iterating pointer to the item being processed in each loop
51 */
52#define LY_ARRAY_FOR_ITER(ARRAY, TYPE, ITER) \
53 for (ITER = ARRAY; \
Radek Krejcic4fa0292020-05-14 10:54:49 +020054 (ARRAY) && ((void*)ITER - (void*)ARRAY)/(sizeof(TYPE)) < (*((LY_ARRAY_SIZE_TYPE*)(ARRAY) - 1)); \
Radek Krejcie7b95092019-05-15 11:03:07 +020055 ITER = (void*)((TYPE*)ITER + 1))
56
57/**
58 * @brief Helper macro to go through sized-arrays with a numeric iterator.
59 *
60 * Use with opening curly bracket (`{`).
61 *
62 * To access an item with the INDEX value, use always LY_ARRAY_INDEX macro!
63 *
64 * @param[in] ARRAY Array to go through
65 * @param[out] INDEX Iterating index of the item being processed in each loop
66 */
67#define LY_ARRAY_FOR_INDEX(ARRAY, INDEX) \
68 for (INDEX = 0; \
Radek Krejcic4fa0292020-05-14 10:54:49 +020069 ARRAY && INDEX < (*((LY_ARRAY_SIZE_TYPE*)(ARRAY) - 1)); \
Radek Krejcie7b95092019-05-15 11:03:07 +020070 ++INDEX)
71
72/**
73 * @defgroup schematree Schema Tree
74 * @{
75 *
76 * Data structures and functions to manipulate and access schema tree.
77 */
78
79/**
80 * @brief Get a number of records in the ARRAY.
81 *
82 * Does not check if array exists!
83 */
Radek Krejcic4fa0292020-05-14 10:54:49 +020084#define LY_ARRAY_SIZE(ARRAY) (*((LY_ARRAY_SIZE_TYPE*)(ARRAY) - 1))
Radek Krejcie7b95092019-05-15 11:03:07 +020085
86/**
87 * @brief Sized-array iterator (for-loop).
88 *
89 * Use with opening curly bracket (`{`).
90 *
91 * There are 2 variants:
92 *
93 * LY_ARRAY_FOR(ARRAY, TYPE, ITER)
94 *
95 * Where ARRAY is a sized-array to go through, TYPE is the type of the items in the ARRAY and ITER is a pointer variable
96 * providing the items of the ARRAY in the loops. This functionality is provided by LY_ARRAY_FOR_ITER macro
97 *
98 * LY_ARRAY_FOR(ARRAY, INDEX)
99 *
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200100 * The ARRAY is again a sized-array to go through, the INDEX is a variable (LY_ARRAY_SIZE_TYPE) for storing iterating ARRAY's index
Radek Krejcie7b95092019-05-15 11:03:07 +0200101 * to access the items of ARRAY in the loops. This functionality is provided by LY_ARRAY_FOR_INDEX macro.
102 */
103#define LY_ARRAY_FOR(ARRAY, ...) LY_ARRAY_SELECT(__VA_ARGS__, LY_ARRAY_FOR_ITER, LY_ARRAY_FOR_INDEX)(ARRAY, __VA_ARGS__)
104
105/**
106 * @brief Macro to iterate via all sibling elements without affecting the list itself
107 *
108 * Works for all types of nodes despite it is data or schema tree, but all the
109 * parameters must be pointers to the same type.
110 *
111 * Use with opening curly bracket (`{`). All parameters must be of the same type.
112 *
113 * @param START Pointer to the starting element.
114 * @param ELEM Iterator.
115 */
116#define LY_LIST_FOR(START, ELEM) \
117 for ((ELEM) = (START); \
118 (ELEM); \
119 (ELEM) = (ELEM)->next)
120
121/**
122 * @brief Macro to iterate via all sibling elements allowing to modify the list itself (e.g. removing elements)
123 *
124 * Use with opening curly bracket (`{`). All parameters must be of the same type.
125 *
126 * @param START Pointer to the starting element.
127 * @param NEXT Temporary storage to allow removing of the current iterator content.
128 * @param ELEM Iterator.
129 */
130#define LY_LIST_FOR_SAFE(START, NEXT, ELEM) \
131 for ((ELEM) = (START); \
132 (ELEM) ? (NEXT = (ELEM)->next, 1) : 0; \
133 (ELEM) = (NEXT))
134
135/**
136 * @brief YANG built-in types
137 */
138typedef enum
139{
140 LY_TYPE_UNKNOWN = 0, /**< Unknown type */
141 LY_TYPE_BINARY, /**< Any binary data ([RFC 6020 sec 9.8](http://tools.ietf.org/html/rfc6020#section-9.8)) */
142 LY_TYPE_UINT8, /**< 8-bit unsigned integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
143 LY_TYPE_UINT16, /**< 16-bit unsigned integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
144 LY_TYPE_UINT32, /**< 32-bit unsigned integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
145 LY_TYPE_UINT64, /**< 64-bit unsigned integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
146 LY_TYPE_STRING, /**< Human-readable string ([RFC 6020 sec 9.4](http://tools.ietf.org/html/rfc6020#section-9.4)) */
147 LY_TYPE_BITS, /**< A set of bits or flags ([RFC 6020 sec 9.7](http://tools.ietf.org/html/rfc6020#section-9.7)) */
148 LY_TYPE_BOOL, /**< "true" or "false" ([RFC 6020 sec 9.5](http://tools.ietf.org/html/rfc6020#section-9.5)) */
149 LY_TYPE_DEC64, /**< 64-bit signed decimal number ([RFC 6020 sec 9.3](http://tools.ietf.org/html/rfc6020#section-9.3))*/
150 LY_TYPE_EMPTY, /**< A leaf that does not have any value ([RFC 6020 sec 9.11](http://tools.ietf.org/html/rfc6020#section-9.11)) */
151 LY_TYPE_ENUM, /**< Enumerated strings ([RFC 6020 sec 9.6](http://tools.ietf.org/html/rfc6020#section-9.6)) */
152 LY_TYPE_IDENT, /**< A reference to an abstract identity ([RFC 6020 sec 9.10](http://tools.ietf.org/html/rfc6020#section-9.10)) */
153 LY_TYPE_INST, /**< References a data tree node ([RFC 6020 sec 9.13](http://tools.ietf.org/html/rfc6020#section-9.13)) */
154 LY_TYPE_LEAFREF, /**< A reference to a leaf instance ([RFC 6020 sec 9.9](http://tools.ietf.org/html/rfc6020#section-9.9))*/
155 LY_TYPE_UNION, /**< Choice of member types ([RFC 6020 sec 9.12](http://tools.ietf.org/html/rfc6020#section-9.12)) */
156 LY_TYPE_INT8, /**< 8-bit signed integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
157 LY_TYPE_INT16, /**< 16-bit signed integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
158 LY_TYPE_INT32, /**< 32-bit signed integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
159 LY_TYPE_INT64, /**< 64-bit signed integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
160} LY_DATA_TYPE;
161#define LY_DATA_TYPE_COUNT 20 /**< Number of different types */
162
163/**
164 * @brief Stringified YANG built-in data types
165 */
166extern const char* ly_data_type2str[LY_DATA_TYPE_COUNT];
167
Radek Krejcie7b95092019-05-15 11:03:07 +0200168/** @} */
169
170#ifdef __cplusplus
171}
172#endif
173
174#endif /* LY_TREE_H_ */