Last active
March 2, 2016 21:38
-
-
Save daviddoria/e25b1a0104e72e43ba3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(AbstractPoint) | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
target_link_libraries(TestLib boost_serialization-mt) | |
ADD_EXECUTABLE(TestExe PolymorphicRegisterMacros.cpp) | |
target_link_libraries(TestExe TestLib boost_serialization-mt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef LIBRARY_H | |
#define LIBRARY_H | |
void LibraryFunction(); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef POINT_H | |
#define POINT_H | |
#include <boost/serialization/export.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(Point) | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
{ | |
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; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment