简体   繁体   English

boost :: property_tree放置用法C ++

[英]boost::property_tree put usage C++

I am doing a ptree.put() inside a class member function. 我在类成员函数内执行ptree.put()

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). 我正在创建该类的实例,并且setData1()由代码中的回调函数(多个ROS订户回调)调用。

When I call setData2() in the main() it works as expected. 当我在main()调用setData2() ,它可以按预期工作。 But when the similar setData1() is called by the callback, ptree_ is empty at the end. 但是,当回调调用类似的setData1()ptree_为空。 When I do both, ptree_ only has the data written by setData2() . 当我两者都做时, ptree_仅具有由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. 当我在每次调用时在setData1()中打印ptree_时,可以在ptree_看到该调用的数据,但不能看到先前调用中写入的数据。 No data written using setData1() is there when printPtree() is called. 没有使用写入数据setData1()是那里当printPtree()被调用。 Only data written using setData1() remains intact. 仅使用setData1()写入的数据保持不变。

I wrote two identical setData methods so that I could explain my problem clearly. 我编写了两个相同的setData方法,以便可以清楚地说明我的问题。 What I am doing wrong here? 我在这里做错了什么?


EDIT: Here's a minimal version of my code. 编辑:这是我的代码的最低版本。 This is a ROS node. 这是一个ROS节点。

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 这是我最后的ptree_

{
    "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. 但是,如果我在每次调用时都打印ptree_ ,则它具有在该特定调用+上面发送的数据。 Since it seems like a problem related to my C++ knowledge, I have posted the problem here instead of ROS answers. 由于这似乎是与我的C ++知识有关的问题,因此我在此处发布了该问题,而不是ROS答案。

You have two Writer instances : 您有两个Writer实例:

  • the writer member of the SomeClass instance created in main . main创建的SomeClass实例的writer成员。 The property tree in it has "Velocity.x" and "Velocity.y" set. 其中的属性树设置了"Velocity.x""Velocity.y"
  • the writer_ member of the SubscriberHandler instance created in the SomeClass constructor. SomeClass构造函数中创建的SubscriberHandler实例的writer_成员。 This is a copy of the first Writer instance (because the SubscriberHandler constructor copied it). 这是第一个Writer实例的副本(因为SubscriberHandler构造函数已复制它)。 The property tree in it has "Velocity.x" , "Velocity.y" and path set. 其中的属性树具有"Velocity.x""Velocity.y"path集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM