Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

344 implement user traits #345

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
61 changes: 61 additions & 0 deletions examples/checkpoint_example_user_traits.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
//@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 darma@sandia.gov
//
// *****************************************************************************
//@HEADER
*/
#include "checkpoint/checkpoint.h"

#include "checkpoint_example_user_traits.hpp"

int main(int, char**){
using namespace 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<TestObj, checkpoint_trait>(obj);
auto s_info_c = checkpoint::serialize<TestObj, checkpoint_trait, checkpoint_trait>(obj);
auto s_info_d = checkpoint::serialize<TestObj, random_trait, checkpoint_trait>(obj);
auto s_info_e = checkpoint::serialize<TestObj, checkpoint_trait, random_trait>(obj);
auto s_info_f = checkpoint::serialize<TestObj, random_trait, random_trait>(obj);
auto s_info_g = checkpoint::serialize<TestObj, shallow_trait>(obj);
auto s_info_h = checkpoint::serialize<TestObj, misc::namespace_trait>(obj);
auto s_info_i = checkpoint::serialize<TestObj, misc::hook_all_trait>(obj);
}
109 changes: 109 additions & 0 deletions examples/checkpoint_example_user_traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "checkpoint/checkpoint.h"

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

using checkpoint::has_user_traits_v;
using checkpoint::has_any_user_traits_v;

namespace test {
struct random_trait {} RandomTrait;

struct TestObj {
Matthew-Whitlock marked this conversation as resolved.
Show resolved Hide resolved
int a = 1;

TestObj() {}

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

static_assert(not has_user_traits_v<SerT, shallow_trait>,
"ShallowTrait should have been removed!\n");
}
};
}

namespace test {
template<
typename SerT,
typename std::enable_if_t<
has_user_traits_v<SerT, random_trait>, void*
> = 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<
has_user_traits_v<SerT, shallow_trait>, void*
> = 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<
has_user_traits_v<SerT, test::random_trait>, void*
> = 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<
has_user_traits_v<SerT, namespace_trait>, void*
> = 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<
has_user_traits_v<SerT, hook_all_trait>, void*
> = 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