From 0caac3fee90253964cb99ea0cf2cfc6aa77ae350 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Thu, 5 Mar 2015 08:22:41 -0500 Subject: [PATCH] update exceptions in static_variant and add header gaurd --- include/fc/static_variant.hpp | 80 ++++++++++++++++++++++++----------- include/fc/variant.hpp | 44 +------------------ 2 files changed, 58 insertions(+), 66 deletions(-) diff --git a/include/fc/static_variant.hpp b/include/fc/static_variant.hpp index 01c132f..a8e2c62 100644 --- a/include/fc/static_variant.hpp +++ b/include/fc/static_variant.hpp @@ -9,8 +9,10 @@ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * **/ +#pragma once #include #include +#include namespace fc { @@ -109,39 +111,27 @@ struct storage_ops { template struct storage_ops { static void del(int n, void *data) { - throw std::runtime_error( - "Internal error: static_variant tag is invalid." - ); + FC_ASSERT( !"Internal error: static_variant tag is invalid."); } static void con(int n, void *data) { - throw std::runtime_error( - "Internal error: static_variant tag is invalid." - ); + FC_ASSERT( !"Internal error: static_variant tag is invalid." ); } template static typename visitor::result_type apply(int n, void *data, visitor& v) { - throw std::runtime_error( - "Internal error: static_variant tag is invalid." - ); + FC_ASSERT( !"Internal error: static_variant tag is invalid." ); } template static typename visitor::result_type apply(int n, void *data, const visitor& v) { - throw std::runtime_error( - "Internal error: static_variant tag is invalid." - ); + FC_ASSERT( !"Internal error: static_variant tag is invalid." ); } template static typename visitor::result_type apply(int n, const void *data, visitor& v) { - throw std::runtime_error( - "Internal error: static_variant tag is invalid." - ); + FC_ASSERT( !"Internal error: static_variant tag is invalid." ); } template static typename visitor::result_type apply(int n, const void *data, const visitor& v) { - throw std::runtime_error( - "Internal error: static_variant tag is invalid." - ); + FC_ASSERT( !"Internal error: static_variant tag is invalid." ); } }; @@ -285,9 +275,10 @@ public: if(_tag == impl::position::pos) { return *reinterpret_cast(storage); } else { - throw std::runtime_error( - std::string("static_variant does not contain value of type ") + typeid(X).name() - ); + FC_ASSERT( !"static_variant does not contain a value of type", + "type ${t}", ("t",fc::get_typename::name()) ); + // std::string("static_variant does not contain value of type ") + typeid(X).name() + // ); } } template @@ -299,9 +290,8 @@ public: if(_tag == impl::position::pos) { return *reinterpret_cast(storage); } else { - throw std::runtime_error( - std::string("static_variant does not contain value of type ") + typeid(X).name() - ); + FC_ASSERT( !"static_variant does not contain a value of type", + "type ${t}", ("t",fc::get_typename::name()) ); } } template @@ -337,4 +327,46 @@ struct visitor { typedef Result result_type; }; + struct from_static_variant + { + variant& var; + from_static_variant( variant& dv ):var(dv){} + + typedef void result_type; + template void operator()( const T& v )const + { + to_variant( v, var ); + } + }; + + struct to_static_variant + { + const variant& var; + to_static_variant( const variant& dv ):var(dv){} + + typedef void result_type; + template void operator()( T& v )const + { + to_variant( var, v ); + } + }; + + + template void to_variant( const fc::static_variant& s, fc::variant& v ) + { + variant tmp; + variants vars(2); + vars[0] = s.which(); + s.visit( from_static_variant(vars[1]) ); + v = std::move(vars); + } + template void from_variant( const fc::variant& v, fc::static_variant& s ) + { + auto ar = v.get_array(); + if( ar.size() ) return; + s.set_which( ar[0].as_uint64() ); + if( ar.size() < 1 ) return; + s.visit( to_static_variant(ar[1]) ); + } + } // namespace fc diff --git a/include/fc/variant.hpp b/include/fc/variant.hpp index 72375ef..9772787 100644 --- a/include/fc/variant.hpp +++ b/include/fc/variant.hpp @@ -9,7 +9,6 @@ #include #include #include -#include namespace fc { @@ -33,6 +32,8 @@ namespace fc class time_point_sec; class microseconds; template struct safe; + template + class static_variant; struct blob { std::vector data; }; @@ -502,47 +503,6 @@ namespace fc } } - struct from_static_variant - { - variant& var; - from_static_variant( variant& dv ):var(dv){} - - typedef void result_type; - template void operator()( const T& v )const - { - to_variant( v, var ); - } - }; - - struct to_static_variant - { - const variant& var; - to_static_variant( const variant& dv ):var(dv){} - - typedef void result_type; - template void operator()( T& v )const - { - to_variant( var, v ); - } - }; - - - template void to_variant( const fc::static_variant& s, fc::variant& v ) - { - variant tmp; - variants vars(2); - vars[0] = s.which(); - s.visit( from_static_variant(vars[1]) ); - v = std::move(vars); - } - template void from_variant( const fc::variant& v, fc::static_variant& s ) - { - auto ar = v.get_array(); - if( ar.size() ) return; - s.set_which( ar[0].as_uint64() ); - if( ar.size() < 1 ) return; - s.visit( to_static_variant(ar[1]) ); - } template void to_variant( const safe& s, variant& v ) { v = s.value; }