doca
doca

Reputation: 1548

boost::property_tree put usage C++

I am doing a ptree.put() inside a class member function.

class Writer{
private:
    boost::property_tree::ptree ptree_;
public:
    Writer(){}
    void setData1(string path, string data){
        ptree_.put(path, data);
    }
    void setData2(string path, string data){
        ptree_.put(path, data);
    }
    void printPtree(){
        boost::property_tree::write_json(std::cout, ptree_);
    }
};

I am creating an instance of the class and setData1() is called by a callback function in the code (Multiple ROS subscriber callbacks).

When I call setData2() in the main() it works as expected. But when the similar setData1() is called by the callback, ptree_ is empty at the end. When I do both, ptree_ only has the data written by setData2().

When I print the ptree_ inside setData1() at each call, data of that call can be seen in the ptree_ but not the data written in previous calls. No data written using setData1() is there when printPtree() is called. Only data written using setData1() remains intact.

I wrote two identical setData methods so that I could explain my problem clearly. What I am doing wrong here?


EDIT: Here's a minimal version of my code. This is a ROS node.

class Writer{
private:
    pt::ptree ptree_;

public:
    Writer(){}

    void setData(string branch, string data){
        ptree_.put(branch, data);
        // gets ptree content at each call
        // pt::write_json(std::cout, ptree_);
    }
    ~Writer(){
        pt::write_json(std::cout, ptree_);
    }
};

class SubscriberHandler{
private:
    ros::Subscriber sub_;
    Writer writer_;
public:
    SubscriberHandler(string topic_name_, Writer & writer) : writer_(writer){
        ros::NodeHandle n;
        sub_ = n.subscribe(topic_name_, 10, &SubscriberHandler::callback, this);
    }
    // this is the ROS callback function
    void callback(const topic_tools::ShapeShifter::ConstPtr& msg){
        --- CODE TO GET DATA FROM msg---
        writer_.setData(path, value);
    }
};

class SomeClass{
    Writer writer;
public:
    SomeClass(){
        writer.setData("Velocity.x", "50");
        writer.setData("Velocity.y", "10.5");
        // I have to create these objects inside a class
        SubscriberHandler sh("my_topic", writer);
    }
};

int main(){
    ros::init("node_name");

    SomeClass sc;

    ros::spin()
    return 0;
}

This is the ptree_ I have at the end

{
    "Velocity": {
        "x": "50",
        "y": "10.5"
    }
}

but if I print the ptree_ at each call, it has the data sent at that particular call + the above. Since it seems like a problem related to my C++ knowledge, I have posted the problem here instead of ROS answers.

Upvotes: 0

Views: 2498

Answers (1)

Sander De Dycker
Sander De Dycker

Reputation: 16243

You have two Writer instances :

  • the writer member of the SomeClass instance created in main. The property tree in it has "Velocity.x" and "Velocity.y" set.
  • the writer_ member of the SubscriberHandler instance created in the SomeClass constructor. This is a copy of the first Writer instance (because the SubscriberHandler constructor copied it). The property tree in it has "Velocity.x", "Velocity.y" and path set.

Upvotes: 2

Related Questions