Skip to content

Instantly share code, notes, and snippets.

@daviddoria
Created March 2, 2016 21:36
Show Gist options
  • Save daviddoria/94277465f43f37f8d91d to your computer and use it in GitHub Desktop.
Save daviddoria/94277465f43f37f8d91d to your computer and use it in GitHub Desktop.
#include "AbstractPoint.h"
BOOST_CLASS_EXPORT_IMPLEMENT(AbstractPoint)
#ifndef ABSTRACT_POINT_H
#define ABSTRACT_POINT_H
#include <boost/serialization/export.hpp>
class AbstractPoint
{
public:
virtual ~AbstractPoint(){}
virtual void DoSomething() = 0;
// This is required if we want to serialize an AbstractPoint pointer
template<class TArchive>
void serialize(TArchive&, const unsigned int /*version*/)
{
// do nothing
}
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT(AbstractPoint)
BOOST_CLASS_EXPORT_KEY(AbstractPoint)
#endif
cmake_minimum_required(VERSION 2.6)
Project(PolymorphicRegisterMacros)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
set(Boost_USE_MULTITHREADED ON) # which is the default
FIND_PACKAGE(Boost COMPONENTS serialization iostreams required)
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS})
add_custom_target(projectHeaders SOURCES AbstractPoint.h Point.h Library.h)
add_library(TestLib Library.cpp AbstractPoint.cpp Point.cpp)
target_link_libraries(TestLib boost_serialization-mt)
ADD_EXECUTABLE(TestExe PolymorphicRegisterMacros.cpp)
target_link_libraries(TestExe TestLib boost_serialization-mt)
#include "Library.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/export.hpp>
#include <fstream>
#include "Point.h"
void LibraryFunction()
{
{
std::shared_ptr<AbstractPoint> point(new Point(7.4));
std::ofstream outputStream("test.txt");
boost::archive::text_oarchive outputArchive(outputStream);
outputArchive << point;
outputStream.close();
}
{
std::shared_ptr<AbstractPoint> pointRead;
std::ifstream inputStream("test.txt");
boost::archive::text_iarchive inputArchive(inputStream);
inputArchive >> pointRead;
if (auto p = std::dynamic_pointer_cast<Point>(pointRead))
std::cout << p->mData << std::endl;
}
}
#ifndef LIBRARY_H
#define LIBRARY_H
void LibraryFunction();
#endif
#ifndef POINT_H
#define POINT_H
#include <boost/serialization/export.hpp>
#include <boost/serialization/base_object.hpp>
#include "AbstractPoint.h"
class Point : public AbstractPoint
{
public:
Point() = default;
Point(const double data) : mData(data) {}
void DoSomething(){}
template<class TArchive>
void serialize(TArchive& archive, const unsigned int /*version*/)
{
archive & boost::serialization::base_object<AbstractPoint>(*this)
& mData;
}
double mData;
};
BOOST_CLASS_EXPORT_KEY(Point)
#endif
#ifndef POINT_H
#define POINT_H
#include <boost/serialization/export.hpp>
#include <boost/serialization/base_object.hpp>
#include "AbstractPoint.h"
class Point : public AbstractPoint
{
public:
Point() = default;
Point(const double data) : mData(data) {}
void DoSomething(){}
template<class TArchive>
void serialize(TArchive& archive, const unsigned int /*version*/)
{
archive & boost::serialization::base_object<AbstractPoint>(*this)
& mData;
}
double mData;
};
BOOST_CLASS_EXPORT_KEY(Point)
#endif
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/export.hpp>
#include <fstream>
#include "Library.h"
#include "Point.h"
int main()
{
LibraryFunction();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment