libyang  1.0.130
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, struct lys_type *type, S_Deleter deleter);
49  ~Value();
51  const char *binary() {return LY_TYPE_BINARY == value_type ? value.binary : throw "wrong type";};
53  std::vector<S_Type_Bit> bit();
55  int8_t bln() {return LY_TYPE_BOOL == value_type ? value.bln : throw "wrong type";};
57  int64_t dec64() {return LY_TYPE_DEC64 == value_type ? value.dec64 : throw "wrong type";};
59  S_Type_Enum enm() {return LY_TYPE_ENUM == value_type ? std::make_shared<Type_Enum>(value.enm, deleter) : throw "wrong type";};
61  S_Ident ident() {return LY_TYPE_IDENT == value_type ? std::make_shared<Ident>(value.ident, deleter) : throw "wrong type";};
63  S_Data_Node instance();
65  int8_t int8() {return LY_TYPE_INT8 == value_type ? value.int8 : throw "wrong type";};
67  int16_t int16() {return LY_TYPE_INT16 == value_type ? value.int16 : throw "wrong type";};
69  int32_t int32() {return LY_TYPE_INT32 == value_type ? value.int32 : throw "wrong type";};
71  int64_t int64() {return LY_TYPE_INT64 == value_type ? value.int64 : throw "wrong type";};
73  S_Data_Node leafref();
75  const char *string() {return LY_TYPE_STRING == value_type ? value.string : throw "wrong type";};
77  uint8_t uint8() {return LY_TYPE_UINT8 == value_type ? value.uint8 : throw "wrong type";};
79  uint16_t uint16() {return LY_TYPE_UINT16 == value_type ? value.uint16 : throw "wrong type";};
81  uint32_t uint32() {return LY_TYPE_UINT32 == value_type ? value.uint32 : throw "wrong type";};
83  uint64_t uint64() {return LY_TYPE_UINT64 == value_type ? value.uint64 : throw "wrong type";};
84 
85 private:
86  lyd_val value;
87  LY_DATA_TYPE value_type;
88  uint8_t value_flags;
89  struct lys_type *type;
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  virtual ~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;};
272  lyd_anydata_value value() {return ((struct lyd_node_anydata *) node)->value;};
274  S_Data_Node child() {return nullptr;};
275 
276 private:
277  struct lyd_node *node;
278  S_Deleter deleter;
279 };
280 
285 class Attr
286 {
287 public:
289  Attr(struct lyd_attr *attr, S_Deleter deleter = nullptr);
290  ~Attr();
292  S_Data_Node parent() LY_NEW(attr, parent, Data_Node);
294  S_Attr next();
295  //struct lys_ext_instance_complex *annotation
297  const char *name() {return attr->name;};
299  const char *value_str() {return attr->value_str;};
301  S_Value value();
303  uint16_t value_type() {return attr->value_type;};
304 private:
305  struct lyd_attr *attr;
306  S_Deleter deleter;
307 };
308 
313 class Difflist
314 {
315 public:
317  Difflist(struct lyd_difflist *diff, S_Deleter deleter);
318  ~Difflist();
320  LYD_DIFFTYPE *type() {return diff->type;};
322  std::vector<S_Data_Node> first();
324  std::vector<S_Data_Node> second();
325 
326 private:
327  struct lyd_difflist *diff;
328  S_Deleter deleter;
329 };
330 
333 }
334 
335 #endif
libyang::Data_Node::first_sibling
S_Data_Node first_sibling()
Definition: Tree_Data.cpp:377
libyang::Data_Node_Anydata::value
lyd_anydata_value value()
Definition: Tree_Data.hpp:272
LY_TYPE_BINARY
@ LY_TYPE_BINARY
Definition: tree_schema.h:794
libyang::Data_Node::schema_sort
int schema_sort(int recursive)
Definition: Tree_Data.cpp:349
libyang::Value::Value
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
libyang::Value
class for wrapping lyd_val.
Definition: Tree_Data.hpp:44
libyang::Attr
class for wrapping lyd_attr.
Definition: Tree_Data.hpp:285
libyang::Data_Node::Set
friend Set
Definition: Tree_Data.hpp:207
libyang::Data_Node::insert
int insert(S_Data_Node new_node)
Definition: Tree_Data.cpp:279
libyang::Data_Node::C_lyd_node
struct lyd_node * C_lyd_node()
Definition: Tree_Data.hpp:214
libyang::Data_Node::Data_Node_Anydata
friend Data_Node_Anydata
Definition: Tree_Data.hpp:210
libyang::Data_Node::~Data_Node
virtual ~Data_Node()
Definition: Tree_Data.cpp:204
libyang::Data_Node_Leaf_List
class for wrapping lyd_node_leaf_list.
Definition: Tree_Data.hpp:227
lys_type
YANG type structure providing information from the schema.
Definition: tree_schema.h:981
libyang::Value::dec64
int64_t dec64()
Definition: Tree_Data.hpp:57
libyang::Attr::~Attr
~Attr()
Definition: Tree_Data.cpp:600
libyang::Data_Node::list_pos
unsigned int list_pos()
Definition: Tree_Data.cpp:464
lyd_value_u::int32
int32_t int32
Definition: tree_data.h:107
libyang::Attr::value_str
const char * value_str()
Definition: Tree_Data.hpp:299
libyang::Difflist::second
std::vector< S_Data_Node > second()
Definition: Tree_Data.cpp:627
lyd_node_leaf_list
Structure for data nodes defined as LYS_LEAF or LYS_LEAFLIST.
Definition: tree_data.h:217
libyang::Difflist
class for wrapping lyd_difflist.
Definition: Tree_Data.hpp:313
lyd_value_u::int8
int8_t int8
Definition: tree_data.h:105
libyang::Value::~Value
~Value()
Definition: Tree_Data.cpp:40
libyang::Data_Node::merge
int merge(S_Data_Node source, int options)
Definition: Tree_Data.cpp:253
LY_TYPE_UINT64
@ LY_TYPE_UINT64
Definition: tree_schema.h:812
libyang::Data_Node::merge_to_ctx
int merge_to_ctx(S_Data_Node source, int options, S_Context context)
Definition: Tree_Data.cpp:266
libyang::Data_Node_Leaf_List::change_leaf
int change_leaf(const char *val_str)
Definition: Tree_Data.cpp:561
libyang::Schema_Node
Definition: Tree_Schema.hpp:523
lyd_value_u::uint8
uint8_t uint8
Definition: tree_data.h:111
libyang::Data_Node::insert_attr
S_Attr insert_attr(S_Module module, const char *name, const char *value)
Definition: Tree_Data.cpp:484
libyang::Data_Node::Data_Node_Leaf_List
friend Data_Node_Leaf_List
Definition: Tree_Data.hpp:211
libyang::Value::ident
S_Ident ident()
Definition: Tree_Data.hpp:61
libyang::Difflist::Difflist
Difflist(struct lyd_difflist *diff, S_Deleter deleter)
Definition: Tree_Data.cpp:607
LY_TYPE_INT8
@ LY_TYPE_INT8
Definition: tree_schema.h:805
lyd_value_u
node's value representation
Definition: tree_data.h:94
libyang::Value::int64
int64_t int64()
Definition: Tree_Data.hpp:71
lyd_value_u::int16
int16_t int16
Definition: tree_data.h:106
lyd_value_u::binary
const char * binary
Definition: tree_data.h:95
lyd_attr
Attribute structure.
Definition: tree_data.h:128
libyang::Value::int8
int8_t int8()
Definition: Tree_Data.hpp:65
libyang
Definition: Libyang.hpp:30
lyd_value_u::ident
struct lys_ident * ident
Definition: tree_data.h:101
LY_TYPE_UINT8
@ LY_TYPE_UINT8
Definition: tree_schema.h:806
libyang::Data_Node::find_path
S_Set find_path(const char *expr)
Definition: Tree_Data.cpp:356
libyang::Data_Node_Leaf_List::child
S_Data_Node child()
Definition: Tree_Data.hpp:242
LY_TYPE_INT64
@ LY_TYPE_INT64
Definition: tree_schema.h:811
libyang::Value::bit
std::vector< S_Type_Bit > bit()
Definition: Tree_Data.cpp:41
libyang::Attr::Attr
Attr(struct lyd_attr *attr, S_Deleter deleter=nullptr)
Definition: Tree_Data.cpp:596
libyang::Attr::name
const char * name()
Definition: Tree_Data.hpp:297
libyang::Data_Node::print_mem
std::string print_mem(LYD_FORMAT format, int options)
Definition: Tree_Data.cpp:504
libyang::Difflist::first
std::vector< S_Data_Node > first()
Definition: Tree_Data.cpp:613
libyang::Data_Node
classes for wrapping lyd_node.
Definition: Tree_Data.hpp:97
libyang::Value::int32
int32_t int32()
Definition: Tree_Data.hpp:69
libyang::Value::uint32
uint32_t uint32()
Definition: Tree_Data.hpp:81
libyang::Data_Node_Leaf_List::leaf_type
S_Type leaf_type()
Definition: Tree_Data.cpp:571
libyang::Data_Node_Leaf_List::value
S_Value value()
Definition: Tree_Data.cpp:556
libyang::Data_Node::path
std::string path()
Definition: Tree_Data.cpp:206
libyang::Data_Node::validate
int validate(int options, S_Context var_arg)
Definition: Tree_Data.cpp:384
libyang::Data_Node_Anydata::~Data_Node_Anydata
~Data_Node_Anydata()
Definition: Tree_Data.cpp:594
libyang::Data_Node::validate_value
int validate_value(const char *value)
Definition: Tree_Data.cpp:405
libyang::Data_Node_Leaf_List::wd_default
int wd_default()
Definition: Tree_Data.cpp:568
lyd_node::when_status
uint8_t when_status
Definition: tree_data.h:180
libyang::Data_Node::unlink
int unlink()
Definition: Tree_Data.cpp:471
lyd_anydata_value
Anydata value union.
Definition: tree_data.h:262
libyang::Data_Node::child
virtual S_Data_Node child()
Definition: Tree_Data.hpp:142
lyd_value_u::dec64
int64_t dec64
Definition: tree_data.h:99
libyang.h
The main libyang public header.
lyd_node
Generic structure for a data node, directly applicable to the data nodes defined as LYS_CONTAINER,...
Definition: tree_data.h:176
libyang::Value::leafref
S_Data_Node leafref()
Definition: Tree_Data.cpp:61
tree_data.h
libyang representation of data trees.
libyang::Difflist::type
LYD_DIFFTYPE * type()
Definition: Tree_Data.hpp:320
libyang::Attr::parent
S_Data_Node parent()
Definition: Tree_Data.hpp:292
libyang::Data_Node::tree_for
std::vector< S_Data_Node > tree_for()
Definition: Tree_Data.cpp:519
LYD_FORMAT
LYD_FORMAT
Data input/output formats supported by libyang parser and printer functions.
Definition: tree_data.h:40
libyang::Data_Node::tree_dfs
std::vector< S_Data_Node > tree_dfs()
Definition: Tree_Data.cpp:529
LY_TYPE_UINT16
@ LY_TYPE_UINT16
Definition: tree_schema.h:808
libyang::Data_Node::attr
S_Attr attr()
Definition: Tree_Data.cpp:205
libyang::Value::instance
S_Data_Node instance()
Definition: Tree_Data.cpp:55
libyang::Data_Node::schema
S_Schema_Node schema()
Definition: Tree_Data.hpp:126
libyang::Value::uint16
uint16_t uint16()
Definition: Tree_Data.hpp:79
libyang::Data_Node_Leaf_List::~Data_Node_Leaf_List
~Data_Node_Leaf_List()
Definition: Tree_Data.cpp:555
libyang::Difflist::~Difflist
~Difflist()
Definition: Tree_Data.cpp:612
Tree_Schema.hpp
Class implementation for libyang C header tree_schema.h.
lyd_difflist::type
LYD_DIFFTYPE * type
Definition: tree_data.h:356
lyd_node_anydata
Structure for data nodes defined as LYS_ANYDATA or LYS_ANYXML.
Definition: tree_data.h:278
libyang::Data_Node::insert_sibling
int insert_sibling(S_Data_Node new_node)
Definition: Tree_Data.cpp:292
lyd_value_u::enm
struct lys_type_enum * enm
Definition: tree_data.h:100
libyang::Attr::next
S_Attr next()
Definition: Tree_Data.cpp:605
LY_DATA_TYPE
LY_DATA_TYPE
YANG built-in types.
Definition: tree_schema.h:792
lyd_value_u::string
const char * string
Definition: tree_data.h:110
libyang::Data_Node_Anydata
class for wrapping lyd_node_anydata.
Definition: Tree_Data.hpp:261
libyang::Data_Node_Leaf_List::value_str
const char * value_str()
Definition: Tree_Data.hpp:236
libyang::Data_Node::Data_Node
Data_Node(struct lyd_node *node, S_Deleter deleter=nullptr)
Definition: Tree_Data.cpp:68
LY_TYPE_ENUM
@ LY_TYPE_ENUM
Definition: tree_schema.h:799
libyang::Value::enm
S_Type_Enum enm()
Definition: Tree_Data.hpp:59
libyang::Data_Node::swig_deleter
S_Deleter swig_deleter()
Definition: Tree_Data.hpp:207
lyd_value_u::int64
int64_t int64
Definition: tree_data.h:108
LY_TYPE_IDENT
@ LY_TYPE_IDENT
Definition: tree_schema.h:800
libyang::Value::string
const char * string()
Definition: Tree_Data.hpp:75
libyang::Data_Node_Anydata::child
S_Data_Node child()
Definition: Tree_Data.hpp:274
libyang::Data_Node::prev
S_Data_Node prev()
Definition: Tree_Data.hpp:138
libyang::Attr::value_type
uint16_t value_type()
Definition: Tree_Data.hpp:303
libyang::Value::bln
int8_t bln()
Definition: Tree_Data.hpp:55
libyang::Data_Node::node_module
S_Module node_module()
Definition: Tree_Data.cpp:494
libyang::Value::uint8
uint8_t uint8()
Definition: Tree_Data.hpp:77
LYD_DIFFTYPE
LYD_DIFFTYPE
list of possible types of differences in lyd_difflist
Definition: tree_data.h:312
lyd_value_u::uint32
uint32_t uint32
Definition: tree_data.h:113
lyd_attr::value_str
const char * value_str
Definition: tree_data.h:133
libyang::Data_Node::dup_withsiblings
S_Data_Node dup_withsiblings(int recursive)
Definition: Tree_Data.cpp:230
LY_TYPE_INT16
@ LY_TYPE_INT16
Definition: tree_schema.h:807
LY_TYPE_DEC64
@ LY_TYPE_DEC64
Definition: tree_schema.h:797
libyang::Data_Node::insert_before
int insert_before(S_Data_Node new_node)
Definition: Tree_Data.cpp:311
libyang::Data_Node::dflt
uint8_t dflt()
Definition: Tree_Data.hpp:130
LY_TYPE_UINT32
@ LY_TYPE_UINT32
Definition: tree_schema.h:810
LY_TYPE_INT32
@ LY_TYPE_INT32
Definition: tree_schema.h:809
libyang::Data_Node::new_path
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:426
LY_TYPE_STRING
@ LY_TYPE_STRING
Definition: tree_schema.h:803
lyd_difflist
Structure for the result of lyd_diff(), describing differences between two data trees.
Definition: tree_data.h:355
libyang::create_new_Data_Node
S_Data_Node create_new_Data_Node(struct lyd_node *node)
Definition: Tree_Data.cpp:642
libyang::Value::int16
int16_t int16()
Definition: Tree_Data.hpp:67
lyd_value_u::bln
int8_t bln
Definition: tree_data.h:98
libyang::Data_Node::diff
S_Difflist diff(S_Data_Node second, int options)
Definition: Tree_Data.cpp:412
LYD_ANYDATA_VALUETYPE
LYD_ANYDATA_VALUETYPE
List of possible value types stored in lyd_node_anydata.
Definition: tree_data.h:50
libyang::Data_Node_Anydata::value_type
LYD_ANYDATA_VALUETYPE value_type()
Definition: Tree_Data.hpp:270
lyd_value_u::uint64
uint64_t uint64
Definition: tree_data.h:114
libyang::Data_Node::insert_after
int insert_after(S_Data_Node new_node)
Definition: Tree_Data.cpp:330
libyang::Data_Node_Leaf_List::value_type
uint16_t value_type()
Definition: Tree_Data.hpp:240
libyang::Data_Node::validity
uint8_t validity()
Definition: Tree_Data.hpp:128
libyang::Data_Node::next
S_Data_Node next()
Definition: Tree_Data.hpp:136
libyang::Value::binary
const char * binary()
Definition: Tree_Data.hpp:51
lyd_attr::value_type
LY_DATA_TYPE _PACKED value_type
Definition: tree_data.h:135
libyang::Value::uint64
uint64_t uint64()
Definition: Tree_Data.hpp:83
LY_TYPE_BOOL
@ LY_TYPE_BOOL
Definition: tree_schema.h:796
libyang::Attr::value
S_Value value()
Definition: Tree_Data.cpp:601
libyang::Data_Node::when_status
uint8_t when_status()
Definition: Tree_Data.hpp:132
lyd_value_u::uint16
uint16_t uint16
Definition: tree_data.h:112
libyang::Data_Node::find_instance
S_Set find_instance(S_Schema_Node schema)
Definition: Tree_Data.cpp:364
lyd_node::dflt
uint8_t dflt
Definition: tree_data.h:179
libyang::Data_Node::dup
S_Data_Node dup(int recursive)
Definition: Tree_Data.cpp:219
libyang::Data_Node::parent
S_Data_Node parent()
Definition: Tree_Data.hpp:140
libyang::Data_Node::swig_node
struct lyd_node * swig_node()
Definition: Tree_Data.hpp:205
libyang::Data_Node::dup_to_ctx
S_Data_Node dup_to_ctx(int recursive, S_Context context)
Definition: Tree_Data.cpp:241