From 24998602ad6dd59c65dbd0e8a8506b9818436a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Skrzy=C5=84ski?= Date: Wed, 20 Nov 2024 12:43:58 +0100 Subject: [PATCH] #376: add magistrate_virtual_serialize macros --- docs/shared/checkpoint_learn_serialize.md | 22 ++++++++-------- .../checkpoint_example_polymorphic_macro.cc | 8 +++--- ..._example_polymorphic_macro_nonintrusive.cc | 8 +++--- src/checkpoint/dispatch/dispatch_virtual.h | 4 +-- src/checkpoint/dispatch/vrt/base.h | 26 ++++++++++++++++--- src/checkpoint/dispatch/vrt/derived.h | 26 ++++++++++++++++--- .../dispatch/vrt/inheritance_assert_helpers.h | 2 +- tests/unit/test_footprinter.cc | 4 +-- .../unit/test_serialization_error_checking.cc | 4 +-- tests/unit/test_virtual_serialize.cc | 20 +++++++------- 10 files changed, 82 insertions(+), 42 deletions(-) diff --git a/docs/shared/checkpoint_learn_serialize.md b/docs/shared/checkpoint_learn_serialize.md index 24fcbda5..a1410fc8 100644 --- a/docs/shared/checkpoint_learn_serialize.md +++ b/docs/shared/checkpoint_learn_serialize.md @@ -2,7 +2,7 @@ *Serialization* is the process of recursively traversing C++ objects into a simple format that can be stored or transmitted and reconstructed later. -*checkpoint* translates the object into a set of contiguous bits and provides +*magistrate* translates the object into a set of contiguous bits and provides the steps to reverse the process, i.e. to reconstitute the object from the set of bits. @@ -55,15 +55,15 @@ to actually perform the serialization. \subsection reconstruct_class Class reconstruction -There are several ways to allow *checkpoint* to reconstruct a -class. *checkpoint* will try to detect a reconstruction strategy in the +There are several ways to allow *magistrate* to reconstruct a +class. *magistrate* will try to detect a reconstruction strategy in the following resolution order: - 1. Tagged constructor: `MyClass(checkpoint::SERIALIZE_CONSTRUCT_TAG) {}` + 1. Tagged constructor: `MyClass(magistrate::SERIALIZE_CONSTRUCT_TAG) {}` 1. Reconstruction `MyClass::reconstruct(buf)` or `reconstruct(MyClass, buf)` 1. Default constructor: `MyClass()` -If no reconstruct strategy is detected with type traits, *checkpoint* will fail -at compile-time with a static assertion indicating that *checkpoint* can't +If no reconstruct strategy is detected with type traits, *magistrate* will fail +at compile-time with a static assertion indicating that *magistrate* can't reconstruct the class. The example in \ref ckpt_learn_ex1 illustrates the reconstruct method. @@ -82,11 +82,11 @@ serialization of the class state. To serialize polymorphic class hierarchies, one must write serializers for each class in the hierarchy. Then, the user should either insert macros -`checkpoint_virtual_serialize_root()` and -`checkpoint_virtual_serialize_derived_from(T)` to inform *checkpoint* of the +`magistrate_virtual_serialize_root()` and +`magistrate_virtual_serialize_derived_from(T)` to inform *magistrate* of the hierarchy so it can automatically traverse the hierarchy. Alternatively, the -user may use the inheritance wrappers `checkpoint::SerializableBase` and -`checkpoint::SerializableDerived` to achieve the same effect. +user may use the inheritance wrappers `magistrate::SerializableBase` and +`magistrate::SerializableDerived` to achieve the same effect. The example in \ref ckpt_learn_ex6_polymorphic_macro illustrates the approach uses the macros. The example in \ref ckpt_learn_example_polymorphic @@ -99,6 +99,6 @@ illustrates this approach. type. - If one has a raw pointer, `Teuchos::RCP`, or `std::shared_ptr`, - `checkpoint::allocateConstructForPointer(s, ptr)` can be + `magistrate::allocateConstructForPointer(s, ptr)` can be invoked to properly allocate and construct the concrete class depending on runtime type. diff --git a/examples/checkpoint_example_polymorphic_macro.cc b/examples/checkpoint_example_polymorphic_macro.cc index 8f66ca1e..0270052e 100644 --- a/examples/checkpoint_example_polymorphic_macro.cc +++ b/examples/checkpoint_example_polymorphic_macro.cc @@ -57,7 +57,7 @@ struct MyBase { virtual ~MyBase() = default; // Add serializing macro - checkpoint_virtual_serialize_root() + magistrate_virtual_serialize_root() int val_ = 0; @@ -76,7 +76,7 @@ struct MyObj : public MyBase { explicit MyObj(::magistrate::SERIALIZE_CONSTRUCT_TAG) {} // Add macro for serialization - checkpoint_virtual_serialize_derived_from(MyBase) + magistrate_virtual_serialize_derived_from(MyBase) template void serialize(SerializerT&) { @@ -94,7 +94,7 @@ struct MyObj2 : public MyBase { explicit MyObj2(::magistrate::SERIALIZE_CONSTRUCT_TAG) {} // Add macro for serialization - checkpoint_virtual_serialize_derived_from(MyBase) + magistrate_virtual_serialize_derived_from(MyBase) template void serialize(SerializerT&) { @@ -114,7 +114,7 @@ struct MyObj3 : public MyBase { explicit MyObj3(::magistrate::SERIALIZE_CONSTRUCT_TAG) {} // Add macro for serialization - checkpoint_virtual_serialize_derived_from(MyBase) + magistrate_virtual_serialize_derived_from(MyBase) template void serialize(SerializerT& s) { diff --git a/examples/checkpoint_example_polymorphic_macro_nonintrusive.cc b/examples/checkpoint_example_polymorphic_macro_nonintrusive.cc index f4e12899..32b4a5d5 100644 --- a/examples/checkpoint_example_polymorphic_macro_nonintrusive.cc +++ b/examples/checkpoint_example_polymorphic_macro_nonintrusive.cc @@ -58,7 +58,7 @@ struct MyBase { virtual ~MyBase() = default; // Add serializing macro - checkpoint_virtual_serialize_root() + magistrate_virtual_serialize_root() int val_ = 0; @@ -71,7 +71,7 @@ struct MyObj : public MyBase { explicit MyObj(::checkpoint::SERIALIZE_CONSTRUCT_TAG) {} // Add macro for serialization - checkpoint_virtual_serialize_derived_from(MyBase) + magistrate_virtual_serialize_derived_from(MyBase) void test() override { printf("test MyObj 10 == %d ?\n", val_); @@ -84,7 +84,7 @@ struct MyObj2 : public MyBase { explicit MyObj2(::checkpoint::SERIALIZE_CONSTRUCT_TAG) {} // Add macro for serialization - checkpoint_virtual_serialize_derived_from(MyBase) + magistrate_virtual_serialize_derived_from(MyBase) void test() override { printf("test MyObj2 20 == %d ?\n", val_); @@ -100,7 +100,7 @@ struct MyObj3 : public MyBase { explicit MyObj3(::checkpoint::SERIALIZE_CONSTRUCT_TAG) {} // Add macro for serialization - checkpoint_virtual_serialize_derived_from(MyBase) + magistrate_virtual_serialize_derived_from(MyBase) void test() override { printf("val_ 30 a 10 b 20 c 100 = %d %d %d %d\n", val_, a, b, c); diff --git a/src/checkpoint/dispatch/dispatch_virtual.h b/src/checkpoint/dispatch/dispatch_virtual.h index 03f12c80..f2b7bad4 100644 --- a/src/checkpoint/dispatch/dispatch_virtual.h +++ b/src/checkpoint/dispatch/dispatch_virtual.h @@ -68,8 +68,8 @@ * * - Insert checkpoint macros in your virtual class hierarchy for derived and * base classes with the corresponding macros: - * - \c checkpoint_virtual_serialize_root() - * - \c checkpoint_virtual_serialize_derived_from(ParentT) + * - \c magistrate_virtual_serialize_root() + * - \c magistrate_virtual_serialize_derived_from(ParentT) * * --------------------------- Invocation ------------------------------------ * diff --git a/src/checkpoint/dispatch/vrt/base.h b/src/checkpoint/dispatch/vrt/base.h index 9e872ba6..1315844f 100644 --- a/src/checkpoint/dispatch/vrt/base.h +++ b/src/checkpoint/dispatch/vrt/base.h @@ -50,7 +50,17 @@ #include "checkpoint/dispatch/vrt/inheritance_assert_helpers.h" #include "checkpoint/dispatch/vrt/serialize_instantiator.h" -#define checkpoint_virtual_serialize_root() \ +[[deprecated("checkpoint_virtual_serialize_root is deprecated," + " please use magistrate_virtual_serialize_root")]] +constexpr bool checkpoint_virtual_serialize_root_is_deprecated() { + return true; +} + +#define checkpoint_virtual_serialize_root \ + static_assert(checkpoint_virtual_serialize_root_is_deprecated()); \ + magistrate_virtual_serialize_root + +#define magistrate_virtual_serialize_root() \ auto _CheckpointVSBaseTypeFn() -> decltype(auto) { return this; } \ virtual void _checkpointDynamicSerialize( \ void* s, \ @@ -74,7 +84,17 @@ return ::checkpoint::dispatch::vrt::objregistry::makeObjIdx<_CheckpointBaseType>(); \ } -#define checkpoint_virtual_serialize_base(BASE) checkpoint_virtual_serialize_root() +[[deprecated("checkpoint_virtual_serialize_base is deprecated," + " please use magistrate_virtual_serialize_base")]] +constexpr bool checkpoint_virtual_serialize_base_is_deprecated() { + return true; +} + +#define checkpoint_virtual_serialize_base \ + static_assert(checkpoint_virtual_serialize_base_is_deprecated()); \ + magistrate_virtual_serialize_base + +#define magistrate_virtual_serialize_base(BASE) magistrate_virtual_serialize_root() namespace checkpoint { namespace dispatch { namespace vrt { @@ -86,7 +106,7 @@ namespace checkpoint { namespace dispatch { namespace vrt { */ template struct SerializableBase { - checkpoint_virtual_serialize_root() + magistrate_virtual_serialize_root() virtual ~SerializableBase() {} }; diff --git a/src/checkpoint/dispatch/vrt/derived.h b/src/checkpoint/dispatch/vrt/derived.h index 8addb0fc..fa2ce302 100644 --- a/src/checkpoint/dispatch/vrt/derived.h +++ b/src/checkpoint/dispatch/vrt/derived.h @@ -50,7 +50,17 @@ #include "checkpoint/dispatch/vrt/inheritance_assert_helpers.h" #include "checkpoint/dispatch/vrt/serialize_instantiator.h" -#define checkpoint_virtual_serialize_derived_from(PARENT) \ +[[deprecated("checkpoint_virtual_serialize_derived_from is deprecated," + " please use magistrate_virtual_serialize_derived_from")]] +constexpr bool checkpoint_virtual_serialize_derived_from_is_deprecated() { + return true; +} + +#define checkpoint_virtual_serialize_derived_from \ + static_assert(checkpoint_virtual_serialize_derived_from_is_deprecated()); \ + magistrate_virtual_serialize_derived_from + +#define magistrate_virtual_serialize_derived_from(PARENT) \ void _checkpointDynamicSerialize( \ void* s, \ ::checkpoint::dispatch::vrt::TypeIdx base_ser_idx, \ @@ -85,7 +95,17 @@ return ::checkpoint::dispatch::vrt::objregistry::makeObjIdx<_CheckpointDerivedType>(); \ } -#define checkpoint_virtual_serialize_derived(DERIVED, PARENT) checkpoint_virtual_serialize_derived_from(PARENT) +[[deprecated("checkpoint_virtual_serialize_derived is deprecated," + " please use magistrate_virtual_serialize_derived")]] +constexpr bool checkpoint_virtual_serialize_derived_is_deprecated() { + return true; +} + +#define checkpoint_virtual_serialize_derived \ + static_assert(checkpoint_virtual_serialize_derived_is_deprecated()); \ + magistrate_virtual_serialize_derived + +#define magistrate_virtual_serialize_derived(DERIVED, PARENT) magistrate_virtual_serialize_derived_from(PARENT) namespace checkpoint { namespace dispatch { namespace vrt { @@ -124,7 +144,7 @@ struct SerializableDerived : BaseT { SerializableDerived() = default; - checkpoint_virtual_serialize_derived(DerivedT, BaseT) + magistrate_virtual_serialize_derived(DerivedT, BaseT) }; }}} /* end namespace checkpoint::dispatch::vrt */ diff --git a/src/checkpoint/dispatch/vrt/inheritance_assert_helpers.h b/src/checkpoint/dispatch/vrt/inheritance_assert_helpers.h index fb0b1e18..ebac715b 100644 --- a/src/checkpoint/dispatch/vrt/inheritance_assert_helpers.h +++ b/src/checkpoint/dispatch/vrt/inheritance_assert_helpers.h @@ -60,7 +60,7 @@ inline void assertTypeIdxMatch(TypeIdx const expected_idx) { "\" does not matched expected value. " "You are probably missing a SerializableBase or SerializableDerived " "in the virtual class hierarchy; or, if you are using macros: " - "checkpoint_virtual_serialize_root() or checkpoint_virtual_serialize_derived_from(..)"; + "magistrate_virtual_serialize_root() or magistrate_virtual_serialize_derived_from(..)"; checkpointAssert( obj_idx == expected_idx or expected_idx == no_type_idx, debug_str.c_str() ); diff --git a/tests/unit/test_footprinter.cc b/tests/unit/test_footprinter.cc index f3f12ea3..ee953abe 100644 --- a/tests/unit/test_footprinter.cc +++ b/tests/unit/test_footprinter.cc @@ -83,7 +83,7 @@ void serialize(Serializer& s, Test2 t) { struct TestBase { - checkpoint_virtual_serialize_root() + magistrate_virtual_serialize_root() virtual ~TestBase() = default; @@ -100,7 +100,7 @@ struct TestDerived2 : TestBase { explicit TestDerived2(int) {} explicit TestDerived2(SERIALIZE_CONSTRUCT_TAG) {} - checkpoint_virtual_serialize_derived_from(TestBase) + magistrate_virtual_serialize_derived_from(TestBase) template < typename SerializerT, diff --git a/tests/unit/test_serialization_error_checking.cc b/tests/unit/test_serialization_error_checking.cc index cb28418e..b17aa74b 100644 --- a/tests/unit/test_serialization_error_checking.cc +++ b/tests/unit/test_serialization_error_checking.cc @@ -115,7 +115,7 @@ TEST_F(TestObject, test_serialization_error_checking) { } struct Base { - checkpoint_virtual_serialize_root() + magistrate_virtual_serialize_root() virtual ~Base() = default; @@ -128,7 +128,7 @@ struct Base { }; struct Derived : public Base { - checkpoint_virtual_serialize_derived_from(Base) + magistrate_virtual_serialize_derived_from(Base) int dd{4}; diff --git a/tests/unit/test_virtual_serialize.cc b/tests/unit/test_virtual_serialize.cc index 85df6c60..ecd53337 100644 --- a/tests/unit/test_virtual_serialize.cc +++ b/tests/unit/test_virtual_serialize.cc @@ -215,7 +215,7 @@ struct TestBase { explicit TestBase(TEST_CONSTRUCT) { init(); } explicit TestBase(SERIALIZE_CONSTRUCT_TAG) {} - checkpoint_virtual_serialize_root() + magistrate_virtual_serialize_root() virtual ~TestBase() = default; @@ -245,7 +245,7 @@ struct TestDerived1 : TestBase { explicit TestDerived1(TEST_CONSTRUCT tag) : Parent(tag) { init(); } explicit TestDerived1(SERIALIZE_CONSTRUCT_TAG tag) : Parent(tag) {} - checkpoint_virtual_serialize_derived_from(TestBase) + magistrate_virtual_serialize_derived_from(TestBase) template void serialize(SerializerT& s) { @@ -278,7 +278,7 @@ struct TestDerived2 : TestBase { explicit TestDerived2(TEST_CONSTRUCT tag) : Parent(tag) { init(); } explicit TestDerived2(SERIALIZE_CONSTRUCT_TAG tag) : Parent(tag) {} - checkpoint_virtual_serialize_derived_from(TestBase) + magistrate_virtual_serialize_derived_from(TestBase) template void serialize(SerializerT& s) { @@ -312,7 +312,7 @@ struct TestDerived3 : TestDerived2 { explicit TestDerived3(TEST_CONSTRUCT tag) : Parent(tag) { init(); } explicit TestDerived3(SERIALIZE_CONSTRUCT_TAG tag) : Parent(tag) {} - checkpoint_virtual_serialize_derived_from(TestDerived2) + magistrate_virtual_serialize_derived_from(TestDerived2) template void serialize(SerializerT& s) { @@ -424,7 +424,7 @@ struct TestBase { explicit TestBase(TEST_CONSTRUCT) { init(); } explicit TestBase(SERIALIZE_CONSTRUCT_TAG) {} - checkpoint_virtual_serialize_root() + magistrate_virtual_serialize_root() virtual ~TestBase() = default; @@ -454,7 +454,7 @@ struct TestDerived1 : TestBase { explicit TestDerived1(TEST_CONSTRUCT tag) : Parent(tag) { init(); } explicit TestDerived1(SERIALIZE_CONSTRUCT_TAG tag) : Parent(tag) {} - checkpoint_virtual_serialize_derived_from(Parent) + magistrate_virtual_serialize_derived_from(Parent) template void serialize(SerializerT& s) { @@ -499,7 +499,7 @@ struct TestDerived2 : TestBase { explicit TestDerived2(TEST_CONSTRUCT tag) : Parent(tag) { init(); } explicit TestDerived2(SERIALIZE_CONSTRUCT_TAG tag) : Parent(tag) {} - checkpoint_virtual_serialize_derived_from(Parent) + magistrate_virtual_serialize_derived_from(Parent) template void serialize(SerializerT& s) { @@ -620,7 +620,7 @@ INSTANTIATE_TYPED_TEST_CASE_P( using TestVirtualSerializeTemplated = TestHarness; struct HolderBase { - checkpoint_virtual_serialize_root() + magistrate_virtual_serialize_root() virtual ~HolderBase() = default; @@ -630,7 +630,7 @@ struct HolderBase { template struct HolderObjBase : HolderBase { - checkpoint_virtual_serialize_derived_from(HolderBase) + magistrate_virtual_serialize_derived_from(HolderBase) virtual ObjT* get() = 0; @@ -640,7 +640,7 @@ struct HolderObjBase : HolderBase { template struct HolderBasic final : HolderObjBase { - checkpoint_virtual_serialize_derived_from(HolderObjBase) + magistrate_virtual_serialize_derived_from(HolderObjBase) ObjT* get() override { return obj_.get(); } std::unique_ptr obj_ = nullptr;