From cb69e2385b7887c2d588a0372e06f5108f84b04e Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Wed, 11 Jun 2014 21:46:26 -0400 Subject: [PATCH] exception updates --- include/fc/exception/exception.hpp | 4 +++- include/fc/optional.hpp | 9 +++++---- src/exception.cpp | 10 +++++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/fc/exception/exception.hpp b/include/fc/exception/exception.hpp index 4b1a12a..19ce75f 100644 --- a/include/fc/exception/exception.hpp +++ b/include/fc/exception/exception.hpp @@ -30,7 +30,8 @@ namespace fc eof_exception_code = 11, std_exception_code = 13, invalid_operation_exception_code = 14, - unknown_host_exception_code = 15 + unknown_host_exception_code = 15, + null_optional_code = 16 }; /** @@ -276,6 +277,7 @@ namespace fc */ FC_DECLARE_EXCEPTION( assert_exception, assert_exception_code, "Assert Exception" ); FC_DECLARE_EXCEPTION( eof_exception, eof_exception_code, "End Of File" ); + FC_DECLARE_EXCEPTION( null_optional, null_optional_code, "null optional" ); std::string except_str(); diff --git a/include/fc/optional.hpp b/include/fc/optional.hpp index a4e774d..6adea6a 100644 --- a/include/fc/optional.hpp +++ b/include/fc/optional.hpp @@ -9,6 +9,7 @@ namespace fc { # pragma warning(disable:4521) /* multiple copy ctors */ # pragma warning(disable:4522) /* multiple assignment operators */ #endif + bool assert_optional(bool is_valid ); // defined in exception.cpp /** * @brief provides stack-based nullable value similar to boost::optional @@ -188,17 +189,17 @@ namespace fc { // casts and comparisons, use valid() or !! explicit operator bool()const { return _valid; } - T& operator*() { assert(_valid); return ref(); } - const T& operator*()const { assert(_valid); return ref(); } + T& operator*() { assert(assert_optional(_valid)); return ref(); } + const T& operator*()const { assert(assert_optional(_valid)); return ref(); } T* operator->() { - assert( _valid ); + assert( assert_optional(_valid) ); return ptr(); } const T* operator->()const { - assert( _valid ); + assert( assert_optional(_valid) ); return ptr(); } diff --git a/src/exception.cpp b/src/exception.cpp index db45da0..816b60c 100644 --- a/src/exception.cpp +++ b/src/exception.cpp @@ -11,13 +11,14 @@ namespace fc (parse_error_exception) (invalid_arg_exception) (invalid_operation_exception) - (unknown_host_exception) (key_not_found_exception) (bad_cast_exception) (out_of_range_exception) (canceled_exception) (assert_exception) (eof_exception) + (unknown_host_exception) + (null_optional) ) namespace detail @@ -195,4 +196,11 @@ namespace fc ("key",k)("enum",e) ); } + bool assert_optional(bool is_valid ) + { + if( !is_valid ) + throw null_optional(); + return true; + } + } // fc