Skip to content

Commit

Permalink
#344 Implement user traits mechanisms
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew-Whitlock committed Jun 6, 2024
1 parent 28e8e4e commit 28e3ef9
Show file tree
Hide file tree
Showing 13 changed files with 542 additions and 70 deletions.
18 changes: 18 additions & 0 deletions examples/checkpoint_example_user_traits.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "checkpoint/checkpoint.h"

#include "checkpoint_example_user_traits.hpp"

int main(int, char**){
test::TestObj obj;

//Each invocation will be handled based on the traits attached
auto s_info_a = checkpoint::serialize(obj);
auto s_info_b = checkpoint::serialize<checkpoint_trait>(obj);
auto s_info_c = checkpoint::serialize<checkpoint_trait, checkpoint_trait>(obj);
auto s_info_d = checkpoint::serialize<test::random_trait, checkpoint_trait>(obj);
auto s_info_e = checkpoint::serialize<checkpoint_trait, test::random_trait>(obj);
auto s_info_f = checkpoint::serialize<test::random_trait, test::random_trait>(obj);
auto s_info_g = checkpoint::serialize<shallow_trait>(obj);
auto s_info_h = checkpoint::serialize<misc::namespace_trait>(obj);
auto s_info_i = checkpoint::serialize<misc::hook_all_trait>(obj);
}
66 changes: 66 additions & 0 deletions examples/checkpoint_example_user_traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "checkpoint/checkpoint.h"

struct checkpoint_trait {} CheckpointTrait;
struct shallow_trait {} ShallowTrait;

namespace test {
struct random_trait {} RandomTrait;

struct TestObj {
int a = 1;

TestObj() {}

template<typename SerT, typename std::enable_if_t<SerT::template has_not_traits<shallow_trait>::value>* = nullptr>
void serialize(SerT& s){
if constexpr(SerT::template has_traits<checkpoint_trait>::value){
if(s.isSizing()) printf("Customizing serialization for checkpoint\n");
s | a;
} else {
if(s.isSizing()) printf("Default serializing testObj\n");
}

static_assert(SerT::template has_not_traits<shallow_trait>::value, "ShallowTrait should have been removed!\n");
}
};
}

namespace test {
template<typename SerT, typename std::enable_if_t<SerT::template has_traits<random_trait>::value>* = nullptr>
void serialize(SerT& s, TestObj& myObj){
if(s.isSizing()) printf("Inserting random extra object serialization step! ");
myObj.serialize(s);
}

template<typename SerT, typename std::enable_if_t<SerT::template has_traits<shallow_trait>::value>* = nullptr>
void serialize(SerT& s, TestObj& myObj){
if(s.isSizing()) printf("Removing shallow trait before passing along!\n");
auto newS = s.template withoutTraits<shallow_trait>();
myObj.serialize(newS);
}
}

namespace misc {
template<typename SerT, typename std::enable_if_t<SerT::template has_traits<test::random_trait>::value>* = nullptr>
void serialize(SerT& s, test::TestObj& myObj){
if(s.isSizing()) printf("Serializers in other namespaces don't usually get found ");
myObj.serialize(s);
}


const struct namespace_trait {} NamespaceTrait;
template<typename SerT, typename std::enable_if_t<SerT::template has_traits<namespace_trait>::value>* = nullptr>
void serialize(SerT& s, test::TestObj& myObj){
if(s.isSizing()) printf("A misc:: trait means we can serialize from misc:: too: ");
myObj.serialize(s);
}


const struct hook_all_trait {} HookAllTrait;
template<typename SerT, typename T, typename std::enable_if_t<SerT::template has_traits<hook_all_trait>::value>* = nullptr>
void serialize(SerT& s, T& myObj){
if(s.isSizing()) printf("We can even add on a generic pre-serialize hook: ");
auto newS = s.template withoutTraits<hook_all_trait>();
myObj.serialize(newS);
}
}
26 changes: 13 additions & 13 deletions src/checkpoint/checkpoint_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ using SerializedReturnType = std::unique_ptr<SerializedInfo>;
* \return a \c std::unique_ptr to a \c SerializedInfo containing the buffer
* with serialized data and the size of the buffer
*/
template <typename T>
template <typename... UserTraits, typename T>
SerializedReturnType serialize(T& target, BufferCallbackType fn = nullptr);

/**
Expand All @@ -101,7 +101,7 @@ SerializedReturnType serialize(T& target, BufferCallbackType fn = nullptr);
*
* \return a pointer to the newly reified \c T based on bytes in \c buf
*/
template <typename T>
template <typename T, typename... UserTraits>
T* deserialize(char* buf, char* object_buf);

/**
Expand All @@ -118,7 +118,7 @@ T* deserialize(char* buf, char* object_buf);
*
* \return a unique pointer to the newly reified \c T based on bytes in \c buf
*/
template <typename T>
template <typename T, typename... UserTraits>
std::unique_ptr<T> deserialize(char* buf);

/**
Expand All @@ -132,7 +132,7 @@ std::unique_ptr<T> deserialize(char* buf);
* \param[in] t a valid pointer to a \c T that has been user-allocated and
* constructed
*/
template <typename T>
template <typename... UserTraits, typename T>
void deserializeInPlace(char* buf, T* t);

/**
Expand All @@ -143,7 +143,7 @@ void deserializeInPlace(char* buf, T* t);
*
* \return a unique pointer to \c T that must be deallocated
*/
template <typename T>
template <typename T, typename... UserTraits>
std::unique_ptr<T> deserialize(SerializedReturnType&& in);

/**
Expand All @@ -153,7 +153,7 @@ std::unique_ptr<T> deserialize(SerializedReturnType&& in);
*
* \return number of bytes for the \c target
*/
template <typename T>
template <typename... UserTraits, typename T>
std::size_t getSize(T& target);

/**
Expand All @@ -170,7 +170,7 @@ std::size_t getSize(T& target);
*
* \return memory footprint of the \c target
*/
template <typename T>
template <typename... UserTraits, typename T>
std::size_t getMemoryFootprint(T& target, std::size_t size_offset = 0);

/**
Expand All @@ -184,7 +184,7 @@ std::size_t getMemoryFootprint(T& target, std::size_t size_offset = 0);
* \param[in] target the \c T to serialize
* \param[in] file name of the file to create
*/
template <typename T>
template <typename... UserTraits, typename T>
void serializeToFile(T& target, std::string const& file);

/**
Expand All @@ -200,7 +200,7 @@ void serializeToFile(T& target, std::string const& file);
*
* \return unique pointer to the new object \c T
*/
template <typename T>
template <typename T, typename... UserTraits>
std::unique_ptr<T> deserializeFromFile(std::string const& file);

/**
Expand All @@ -214,7 +214,7 @@ std::unique_ptr<T> deserializeFromFile(std::string const& file);
* \param[in] file the filename to read with bytes for \c T
* \param[in] t a valid, constructed \c T to deserialize into
*/
template <typename T>
template <typename... UserTraits, typename T>
void deserializeInPlaceFromFile(std::string const& file, T* buf);

/**
Expand All @@ -227,7 +227,7 @@ void deserializeInPlaceFromFile(std::string const& file, T* buf);
* \param[in] target the \c T to serialize
* \param[in] stream to serialize into, with tellp and write functions.
*/
template <typename T, typename StreamT>
template <typename... Traits, typename T, typename StreamT>
void serializeToStream(T& target, StreamT& stream);

/**
Expand All @@ -243,7 +243,7 @@ void serializeToStream(T& target, StreamT& stream);
*
* \return unique pointer to the new object \c T
*/
template <typename T, typename StreamT>
template <typename T, typename... Traits, typename StreamT>
std::unique_ptr<T> deserializeFromStream(StreamT& stream);

/**
Expand All @@ -257,7 +257,7 @@ std::unique_ptr<T> deserializeFromStream(StreamT& stream);
* \param[in] stream the stream to read with bytes for \c T, with tellg and read functions
* \param[in] t a valid, constructed \c T to deserialize into
*/
template <typename T, typename StreamT>
template <typename... Traits, typename T, typename StreamT>
void deserializeInPlaceFromStream(StreamT& stream, T* buf);


Expand Down
54 changes: 27 additions & 27 deletions src/checkpoint/checkpoint_api.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,97 +53,97 @@

namespace checkpoint {

template <typename T>
template <typename... Traits, typename T>
SerializedReturnType serialize(T& target, BufferCallbackType fn) {
auto ret = dispatch::serializeType<T>(target, fn);
auto ret = dispatch::serializeType<T, Traits...>(target, fn);
auto& buf = std::get<0>(ret);
std::unique_ptr<SerializedInfo> base_ptr(
static_cast<SerializedInfo*>(buf.release())
);
return base_ptr;
}

template <typename T>
template <typename T, typename... Traits>
T* deserialize(char* buf, char* object_buf) {
return dispatch::deserializeType<T>(buf, object_buf);
return dispatch::deserializeType<T, Traits...>(buf, object_buf);
}

template <typename T>
template <typename T, typename... Traits>
std::unique_ptr<T> deserialize(char* buf) {
auto t = dispatch::deserializeType<T>(buf);
auto t = dispatch::deserializeType<T, Traits...>(buf);
return std::unique_ptr<T>(t);
}

template <typename T>
template <typename T, typename... Traits>
std::unique_ptr<T> deserialize(SerializedReturnType&& in) {
auto t = dispatch::deserializeType<T>(in->getBuffer());
auto t = dispatch::deserializeType<T, Traits...>(in->getBuffer());
return std::unique_ptr<T>(t);
}

template <typename T>
template <typename... Traits, typename T>
void deserializeInPlace(char* buf, T* t) {
return dispatch::deserializeType<T>(dispatch::InPlaceTag{}, buf, t);
return dispatch::deserializeType<T, Traits...>(dispatch::InPlaceTag{}, buf, t);
}

template <typename T>
template <typename... Traits, typename T>
std::size_t getSize(T& target) {
return dispatch::Standard::size<T, Sizer>(target);
return dispatch::Standard::size<T, SerializerRef<Sizer, Traits...>>(target);
}

template <typename T>
template <typename... Traits, typename T>
std::size_t getMemoryFootprint(T& target, std::size_t size_offset) {
return size_offset + std::max(
dispatch::Standard::footprint<T, Footprinter>(target),
dispatch::Standard::footprint<T, SerializerRef<Footprinter, Traits...>>(target),
sizeof(target)
);
}

template <typename T>
template <typename... Traits, typename T>
void serializeToFile(T& target, std::string const& file) {
auto len = getSize<T>(target);
dispatch::Standard::pack<T, PackerBuffer<buffer::IOBuffer>>(
auto len = getSize<Traits...>(target);
dispatch::Standard::pack<T, SerializerRef<PackerBuffer<buffer::IOBuffer>, Traits...>>(
target, len, buffer::IOBuffer::WriteToFileTag{}, len, file
);
}

template <typename T>
template <typename T, typename... Traits>
std::unique_ptr<T> deserializeFromFile(std::string const& file) {
auto mem = dispatch::Standard::allocate<T>();
T* t_buf = dispatch::Standard::construct<T>(mem);
auto t = dispatch::Standard::unpack<T, UnpackerBuffer<buffer::IOBuffer>>(
auto t = dispatch::Standard::unpack<T, SerializerRef<UnpackerBuffer<buffer::IOBuffer>, Traits...>>(
t_buf, buffer::IOBuffer::ReadFromFileTag{}, file
);
return std::unique_ptr<T>(t);
}

template <typename T>
template <typename... Traits, typename T>
void deserializeInPlaceFromFile(std::string const& file, T* t) {
dispatch::Standard::unpack<T, UnpackerBuffer<buffer::IOBuffer>>(
dispatch::Standard::unpack<T, SerializerRef<UnpackerBuffer<buffer::IOBuffer>, Traits...>>(
t, buffer::IOBuffer::ReadFromFileTag{}, file
);
}

template <typename T, typename StreamT>
template <typename... Traits, typename T, typename StreamT>
void serializeToStream(T& target, StreamT& stream) {
auto len = getSize<T>(target);
dispatch::Standard::pack<T, StreamPacker<StreamT>>(
dispatch::Standard::pack<T, SerializerRef<StreamPacker<StreamT>, Traits...>>(
target, len, stream
);
}

template <typename T, typename StreamT>
template <typename T, typename... Traits, typename StreamT>
std::unique_ptr<T> deserializeFromStream(StreamT& stream) {
auto mem = dispatch::Standard::allocate<T>();
T* t_buf = dispatch::Standard::construct<T>(mem);
auto t = dispatch::Standard::unpack<T, StreamUnpacker<StreamT>>(
auto t = dispatch::Standard::unpack<T, SerializerRef<StreamUnpacker<StreamT>, Traits...>>(
t_buf, stream
);
return std::unique_ptr<T>(t);
}

template <typename T, typename StreamT>
template <typename... Traits, typename T, typename StreamT>
void deserializeInPlaceFromStream(StreamT& stream, T* t) {
dispatch::Standard::unpack<T, StreamUnpacker<StreamT>>(
dispatch::Standard::unpack<T, SerializerRef<StreamUnpacker<StreamT>, Traits...>>(
t, stream
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/checkpoint/dispatch/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,21 @@ struct Standard {
static SerialByteType* allocate();
};

template <typename T>
template <typename T, typename... UserTraits>
buffer::ImplReturnType packBuffer(
T& target, SerialSizeType size, BufferObtainFnType fn
);

template <typename Serializer, typename T>
inline void serializeArray(Serializer& s, T* array, SerialSizeType const len);

template <typename T>
template <typename T, typename... UserTraits>
buffer::ImplReturnType serializeType(T& target, BufferObtainFnType fn = nullptr);

template <typename T>
template <typename T, typename... UserTraits>
T* deserializeType(SerialByteType* data, SerialByteType* allocBuf = nullptr);

template <typename T>
template <typename T, typename... UserTraits>
void deserializeType(InPlaceTag, SerialByteType* data, T* t);

template <typename T>
Expand Down
Loading

0 comments on commit 28e3ef9

Please sign in to comment.