libyang  0.16.105
YANG data modeling language library
Tree_Data.hpp
Go to the documentation of this file.
1 
15 #ifndef LIBYANG_CPP_TREE_DATA_H
16 #define LIBYANG_CPP_TREE_DATA_H
17 
18 #include <iostream>
19 #include <memory>
20 #include <exception>
21 #include <vector>
22 
23 #include "Internal.hpp"
24 #include "Tree_Schema.hpp"
25 
26 extern "C" {
27 #include "libyang.h"
28 #include "tree_data.h"
29 }
30 
31 namespace libyang {
32 
44 class Value
45 {
46 public:
48  Value(lyd_val value, LY_DATA_TYPE* value_type, uint8_t value_flags, S_Deleter deleter);
49  ~Value();
51  const char *binary() {return LY_TYPE_BINARY == type ? value.binary : throw "wrong type";};
52  //struct lys_type_bit **bit();
53  //TODO, check size
54  //its size is always the number of defined bits in the schema
56  int8_t bln() {return LY_TYPE_BOOL == type ? value.bln : throw "wrong type";};
58  int64_t dec64() {return LY_TYPE_DEC64 == type ? value.dec64 : throw "wrong type";};
60  S_Type_Enum enm() {return LY_TYPE_ENUM == type ? std::make_shared<Type_Enum>(value.enm, deleter) : throw "wrong type";};
62  S_Ident ident() {return LY_TYPE_IDENT == type ? std::make_shared<Ident>(value.ident, deleter) : throw "wrong type";};
64  S_Data_Node instance();
66  int8_t int8() {return LY_TYPE_INT8 == type ? value.int8 : throw "wrong type";};
68  int16_t int16() {return LY_TYPE_INT16 == type ? value.int16 : throw "wrong type";};
70  int32_t int32() {return LY_TYPE_INT32 == type ? value.int32 : throw "wrong type";};
72  int64_t int64() {return LY_TYPE_INT64 == type ? value.int64 : throw "wrong type";};
74  S_Data_Node leafref();
76  const char *string() {return LY_TYPE_STRING == type ? value.string : throw "wrong type";};
78  uint8_t uint8() {return LY_TYPE_UINT8 == type ? value.uint8 : throw "wrong type";};
80  uint16_t uint16() {return LY_TYPE_UINT16 == type ? value.uint16 : throw "wrong type";};
82  uint32_t uintu32() {return LY_TYPE_UINT32 == type ? value.uint32 : throw "wrong type";};
84  uint64_t uint64() {return LY_TYPE_UINT64 == type ? value.uint64 : throw "wrong type";};
85 
86 private:
87  lyd_val value;
88  LY_DATA_TYPE type;
89  uint8_t flags;
90  S_Deleter deleter;
91 };
92 
97 class Data_Node
98 {
99 public:
101  Data_Node(struct lyd_node *node, S_Deleter deleter = nullptr);
103  Data_Node(S_Data_Node parent, S_Module module, const char *name);
105  Data_Node(S_Data_Node parent, S_Module module, const char *name, const char *val_str);
107  Data_Node(S_Data_Node parent, S_Module module, const char *name, const char *value, LYD_ANYDATA_VALUETYPE value_type);
109  Data_Node(S_Data_Node parent, S_Module module, const char *name, S_Data_Node value);
111  Data_Node(S_Data_Node parent, S_Module module, const char *name, S_Xml_Elem value);
113  Data_Node(S_Context context, const char *path, const char *value, LYD_ANYDATA_VALUETYPE value_type, int options);
115  Data_Node(S_Context context, const char *path, S_Data_Node value, int options);
117  Data_Node(S_Context context, const char *path, S_Xml_Elem value, int options);
118  //TODO
119  //struct lyd_node *lyd_new_output(struct lyd_node *parent, const struct lys_module *module, const char *name);
120  //struct lyd_node *lyd_new_output_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
121  // const char *val_str);
122  //struct lyd_node *lyd_new_output_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
123  // void *value, LYD_ANYDATA_VALUETYPE value_type);
124  ~Data_Node();
126  S_Schema_Node schema() LY_NEW(node, schema, Schema_Node);
128  uint8_t validity() {return node->validity;};
130  uint8_t dflt() {return node->dflt;};
132  uint8_t when_status() {return node->when_status;};
134  S_Attr attr();
136  S_Data_Node next() LY_NEW(node, next, Data_Node);
138  S_Data_Node prev() LY_NEW(node, prev, Data_Node);
140  S_Data_Node parent() LY_NEW(node, parent, Data_Node);
142  virtual S_Data_Node child() LY_NEW(node, child, Data_Node);
143 
144  /* functions */
146  std::string path();
148  S_Data_Node dup(int recursive);
150  S_Data_Node dup_withsiblings(int recursive);
152  S_Data_Node dup_to_ctx(int recursive, S_Context context);
154  int merge(S_Data_Node source, int options);
156  int merge_to_ctx(S_Data_Node source, int options, S_Context context);
158  int insert(S_Data_Node new_node);
160  int insert_sibling(S_Data_Node new_node);
162  int insert_before(S_Data_Node new_node);
164  int insert_after(S_Data_Node new_node);
166  int schema_sort(int recursive);
168  S_Set find_path(const char *expr);
170  S_Set find_instance(S_Schema_Node schema);
172  S_Data_Node first_sibling();
174  int validate(int options, S_Context var_arg);
176  int validate(int options, S_Data_Node var_arg);
178  int validate_value(const char *value);
180  S_Difflist diff(S_Data_Node second, int options);
182  S_Data_Node new_path(S_Context ctx, const char *path, const char *value, LYD_ANYDATA_VALUETYPE value_type, int options);
184  S_Data_Node new_path(S_Context ctx, const char *path, S_Data_Node value, int options);
186  S_Data_Node new_path(S_Context ctx, const char *path, S_Xml_Elem value, int options);
188  unsigned int list_pos();
190  int unlink();
192  S_Attr insert_attr(S_Module module, const char *name, const char *value);
194  S_Module node_module();
196  std::string print_mem(LYD_FORMAT format, int options);
197 
198  /* emulate TREE macro's */
200  std::vector<S_Data_Node> tree_for();
202  std::vector<S_Data_Node> tree_dfs();
203 
205  struct lyd_node *swig_node() {return node;};
207  S_Deleter swig_deleter() {return deleter;};
208 
209  friend Set;
212 
214  struct lyd_node *C_lyd_node() {return node;};
215 
216 private:
217  struct lyd_node *node;
218  S_Deleter deleter;
219 };
220 
221 S_Data_Node create_new_Data_Node(struct lyd_node *node);
222 
228 {
229 public:
231  Data_Node_Leaf_List(S_Data_Node derived);
233  Data_Node_Leaf_List(struct lyd_node *node, S_Deleter deleter = nullptr);
236  const char *value_str() {return ((struct lyd_node_leaf_list *) node)->value_str;};
238  S_Value value();
240  uint16_t value_type() {return ((struct lyd_node_leaf_list *) node)->value_type;};
242  S_Data_Node child() {return nullptr;};
243 
244  /* functions */
246  int change_leaf(const char *val_str);
248  int wd_default();
250  S_Type leaf_type();
251 
252 private:
253  struct lyd_node *node;
254  S_Deleter deleter;
255 };
256 
262 {
263 public:
265  Data_Node_Anydata(S_Data_Node derived);
267  Data_Node_Anydata(struct lyd_node *node, S_Deleter deleter = nullptr);
270  LYD_ANYDATA_VALUETYPE value_type() {return ((struct lyd_node_anydata *) node)->value_type;};
271  //union value
273  S_Data_Node child() {return nullptr;};
274 
275 private:
276  struct lyd_node *node;
277  S_Deleter deleter;
278 };
279 
284 class Attr
285 {
286 public:
288  Attr(struct lyd_attr *attr, S_Deleter deleter = nullptr);
289  ~Attr();
291  S_Data_Node parent() LY_NEW(attr, parent, Data_Node);
293  S_Attr next();
294  //struct lys_ext_instance_complex *annotation
296  const char *name() {return attr->name;};
298  const char *value_str() {return attr->value_str;};
300  S_Value value();
302  uint16_t value_type() {return attr->value_type;};
303 private:
304  struct lyd_attr *attr;
305  S_Deleter deleter;
306 };
307 
312 class Difflist
313 {
314 public:
316  Difflist(struct lyd_difflist *diff, S_Deleter deleter);
317  ~Difflist();
319  LYD_DIFFTYPE *type() {return diff->type;};
321  std::vector<S_Data_Node> first();
323  std::vector<S_Data_Node> second();
324 
325 private:
326  struct lyd_difflist *diff;
327  S_Deleter deleter;
328 };
329 
332 }
333 
334 #endif
int insert(S_Data_Node new_node)
Definition: Tree_Data.cpp:246
S_Value value()
Definition: Tree_Data.cpp:520
int insert_before(S_Data_Node new_node)
Definition: Tree_Data.cpp:266
uint32_t uint32
Definition: tree_data.h:113
struct lyd_node * swig_node()
Definition: Tree_Data.hpp:205
uint8_t validity()
Definition: Tree_Data.hpp:128
int schema_sort(int recursive)
Definition: Tree_Data.cpp:292
int change_leaf(const char *val_str)
Definition: Tree_Data.cpp:480
const char * binary()
Definition: Tree_Data.hpp:51
int merge_to_ctx(S_Data_Node source, int options, S_Context context)
Definition: Tree_Data.cpp:239
const char * value_str
Definition: tree_data.h:133
std::string path()
Definition: Tree_Data.cpp:189
int validate(int options, S_Context var_arg)
Definition: Tree_Data.cpp:322
uint16_t value_type()
Definition: Tree_Data.hpp:302
int8_t bln
Definition: tree_data.h:98
uint8_t when_status
Definition: tree_data.h:190
int64_t dec64
Definition: tree_data.h:99
int64_t int64()
Definition: Tree_Data.hpp:72
S_Data_Node create_new_Data_Node(struct lyd_node *node)
Definition: Tree_Data.cpp:560
uint16_t uint16
Definition: tree_data.h:112
S_Data_Node dup(int recursive)
Definition: Tree_Data.cpp:202
node's value representation
Definition: tree_data.h:94
S_Data_Node parent()
Definition: Tree_Data.hpp:140
uint8_t dflt
Definition: tree_data.h:189
S_Data_Node first_sibling()
Definition: Tree_Data.cpp:315
S_Data_Node parent()
Definition: Tree_Data.hpp:291
int merge(S_Data_Node source, int options)
Definition: Tree_Data.cpp:232
uint64_t uint64
Definition: tree_data.h:114
LY_DATA_TYPE _PACKED value_type
Definition: tree_data.h:135
S_Data_Node leafref()
Definition: Tree_Data.cpp:46
int8_t bln()
Definition: Tree_Data.hpp:56
Data_Node_Anydata(S_Data_Node derived)
Definition: Tree_Data.cpp:499
unsigned int list_pos()
Definition: Tree_Data.cpp:384
S_Attr next()
Definition: Tree_Data.cpp:524
Data_Node(struct lyd_node *node, S_Deleter deleter=nullptr)
Definition: Tree_Data.cpp:53
int64_t int64
Definition: tree_data.h:108
S_Deleter swig_deleter()
Definition: Tree_Data.hpp:207
class for wrapping lyd_val.
Definition: Tree_Data.hpp:44
friend Data_Node_Leaf_List
Definition: Tree_Data.hpp:211
S_Set find_path(const char *expr)
Definition: Tree_Data.cpp:299
libyang representation of data trees.
const char * string
Definition: tree_data.h:110
uint8_t uint8
Definition: tree_data.h:111
uint32_t uintu32()
Definition: Tree_Data.hpp:82
The main libyang public header.
Class implementation for libyang C header tree_schema.h.
LYD_FORMAT
Data input/output formats supported by libyang parser and printer functions.
Definition: tree_data.h:40
int8_t int8
Definition: tree_data.h:105
LYD_DIFFTYPE * type()
Definition: Tree_Data.hpp:319
std::vector< S_Data_Node > first()
Definition: Tree_Data.cpp:531
int16_t int16
Definition: tree_data.h:106
LYD_ANYDATA_VALUETYPE value_type()
Definition: Tree_Data.hpp:270
S_Attr insert_attr(S_Module module, const char *name, const char *value)
Definition: Tree_Data.cpp:404
Attr(struct lyd_attr *attr, S_Deleter deleter=nullptr)
Definition: Tree_Data.cpp:515
S_Data_Node dup_withsiblings(int recursive)
Definition: Tree_Data.cpp:213
class for wrapping lyd_node_anydata.
Definition: Tree_Data.hpp:261
uint16_t uint16()
Definition: Tree_Data.hpp:80
std::vector< S_Data_Node > second()
Definition: Tree_Data.cpp:545
S_Difflist diff(S_Data_Node second, int options)
Definition: Tree_Data.cpp:344
int64_t dec64()
Definition: Tree_Data.hpp:58
Structure for data nodes defined as LYS_LEAF or LYS_LEAFLIST.
Definition: tree_data.h:227
Attribute structure.
Definition: tree_data.h:128
S_Data_Node next()
Definition: Tree_Data.hpp:136
Difflist(struct lyd_difflist *diff, S_Deleter deleter)
Definition: Tree_Data.cpp:526
classes for wrapping lyd_node.
Definition: Tree_Data.hpp:97
S_Schema_Node schema()
Definition: Tree_Data.hpp:126
class for wrapping lyd_difflist.
Definition: Tree_Data.hpp:312
struct lys_ident * ident
Definition: tree_data.h:101
int8_t int8()
Definition: Tree_Data.hpp:66
Structure for the result of lyd_diff(), describing differences between two data trees.
Definition: tree_data.h:360
int validate_value(const char *value)
Definition: Tree_Data.cpp:337
Data_Node_Leaf_List(S_Data_Node derived)
Definition: Tree_Data.cpp:461
std::string print_mem(LYD_FORMAT format, int options)
Definition: Tree_Data.cpp:424
class for wrapping lyd_node_leaf_list.
Definition: Tree_Data.hpp:227
S_Ident ident()
Definition: Tree_Data.hpp:62
class for wrapping lyd_attr.
Definition: Tree_Data.hpp:284
int insert_sibling(S_Data_Node new_node)
Definition: Tree_Data.cpp:253
uint8_t uint8()
Definition: Tree_Data.hpp:78
LYD_DIFFTYPE
list of possible types of differences in lyd_difflist
Definition: tree_data.h:319
S_Data_Node prev()
Definition: Tree_Data.hpp:138
LY_DATA_TYPE
YANG built-in types.
Definition: tree_schema.h:792
struct lyd_node * C_lyd_node()
Definition: Tree_Data.hpp:214
friend Data_Node_Anydata
Definition: Tree_Data.hpp:210
std::vector< S_Data_Node > tree_dfs()
Definition: Tree_Data.cpp:449
int32_t int32
Definition: tree_data.h:107
S_Type_Enum enm()
Definition: Tree_Data.hpp:60
Value(lyd_val value, LY_DATA_TYPE *value_type, uint8_t value_flags, S_Deleter deleter)
Definition: Tree_Data.cpp:33
virtual S_Data_Node child()
Definition: Tree_Data.hpp:142
S_Data_Node new_path(S_Context ctx, const char *path, const char *value, LYD_ANYDATA_VALUETYPE value_type, int options)
Definition: Tree_Data.cpp:354
int16_t int16()
Definition: Tree_Data.hpp:68
Generic structure for a data node, directly applicable to the data nodes defined as LYS_CONTAINER,...
Definition: tree_data.h:186
std::vector< S_Data_Node > tree_for()
Definition: Tree_Data.cpp:439
int32_t int32()
Definition: Tree_Data.hpp:70
const char * binary
Definition: tree_data.h:95
LYD_ANYDATA_VALUETYPE
List of possible value types stored in lyd_node_anydata.
Definition: tree_data.h:50
S_Data_Node instance()
Definition: Tree_Data.cpp:40
const char * name()
Definition: Tree_Data.hpp:296
S_Module node_module()
Definition: Tree_Data.cpp:414
uint64_t uint64()
Definition: Tree_Data.hpp:84
const char * value_str()
Definition: Tree_Data.hpp:298
int insert_after(S_Data_Node new_node)
Definition: Tree_Data.cpp:279
Structure for data nodes defined as LYS_ANYDATA or LYS_ANYXML.
Definition: tree_data.h:279
const char * string()
Definition: Tree_Data.hpp:76
LYD_DIFFTYPE * type
Definition: tree_data.h:361
S_Data_Node dup_to_ctx(int recursive, S_Context context)
Definition: Tree_Data.cpp:224
uint8_t when_status()
Definition: Tree_Data.hpp:132
S_Set find_instance(S_Schema_Node schema)
Definition: Tree_Data.cpp:307
struct lys_type_enum * enm
Definition: tree_data.h:100