1

I know it is not default and maybe not prefered way to use Boost Property tree. But It seems have all needed to create tree of named pointers. So I tried:

#include <boost/property_tree/ptree.hpp>

#include <iostream>
#include <string>

template <class T>
int append(T val)
{
    std::cout << "hello";
    return 0;
}

int main()
{
    using boost::property_tree::ptree;
    ptree pt;
    pt.put("function-int-pointer", &append<int>);

    (pt.get("function-int-pointer", NULL))(123);
    // ^-- error C2064: term does not evaluate to a function taking 1 arguments

    (pt.get<int(*)(int)>("function-int-pointer"))(123);
    // ^-- error C2678: binary '>>' : no operator found which takes a left-hand 
    // operand of type 'std::basic_istream<_Elem,_Traits>' (or there is no 
    // acceptable conversion)
} 

If it is possible I would love tham to be auto restorable (with simple .get() not .get<T>)

It seems like it can store pointers to functions (main reson I want to use it). But I can not get them from it( So I wonder how to store pointers in Boost property tree so that thay would be auto restorable?

myWallJSON
  • 9,110
  • 22
  • 78
  • 149

1 Answers1

2

I'm going to try to answer the gist of the question here. I think you might be content with Boost Serialization too?

Boost serialization is a very powerful general-purpose serialization library, able to serialize to

  • plain text
  • XML
  • binary archives

It comes with full support for a wide range of data types1 - though not for function pointers out of the box. However, due to it's extensible nature, Peter Dimov came up with a way to serialize functions by name, registering the functions manually. In a sketch, that would look like:

template<class T> void register_static( T * pt, std::string const & name ) 
 { 
 // add to name<->address maps 
 } 

template<class T> void save(T * const & pt) 
 { 
 // look up the name of pt and save 
 } 

template<class T> void load(T * & t) 
 { 
 // load name, look up address, store in pt 
 }

Note that T could be any callable type

  • function type
  • function pointer
  • function object (e.g. std::less())
  • a lambda expression

It is important, however, to register each instance of a callable type by name, since the addresses wouldn't be the sam for separate instances.


1 standard containers, pointers, data structures with internal (pointer) references, aliased pointers, cyclic trees etc.

sehe
  • 374,641
  • 47
  • 450
  • 633