how to use boost::serialize::make_binary_object ag

2019-07-27 01:28发布

my intention is to get input from map<int,std::string> and create the binary archive which the below code is failing to do , is this the right way of creating it? And how to know whether the data is correctly archived or not(desirializing using boost) Do i just need to create a boost::archive::binary_iarchive oa(oss,1); and copy it to oss ?

#include <fstream>
#include <boost/serialization/map.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <string>
#include <iostream>
#include <map>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/serialization.hpp>

using namespace std;

class MyConnections
{
  public:
    MyConnections()
    {

       e_group.insert( std::make_pair(1,"ETOTO") ) ;
       e_group.insert( std::make_pair(2,"ETOTO") ) ;

   }

 template<class archive>
void serialize(archive& ar, const unsigned int version)
{
    using boost::serialization::make_binary_object;
    ar &  boost::serialization::make_binary_object(e_group);
}


  public:
   typedef   map<int,std::string> groups;
   groups  e_group;
};

int main(int argc, char* argv[])
{
    const MyConnections conn;
    stringstream oss(ios_base::out|ios_base::binary);
    boost::archive::binary_oarchive oa(oss,1);
    oa << boost::serialization::make_binary_object(conn)
    std::cout<<oss.str;
}

Error

main.cpp: In member function 'void MyConnections::serialize(archive&, unsigned int)':
main.cpp:29:63: error: cannot convert 'MyConnections::groups {aka std::map<int, std::__cxx11::basic_string<char> >}' to 'void*' for argument '1' to 'const boost::serialization::binary_object boost::serialization::make_binary_object(void*, std::size_t)'
         ar &  boost::serialization::make_binary_object(e_group);
                                                               ^
main.cpp: In function 'int main(int, char**)':
main.cpp:42:55: error: cannot convert 'const MyConnections' to 'void*' for argument '1' to 'const boost::serialization::binary_object boost::serialization::make_binary_object(void*, std::size_t)'
    oa << boost::serialization::make_binary_object(conn)
                                                   ^

1条回答
Evening l夕情丶
2楼-- · 2019-07-27 02:11

Std output doesn't support random binary output. You'd probably use hex of base64 encoding.

Here's a simplistic approach writing out hex bytes:

#include <sstream>
#include <iostream>
#include <iomanip>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/map.hpp>

using namespace std;

class MyConnections
{
    public:
        MyConnections()
        {

            e_group.insert( std::make_pair(1,"ETOTO") ) ;
            e_group.insert( std::make_pair(2,"ETOTO") ) ;

        }

        template<class archive>
            void serialize(archive& ar, const unsigned int /*version*/) {
                using boost::serialization::make_nvp;
                ar & make_nvp("Connections", e_group);
            }

    public:
        typedef   map<int,std::string> groups;
        groups  e_group;
};

std::string foo() {
    const MyConnections conn;

    stringstream oss(ios_base::out|ios_base::binary);
    boost::archive::binary_oarchive oa(oss);
    oa << boost::serialization::make_nvp("Connections", conn);
    std::string data = oss.str();
    return oss.str();
}

int main()
{
    std::string const data = foo();

    for (uint8_t ch : data) {
        std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(ch) << " ";
    }
}

Prints:

    00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 05 00 00 00 00 00 00 00 45 54 4f 54 4f 02 00 00 00 05 00 00 00 00 00 00 00 45 54 4f 54 4f 

See it Live On Coliru

NOTE: I think the ,1 parameter to the archive constructor was weird. If you're going to pass flags, use the proper enum values.

查看更多
登录 后发表回答