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

Auth sample disqualifications (type 2) #393

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Another implementation of binary serialization.
AlexanderSuprunenko committed Jun 20, 2019
commit 41d5e6fb9268608f6ade709f18911cff905eb34d
142 changes: 69 additions & 73 deletions include/lib/graft/binary_serialize.h
Original file line number Diff line number Diff line change
@@ -148,107 +148,103 @@ void bserialize(Arch& ar, V& v);
template<typename Arch, typename V>
void bdeserialize(Arch& ar, V& v);
//////////////

template<typename Arch, typename V>
void ser(Arch& ar, V& v)
typename std::enable_if< !is_serializable_v<V> && !is_container_v<V> && !is_trivial_class_v<V> >::type
ser(Arch& ar, V& v)
{
if constexpr( is_container_v<V> )
{
size_t size = v.size();
write_varint(ar, size);
std::for_each(v.begin(), v.end(), [&](auto& item)
{
if constexpr( is_serializable_v<decltype(item)> )
{
bserialize(ar, item);
}
else
{
ser(ar, item);
}
});
}
else if constexpr( is_trivial_class_v<V> )
{
const uint8_t* p = reinterpret_cast<const uint8_t*>(&v);
for(int i=0; i<sizeof(V); ++i, ++p)
{
ar << *p;
}
}
else
ar << v;
}

template<typename Arch, typename V>
typename std::enable_if< !is_serializable_v<V> && !is_container_v<V> && is_trivial_class_v<V> >::type
ser(Arch& ar, V& v)
{
const uint8_t* p = reinterpret_cast<const uint8_t*>(&v);
for(int i=0; i<sizeof(V); ++i, ++p)
{
ar << v;
ar << *p;
}
}

template<typename Arch, typename V>
void deser(Arch& ar, V& v)
typename std::enable_if< is_container_v<V> >::type //it means also !is_serializable_v<V>
ser(Arch& ar, V& v)
{
if constexpr(is_container<V>::value)
size_t size = v.size();
write_varint(ar, size);
std::for_each(v.begin(), v.end(), [&](auto& item)
{
size_t size;
read_varint(ar, size);
v.resize(size);
std::for_each(v.begin(), v.end(), [&](auto& item)
{
if constexpr( is_serializable_v<decltype(item)> )
{
bdeserialize(ar, item);
}
else
{
deser(ar, item);
}
});
}
else if constexpr( is_trivial_class_v<V> )
bserialize(ar, item);
});
}

template<typename Arch, typename V>
typename std::enable_if< is_serializable_v<V> >::type
ser(Arch& ar, V& v)
{
boost::hana::for_each(boost::hana::keys(v), [&](auto key)
{
uint8_t* p = reinterpret_cast<uint8_t*>(&v);
for(int i=0; i<sizeof(V); ++i, ++p)
{
ar >> *p;
}
}
else
const auto& member = boost::hana::at_key(v, key);
ser(ar, member);
});
}

template<typename Arch, typename V>
void bserialize(Arch& ar, V& v)
{
ser(ar, v);
}

template<typename Arch, typename V>
typename std::enable_if< !is_serializable_v<V> && !is_container_v<V> && !is_trivial_class_v<V> >::type
deser(Arch& ar, V& v)
{
ar >> v;
}

template<typename Arch, typename V>
typename std::enable_if< !is_serializable_v<V> && !is_container_v<V> && is_trivial_class_v<V> >::type
deser(Arch& ar, V& v)
{
uint8_t* p = reinterpret_cast<uint8_t*>(&v);
for(int i=0; i<sizeof(V); ++i, ++p)
{
ar >> v;
ar >> *p;
}
}

template<typename Arch, typename V>
void bserialize(Arch& ar, V& v)
typename std::enable_if< is_container_v<V> >::type //it means also !is_serializable_v<V>
deser(Arch& ar, V& v)
{
boost::hana::for_each(boost::hana::keys(v), [&](auto key)
size_t size;
read_varint(ar, size);
v.resize(size);
std::for_each(v.begin(), v.end(), [&](auto& item)
{
const auto& member = boost::hana::at_key(v, key);
if constexpr( is_serializable_v<decltype(member)> )
{
bserialize(ar, member);
}
else
{
ser(ar, member);
}
bdeserialize(ar, item);
});
}

template<typename Arch, typename V>
void bdeserialize(Arch& ar, V& v)
typename std::enable_if< is_serializable_v<V> >::type
deser(Arch& ar, V& v)
{
boost::hana::for_each(boost::hana::keys(v), [&](auto key)
{
auto& member = boost::hana::at_key(v, key);
if constexpr( is_serializable_v<decltype(member)> )
{
bdeserialize(ar, member);
}
else
{
deser(ar, member);
}
deser(ar, member);
});
}


template<typename Arch, typename V>
void bdeserialize(Arch& ar, V& v)
{
deser(ar, v);
}

} //namespace binary_details

//It is expected that serialize and deserialize throw exception on error