Home > Net >  Boost : serialize long unsigned int
Boost : serialize long unsigned int

Time:03-08

I have compilation error from boost because of a long unsigned int serialization and I cannot find out where does it come from..

Here is my class to serialize:

#ifndef JUCECMAKEREPO_PERFAUDITRESPONSE_H
#define JUCECMAKEREPO_PERFAUDITRESPONSE_H

#include <string>
#include <utility>
#include <vector>
#include "boost/serialization/nvp.hpp"
#include <boost/serialization/array_wrapper.hpp>
#include "CPUStructs.h"

namespace API::Network {
    class PerfAuditResponse {
    public:
        PerfAuditResponse() = delete;

        explicit PerfAuditResponse(std::string msg, int code, const Performance::CPUData &CPUData,
                                   const std::vector<Performance::ProcessData> &processesData)
                : message(std::move(msg)),
                  httpCode(code),
                  CPUIdleTime(CPUData.GetIdleTime()),
                  CPUActiveTime(CPUData.GetActiveTime()),
                  _processesData(processesData) {
        };

    private:
        friend class boost::serialization::access;

        template<typename Archive>
        void serialize(Archive &ar, unsigned) {
            ar & BOOST_SERIALIZATION_NVP(CPUIdleTime)
            & BOOST_SERIALIZATION_NVP(CPUActiveTime)
            & BOOST_SERIALIZATION_NVP(message)
            & BOOST_SERIALIZATION_NVP(httpCode);
        }

        std::string message;
        boost::int32_t httpCode;
        std::size_t CPUIdleTime;
        std::size_t CPUActiveTime;
        const std::vector<Performance::ProcessData> &_processesData;
    };
}
#endif //JUCECMAKEREPO_PERFAUDITRESPONSE_H

And here is the compiler output:

[...]/boost-src/libs/serialization/include/boost/serialization/access.hpp:116:11 error: request for member ‘serialize’ in ‘t’, which is of non-class type ‘long unsigned int’ t.serialize(ar, file_version);

Does someone have any clue about where does it come from?

CodePudding user response:

If I only keep message and httpCode serialization the compilation works

I don't see how. You explicitly deleted the default constructor. Boost Serialization requires default construction (or you MUST implement save_construct_data/load_construct_data.

Here's my simple tester, you can compare notes and see what you missed:

Live On Coliru

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/integer.hpp>
//#include <boost/serialization/array_wrapper.hpp>
#include <boost/serialization/nvp.hpp>
#include <iostream>
#include <sstream>
#include <vector>

namespace Performance {
    struct CPUData {
        std::size_t idle, active;
        std::size_t GetIdleTime()  const { return 0; }
        std::size_t GetActiveTime()  const { return 0; }
    };
    struct ProcessData {
    };
} // namespace Performance

namespace API::Network {
    class PerfAuditResponse {
    public:
        //PerfAuditResponse() = delete;

        explicit PerfAuditResponse(std::string msg, int code, const Performance::CPUData &CPUData,
                                   const std::vector<Performance::ProcessData> &processesData)
                : message(std::move(msg)),
                  httpCode(code),
                  CPUIdleTime(CPUData.GetIdleTime()),
                  CPUActiveTime(CPUData.GetActiveTime()),
                  _processesData(processesData) {
        };

    private:
        friend class boost::serialization::access;

        template<typename Archive>
        void serialize(Archive &ar, unsigned) {
            ar& BOOST_SERIALIZATION_NVP(CPUIdleTime) &
                BOOST_SERIALIZATION_NVP(CPUActiveTime) &
                BOOST_SERIALIZATION_NVP(message) &
                BOOST_SERIALIZATION_NVP(httpCode);
        }

        std::string                                  message;
        boost::int32_t                               httpCode;
        std::size_t                                  CPUIdleTime;
        std::size_t                                  CPUActiveTime;
        const std::vector<Performance::ProcessData>& _processesData;
    };
}

int main() {
    std::stringstream ss;
    std::vector<Performance::ProcessData> const pdata(13, Performance::ProcessData{});

    {
        boost::archive::text_oarchive oa(ss);
        API::Network::PerfAuditResponse par("Yo", 42, {99, 320}, pdata);
        oa << par;
    }

    std::cout << ss.str() << "\n";

    {
        API::Network::PerfAuditResponse par("default", -1, {}, pdata);

        {
            boost::archive::text_iarchive ia(ss);
            ia >> par;
        }
        ss.str("");
        ss.clear();
        {
            boost::archive::text_oarchive   oa(ss);
            oa << par;
        }
        std::cout << " --- COMPARE --- \n" << ss.str() << "\n";;
    }
}

Prints e.g.

22 serialization::archive 19 0 0 0 0 2 Yo 42

 --- COMPARE ---
22 serialization::archive 19 0 0 0 0 2 Yo 42
  • Related