Learn » Examples » Polymorphic Serialization Example w/Macros

The full code for this magistrate example can be found here: examples/checkpoint_example_polymorphic_macro.cc

#include <checkpoint/checkpoint.h>
#include "checkpoint/dispatch/dispatch_virtual.h"

namespace checkpoint { namespace intrusive { namespace examples {

// \struct Abstract base class
struct MyBase {

  MyBase() { printf("MyBase cons\n"); }
  explicit MyBase(::magistrate::SERIALIZE_CONSTRUCT_TAG) { printf("MyBase recons\n"); }

  virtual ~MyBase() = default;

  // Add serializing macro
  magistrate_virtual_serialize_root()

  int val_ = 0;

  template <typename S>
  void serialize(S& s) {
    s | val_;
    printf("MyBase: serialize val %d\n", val_);
  }

  virtual void test() = 0;
};

struct MyObj : public MyBase {

  explicit MyObj(int val) : MyBase() { printf("MyObj cons\n"); val_ = val;}
  explicit MyObj(::magistrate::SERIALIZE_CONSTRUCT_TAG) {}

  // Add macro for serialization
  magistrate_virtual_serialize_derived_from(MyBase)

  template <typename SerializerT>
  void serialize(SerializerT&) {
    printf("MyObj: serialize\n");
  }

  void test() override {
    printf("test MyObj 10 == %d ?\n", val_);
    assert(val_ == 10);
  }
};

struct MyObj2 : public MyBase {
  explicit MyObj2(int val) { printf("MyObj2 cons\n"); val_=val; }
  explicit MyObj2(::magistrate::SERIALIZE_CONSTRUCT_TAG) {}

  // Add macro for serialization
  magistrate_virtual_serialize_derived_from(MyBase)

  template <typename SerializerT>
  void serialize(SerializerT&) {
    printf("MyObj2: serialize\n");
  }
  void test() override {
    printf("test MyObj2 20 == %d ?\n", val_);
    assert(val_ == 20);
  }
};

struct MyObj3 : public MyBase {

  int a=0, b=0, c=0;

  explicit MyObj3(int val) { printf("MyObj3 cons\n"); a= 10; b=20; c=100; val_=val;}
  explicit MyObj3(::magistrate::SERIALIZE_CONSTRUCT_TAG) {}

  // Add macro for serialization
  magistrate_virtual_serialize_derived_from(MyBase)

  template <typename SerializerT>
  void serialize(SerializerT& s) {
    s|a|b|c;
    printf("MyObj3: serialize a b c %d %d %d\n", a, b, c);
  }
  void test() override {
    printf("val_ 30  a 10 b 20 c 100 = %d %d %d %d\n", val_, a, b, c);
    assert(val_ == 30);
    assert(a==10);
    assert(b==20);
    assert(c==100);
  }
};

/*
 * Example vector that holds a vector of unique_ptr to MyBase
 */

struct ExampleVector {

  template <typename SerializerT>
  void serialize(SerializerT& s) {
    s | vec;
  }

  std::vector<std::unique_ptr<MyBase>> vec;
};

void test() {

  ExampleVector v;
  v.vec.push_back(std::make_unique<MyObj3>(30));
  v.vec.push_back(std::make_unique<MyObj2>(20));
  v.vec.push_back(std::make_unique<MyObj>(10));

  auto ret = magistrate::serialize(v);

  {
    // Display information about serialization result
    auto const& buf = ret->getBuffer();
    auto const& buf_size = ret->getSize();
    printf("ptr=%p, size=%ld\n*****\n\n", static_cast<void*>(buf), buf_size);
  }

  auto t = magistrate::deserialize<ExampleVector>(ret->getBuffer());

  for (auto&& elm : t->vec) {
    elm->test();
  }
}

}}} // end namespace checkpoint::intrusive::examples

int main(int, char**) {
  using namespace magistrate::intrusive::examples;

  test();

  return 0;
}