Skip to content

Commit

Permalink
#268: Add comments in the deserializeType method
Browse files Browse the repository at this point in the history
  • Loading branch information
thearusable committed Jul 23, 2024
1 parent 85e0597 commit 7b83558
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/checkpoint/dispatch/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ struct Prefixed {
/**
* \brief Unpack \c T from packed byte-buffer \c mem
*
* \param[in] t_buf bytes holding a serialized \c T
* \param[in] mem bytes holding a serialized \c T
* \param[in] check_type the flag to control type validation
* \param[in] check_mem the flag to control memory validation
* \param[in] args arguments to the unpacker's constructor
*
* \return a pointer to an unpacked \c T
*/
template <typename T, typename UnpackerT, typename... Args>
static T* unpack(T* t_buf, bool check_type = true, bool check_mem = true, Args&&... args);
static T* unpack(T* mem, bool check_type = true, bool check_mem = true, Args&&... args);
};

template <typename T>
Expand Down
27 changes: 17 additions & 10 deletions src/checkpoint/dispatch/dispatch.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,19 @@ packBuffer(T& target, SerialSizeType size, BufferObtainFnType fn) {
}

template <typename T>
typename std::enable_if<!std::is_class<T>::value || !vrt::VirtualSerializeTraits<T>::has_virtual_serialize, buffer::ImplReturnType>::type
typename std::enable_if<
!std::is_class<T>::value || !vrt::VirtualSerializeTraits<T>::has_virtual_serialize,
buffer::ImplReturnType>::type
serializeType(T& target, BufferObtainFnType fn) {
auto len = Standard::size<T, Sizer>(target);
debug_checkpoint("serializeType: len=%ld\n", len);
return packBuffer<T>(target, len, fn);
}

template <typename T>
typename std::enable_if<!std::is_class<T>::value
|| !vrt::VirtualSerializeTraits<T>::has_virtual_serialize, T*>::type
typename std::enable_if<
!std::is_class<T>::value || !vrt::VirtualSerializeTraits<T>::has_virtual_serialize,
T*>::type
deserializeType(SerialByteType* data, SerialByteType* allocBuf) {
auto mem = allocBuf ? allocBuf : Standard::allocate<T>();
auto t_buf = std::unique_ptr<T>(Standard::construct<T>(mem));
Expand All @@ -322,7 +325,6 @@ deserializeType(SerialByteType* data, SerialByteType* allocBuf) {
return traverser;
}

// TODO: this also needs to be updated
template <typename T>
void deserializeType(InPlaceTag, SerialByteType* data, T* t) {
Standard::unpack<T, UnpackerBuffer<buffer::UserBuffer>>(t, data);
Expand All @@ -345,30 +347,35 @@ struct PrefixedType {
};

template <typename T>
typename std::enable_if<std::is_class<T>::value && vrt::VirtualSerializeTraits<T>::has_virtual_serialize, buffer::ImplReturnType>::type
typename std::enable_if<
std::is_class<T>::value && vrt::VirtualSerializeTraits<T>::has_virtual_serialize,
buffer::ImplReturnType>::type
serializeType(T& target, BufferObtainFnType fn) {
auto prefixed = PrefixedType(&target);
auto len = Standard::size<PrefixedType<T>, Sizer>(prefixed);
auto len = Standard::size<decltype(prefixed), Sizer>(prefixed);
debug_checkpoint("serializeType: len=%ld\n", len);
return packBuffer<PrefixedType<T>>(prefixed, len, fn);
return packBuffer<decltype(prefixed)>(prefixed, len, fn);
}

template <typename T>
typename std::enable_if<std::is_class<T>::value
&& vrt::VirtualSerializeTraits<T>::has_virtual_serialize, T*>::type
typename std::enable_if<
std::is_class<T>::value && vrt::VirtualSerializeTraits<T>::has_virtual_serialize,
T*>::type
deserializeType(SerialByteType* data, SerialByteType* allocBuf) {
using BaseType = vrt::checkpoint_base_type_t<T>;

auto prefix_mem = Standard::allocate<vrt::TypeIdx>();
auto prefix_buf = std::unique_ptr<vrt::TypeIdx>(Standard::construct<vrt::TypeIdx>(prefix_mem));
// Unpack TypeIdx, ignore checks for type and memory used - unpacking will only use a part of the data
vrt::TypeIdx* prefix =
Prefixed::unpack<vrt::TypeIdx, UnpackerBuffer<buffer::UserBuffer>>(prefix_buf.get(), false, false, data);
prefix_buf.release();

// allocate memory based on the readed TypeIdx
auto mem = allocBuf ? allocBuf : vrt::objregistry::allocateConcreteType<BaseType>(*prefix);
auto t_buf = vrt::objregistry::constructConcreteType<BaseType>(*prefix, mem);
auto prefixed = PrefixedType(t_buf);

// Unpack PrefixedType, ignore checks for unpacked type and execute checks for memory used
auto* traverser =
Prefixed::unpack<decltype(prefixed), UnpackerBuffer<buffer::UserBuffer>>(&prefixed, false, true, data);
return static_cast<T*>(traverser->target_);
Expand Down

0 comments on commit 7b83558

Please sign in to comment.