working on pointer support

This commit is contained in:
Shane Grant 2013-06-13 12:17:59 -07:00
parent 4d56ae215b
commit 97304f8272
8 changed files with 99 additions and 155 deletions

View File

@ -1,2 +1,3 @@
all: test.cpp cereal.hpp
g++ -std=c++0x test.cpp -o test -ljsoncpp
all: test.cpp
g++ -std=c++0x test.cpp -o test -ljsoncpp -I./..

View File

@ -1,8 +1,10 @@
#pragma once
#ifndef CEREAL_BINARY_ARCHIVE_BINARY_ARCHIVE_HPP_
#define CEREAL_BINARY_ARCHIVE_BINARY_ARCHIVE_HPP_
#include <cereal/cereal.hpp>
namespace cereal
{
// ######################################################################
class BinaryOutputArchive : public OutputArchive<BinaryOutputArchive>
{
@ -47,60 +49,39 @@ namespace cereal
std::istream & itsStream;
};
//! Saving for POD types to binary
template<class T>
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
save(BinaryOutputArchive & ar, T const & t)
{
ar.save_binary(std::addressof(t), sizeof(t));
//std::cout << "Saving POD size: " << sizeof(T) << " [" << t << "]" << std::endl;
}
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
save(BinaryOutputArchive & ar, T const & t)
{
ar.save_binary(std::addressof(t), sizeof(t));
//std::cout << "Saving POD size: " << sizeof(T) << " [" << t << "]" << std::endl;
}
//! Loading for POD types from binary
template<class T>
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
load(BinaryInputArchive & ar, T & t)
{
ar.load_binary(std::addressof(t), sizeof(t));
//std::cout << "Loading POD size: " << sizeof(T) << " [" << t << "]" << std::endl;
}
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
load(BinaryInputArchive & ar, T & t)
{
ar.load_binary(std::addressof(t), sizeof(t));
//std::cout << "Loading POD size: " << sizeof(T) << " [" << t << "]" << std::endl;
}
//! Saving for NVP types to binary
template<class T>
void save(BinaryOutputArchive & ar, NameValuePair<T> const & t)
{
//std::cout << "Saving NVP: " << t.name << std::endl;
ar & t.value;
}
void save(BinaryOutputArchive & ar, NameValuePair<T> const & t)
{
//std::cout << "Saving NVP: " << t.name << std::endl;
ar & t.value;
}
//! Loading for NVP types from binary
template<class T>
void load(BinaryInputArchive & ar, NameValuePair<T> t)
{
//std::cout << "Loading NVP... " << std::endl;
ar & t.value;
}
//! Serialization for basic_string types to binary
template<class CharT, class Traits, class Alloc>
void save(BinaryOutputArchive & ar, std::basic_string<CharT, Traits, Alloc> const & str)
{
// Save number of chars + the data
ar & str.size();
ar.save_binary(str.data(), str.size() * sizeof(CharT));
//std::cout << "Saving string: " << str << std::endl;
}
//! Serialization for basic_string types from binary
template<class CharT, class Traits, class Alloc>
void load(BinaryInputArchive & ar, std::basic_string<CharT, Traits, Alloc> & str)
{
size_t size;
ar & size;
str.resize(size);
ar.load_binary(const_cast<CharT*>(str.data()), size * sizeof(CharT));
//std::cout << "Loading string: " << str << std::endl;
}
void load(BinaryInputArchive & ar, NameValuePair<T> t)
{
//std::cout << "Loading NVP... " << std::endl;
ar & t.value;
}
}
#endif // CEREAL_BINARY_ARCHIVE_BINARY_ARCHIVE_HPP_

View File

@ -20,10 +20,10 @@ namespace cereal
}
//! Serialization for std::vector to binary
template <class T>
template <class T, class A>
void load( BinaryInputArchive & ar, std::vector<T, A> & vector )
{
std::cout << "Loading array" << std::endl;
std::cout << "Loading vector" << std::endl;
size_t dataSize;
size_t vectorSize;
@ -34,6 +34,29 @@ namespace cereal
ar.load_binary( vector.data(), dataSize );
}
//! Serialization for std::vector<bool, A> types to binary
template <class A>
void save( BinaryOutputArchive & ar, std::vector<bool, A> const & vector )
{
std::cout << "Saving vector of bool" << std::endl;
ar & vector.size(); // number of elements
for( auto it = vector.begin(), end = vector.end(); it != end; ++it )
ar & (*it);
}
//! Serialization for std::vector<bool, A> to binary
template <class A>
void load( BinaryInputArchive & ar, std::vector<bool, A> & vector )
{
size_t size;
ar & size;
vector.resize( size );
for( auto it = vector.begin(), end = vector.end(); it != end; ++it )
ar & (*it);
}
} // namespace cereal
#endif // CEREAL_BINARY_ARCHIVE_VECTOR_HPP_

View File

@ -1,8 +1,10 @@
#pragma once
#ifndef CEREAL_CEREAL_HPP_
#define CEREAL_CEREAL_HPP_
#include <iostream>
#include <type_traits>
#include "traits.hpp"
#include <cereal/details/traits.hpp>
namespace cereal
{
@ -163,3 +165,5 @@ namespace cereal
ArchiveType * const self;
}; // class InputArchive
}
#endif // CEREAL_CEREAL_HPP_

View File

@ -1,96 +0,0 @@
namespace cereal
{
namespace traits
{
template<typename> struct Void { typedef void type; };
// ######################################################################
// Member Serialize
template<typename T, class A, typename Sfinae = void>
struct has_member_serialize: std::false_type {};
template<typename T, class A>
struct has_member_serialize< T, A,
typename Void<
decltype( std::declval<T&>().serialize( std::declval<A&>() ) )
>::type
>: std::true_type {};
// ######################################################################
// Non Member Serialize
template<typename T, typename A> char & serialize(A&, T&);
template<typename T, typename A>
bool constexpr has_non_member_serialize()
{ return std::is_void<decltype(serialize(std::declval<A&>(), std::declval<T&>()))>::value; };
// ######################################################################
// Member Load
template<typename T, class A, typename Sfinae = void>
struct has_member_load: std::false_type {};
template<typename T, class A>
struct has_member_load< T, A,
typename Void<
decltype( std::declval<T&>().load( std::declval<A&>() ) )
>::type
>: std::true_type {};
// ######################################################################
// Non Member Load
template<typename T, typename A> char & load(A&, T&);
template<typename T, typename A>
bool constexpr has_non_member_load()
{ return std::is_void<decltype(load(std::declval<A&>(), std::declval<T&>()))>::value; };
// ######################################################################
// Member Save
template<typename T, class A, typename Sfinae = void>
struct has_member_save: std::false_type {};
template<typename T, class A>
struct has_member_save< T, A,
typename Void<
decltype( std::declval<T&>().save( std::declval<A&>() ) )
>::type
>: std::true_type {};
// ######################################################################
// Non Member Save
template<typename T, typename A> char & save(A&, T const &);
template<typename T, typename A>
bool constexpr has_non_member_save()
{ return std::is_void<decltype(save(std::declval<A&>(), std::declval<T&>()))>::value; };
// ######################################################################
template <class T, class InputArchive, class OutputArchive>
constexpr bool has_member_split()
{ return has_member_load<T, InputArchive>() && has_member_save<T, OutputArchive>(); }
// ######################################################################
template <class T, class InputArchive, class OutputArchive>
constexpr bool has_non_member_split()
{ return has_non_member_load<T, InputArchive>() && has_non_member_save<T, OutputArchive>(); }
// ######################################################################
template <class T, class OutputArchive>
constexpr bool is_output_serializable()
{
return
has_member_save<T, OutputArchive>() ^
has_non_member_save<T, OutputArchive>() ^
has_member_serialize<T, OutputArchive>() ^
has_non_member_serialize<T, OutputArchive>();
}
// ######################################################################
template <class T, class InputArchive>
constexpr bool is_input_serializable()
{
return
has_member_load<T, InputArchive>() ^
has_non_member_load<T, InputArchive>() ^
has_member_serialize<T, InputArchive>() ^
has_non_member_serialize<T, InputArchive>();
}
}
}

View File

@ -2,6 +2,7 @@
#define CEREAL_DETAILS_TRAITS_HPP_
#include <type_traits>
#include <memory>
namespace cereal
{
@ -97,6 +98,30 @@ namespace cereal
has_member_serialize<T, InputArchive>() ^
has_non_member_serialize<T, InputArchive>();
}
constexpr std::false_type is_smart_ptr(...)
{
return {};
}
template <class T, class D>
constexpr std::true_type is_smart_ptr( std::unique_ptr<T, D> const & p )
{
return {};
}
template <class T>
constexpr std::true_type is_smart_ptr( std::shared_ptr<T> const & p )
{
return {};
}
//! Returns true if the type T is a pointer or smart pointer (in std library)
template <class T>
constexpr bool is_any_pointer()
{
return std::is_pointer<T>() || std::is_same<std::true_type, decltype(is_smart_ptr( std::declval<T&>() ))>::value;
}
}
}

View File

@ -1,6 +1,7 @@
#pragma once
#ifndef CEREAL_JSON_ARCHIVE_JSON_ARCHIVE_HPP_
#define CEREAL_JSON_ARCHIVE_JSON_ARCHIVE_HPP_
#include "cereal.hpp"
#include <cereal/cereal.hpp>
#include <cassert>
#include <jsoncpp/json/json.h>
@ -96,3 +97,5 @@ namespace cereal
//std::cout << "Loading string: " << str << std::endl;
}
}
#endif // CEREAL_JSON_ARCHIVE_JSON_ARCHIVE_HPP_

View File

@ -1,6 +1,9 @@
#include "cereal.hpp"
#include "binary_archive.hpp"
#include "json_archive.hpp"
#include <cereal/cereal.hpp>
#include <cereal/binary_archive/binary_archive.hpp>
#include <cereal/binary_archive/string.hpp>
#include <cereal/json_archive/json_archive.hpp>
#include <cxxabi.h>
#include <sstream>
#include <fstream>
@ -102,7 +105,6 @@ struct Everything
t4.a == o.t4.a &&
s == o.s;
}
};
// ######################################################################
@ -138,6 +140,7 @@ int main()
std::string hello = "Hello, World!";
json & CEREAL_NVP(hello);
//json & CEREAL_NVP(e_out); <<< Need to figure out how to recurse!
//
return 0;
}