Program Example 3
The full code for this magistrate example can be found here: examples/checkpoint_example_3.cc
Example source code:
#include <checkpoint/checkpoint.h> #include <cstdio> // // This example illustrates how checkpoint uses traits to determine a // reconstruction strategy and shows several mechanisms for reconstructing a // class of a serializable/deserializable type. // namespace checkpoint { namespace intrusive { namespace examples { // \brief Structure with a variable of built-in type. // // \note This structure is a serializable / deserializable type. It has a // default constructor and a `serialize` function. struct TestDefaultCons { int a = 29; TestDefaultCons() = default; template <typename Serializer> void serialize(Serializer& s) { s | a; } }; // \brief Structure with a variable of built-in type. // // \note This structure is a byte-serializable/deserializable type but doesn't // contain any traits to indicate that making it not serializable. // struct TestNoSerialize { int a = 29; }; // \brief Structure with a variable of built-in type. // // \note This structure is not a serializable / deserializable type. The // structure has an explicitly deleted default constructor and a constructor // that takes an `int` parameter. Serialization has no way to construct the // structure. struct TestShouldFailReconstruct { int a = 29; TestShouldFailReconstruct(int const) { } TestShouldFailReconstruct() = delete; template <typename Serializer> void serialize(Serializer& s) { s | a; } }; // \brief Structure with a variable of built-in type. // // \note This structure is a serializable / deserializable type. The structure // has an explicitly deleted default constructor and a constructor that takes an // `int` parameter. However, the structure provides a static reconstruct // method that serialization can use to construct the structure. struct TestReconstruct { int a = 29; TestReconstruct(int const) { } TestReconstruct() = delete; static TestReconstruct& reconstruct(void* buf) { auto a = new (buf) TestReconstruct(100); return *a; } template <typename Serializer> void serialize(Serializer& s) { s | a; } }; }}} // end namespace checkpoint::intrusive::examples #include "checkpoint/traits/serializable_traits.h" namespace checkpoint { using namespace intrusive::examples; static_assert( SerializableTraits<TestDefaultCons>::is_serializable, "Should be serializable" ); static_assert( ! SerializableTraits<TestNoSerialize>::is_serializable, "Should not be serializable" ); static_assert( ! SerializableTraits<TestShouldFailReconstruct>::is_serializable, "Should not be serializable" ); static_assert( SerializableTraits<TestReconstruct>::is_serializable, "Should be serializable" ); } // end namespace checkpoint int main(int, char**) { // Example is a compile-time test of serializability traits return 0; }