c++ - how to use boost::serialize::make_binary_object against a map -


my intention input map<int,std::string> , create binary archive below code failing , right way of creating it? , how know whether data correctly archived or not(desirializing using boost) need create boost::archive::binary_iarchive oa(oss,1); , copy 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> >}' 'void*' argument '1' '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' 'void*' argument '1' 'const boost::serialization::binary_object boost::serialization::make_binary_object(void*, std::size_t)'     oa << boost::serialization::make_binary_object(conn)                                                    ^ 

std output doesn't support random binary output. you'd use hex of base64 encoding.

here's 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();      (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 live on coliru

note: think ,1 parameter archive constructor weird. if you're going pass flags, use proper enum values.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -