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

Add fc::raw::pack/unpack for std::multiset #40

Open
wants to merge 4 commits into
base: master
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
32 changes: 27 additions & 5 deletions include/fc/io/raw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,8 @@ namespace fc {
}
}

template<typename Stream, typename T>
inline void pack( Stream& s, const std::set<T>& value ) {
template<typename Stream, typename... T>
inline void pack( Stream& s, const std::set<T...>& value ) {
fc::raw::pack( s, unsigned_int((uint32_t)value.size()) );
auto itr = value.begin();
auto end = value.end();
Expand All @@ -511,12 +511,34 @@ namespace fc {
}
}

template<typename Stream, typename T>
inline void unpack( Stream& s, std::set<T>& value ) {
template<typename Stream, typename... T>
inline void unpack( Stream& s, std::set<T...>& value ) {
unsigned_int size; fc::raw::unpack( s, size );
for( uint64_t i = 0; i < size.value; ++i )
{
typename std::set<T...>::value_type tmp;
fc::raw::unpack( s, tmp );
value.insert( std::move(tmp) );
}
}

template<typename Stream, typename... T>
inline void pack( Stream& s, const std::multiset<T...>& value ) {
fc::raw::pack( s, unsigned_int((uint32_t)value.size()) );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
fc::raw::pack( s, *itr );
++itr;
}
}

template<typename Stream, typename... T>
inline void unpack( Stream& s, std::multiset<T...>& value ) {
unsigned_int size; fc::raw::unpack( s, size );
for( uint64_t i = 0; i < size.value; ++i )
{
T tmp;
typename std::multiset<T...>::value_type tmp;
fc::raw::unpack( s, tmp );
value.insert( std::move(tmp) );
}
Expand Down
6 changes: 4 additions & 2 deletions include/fc/io/raw_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ namespace fc {



template<typename Stream, typename T> inline void pack( Stream& s, const std::set<T>& value );
template<typename Stream, typename T> inline void unpack( Stream& s, std::set<T>& value );
template<typename Stream, typename... T> inline void pack( Stream& s, const std::set<T...>& value );
template<typename Stream, typename... T> inline void unpack( Stream& s, std::set<T...>& value );
template<typename Stream, typename... T> inline void pack( Stream& s, const std::multiset<T...>& value );
template<typename Stream, typename... T> inline void unpack( Stream& s, std::multiset<T...>& value );
template<typename Stream, typename T> inline void pack( Stream& s, const std::unordered_set<T>& value );
template<typename Stream, typename T> inline void unpack( Stream& s, std::unordered_set<T>& value );

Expand Down
42 changes: 33 additions & 9 deletions include/fc/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,15 @@ namespace fc
template<typename T>
void from_variant( const variant& var, fc::flat_set<T>& vo );

template<typename T>
void to_variant( const std::set<T>& var, variant& vo );
template<typename T>
void from_variant( const variant& var, std::set<T>& vo );
template<typename... T>
void to_variant( const std::set<T...>& var, variant& vo );
template<typename... T>
void from_variant( const variant& var, std::set<T...>& vo );

template<typename... T>
void to_variant( const std::multiset<T...>& var, variant& vo );
template<typename... T>
void from_variant( const variant& var, std::multiset<T...>& vo );

void to_variant( const time_point& var, variant& vo );
void from_variant( const variant& var, time_point& vo );
Expand Down Expand Up @@ -453,23 +458,42 @@ namespace fc
}


template<typename T>
void to_variant( const std::set<T>& var, variant& vo )
template<typename... T>
void to_variant( const std::set<T...>& var, variant& vo )
{
std::vector<variant> vars(var.size());
size_t i = 0;
for( auto itr = var.begin(); itr != var.end(); ++itr, ++i )
vars[i] = variant(*itr);
vo = vars;
}
template<typename T>
void from_variant( const variant& var, std::set<T>& vo )
template<typename... T>
void from_variant( const variant& var, std::set<T...>& vo )
{
const variants& vars = var.get_array();
vo.clear();
//vo.reserve( vars.size() );
for( auto itr = vars.begin(); itr != vars.end(); ++itr )
vo.insert( itr->as<T>() );
vo.insert( itr->as<typename std::set<T...>::value_type>() );
}

template<typename... T>
void to_variant( const std::multiset<T...>& var, variant& vo )
{
std::vector<variant> vars(var.size());
size_t i = 0;
for( auto itr = var.begin(); itr != var.end(); ++itr, ++i )
vars[i] = variant(*itr);
vo = vars;
}
template<typename... T>
void from_variant( const variant& var, std::multiset<T...>& vo )
{
const variants& vars = var.get_array();
vo.clear();
//vo.reserve( vars.size() );
for( auto itr = vars.begin(); itr != vars.end(); ++itr )
vo.insert( itr->as<typename std::multiset<T...>::value_type>() );
}

/** @ingroup Serializable */
Expand Down