2014-04-11 1 views
3

부스터 역 직렬화를 위해/시작하는 방법을 잘 모르겠습니다. 다음은 부스트 ​​deserialization 코드가 어떻게 될 것인지 알고 싶어하는 샘플 코드입니다.부스트에서 비 직렬화 예제를 찾으십시오.

#include <boost/serialization/access.hpp> 
#include <boost/serialization/string.hpp> 
#include <boost/serialization/shared_ptr.hpp> 
#include <boost/archive/text_oarchive.hpp> 
#include <boost/smart_ptr/make_shared.hpp> 

namespace mydata 
{ 
    struct MyInfo 
    { 
     std::string info = "extra info"; 

     MyInfo(std::string info) : info(std::move(info)) {} 

     friend class boost::serialization::access; 
     template<class Archive> 
      void serialize(Archive &ar, const unsigned int /*version*/) 
      { 
       ar & info; 
      } 
    }; 

    struct MyData 
    { 
     std::string name; 
     std::string type; 
     boost::shared_ptr<MyInfo> myref; 

     private: 
     friend class boost::serialization::access; 
     template<class Archive> 
      void serialize(Archive &ar, const unsigned int /*version*/) 
      { 
       ar & name; 
       ar & type; 
       ar & myref; 
      } 
    }; 
} 



int main() 
    { 

     using namespace mydata; 
     MyData data { "this is a name", "this is a type", boost::make_shared<MyInfo>("this is info") }; 

     boost::archive::text_oarchive oa(std::cout); 
     oa << data; 
    } 

도 직렬화와 유사한 링크하지만 Boost deserialize a derived class to base class pointer에 명확하지 않습니다.

정말 많이 모르겠다. 도움이된다면, 부스터 역 직렬화에 대한 링크도있다.

답변

4

당신은 거의 있었다 :

int main() 
{ 
    using namespace mydata; 
    MyData data { "this is a name", "this is a type", boost::make_shared<MyInfo>("this is info") }; 

    std::ostringstream oss; 
    { 
     boost::archive::text_oarchive oa(oss); 
     oa << data; 
    } 

    std::istringstream iss(oss.str()); 
    { 
     boost::archive::text_iarchive ia(iss); 
     ia >> data; 
    } 
} 

사실, 당신은 입력과 출력 모두 std::stringstream을 사용할 수 있지만 순도 내가 (중복 복사를 수행하는) 대칭 접근 방식을 보여 주었다.

당신은

#include <sstream> 
#include <boost/archive/text_iarchive.hpp> 

이 필요합니다 및 역 직렬화에 대한 귀하의 클래스는 defaultconstructible 할 필요가 :

MyInfo(std::string info = "") : info(std::move(info)) {} 

(관련이없는 경고 : 할 하지 사용하는이 컴파일러를 유발하기 때문에 여기 std::string info = {} MSVC의 버그)

다음은 전체입니다. 역 직렬화 된 오브젝트가 동일한 데이터를 가지고 있음을 나타내고, Y 작업 샘플 : Live On Coliru

#include <boost/serialization/access.hpp> 
#include <boost/serialization/string.hpp> 
#include <boost/serialization/shared_ptr.hpp> 
#include <boost/archive/text_oarchive.hpp> 
#include <boost/smart_ptr/make_shared.hpp> 

#include <sstream> 
#include <boost/archive/text_iarchive.hpp> 

namespace mydata 
{ 
    struct MyInfo 
    { 
     std::string info = "extra info"; 

     MyInfo(std::string info = "") : info(std::move(info)) {} 

     friend class boost::serialization::access; 
     template<class Archive> 
      void serialize(Archive &ar, const unsigned int /*version*/) 
      { 
       ar & info; 
      } 
    }; 

    struct MyData 
    { 
     std::string name; 
     std::string type; 
     boost::shared_ptr<MyInfo> myref; 

     private: 
     friend class boost::serialization::access; 
     template<class Archive> 
      void serialize(Archive &ar, const unsigned int /*version*/) 
      { 
       ar & name; 
       ar & type; 
       ar & myref; 
      } 
    }; 
} 

int main() 
{ 
    using namespace mydata; 

    std::ostringstream oss; 
    { 
     MyData data { "this is a name", "this is a type", boost::make_shared<MyInfo>("this is info") }; 

     boost::archive::text_oarchive oa(oss); 
     oa << data; 
    } 

    MyData cloned; 
    std::istringstream iss(oss.str()); 
    { 
     boost::archive::text_iarchive ia(iss); 
     ia >> cloned; 
    } 

    // check equality 
    { 
     std::ostringstream oss2; 

     boost::archive::text_oarchive oa(oss2); 
     oa << cloned; 

     std::cout << oss.str() << "\n"; 
     std::cout << oss2.str() << "\n"; 
    } 
} 

출력 :

22 serialization::archive 10 0 0 14 this is a name 14 this is a type 0 1 2 1 0 
0 12 this is info 

22 serialization::archive 10 0 0 14 this is a name 14 this is a type 0 1 2 1 0 
0 12 this is info 
+0

감사 Sehe. 그래서 MyData 구조체에 대한 deserialize 템플릿을 추가 할 필요가 없습니다. 위의 코드에 나와있는 방식으로 시도해 보겠습니다. – sia

+0

나는 완전히 작동하는 샘플을 추가했습니다 (http://coliru.stacked-crooked.com/a/f07cc68f6a457a7d). 'serialize'는 (de) serialization을 결합합니다 (그래서'const' 멤버가 아닙니다). 어떤 타입은 직렬화 해제 함수를 분할 할 필요가있다. [여기 (할당이있는 배열)] (http://stackoverflow.com/a/20599449/85371) 및 [여기 (유리수)] (http://stackoverflow.com/questions/22563255/how-to-serialize- boostrational/22564432 # 22564432) – sehe

+0

많이 고마워 .. 내가 코드 – sia