From 3c3b76920e7ca5431d116b6bce4b6ff43e8b7061 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sun, 4 Nov 2018 13:54:05 +0100 Subject: [PATCH 1/2] Use function pointers instead of std::function objects in static variant visitors --- include/fc/static_variant.hpp | 46 ++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/include/fc/static_variant.hpp b/include/fc/static_variant.hpp index 1b2628b..a1f8583 100644 --- a/include/fc/static_variant.hpp +++ b/include/fc/static_variant.hpp @@ -178,33 +178,39 @@ struct type_info<> { } // namespace impl -template -std::vector> init_wrappers() +template +static const fc::array + init_wrappers( Visitor& v, Data d, typename Visitor::result_type(**funcs)(Visitor&,Data) = 0) { - return std::vector>(); + return fc::array(); } -template -std::vector> init_wrappers() +template +static const fc::array + init_wrappers( Visitor& v, Data d, typename Visitor::result_type(**funcs)(Visitor&,Data) = 0 ) { - std::vector> result - = init_wrappers(); - result.insert( result.begin(), [] ( Visitor& v, Data d ) { return v( *reinterpret_cast( d ) ); } ); + fc::array result; + if( !funcs ) funcs = result.begin(); + *funcs++ = [] ( Visitor& v, Data d ) { return v( *reinterpret_cast( d ) ); }; + init_wrappers( v, d, funcs ); return result; } -template -std::vector> init_const_wrappers() +template +static const fc::array + init_const_wrappers( Visitor& v, Data d, typename Visitor::result_type(**funcs)(Visitor&,Data) = 0 ) { - return std::vector>(); + return fc::array(); } -template -std::vector> init_const_wrappers() +template +static const fc::array + init_const_wrappers( Visitor& v, Data d, typename Visitor::result_type(**funcs)(Visitor&,Data) = 0 ) { - std::vector> result - = init_const_wrappers(); - result.insert( result.begin(), [] ( Visitor& v, Data d ) { return v( *reinterpret_cast( d ) ); } ); + fc::array result; + if( !funcs ) funcs = result.begin(); + *funcs++ = [] ( Visitor& v, Data d ) { return v( *reinterpret_cast( d ) ); }; + init_const_wrappers( v, d, funcs ); return result; } @@ -361,7 +367,7 @@ public: template static typename visitor::result_type visit( tag_type tag, visitor& v, void* data ) { - static auto wrappers = init_wrappers(); + static const auto wrappers = init_wrappers::count,visitor,void*,Types...>( v, data ); FC_ASSERT( tag >= 0 && tag < count(), "Unsupported type ${tag}!", ("tag",tag) ); return wrappers[tag]( v, data ); } @@ -369,7 +375,7 @@ public: template static typename visitor::result_type visit( tag_type tag, const visitor& v, void* data ) { - static auto wrappers = init_wrappers(); + static const auto wrappers = init_wrappers::count,const visitor,void*,Types...>( v, data ); FC_ASSERT( tag >= 0 && tag < count(), "Unsupported type ${tag}!", ("tag",tag) ); return wrappers[tag]( v, data ); } @@ -377,7 +383,7 @@ public: template static typename visitor::result_type visit( tag_type tag, visitor& v, const void* data ) { - static auto wrappers = init_const_wrappers(); + static const auto wrappers = init_const_wrappers::count,visitor,const void*,Types...>( v, data ); FC_ASSERT( tag >= 0 && tag < count(), "Unsupported type ${tag}!", ("tag",tag) ); return wrappers[tag]( v, data ); } @@ -385,7 +391,7 @@ public: template static typename visitor::result_type visit( tag_type tag, const visitor& v, const void* data ) { - static auto wrappers = init_const_wrappers(); + static const auto wrappers = init_const_wrappers::count,const visitor,const void*,Types...>( v, data ); FC_ASSERT( tag >= 0 && tag < count(), "Unsupported type ${tag}!", ("tag",tag) ); return wrappers[tag]( v, data ); } From ed4a300fd56a5b1857ce43c17f6d3a39958e7bf5 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sun, 4 Nov 2018 21:14:29 +0100 Subject: [PATCH 2/2] Added missing include --- include/fc/static_variant.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/fc/static_variant.hpp b/include/fc/static_variant.hpp index a1f8583..f5227af 100644 --- a/include/fc/static_variant.hpp +++ b/include/fc/static_variant.hpp @@ -14,6 +14,8 @@ #include #include #include + +#include #include namespace fc {