gpt4 book ai didi

c++ - 如何使用 boost 库反序列化和获取成员值

转载 作者:行者123 更新时间:2023-12-03 07:23:15 26 4
gpt4 key购买 nike

首先,我想告诉你

  • 我是序列化技术的新手
  • 我对使用 boost 序列化并不完全空白,但是我经历的大多数示例都非常直接(例如,只有一个类,最多一个基类和一个派生类)。

  • 我以 here 为例.在这里,代码的作者使用了 saveload使用类 bus_schedule .在此处调用的对象的帮助下,我遇到了获取所有成员变量的问题 new_schedule .我假设,这个对象将帮助我获取其他类的所有成员(也许我在这一点上完全错了)。
    除此之外,我看到,大部分成员变量都在这里 private这意味着我无法从 main 访问它们功能。这就是为什么,我设置了一些 getter 函数来返回一些成员值。例如,来自 bus_schedule如果我可以返回类(class) schedule列出然后我可以访问 trip_info结构值以及类型为 bus_route的指针.
    返回函数 schedule我使用的列表是:
    std::list<std::pair<trip_info, bus_route *> >  return_schedule()
    {
    return schedule;
    }
    main我用过的功能:
    bus_schedule new_schedule;
    auto returning_schedule = new_schedule.return_schedule();
    for(auto i:returning_schedule)
    {
    cout<<(i.first)<<" ~~~~~~~ "<<(i.second)<<endl;
    /*with i.first I can access the struct info, i.second pointer gives me the info of
    bus_route pointer value ater dereferencing*/
    }
    但是在这里,我再次坚持从 i.second 指针中获取单独的值。我知道这里, bus_route类有一个名为 stops 的列表. Here也返回了这个。但在我看来,每次做这个反算都很麻烦。
    我的问题是,有没有来自 boost 的直接解决方案?反序列化后访问所有成员变量?我将为所有类(class)设置 setter/getter 功能,但害怕找到使用这些功能的途径。

    最佳答案

    这个问题实际上与序列化是分开的。这是没有序列化内容的数据结构。我对代码进行了现代化改造以使用 C++17 功能

  • auto 和移动语义
  • default 编辑特殊成员
  • [[nodiscard]] , override 虚拟机关键字
  • 显式构造函数和emplace
  • 带解构的 ranged-for 循环
  • make_unique<> 而不是 new/delete
  • 扩展聚合初始化器

  • 总而言之,它使代码更短、更简单、更高效¹。此外,它突出显示了您想知道的一些事情,我将在下面展示。
    Live On Coliru
    #include <iostream>
    #include <list>
    #include <memory>
    #include <string>

    struct gps_position {
    int degrees;
    int minutes;
    float seconds;

    friend std::ostream& operator<<(std::ostream& os, const gps_position& gp) {
    return os << ' ' << gp.degrees << "º" << gp.minutes << "'" << gp.seconds << '"';
    }
    };

    /////////////////////////////////////////////////////////////
    // One bus stop
    struct bus_stop {
    gps_position latitude, longitude;

    [[nodiscard]] virtual std::string description() const = 0;
    virtual ~bus_stop() = default;
    friend std::ostream& operator<<(std::ostream& os, const bus_stop& bs) {
    return os << bs.latitude << bs.longitude << ' ' << bs.description();
    }

    protected:
    explicit bus_stop(gps_position _lat, gps_position _long)
    : latitude(_lat), longitude(_long) {}
    };

    /////////////////////////////////////////////////////////////
    // Several kinds of bus stops
    struct bus_stop_corner : bus_stop {
    std::string street1, street2;
    [[nodiscard]] std::string description() const override {
    return street1 + " and " + street2;
    }

    explicit bus_stop_corner(gps_position _lat, gps_position _long, std::string _s1, std::string _s2)
    : bus_stop(_lat, _long), street1(std::move(_s1)), street2(std::move(_s2)) {}
    };

    struct bus_stop_destination : bus_stop {
    std::string name;
    [[nodiscard]] std::string description() const override { return name; }

    bus_stop_destination(gps_position _lat, gps_position _long, std::string _name)
    : bus_stop(_lat, _long), name(std::move(_name)) {}
    };

    struct bus_route {
    using bus_stop_pointer = bus_stop*;
    std::list<bus_stop_pointer> stops;

    void append(bus_stop* _bs) { stops.insert(stops.end(), _bs); }
    friend std::ostream& operator<<(std::ostream& os, const bus_route& br) {
    for (auto& stop : br.stops) {
    os << '\n' << std::hex << "0x" << stop << std::dec << ' ' << *stop;
    }
    return os;
    }
    };

    /////////////////////////////////////////////////////////////
    // a bus schedule is a collection of routes each with a starting time
    struct bus_schedule {
    struct trip_info { int hour, minute; std::string driver; };

    void append(const std::string& _d, int _h, int _m, bus_route* _br) {
    schedule.emplace(schedule.end(), trip_info{_h, _m, _d}, _br);
    }

    private:
    friend std::ostream& operator<<(std::ostream& os, const bus_schedule& bs) {
    for (auto const& [k,v] : bs.schedule) { os << k << *v; }
    return os;
    }
    friend std::ostream& operator<<(std::ostream& os, const bus_schedule::trip_info& ti) {
    return os << '\n' << ti.hour << ':' << ti.minute << ' ' << ti.driver << ' ';
    }
    std::list<std::pair<trip_info, bus_route*>> schedule;
    };

    int main() {
    // fill in the data
    // make a few stops
    auto bs0 = std::make_unique<bus_stop_corner>(
    gps_position{ 34, 135, 52.560F }, gps_position{ 134, 22, 78.30F },
    "24th Street", "10th Avenue");
    auto bs1 = std::make_unique<bus_stop_corner>(
    gps_position{ 35, 137, 23.456F }, gps_position{ 133, 35, 54.12F },
    "State street", "Cathedral Vista Lane");
    auto bs2 = std::make_unique<bus_stop_destination>(
    gps_position{ 35, 136, 15.456F }, gps_position{ 133, 32, 15.300F },
    "White House");
    auto bs3 = std::make_unique<bus_stop_destination>(
    gps_position{ 35, 134, 48.789F }, gps_position{ 133, 32, 16.230F },
    "Lincoln Memorial");

    // make the schedule
    bus_schedule original_schedule;
    bus_route route0;

    bus_route route1;

    {
    // make a route
    route0.append(bs0.get());
    route0.append(bs1.get());
    route0.append(bs2.get());

    // add trips to schedule
    original_schedule.append("bob", 6, 24, &route0);
    original_schedule.append("bob", 9, 57, &route0);
    original_schedule.append("alice", 11, 2, &route0);
    }

    {
    // make aother routes
    route1.append(bs3.get());
    route1.append(bs2.get());
    route1.append(bs1.get());

    // add trips to schedule
    original_schedule.append("ted", 7, 17, &route1);
    original_schedule.append("ted", 9, 38, &route1);
    original_schedule.append("alice", 11, 47, &route1);
    }

    // display the complete schedule
    std::cout << "schedule" << original_schedule;
    }
    打印:
    schedule
    6:24 bob
    34º135'52.56" 134º22'78.3" 24th Street and 10th Avenue
    35º137'23.456" 133º35'54.12" State street and Cathedral Vista Lane
    35º136'15.456" 133º32'15.3" White House
    9:57 bob
    34º135'52.56" 134º22'78.3" 24th Street and 10th Avenue
    35º137'23.456" 133º35'54.12" State street and Cathedral Vista Lane
    35º136'15.456" 133º32'15.3" White House
    11:2 alice
    34º135'52.56" 134º22'78.3" 24th Street and 10th Avenue
    35º137'23.456" 133º35'54.12" State street and Cathedral Vista Lane
    35º136'15.456" 133º32'15.3" White House
    7:17 ted
    35º134'48.789" 133º32'16.23" Lincoln Memorial
    35º136'15.456" 133º32'15.3" White House
    35º137'23.456" 133º35'54.12" State street and Cathedral Vista Lane
    9:38 ted
    35º134'48.789" 133º32'16.23" Lincoln Memorial
    35º136'15.456" 133º32'15.3" White House
    35º137'23.456" 133º35'54.12" State street and Cathedral Vista Lane
    11:47 alice
    35º134'48.789" 133º32'16.23" Lincoln Memorial
    35º136'15.456" 133º32'15.3" White House
    35º137'23.456" 133º35'54.12" State street and Cathedral Vista Lane
    结论
    为了方便地使用该 map 数据结构,您可以使用循环 as the example has it :
    std::list<
    std::pair<bus_schedule::trip_info, bus_route*>>::const_iterator it;
    for (it = bs.schedule.begin(); it != bs.schedule.end(); it++) {
    os << it->first << *(it->second);
    }
    但是,在 C++17 中,您的编写方式与以下内容完全相同:
    for (auto const& [info,route] : bs.schedule) { os << info << *route; }
    您可以看到 [k, v] 使用结构化绑定(bind)来提取值类型的“第一”和“第二”部分。如果您的编译器中没有 C++17 功能,您始终可以手动执行相同操作:
    for (auto const& pair : bs.schedule) {
    trip_info const& info = pair.first;
    bus_route const& route = *pair.second;
    os << info << route;
    }
    游览 private 成员
    我认为这个数据结构不是典型的或“好”的惯用 C++。使用原始指针会导致内存管理错误(例如,反序列化路由的每一站都会泄露)。
    添加到这个“装置”getter/setter 风险 putting lipstick on a pig ²。
    相反,我会选择公开您需要公开的内容。这是一个从 main 打印详细信息的示例,而不会重载 operator<< 运算符:
    // display the complete schedule
    std::cout << "schedule";
    for (auto const& [info,route] : original_schedule.schedule) {
    std::cout << '\n' << info.hour << ':' << info.minute << ' ' << info.driver << ' ';
    for (auto& stop : route->stops)
    std::cout << '\n' << stop->latitude << stop->longitude << ' ' << stop->description();
    }
    打印和以前一样。
    完整列表
    Live On Coliru
    #include <iostream>
    #include <list>
    #include <memory>
    #include <string>

    struct gps_position {
    int degrees;
    int minutes;
    float seconds;

    friend std::ostream& operator<<(std::ostream& os, const gps_position& gp) {
    return os << ' ' << gp.degrees << "º" << gp.minutes << "'" << gp.seconds << '"';
    }
    };

    /////////////////////////////////////////////////////////////
    // One bus stop
    struct bus_stop {
    gps_position latitude, longitude;

    [[nodiscard]] virtual std::string description() const = 0;
    virtual ~bus_stop() = default;

    explicit bus_stop(gps_position _lat, gps_position _long)
    : latitude(_lat), longitude(_long) {}
    };

    /////////////////////////////////////////////////////////////
    // Several kinds of bus stops
    struct bus_stop_corner : bus_stop {
    std::string street1, street2;
    [[nodiscard]] std::string description() const override {
    return street1 + " and " + street2;
    }

    explicit bus_stop_corner(gps_position _lat, gps_position _long, std::string _s1, std::string _s2)
    : bus_stop(_lat, _long), street1(std::move(_s1)), street2(std::move(_s2)) {}
    };

    struct bus_stop_destination : bus_stop {
    std::string name;
    [[nodiscard]] std::string description() const override { return name; }

    bus_stop_destination(gps_position _lat, gps_position _long, std::string _name)
    : bus_stop(_lat, _long), name(std::move(_name)) {}
    };

    struct bus_route {
    std::list<bus_stop*> stops;
    void append(bus_stop* _bs) { stops.push_back(_bs); }
    };

    /////////////////////////////////////////////////////////////
    // a bus schedule is a collection of routes each with a starting time
    struct bus_schedule {
    struct trip_info { int hour, minute; std::string driver; };

    void append(const std::string& _d, int _h, int _m, bus_route* _br) {
    schedule.emplace(schedule.end(), trip_info{_h, _m, _d}, _br);
    }

    std::list<std::pair<trip_info, bus_route*>> schedule;
    };

    int main() {
    // fill in the data
    // make a few stops
    auto bs0 = std::make_unique<bus_stop_corner>(
    gps_position{ 34, 135, 52.560F }, gps_position{ 134, 22, 78.30F },
    "24th Street", "10th Avenue");
    auto bs1 = std::make_unique<bus_stop_corner>(
    gps_position{ 35, 137, 23.456F }, gps_position{ 133, 35, 54.12F },
    "State street", "Cathedral Vista Lane");
    auto bs2 = std::make_unique<bus_stop_destination>(
    gps_position{ 35, 136, 15.456F }, gps_position{ 133, 32, 15.300F },
    "White House");
    auto bs3 = std::make_unique<bus_stop_destination>(
    gps_position{ 35, 134, 48.789F }, gps_position{ 133, 32, 16.230F },
    "Lincoln Memorial");

    // make the schedule
    bus_schedule original_schedule;
    bus_route route0;

    bus_route route1;

    {
    // make a route
    route0.append(bs0.get());
    route0.append(bs1.get());
    route0.append(bs2.get());

    // add trips to schedule
    original_schedule.append("bob", 6, 24, &route0);
    original_schedule.append("bob", 9, 57, &route0);
    original_schedule.append("alice", 11, 2, &route0);
    }

    {
    // make aother routes
    route1.append(bs3.get());
    route1.append(bs2.get());
    route1.append(bs1.get());

    // add trips to schedule
    original_schedule.append("ted", 7, 17, &route1);
    original_schedule.append("ted", 9, 38, &route1);
    original_schedule.append("alice", 11, 47, &route1);
    }

    // display the complete schedule
    std::cout << "schedule";
    for (auto const& [info,route] : original_schedule.schedule) {
    std::cout << '\n' << info.hour << ':' << info.minute << ' ' << info.driver << ' ';
    for (auto& stop : route->stops)
    std::cout << '\n' << stop->latitude << stop->longitude << ' ' << stop->description();
    }
    }

    Note how the code is 100 lines of code shorter than the sample even when removing all blank lines and comments, and using the same formatting.



    ¹ 请注意,代码显然根本不是客观上“高效”的,因为它执行各种动态分配和运行时多态性
    ² 这种反模式对于具有传统 OOP 背景的人来说很常见,请参阅 pseudo/quasi-classes (PDF)

    关于c++ - 如何使用 boost 库反序列化和获取成员值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64430186/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com