title: boost serialize polymorphic class
body: <p>With the following example I attempting to learn a few new to me concepts.</p>
  
  <ol>
  <li>abstraction </li>
  <li>polymorphic classes</li>
  <li>factory programming.  </li>
  <li>boost serialization</li>
  </ol>
  
  <p>The nuances of how pointers behave are still something I am working to figure out.</p>
  
  <p>Here is a small program that I have written to show you the issue I am struggling to understand.
  When I unserialize the polymorphic object below I only get an object created from the default constructor.<br>
  TodoFactory::retrieveATodo is not recreating the object from the serialized data.  This is displayed by the output of "unserialzed command" in that function.</p>
  
  <p>Here is the full program:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;bitset&gt;
  #include &lt;boost/serialization/string.hpp&gt;
  #include &lt;sstream&gt;
  #include &lt;boost/archive/text_oarchive.hpp&gt;
  #include &lt;boost/archive/text_iarchive.hpp&gt;
  #include &lt;boost/serialization/export.hpp&gt;
  
  //abstract class
  class aTodo{
  private:
  
     friend class boost::serialization::access;
  
  protected:
     const char _initType;
  
  public:
     aTodo():_initType(0x00){};
  
     aTodo(const char type):_initType(type){};
  
  std::string  oarchive(){
     std::ostringstream archive_stream;
     {
     boost::archive::text_oarchive archive(archive_stream);
     archive &lt;&lt; *this;
     }
  
     archive_stream.flush();
     std::string outbound_data=archive_stream.str();
  
     std::string  foutbound_data;
     foutbound_data=_initType;
     foutbound_data+=outbound_data;
     std::cout &lt;&lt; "length: " &lt;&lt; foutbound_data.length() &lt;&lt; std::endl;
     return foutbound_data;
  }
  
  
     virtual void Do()=0;
     virtual ~aTodo(){};
  
     template&lt;class Archive&gt;
     void serialize(Archive &amp; ar, unsigned int version){
        ar &amp; _initType;
     };
     char getInitType(){return _initType;};
  };
  
  // include headers that implement a archive in simple text format
  class todoExec:public aTodo{
  private:
    friend class boost::serialization::access;
     template&lt;class Archive&gt;
     void serialize(
              Archive&amp; ar,
              unsigned int version
              )
      {
        std::cout &lt;&lt; "serialize todoexec" &lt;&lt; std::endl;
      //base
      boost::serialization::base_object&lt;aTodo&gt;(*this);
  //derived
          ar &amp; _command;
      }
  
    std::string _command;
  protected:
  
  public:
     static const char _TYPE=0x01;
     todoExec():aTodo(_TYPE){};
     todoExec(std::string command):aTodo(_TYPE){_command=command;};
     void Do(){std::cout &lt;&lt; "foo" &lt;&lt; std::endl;};
     virtual ~todoExec(){};
  
     std::string getCommand(){return _command;};
  
  
  };
  
  class todoFactory{
  private:
  
  protected:
  
  
  public:
     std::unique_ptr&lt;aTodo&gt; retrieveAtodo(const std::string &amp; total){
     std::cout &lt;&lt; "here" &lt;&lt; std::endl;
     char type=total.at(0);
     std::cout &lt;&lt; "bitset: " &lt;&lt; std::bitset&lt;8&gt;(type) &lt;&lt; std::endl;
     std::string remainder=total.substr(1);
     if(type==0x01){
        std::cout &lt;&lt; "remainder in retrieve: " &lt;&lt; remainder &lt;&lt; std::endl;
        std::unique_ptr&lt;todoExec&gt; tmp(new todoExec());
        std::stringstream archive_stream(remainder);
        std::cout &lt;&lt; "stream remainder: " &lt;&lt; archive_stream.str() &lt;&lt; std::endl;
     {     
        boost::archive::text_iarchive archive(archive_stream);
        archive &gt;&gt; *tmp;
        }
        std::cout &lt;&lt; "unserialized type: " &lt;&lt; std::bitset&lt;8&gt;(tmp-&gt;getInitType()) &lt;&lt; std::endl;
        std::cout &lt;&lt; "unserialized command: " &lt;&lt; tmp-&gt;getCommand() &lt;&lt; std::endl;
        return std::move(tmp);
     }
     };
  
     std::unique_ptr&lt;aTodo&gt; createAtodo(char type,std::string command){
  
        if(type==0x01){
           std::unique_ptr&lt;todoExec&gt; tmp(new todoExec(command));
           return std::move(tmp);
        }
     };
  
  
  };
  
  int main(){
     char mtype=0x01;
     std::string dataToSend = "ls -al /home/ajonen";
     std::unique_ptr&lt;todoFactory&gt; tmpTodoFactory; //create factory
     std::unique_ptr&lt;aTodo&gt; anExecTodo=tmpTodoFactory-&gt;createAtodo(mtype,dataToSend); //create ExecTodo from factory
     if(auto* m = dynamic_cast&lt;todoExec*&gt;(anExecTodo.get()))
        std::cout &lt;&lt; "command to serialize: " &lt;&lt; m-&gt;getCommand() &lt;&lt; std::endl;
     //archive
     std::string remainder = anExecTodo-&gt;oarchive();
     //now read in results that are sent back
     std::unique_ptr&lt;aTodo&gt; theResult;
     theResult=tmpTodoFactory-&gt;retrieveAtodo(remainder);
     std::cout &lt;&lt; "resultant type: " &lt;&lt; std::bitset&lt;8&gt;(theResult-&gt;getInitType()) &lt;&lt;std::endl;
     if(auto* d = dynamic_cast&lt;todoExec*&gt;(theResult.get()))
        std::cout &lt;&lt; "resultant Command: " &lt;&lt; d-&gt;getCommand() &lt;&lt;std::endl;
  
  
     return 0;
  }
  </code></pre>
  
  <p>And here is the program output:</p>
  
  <pre><code>command to serialize: ls -al /home/ajonen
  length: 36
  here
  bitset: 00000001
  remainder in retrieve: 22 serialization::archive 12 0 0 1
  
  stream remainder: 22 serialization::archive 12 0 0 1
  
  serialize todoexec
  unserialized type: 00000001
  unserialized command: 
  resultant type: 00000001
  resultant Command: 
  </code></pre>
  
  <p>I also found out that the serialize method is only being called for the base class aTodo.  I am going to need to find a way to make that virtual, but it is a template function.  That is problem number one.</p>