Skip to content

Commit

Permalink
#132 Virtual Serialize: Spare need to name base class in macro
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilMiller committed Oct 7, 2020
1 parent 2665ddc commit e7dcac9
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/checkpoint/dispatch/dispatch_virtual.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_base(T)
* - \c checkpoint_virtual_serialize_dervied(T,BaseT)
* - \c checkpoint_virtual_serialize_base()
* - \c checkpoint_virtual_serialize_derived_from(ParentT)
*
* Invoking the virtual serializer:
*
Expand Down
8 changes: 5 additions & 3 deletions src/checkpoint/dispatch/vrt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@
#include "checkpoint/dispatch/vrt/inheritance_assert_helpers.h"
#include "checkpoint/dispatch/vrt/serialize_instantiator.h"

#define checkpoint_virtual_serialize_base(BASE) \
using _CheckpointVirtualSerializerBaseType = BASE; \
#define checkpoint_virtual_serialize_base() \
auto _CheckpointVSBaseTypeFn() -> decltype(auto) { return this; } \
virtual void _checkpointDynamicSerialize( \
void* s, \
::checkpoint::dispatch::vrt::TypeIdx ser_idx, \
::checkpoint::dispatch::vrt::TypeIdx expected_idx \
) { \
using _CheckpointVirtualSerializerBaseType = ::checkpoint::dispatch::vrt::checkpoint_base_type_t<decltype(*this)>; \
::checkpoint::instantiateObjSerializer< \
_CheckpointVirtualSerializerBaseType, \
checkpoint_serializer_variadic_args() \
Expand All @@ -68,6 +69,7 @@
dispatcher(s, *static_cast<_CheckpointVirtualSerializerBaseType*>(this)); \
} \
virtual ::checkpoint::dispatch::vrt::TypeIdx _checkpointDynamicTypeIndex() { \
using _CheckpointVirtualSerializerBaseType = ::checkpoint::dispatch::vrt::checkpoint_base_type_t<decltype(*this)>; \
return ::checkpoint::dispatch::vrt::objregistry::makeObjIdx<_CheckpointVirtualSerializerBaseType>(); \
} \

Expand All @@ -81,7 +83,7 @@ namespace checkpoint { namespace dispatch { namespace vrt {
*/
template <typename BaseT>
struct SerializableBase {
checkpoint_virtual_serialize_base(BaseT)
checkpoint_virtual_serialize_base()
};

}}} /* end namespace checkpoint::dispatch::vrt */
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/dispatch/vrt/derived.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
>(); \
debug_checkpoint( \
"%s: BEGIN: _checkpointDynamicSerialize: serializer_idx=%d {\n", \
typeid(DERIVED).name(), base_ser_idx \
typeid(_CheckpointDerivedType).name(), base_ser_idx \
); \
::checkpoint::dispatch::vrt::assertTypeIdxMatch<_CheckpointDerivedType>(expected_idx); \
auto base_idx = ::checkpoint::dispatch::vrt::objregistry::makeObjIdx<PARENT>(); \
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/dispatch/vrt/inheritance_assert_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ inline void assertTypeIdxMatch(TypeIdx const expected_idx) {
"\" does not matched expected value. "
"You are probably missing a SerializableBase<T> or SerializableDerived<T> "
"in the virtual class hierarchy; or, if you are using macros: "
"checkpoint_virtual_serialize_base(..) or checkpoint_virtual_serialize_derived(..)";
"checkpoint_virtual_serialize_base() or checkpoint_virtual_serialize_derived(..)";
checkpointAssert(
obj_idx == expected_idx or expected_idx == no_type_idx, debug_str.c_str()
);
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/dispatch/vrt/object_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ inline RegistryType<T>& getRegistry() {

template <typename ObjT>
Registrar<ObjT>::Registrar() {
using BaseType = typename ObjT::_CheckpointVirtualSerializerBaseType;
using BaseType = ::checkpoint::dispatch::vrt::checkpoint_base_type_t<ObjT>;

auto& reg = getRegistry<BaseType>();
index = static_cast<TypeIdx>(reg.size());
Expand Down
16 changes: 16 additions & 0 deletions src/checkpoint/dispatch/vrt/registry_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,28 @@
#if !defined INCLUDED_CHECKPOINT_DISPATCH_VRT_REGISTRY_COMMON_H
#define INCLUDED_CHECKPOINT_DISPATCH_VRT_REGISTRY_COMMON_H

#include <type_traits>

namespace checkpoint { namespace dispatch { namespace vrt {

using TypeIdx = int;

static constexpr TypeIdx const no_type_idx = -1;

template <typename BaseT>
struct SerializableBase;

template <typename BaseT>
struct _CheckpointBaseType {
using type = BaseT;
};
template <typename BaseT>
struct _CheckpointBaseType<SerializableBase<BaseT>> {
using type = BaseT;
};
template <typename ObjT>
using checkpoint_base_type_t = typename _CheckpointBaseType<std::remove_pointer_t<decltype(std::declval<ObjT>()._CheckpointVSBaseTypeFn())>>::type;

}}} /* end namespace checkpoint::dispatch::vrt */

#endif /*INCLUDED_CHECKPOINT_DISPATCH_VRT_REGISTRY_COMMON_H*/
2 changes: 1 addition & 1 deletion src/checkpoint/dispatch/vrt/serialize_instantiator.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct InstantiateIfPossible<
// running on the derived type using the base idx.
// using BaseType = typename ObjT::_CheckpointVirtualSerializerBaseType;
// linkDerivedToBase<SerializerT, ObjT, BaseType>();
using BaseType = typename ObjT::_CheckpointVirtualSerializerBaseType;
using BaseType = ::checkpoint::dispatch::vrt::checkpoint_base_type_t<ObjT>;
linkDerivedToBase<SerializerT, ObjT, BaseType>();
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/checkpoint/dispatch/vrt/virtual_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace checkpoint { namespace dispatch { namespace vrt {
template <typename T, typename SerializerT>
void virtualSerialize(T*& base, SerializerT& s) {
// Get the real base in case this is called on a derived type
using BaseT = typename T::_CheckpointVirtualSerializerBaseType;
using BaseT = ::checkpoint::dispatch::vrt::checkpoint_base_type_t<T>;
auto serializer_idx = serializer_registry::makeObjIdx<BaseT, SerializerT>();
base->_checkpointDynamicSerialize(&s, serializer_idx, no_type_idx);
}
Expand Down Expand Up @@ -130,7 +130,7 @@ struct SerializeAsVirtualIfNeeded<
s | entry;

if (s.isUnpacking()) {
using BaseT = typename T::_CheckpointVirtualSerializerBaseType;
using BaseT = ::checkpoint::dispatch::vrt::checkpoint_base_type_t<T>;

// use type idx here, registration needed for proper type re-construction
auto t = dispatch::vrt::objregistry::allocateConcreteType<BaseT>(entry);
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/dispatch/vrt/virtual_serialize_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct VirtualSerializeTraits {
* using _CheckpointVirtualSerializerBaseType = T;
*/
template <typename U>
using has_base_typedef_t = typename U::_CheckpointVirtualSerializerBaseType;
using has_base_typedef_t = ::checkpoint::dispatch::vrt::checkpoint_base_type_t<U>;

using has_base_typedef = detection::is_detected<has_base_typedef_t, T>;

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_footprinter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ TEST_F(TestFootprinter, test_vector) {

struct TestBase {

checkpoint_virtual_serialize_base(TestBase)
checkpoint_virtual_serialize_base()

virtual ~TestBase() = default;

Expand All @@ -495,7 +495,7 @@ struct TestDerived2 : TestBase {
explicit TestDerived2(int i) {}
explicit TestDerived2(SERIALIZE_CONSTRUCT_TAG) {}

checkpoint_virtual_serialize_derived(TestDerived2, TestBase)
checkpoint_virtual_serialize_derived_from(TestBase)

template <
typename SerializerT,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_virtual_serialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ struct TestBase {
explicit TestBase(TEST_CONSTRUCT) { init(); }
explicit TestBase(SERIALIZE_CONSTRUCT_TAG) {}

checkpoint_virtual_serialize_base(TestBase)
checkpoint_virtual_serialize_base()

virtual ~TestBase() = default;

Expand Down Expand Up @@ -425,7 +425,7 @@ struct TestBase {
explicit TestBase(TEST_CONSTRUCT) { init(); }
explicit TestBase(SERIALIZE_CONSTRUCT_TAG) {}

checkpoint_virtual_serialize_base(TestBase)
checkpoint_virtual_serialize_base()

virtual ~TestBase() = default;

Expand Down

0 comments on commit e7dcac9

Please sign in to comment.