Skip to content

Commit

Permalink
#344 Implement user traits mechanisms
Browse files Browse the repository at this point in the history
Also adds is_footprinter_v
  • Loading branch information
Matthew-Whitlock committed Sep 25, 2024
1 parent 7a1475e commit 783da5f
Show file tree
Hide file tree
Showing 26 changed files with 822 additions and 114 deletions.
11 changes: 6 additions & 5 deletions examples/checkpoint_example_to_file_nonintrusive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,19 @@ int main(int, char**) {
// Call the serialization routine for the variable `my_test_inst`
// The output is a unique pointer: `std::unique_ptr<SerializedInfo>`
// (defined in `src/checkpoint_api.h`)
magistrate::serializeToFile(my_test_inst, "hello.txt");
std::string my_filename = "CheckpointExampleToFileNoninstrusive.txt";
magistrate::serializeToFile(my_test_inst, my_filename);

//
// De-serializes from the file an object of type 'MyTestType'
// out will be an object of type 'std::unique_ptr<MyTestType>'
//
auto out = magistrate::deserializeFromFile<MyTestType>("hello.txt");
auto out = magistrate::deserializeFromFile<MyTestType>(my_filename);

if (my_test_inst == *out) {
std::cout << " Serialization / Deserialization from file worked. \n";
} else {
std::cout << " Serialization / Deserialization from file failed. \n";
std::cout << " Serialization / Deserialization from file failed. \n" << std::flush;
assert(false);
}

Expand All @@ -164,7 +165,7 @@ int main(int, char**) {
// Here 'out_2' will contain an empty vector and an integer 'len_' set to 0.
//

magistrate::deserializeInPlaceFromFile<MyTestType>("hello.txt", &out_2);
magistrate::deserializeInPlaceFromFile<MyTestType>(my_filename, &out_2);

//
// Now 'out_2' will contain:
Expand All @@ -175,7 +176,7 @@ int main(int, char**) {
if (my_test_inst == out_2) {
std::cout << " Deserialization in-place from file worked. \n";
} else {
std::cout << " Deserialization in-place from file failed. \n";
std::cout << " Deserialization in-place from file failed. \n" << std::flush;
assert(false);
}

Expand Down
60 changes: 60 additions & 0 deletions examples/checkpoint_example_user_traits.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
//@HEADER
// *****************************************************************************
//
// checkpoint_example_user_traits.cc
// DARMA/magistrate => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact [email protected]
//
// *****************************************************************************
//@HEADER
*/
#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<test::TestObj, checkpoint_trait>(obj);
auto s_info_c = checkpoint::serialize<test::TestObj, checkpoint_trait, checkpoint_trait>(obj);
auto s_info_d = checkpoint::serialize<test::TestObj, test::random_trait, checkpoint_trait>(obj);
auto s_info_e = checkpoint::serialize<test::TestObj, checkpoint_trait, test::random_trait>(obj);
auto s_info_f = checkpoint::serialize<test::TestObj, test::random_trait, test::random_trait>(obj);
auto s_info_g = checkpoint::serialize<test::TestObj, shallow_trait>(obj);
auto s_info_h = checkpoint::serialize<test::TestObj, misc::namespace_trait>(obj);
auto s_info_i = checkpoint::serialize<test::TestObj, 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 SerT::template has_not_traits<shallow_trait>::type = 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 SerT::template has_traits<random_trait>::type = nullptr>
void serialize(SerT& s, TestObj& myObj){
if(s.isSizing()) printf("Inserting random extra object serialization step! ");
myObj.serialize(s);
}

template<typename SerT, typename SerT::template has_traits<shallow_trait>::type = 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 SerT::template has_traits<test::random_trait>::type = 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 SerT::template has_traits<namespace_trait>::type = 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 SerT::template has_traits<hook_all_trait>::type = 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 T, typename... UserTraits>
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 T, typename... UserTraits>
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 T, typename... UserTraits>
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 T, typename... UserTraits>
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 T, typename... UserTraits>
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 T, typename... UserTraits>
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 T, typename... UserTraits, 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... UserTraits, 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 T, typename... UserTraits, typename StreamT>
void deserializeInPlaceFromStream(StreamT& stream, T* buf);


Expand Down
Loading

0 comments on commit 783da5f

Please sign in to comment.