vendredi 13 septembre 2019

Not able to serialize data properly using boost

I am very new to Serialization and i am trying to use boost to serialize my data.

This is dummy code of what i intend to do.

I am using Geometry class as interface for other classes like Sphere , Rectangle etc.

The issue i have is this line "c_Restored->PrintContainer();" should call the Sphere class Virtual print function but it calls Geometry class print function which is the Parent of Sphere class.

#include "pch.h"
#include <iostream>
#include<fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "Container.h"
#include "Geometry.h"
#include "Sphere.h"

int main()
{
    const char* fileName = "saved.txt";
    Geometry* gg = new Sphere;
    Container *c = new Container("My Container", gg); // I have created a sphere object
    // save data
    {
        // Create an output archive
        std::ofstream ofs(fileName);
        boost::archive::text_oarchive ar(ofs);
        ar & c ;            
    }
    Container* c_Restored;  
    //load data
    {
        // create an input stream
        std::ifstream ifs(fileName);
        boost::archive::text_iarchive ar(ifs);
        ar & c_Restored;
    }

    Sphere* g1 = (Sphere*)c_Restored->getGeomtry();
    c_Restored->PrintContainer();
    g1->PrintGeom();  // This should call the Sphere PrintGeom function ???
    do
    {
        std::cout << '\n' << "Press a key to continue...";
    } while (std::cin.get() != '\n');
}

This is container class.

#pragma once

#include <boost/serialization/base_object.hpp>
#include <boost/serialization/split_member.hpp>
#include "Geometry.h"
class Container
{
private:
    std::string stdstrCont;
    Geometry* geom;

public:
    Container() : stdstrCont() { geom = new Geometry; }


    Container( std::string str , Geometry* geometry) : stdstrCont(str )
    { 
        geom = new Geometry;  // If i try to create a new Sphere instead of Geometry i get error
       *geom = *geometry;   
    }


    ~Container()
    {
        if (geom != nullptr)
            delete geom;
    }

    Geometry* getGeomtry()
    {
        return geom;
    }

    void PrintContainer()
    {
        std::cout << stdstrCont;
    }

private:

    friend class boost::serialization::access;

    template <typename Archive>
    void serialize(Archive& ar, const unsigned version) {
        ar & stdstrCont & geom;
    }

};

This is Geometry Class

#pragma once

#include <string>

class Geometry
{
private:
    std::string stdstringGeom;

public:
     virtual void  PrintGeom()
    {
        std::cout << "geometry virtual function";
    }

private:

    friend class boost::serialization::access;


    template <typename Archive>
    void serialize(Archive& ar, const unsigned version) {
        ar & stdstringGeom;
    }
};

This is sphere class.

include "Geometry.h"

class Sphere : public Geometry
{
private:
    std::string stdstrSphere;


public:
    Sphere() : stdstrSphere( "DefaultSphere"){}
    Sphere( std::string str) : stdstrSphere(str) {}
    void PrintGeom()
    {
        std::cout << "Sphere Virtual Function" << std::endl;
    }

private:
    typedef Geometry _Super;
    friend class boost::serialization::access;


    template <typename Archive>
    void serialize(Archive& ar, const unsigned version) {
        ar & boost::serialization::base_object<_Super>(*this);
        ar & stdstrSphere;
    }

};

Aucun commentaire:

Enregistrer un commentaire