C++ boost::property_tree::ptree














































C++ boost::property_tree::ptree



Description :-

 A property tree is a data structure which stores the different data in different hierarchical order. In property tree there is a parent node and can have many child node each having some data or key.


Header files :-
#include <boost/property_tree/ptree.hpp>
#include <boost/any.hpp>

Advantages :-

Trees are so useful and frequently used, because they have some very serious advantages:

  • Trees reflect structural relationships in the data.
  • Trees are used to represent hierarchies.
  • Trees provide an efficient insertion and searching.
  • Trees are very flexible data, allowing to move subtrees around with minimum effort.


Program :- To show custom data type in property tree

#include "pch.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/any.hpp>
#include <list>
#include <string>
#include <iostream>

// Custom translator that works with boost::any instead of std::string
template <class Ext, class Int = boost::any>
struct variant_translator
{
typedef Ext external_type;
typedef Int internal_type;

external_type
get_value(const internal_type &value) const
{
return boost::any_cast<external_type>(value);
}
internal_type
put_value(const external_type &value) const
{
return value;
}
};

int main()
{

using namespace boost::property_tree;

// Property_tree with boost::any as data type
// Key type: std::string
// Data type: boost::any
// Key comparison: default (std::less<std::string>)
typedef basic_ptree<std::string, boost::any> my_ptree;
my_ptree pt;

// Put/get int value
typedef variant_translator<int> int_tran;
pt.put("int value", 3, int_tran());
int int_value = pt.get<int>("int value", int_tran());
std::cout << "Int value: " << int_value << "\n";

// Put/get string value
typedef variant_translator<std::string> string_tran;
pt.put<std::string>("string value", "cppsecrets", string_tran());
std::string string_value = pt.get<std::string>(
"string value"
, string_tran()
);
std::cout << "String value: " << string_value << "\n";

// Put/get list<int> value
typedef std::list<int> intlist;
typedef variant_translator<intlist> intlist_tran;
int list_data[] = { 1, 2, 3, 4, 5 };
pt.put<intlist>(
"list value"
, intlist(
list_data
, list_data + sizeof(list_data) / sizeof(*list_data)
)
, intlist_tran()
);
intlist list_value = pt.get<intlist>(
"list value"
, intlist_tran()
);
std::cout << "List value: ";
for (intlist::iterator it = list_value.begin(); it != list_value.end(); ++it)
std::cout << *it << ' ';
std::cout << '\n';
return 0;
}


Output :-

Int value:  3
String value:  cppsecrets
List value:  1  2  3  4  5


More Articles of VIKRANT KUMAR:

Name Views Likes
C++ boost::property_tree::speed_test 337 8
C++ boost::fusion::algorithm::query::function 305 10
C++ boost::fusion::sequence::operator::i/o 322 7
C++ boost::fusion::sequence::Associative_sequence 286 7
C++ boost::fusion::container::deque 486 8
C++ boost::fusion::sequence::intrinsic::front 268 7
C++ boost::fusion::sequence::intrinsic::swap 275 7
C++ boost::fusion::sequence::operator::hashing 373 7
C++ boost::fusion::sequence::intrinsic::has_key 276 7
C++ boost::algorithm::iteration::function::reverse_fold 293 6
C++ boost::fusion::sequence::intrinsic::end 289 7
C++ boost::fusion::algorithm::transformation::pop_front 254 6
c++ boost::fusion::sequence::Unbounded_sequence 303 7
C++ boost::fusion::algorithm::transformation::transform 384 6
C++ boost::fusion::container::vector 429 7
C++ boost::fusion::sequence::bidirectional_sequence 265 7
C++ boost::fusion::sequence::forward_sequence 288 7
C++ boost::property_tree::ptree 1481 8
C++ boost::fusion::algorithm::transformation::replace 259 6
C++ boost::fusion::sequence::Random_Access_Sequence 298 7
C++ boost::fusion::algorithm::transformation::join 381 6
C++ boost::fusion::container::list 321 8
C++ boost::fusion::algorithm::iteration::function::fold 437 6
C++ boost::fusion::container::front_ectended_deque 283 8
C++ boost::algorithm::iteration::function::accumulate 381 7
C++ boost::fusion::algorithm::transformation::reverse 249 6
C++ boost::fusion::algorithm::transformation::flatten 598 6
C++ boost::fusion::sequence::intrinsic::begin 337 8
C++ boost::fusion::sequence::intrinsic::at_has_key 325 6
C++ boost::property_tree::xml_parser 1073 7
C++ boost::sequence::intrinsic::back 253 7
C++ fusion::container::cons 309 7
C++ boost::fusion::algorithm::transformation::filter 261 7
C++ boost::fusion::algorithm::transformation::clear 301 6
C++ boost::fusion::algorithm::iteration::function::iter_fold 271 7
C++ boost::fusion::algorithm::transformation::insert 300 7
C++ boost::fusion::sequence::operator::comparision 346 7
C++ boost::fusion::sequence::intrinsic::size 257 8
C++ boost::fusion::algorithm::auxiliary::function 288 6
C++ boost::fusion::algorithm::transformation::push_front 270 6
C++ boost::fusion::algorithm::transformation::remove 305 6
C++ boost::fusion::algorithm::function::for_each 509 7
C++ boost::fusion::sequence::intrinsic::at 268 7
C++ boost::fusion::algorithm::transformation::erase 284 6
C++ boost::Fusion::sequence::intrinsic::empty 277 7

Comments