libyang  1.0.253
YANG data modeling language library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
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 
40 struct Decimal64
41 {
42  int64_t value;
43  uint8_t digits;
44 };
45 
50 class Value
51 {
52 public:
54  Value(lyd_val value, LY_DATA_TYPE* value_type, uint8_t value_flags, struct lys_type *type, S_Deleter deleter);
55  ~Value();
57  const char *binary() {return LY_TYPE_BINARY == value_type ? value.binary : throw "wrong type";};
59  std::vector<S_Type_Bit> bit();
61  bool bln() {return LY_TYPE_BOOL == value_type ? value.bln : throw "wrong type";};
63  Decimal64 dec64() {return LY_TYPE_DEC64 == value_type ? Decimal64{ value.dec64, type->info.dec64.dig } : throw "wrong type";};
65  S_Type_Enum enm() {return LY_TYPE_ENUM == value_type ? std::make_shared<Type_Enum>(value.enm, deleter) : throw "wrong type";};
67  S_Ident ident() {return LY_TYPE_IDENT == value_type ? std::make_shared<Ident>(value.ident, deleter) : throw "wrong type";};
69  S_Data_Node instance();
71  int8_t int8() {return LY_TYPE_INT8 == value_type ? value.int8 : throw "wrong type";};
73  int16_t int16() {return LY_TYPE_INT16 == value_type ? value.int16 : throw "wrong type";};
75  int32_t int32() {return LY_TYPE_INT32 == value_type ? value.int32 : throw "wrong type";};
77  int64_t int64() {return LY_TYPE_INT64 == value_type ? value.int64 : throw "wrong type";};
79  S_Data_Node leafref();
81  const char *string() {return LY_TYPE_STRING == value_type ? value.string : throw "wrong type";};
83  uint8_t uint8() {return LY_TYPE_UINT8 == value_type ? value.uint8 : throw "wrong type";};
85  uint16_t uint16() {return LY_TYPE_UINT16 == value_type ? value.uint16 : throw "wrong type";};
87  uint32_t uint32() {return LY_TYPE_UINT32 == value_type ? value.uint32 : throw "wrong type";};
89  uint64_t uint64() {return LY_TYPE_UINT64 == value_type ? value.uint64 : throw "wrong type";};
90 
91 private:
92  lyd_val value;
93  LY_DATA_TYPE value_type;
94  uint8_t value_flags;
95  struct lys_type *type;
96  S_Deleter deleter;
97 };
98 
104 {
105 public:
107  Data_Node(struct lyd_node *node, S_Deleter deleter = nullptr);
109  Data_Node(S_Data_Node parent, S_Module module, const char *name);
111  Data_Node(S_Data_Node parent, S_Module module, const char *name, const char *val_str);
113  Data_Node(S_Data_Node parent, S_Module module, const char *name, const char *value, LYD_ANYDATA_VALUETYPE value_type);
115  Data_Node(S_Data_Node parent, S_Module module, const char *name, S_Data_Node value);
117  Data_Node(S_Data_Node parent, S_Module module, const char *name, S_Xml_Elem value);
119  Data_Node(S_Context context, const char *path, const char *value, LYD_ANYDATA_VALUETYPE value_type, int options);
121  Data_Node(S_Context context, const char *path, S_Data_Node value, int options);
123  Data_Node(S_Context context, const char *path, S_Xml_Elem value, int options);
124  //TODO
125  //struct lyd_node *lyd_new_output(struct lyd_node *parent, const struct lys_module *module, const char *name);
126  //struct lyd_node *lyd_new_output_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
127  // const char *val_str);
128  //struct lyd_node *lyd_new_output_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
129  // void *value, LYD_ANYDATA_VALUETYPE value_type);
130  virtual ~Data_Node();
132  S_Schema_Node schema() LY_NEW(node, schema, Schema_Node);
134  uint8_t validity() {return node->validity;};
136  uint8_t dflt() {return node->dflt;};
138  uint8_t when_status() {return node->when_status;};
140  S_Attr attr();
142  S_Data_Node next() LY_NEW(node, next, Data_Node);
144  S_Data_Node prev() LY_NEW(node, prev, Data_Node);
146  S_Data_Node parent() LY_NEW(node, parent, Data_Node);
148  virtual S_Data_Node child() LY_NEW(node, child, Data_Node);
149 
150  /* functions */
152  std::string path();
154  S_Data_Node dup(int recursive);
156  S_Data_Node dup_withsiblings(int recursive);
158  S_Data_Node dup_to_ctx(int recursive, S_Context context);
160  int merge(S_Data_Node source, int options);
162  int merge_to_ctx(S_Data_Node source, int options, S_Context context);
164  int insert(S_Data_Node new_node);
166  int insert_sibling(S_Data_Node new_node);
168  int insert_before(S_Data_Node new_node);
170  int insert_after(S_Data_Node new_node);
172  int schema_sort(int recursive);
174  S_Set find_path(const char *expr);
176  S_Set find_instance(S_Schema_Node schema);
178  S_Data_Node first_sibling();
180  int validate(int options, S_Context var_arg);
182  int validate(int options, S_Data_Node var_arg);
184  int validate_value(const char *value);
186  S_Difflist diff(S_Data_Node second, int options);
188  S_Data_Node new_path(S_Context ctx, const char *path, const char *value, LYD_ANYDATA_VALUETYPE value_type, int options);
190  S_Data_Node new_path(S_Context ctx, const char *path, S_Data_Node value, int options);
192  S_Data_Node new_path(S_Context ctx, const char *path, S_Xml_Elem value, int options);
194  unsigned int list_pos();
196  int unlink();
198  S_Attr insert_attr(S_Module module, const char *name, const char *value);
200  S_Module node_module();
202  std::string print_mem(LYD_FORMAT format, int options);
203 
204  /* emulate TREE macro's */
206  std::vector<S_Data_Node> tree_for();
208  std::vector<S_Data_Node> tree_dfs();
209 
211  struct lyd_node *swig_node() {return node;};
213  S_Deleter swig_deleter() {return deleter;};
214 
215  friend Set;
218 
220  struct lyd_node *C_lyd_node() {return node;};
221 
222 private:
223  struct lyd_node *node;
224  S_Deleter deleter;
225 };
226 
227 S_Data_Node create_new_Data_Node(struct lyd_node *node);
228 
234 {
235 public:
237  Data_Node_Leaf_List(S_Data_Node derived);
239  Data_Node_Leaf_List(struct lyd_node *node, S_Deleter deleter = nullptr);
242  const char *value_str() {return ((struct lyd_node_leaf_list *) node)->value_str;};
244  S_Value value();
246  LY_DATA_TYPE value_type() {return ((struct lyd_node_leaf_list *) node)->value_type;};
248  S_Data_Node child() {return nullptr;};
249 
250  /* functions */
252  int change_leaf(const char *val_str);
254  int wd_default();
256  S_Type leaf_type();
257 
258 private:
259  struct lyd_node *node;
260  S_Deleter deleter;
261 };
262 
268 {
269 public:
271  Data_Node_Anydata(S_Data_Node derived);
273  Data_Node_Anydata(struct lyd_node *node, S_Deleter deleter = nullptr);
276  LYD_ANYDATA_VALUETYPE value_type() {return ((struct lyd_node_anydata *) node)->value_type;};
278  lyd_anydata_value value() {return ((struct lyd_node_anydata *) node)->value;};
280  S_Data_Node child() {return nullptr;};
281 
282 private:
283  struct lyd_node *node;
284  S_Deleter deleter;
285 };
286 
291 class Attr
292 {
293 public:
295  Attr(struct lyd_attr *attr, S_Deleter deleter = nullptr);
296  ~Attr();
298  S_Data_Node parent() LY_NEW(attr, parent, Data_Node);
300  S_Attr next();
301  //struct lys_ext_instance_complex *annotation
303  const char *name() {return attr->name;};
305  const char *value_str() {return attr->value_str;};
307  S_Value value();
309  LY_DATA_TYPE value_type() {return attr->value_type;};
310 private:
311  struct lyd_attr *attr;
312  S_Deleter deleter;
313 };
314 
319 class Difflist
320 {
321 public:
323  Difflist(struct lyd_difflist *diff, S_Deleter deleter);
324  ~Difflist();
326  LYD_DIFFTYPE *type() {return diff->type;};
328  std::vector<S_Data_Node> first();
330  std::vector<S_Data_Node> second();
331 
332 private:
333  struct lyd_difflist *diff;
334  S_Deleter deleter;
335 };
336 
339 }
340 
341 #endif
int insert(S_Data_Node new_node)
Definition: Tree_Data.cpp:284
S_Value value()
Definition: Tree_Data.cpp:607
int insert_before(S_Data_Node new_node)
Definition: Tree_Data.cpp:316
uint32_t uint32
Definition: tree_data.h:113
struct lyd_node * swig_node()
Definition: Tree_Data.hpp:211
uint8_t validity()
Definition: Tree_Data.hpp:134
int schema_sort(int recursive)
Definition: Tree_Data.cpp:354
int change_leaf(const char *val_str)
Definition: Tree_Data.cpp:567
const char * binary()
Definition: Tree_Data.hpp:57
int merge_to_ctx(S_Data_Node source, int options, S_Context context)
Definition: Tree_Data.cpp:271
const char * value_str
Definition: tree_data.h:133
std::string path()
Definition: Tree_Data.cpp:211
int validate(int options, S_Context var_arg)
Definition: Tree_Data.cpp:389
int8_t bln
Definition: tree_data.h:98
uint8_t when_status
Definition: tree_data.h:187
lyd_anydata_value value()
Definition: Tree_Data.hpp:278
uint32_t uint32()
Definition: Tree_Data.hpp:87
int64_t dec64
Definition: tree_data.h:99
LY_DATA_TYPE value_type()
Definition: Tree_Data.hpp:309
int64_t int64()
Definition: Tree_Data.hpp:77
S_Data_Node create_new_Data_Node(struct lyd_node *node)
Definition: Tree_Data.cpp:648
uint16_t uint16
Definition: tree_data.h:112
S_Data_Node dup(int recursive)
Definition: Tree_Data.cpp:224
node&#39;s value representation
Definition: tree_data.h:94
S_Data_Node parent()
Definition: Tree_Data.hpp:146
uint8_t dflt
Definition: tree_data.h:186
S_Data_Node first_sibling()
Definition: Tree_Data.cpp:382
S_Data_Node parent()
Definition: Tree_Data.hpp:298
int merge(S_Data_Node source, int options)
Definition: Tree_Data.cpp:258
uint64_t uint64
Definition: tree_data.h:114
Value(lyd_val value, LY_DATA_TYPE *value_type, uint8_t value_flags, struct lys_type *type, S_Deleter deleter)
Definition: Tree_Data.cpp:33
virtual ~Data_Node()
Definition: Tree_Data.cpp:209
LY_DATA_TYPE _PACKED value_type
Definition: tree_data.h:135
S_Data_Node leafref()
Definition: Tree_Data.cpp:66
Data_Node_Anydata(S_Data_Node derived)
Definition: Tree_Data.cpp:586
unsigned int list_pos()
Definition: Tree_Data.cpp:470
S_Attr next()
Definition: Tree_Data.cpp:611
Data_Node(struct lyd_node *node, S_Deleter deleter=nullptr)
Definition: Tree_Data.cpp:73
int64_t int64
Definition: tree_data.h:108
S_Deleter swig_deleter()
Definition: Tree_Data.hpp:213
union lys_type_info info
Definition: tree_schema.h:989
class for wrapping lyd_val.
Definition: Tree_Data.hpp:50
friend Data_Node_Leaf_List
Definition: Tree_Data.hpp:217
S_Set find_path(const char *expr)
Definition: Tree_Data.cpp:361
libyang representation of data trees.
const char * string
Definition: tree_data.h:110
uint8_t uint8
Definition: tree_data.h:111
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:326
std::vector< S_Data_Node > first()
Definition: Tree_Data.cpp:619
int16_t int16
Definition: tree_data.h:106
LYD_ANYDATA_VALUETYPE value_type()
Definition: Tree_Data.hpp:276
S_Attr insert_attr(S_Module module, const char *name, const char *value)
Definition: Tree_Data.cpp:490
Attr(struct lyd_attr *attr, S_Deleter deleter=nullptr)
Definition: Tree_Data.cpp:602
S_Data_Node dup_withsiblings(int recursive)
Definition: Tree_Data.cpp:235
class for wrapping lyd_node_anydata.
Definition: Tree_Data.hpp:267
uint16_t uint16()
Definition: Tree_Data.hpp:85
std::vector< S_Data_Node > second()
Definition: Tree_Data.cpp:633
S_Difflist diff(S_Data_Node second, int options)
Definition: Tree_Data.cpp:417
Structure for data nodes defined as LYS_LEAF or LYS_LEAFLIST.
Definition: tree_data.h:224
Attribute structure.
Definition: tree_data.h:128
S_Data_Node next()
Definition: Tree_Data.hpp:142
Anydata value union.
Definition: tree_data.h:271
Difflist(struct lyd_difflist *diff, S_Deleter deleter)
Definition: Tree_Data.cpp:613
classes for wrapping lyd_node.
Definition: Tree_Data.hpp:103
S_Schema_Node schema()
Definition: Tree_Data.hpp:132
class for wrapping lyd_difflist.
Definition: Tree_Data.hpp:319
struct lys_ident * ident
Definition: tree_data.h:101
int8_t int8()
Definition: Tree_Data.hpp:71
Structure for the result of lyd_diff(), describing differences between two data trees.
Definition: tree_data.h:364
int validate_value(const char *value)
Definition: Tree_Data.cpp:410
Data_Node_Leaf_List(S_Data_Node derived)
Definition: Tree_Data.cpp:547
std::string print_mem(LYD_FORMAT format, int options)
Definition: Tree_Data.cpp:510
class for wrapping lyd_node_leaf_list.
Definition: Tree_Data.hpp:233
S_Ident ident()
Definition: Tree_Data.hpp:67
class for wrapping lyd_attr.
Definition: Tree_Data.hpp:291
int insert_sibling(S_Data_Node new_node)
Definition: Tree_Data.cpp:297
uint8_t uint8()
Definition: Tree_Data.hpp:83
LYD_DIFFTYPE
list of possible types of differences in lyd_difflist
Definition: tree_data.h:321
S_Data_Node prev()
Definition: Tree_Data.hpp:144
LY_DATA_TYPE
YANG built-in types.
Definition: tree_schema.h:791
struct lyd_node * C_lyd_node()
Definition: Tree_Data.hpp:220
struct lys_type_info_dec64 dec64
Definition: tree_schema.h:967
friend Data_Node_Anydata
Definition: Tree_Data.hpp:216
std::vector< S_Data_Node > tree_dfs()
Definition: Tree_Data.cpp:535
YANG type structure providing information from the schema.
Definition: tree_schema.h:980
int32_t int32
Definition: tree_data.h:107
S_Type_Enum enm()
Definition: Tree_Data.hpp:65
virtual S_Data_Node child()
Definition: Tree_Data.hpp:148
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:431
int16_t int16()
Definition: Tree_Data.hpp:73
Generic structure for a data node, directly applicable to the data nodes defined as LYS_CONTAINER...
Definition: tree_data.h:183
std::vector< S_Data_Node > tree_for()
Definition: Tree_Data.cpp:525
int32_t int32()
Definition: Tree_Data.hpp:75
std::vector< S_Type_Bit > bit()
Definition: Tree_Data.cpp:41
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:60
const char * name()
Definition: Tree_Data.hpp:303
S_Module node_module()
Definition: Tree_Data.cpp:500
uint64_t uint64()
Definition: Tree_Data.hpp:89
const char * value_str()
Definition: Tree_Data.hpp:305
int insert_after(S_Data_Node new_node)
Definition: Tree_Data.cpp:335
Structure for data nodes defined as LYS_ANYDATA or LYS_ANYXML.
Definition: tree_data.h:287
const char * string()
Definition: Tree_Data.hpp:81
Decimal64 dec64()
Definition: Tree_Data.hpp:63
LYD_DIFFTYPE * type
Definition: tree_data.h:365
S_Data_Node dup_to_ctx(int recursive, S_Context context)
Definition: Tree_Data.cpp:246
uint8_t when_status()
Definition: Tree_Data.hpp:138
S_Set find_instance(S_Schema_Node schema)
Definition: Tree_Data.cpp:369
struct lys_type_enum * enm
Definition: tree_data.h:100