0

I have a MyFile.hpp header file which contains various types and enums. How do i do serialization/ desrialization of given example code.

//MyFile.hpp

namespace A { 
   namespace B {

      typedef std::string MyString;
      typedef std::map<std::string,std::string> my_type;
      typedef bool result;

      struct MyTimer
      {
         int time;

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

      enum MODE
      {
          Sleep=1,
          HybridSleep,
          Hybernate
      }

   }
}

I need to do implementation in corresponding MyFile.cpp but don't know how do i go ahead.

Thanks,

sia
  • 1,872
  • 1
  • 23
  • 54
  • 2
    With respect to the actual `typedef` portion of your question, remember that typedefs are just syntactic sugar that makes the programmer's life easier. They're expanded to their actual types before the code is compiled. So a typedef is serializable if the underlying 'real' type is. – aruisdante Mar 20 '14 at 13:12

1 Answers1

3

Maps, strings etc. can just be serialized by including the relevant header:

#include <boost/serialization/map.hpp>
#include <boost/serialization/string.hpp>

The enum counts as a primitive type:

A type T is Serializable if and only if one of the following is true:

  • it is a primitive type.

    By primitive type we mean a C++ built-in type and ONLY a C++ built-in type. Arithmetic (including characters), bool, enum are primitive types. Below in serialization traits, we define a "primitive" implementation level in a different way for a different purpose. This can be a source of confusion.

  • It is a class type and one of the following has been declared according to the prototypes detailed below:
    • a class member function serialize
    • a global function serialize
  • it is a pointer to a Serializable type.
  • it is a reference to a Serializable type.
  • it is a native C++ Array of Serializable type.

For more tricky cases there is BOOST_STRONG_TYPEDEF (see documentation "Serialization Wrappers")

sehe
  • 374,641
  • 47
  • 450
  • 633