• demo_xml.cpp


    demo_xml.cpp

    // demo_xml.cpp
    //
    // (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
    // Use, modification and distribution is subject to the Boost Software
    // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    // http://www.boost.org/LICENSE_1_0.txt)
    
    #include 
    #include 
    #include 
    #include 
    
    #include  // remove
    #include 
    #if defined(BOOST_NO_STDC_NAMESPACE)
    namespace std{ 
        using ::remove;
    }
    #endif
    
    #include 
    #include 
    #include 
    
    #include "demo_gps.hpp"
    
    void save_schedule(const bus_schedule &s, const char * filename){
        // make an archive
        std::ofstream ofs(filename);
        assert(ofs.good());
        boost::archive::xml_oarchive oa(ofs);
        oa << BOOST_SERIALIZATION_NVP(s);
    }
    
    void
    restore_schedule(bus_schedule &s, const char * filename)
    {
        // open the archive
        std::ifstream ifs(filename);
        assert(ifs.good());
        boost::archive::xml_iarchive ia(ifs);
    
        // restore the schedule from the archive
        ia >> BOOST_SERIALIZATION_NVP(s);
    }
    
    int main(int argc, char *argv[])
    {   
        // make the schedule
        bus_schedule original_schedule;
    
        // fill in the data
        // make a few stops
        bus_stop *bs0 = new bus_stop_corner(
            gps_position(34, 135, 52.560f),
            gps_position(134, 22, 78.30f),
            "24th Street", "10th Avenue"
        );
        bus_stop *bs1 = new bus_stop_corner(
            gps_position(35, 137, 23.456f),
            gps_position(133, 35, 54.12f),
            "State street", "Cathedral Vista Lane"
        );
        bus_stop *bs2 = new bus_stop_destination(
            gps_position(35, 136, 15.456f),
            gps_position(133, 32, 15.300f),
            "White House"
        );
        bus_stop *bs3 = new bus_stop_destination(
            gps_position(35, 134, 48.789f),
            gps_position(133, 32, 16.230f),
            "Lincoln Memorial"
        );
    
        // make a  routes
        bus_route route0;
        route0.append(bs0);
        route0.append(bs1);
        route0.append(bs2);
    
        // add trips to schedule
        original_schedule.append("bob", 6, 24, &route0);
        original_schedule.append("bob", 9, 57, &route0);
        original_schedule.append("alice", 11, 02, &route0);
    
        // make aother routes
        bus_route route1;
        route1.append(bs3);
        route1.append(bs2);
        route1.append(bs1);
    
        // 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 << "original schedule";
        std::cout << original_schedule;
        
        std::string filename(boost::archive::tmpdir());
        filename += "/demo.xml";
    
        // save the schedule
        save_schedule(original_schedule, filename.c_str());
    
        // ... some time later
        // make  a new schedule
        bus_schedule new_schedule;
    
        restore_schedule(new_schedule, filename.c_str());
    
        // and display
        std::cout << "\nrestored schedule";
        std::cout << new_schedule;
        // should be the same as the old one. (except for the pointer values)
    
        std::remove(filename.c_str());
    
        delete bs0;
        delete bs1;
        delete bs2;
        delete bs3;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125

    demo_gps.hpp

    #ifndef BOOST_SERIALIZATION_EXAMPLE_DEMO_GPS_HPP
    #define BOOST_SERIALIZATION_EXAMPLE_DEMO_GPS_HPP
    
    /1/2/3/4/5/6/7/8
    //
    // demo_gps.hpp
    //
    // (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
    // Use, modification and distribution is subject to the Boost Software
    // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    // http://www.boost.org/LICENSE_1_0.txt)
    
    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    // This illustration models the bus system of a small city.
    // This includes, multiple bus stops,  bus routes and schedules.
    // There are different kinds of stops.  Bus stops in general will
    // will appear on multiple routes.  A schedule will include
    // muliple trips on the same route.
    
    /
    // gps coordinate
    //
    // llustrates serialization for a simple type
    //
    class gps_position
    {
        friend class boost::serialization::access;
        friend std::ostream & operator<<(std::ostream &os, const gps_position &gp);
    
        int degrees;
        int minutes;
        float seconds;
    
        template<class Archive>
        void serialize(Archive & ar, const unsigned int /* file_version */){
            ar  & BOOST_SERIALIZATION_NVP(degrees)
                & BOOST_SERIALIZATION_NVP(minutes)
                & BOOST_SERIALIZATION_NVP(seconds);
        }
    
    public:
        // every serializable class needs a constructor
        gps_position(){};
        gps_position(int _d, int _m, float _s) :
            degrees(_d), minutes(_m), seconds(_s)
        {}
    };
    
    std::ostream & operator<<(std::ostream &os, const gps_position &gp)
    {
        return os << ' ' << gp.degrees << (unsigned char)186 << gp.minutes << '\'' << gp.seconds << '"';
    }
    
    /
    // One bus stop
    //
    // illustrates serialization of serializable members
    //
    
    class bus_stop
    {
        friend class boost::serialization::access;
        virtual std::string description() const = 0;
        gps_position latitude;
        gps_position longitude;
    
        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            ar & BOOST_SERIALIZATION_NVP(latitude);
            ar & BOOST_SERIALIZATION_NVP(longitude);
        }
    
    protected:
        bus_stop(const gps_position & _lat, const gps_position & _long) :
            latitude(_lat), longitude(_long)
        {}
    public:
        bus_stop(){}
        friend std::ostream & operator<<(std::ostream &os, const bus_stop &gp);
        virtual ~bus_stop(){}
    };
    
    BOOST_SERIALIZATION_ASSUME_ABSTRACT(bus_stop)
    
    std::ostream & operator<<(std::ostream &os, const bus_stop &bs)
    {
        return os << bs.latitude << bs.longitude << ' ' << bs.description();
    }
    
    /
    // Several kinds of bus stops
    //
    // illustrates serialization of derived types
    //
    class bus_stop_corner : public bus_stop
    {
        friend class boost::serialization::access;
        std::string street1;
        std::string street2;
        virtual std::string description() const
        {
            return street1 + " and " + street2;
        }
        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            // save/load base class information
            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(bus_stop);
            ar & BOOST_SERIALIZATION_NVP(street1);
            ar & BOOST_SERIALIZATION_NVP(street2);
        }
    public:
        bus_stop_corner(){}
        bus_stop_corner(const gps_position & _lat, const gps_position & _long,
            const std::string & _s1, const std::string & _s2
        ) :
            bus_stop(_lat, _long), street1(_s1), street2(_s2)
        {
        }
    };
    
    class bus_stop_destination : public bus_stop
    {
        friend class boost::serialization::access;
        std::string name;
        virtual std::string description() const
        {
            return name;
        }
        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(bus_stop)
                & BOOST_SERIALIZATION_NVP(name);
        }
    public:
        bus_stop_destination(){}
        bus_stop_destination(
            const gps_position & _lat, const gps_position & _long, const std::string & _name
        ) :
            bus_stop(_lat, _long), name(_name)
        {
        }
    };
    
    /
    // a bus route is a collection of bus stops
    //
    // illustrates serialization of STL collection templates.
    //
    // illustrates serialzation of polymorphic pointer (bus stop *);
    //
    // illustrates storage and recovery of shared pointers is correct
    // and efficient.  That is objects pointed to by more than one
    // pointer are stored only once.  In such cases only one such
    // object is restored and pointers are restored to point to it
    //
    class bus_route
    {
        friend class boost::serialization::access;
        friend std::ostream & operator<<(std::ostream &os, const bus_route &br);
        typedef bus_stop * bus_stop_pointer;
        std::list<bus_stop_pointer> stops;
        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            // in this program, these classes are never serialized directly but rather
            // through a pointer to the base class bus_stop. So we need a way to be
            // sure that the archive contains information about these derived classes.
            //ar.template register_type();
            ar.register_type(static_cast<bus_stop_corner *>(NULL));
            //ar.template register_type();
            ar.register_type(static_cast<bus_stop_destination *>(NULL));
            // serialization of stl collections is already defined
            // in the header
            ar & BOOST_SERIALIZATION_NVP(stops);
        }
    public:
        bus_route(){}
        void append(bus_stop *_bs)
        {
            stops.insert(stops.end(), _bs);
        }
    };
    std::ostream & operator<<(std::ostream &os, const bus_route &br)
    {
        std::list<bus_stop *>::const_iterator it;
        // note: we're displaying the pointer to permit verification
        // that duplicated pointers are properly restored.
        for(it = br.stops.begin(); it != br.stops.end(); it++){
            os << '\n' << std::hex << "0x" << *it << std::dec << ' ' << **it;
        }
        return os;
    }
    
    /
    // a bus schedule is a collection of routes each with a starting time
    //
    // Illustrates serialization of STL objects(pair) in a non-intrusive way.
    // See definition of operator<<  >(ar, pair)
    //
    // illustrates nesting of serializable classes
    //
    // illustrates use of version number to automatically grandfather older
    // versions of the same class.
    
    class bus_schedule
    {
        friend class boost::serialization::access;
        friend std::ostream & operator<<(std::ostream &os, const bus_schedule &bs);
        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            ar & BOOST_SERIALIZATION_NVP(schedule);
        }
        // note: this structure was made public. because the friend declarations
        // didn't seem to work as expected.
    public:
        struct trip_info
        {
            template<class Archive>
            void serialize(Archive &ar, const unsigned int file_version)
            {
                // in versions 2 or later
                if(file_version >= 2)
                    // read the drivers name
                    ar & BOOST_SERIALIZATION_NVP(driver);
                // all versions have the follwing info
                ar  & BOOST_SERIALIZATION_NVP(hour)
                    & BOOST_SERIALIZATION_NVP(minute);
            }
    
            // starting time
            int hour;
            int minute;
            // only after system shipped was the driver's name added to the class
            std::string driver;
    
            trip_info(){}
            trip_info(int _h, int _m, const std::string &_d) :
                hour(_h), minute(_m), driver(_d)
            {}
            ~trip_info(){
            }
        };
    //  friend std::ostream & operator<<(std::ostream &os, const trip_info &ti);
    private:
        std::list<std::pair<trip_info, bus_route *> > schedule;
    public:
        void append(const std::string &_d, int _h, int _m, bus_route *_br)
        {
            schedule.insert(schedule.end(), std::make_pair(trip_info(_h, _m, _d), _br));
        }
        bus_schedule(){}
    };
    
    BOOST_CLASS_VERSION(bus_schedule::trip_info, 3)
    BOOST_CLASS_VERSION(bus_schedule, 2)
    
    std::ostream & operator<<(std::ostream &os, const bus_schedule::trip_info &ti)
    {
        return os << '\n' << ti.hour << ':' << ti.minute << ' ' << ti.driver << ' ';
    }
    std::ostream & operator<<(std::ostream &os, const bus_schedule &bs)
    {
        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);
        }
        return os;
    }
    
    #endif // BOOST_SERIALIZATION_EXAMPLE_DEMO_GPS_HPP
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
  • 相关阅读:
    【SpringBoot的自动配置--下篇】架构师如何自定义自己的条件注解与自动配置
    在线问诊 Python、FastAPI、Neo4j — 创建 节点关系
    ORA-01861: literal does not match format string
    element ui的@change事件进行计算
    Altium Designer 相同模块的布局布线复用-AD
    Vue使用总结-包括Vue2和Vue3
    sed命令使用总结
    【Prometheus】监控Kubernetes
    切片比数组好用在哪
    K8S的pod创建过程
  • 原文地址:https://blog.csdn.net/qq_40178082/article/details/132853978