exception updates

This commit is contained in:
Daniel Larimer 2014-06-11 21:46:26 -04:00
parent 454da57a32
commit cb69e2385b
3 changed files with 17 additions and 6 deletions

View file

@ -30,7 +30,8 @@ namespace fc
eof_exception_code = 11, eof_exception_code = 11,
std_exception_code = 13, std_exception_code = 13,
invalid_operation_exception_code = 14, 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( assert_exception, assert_exception_code, "Assert Exception" );
FC_DECLARE_EXCEPTION( eof_exception, eof_exception_code, "End Of File" ); 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(); std::string except_str();

View file

@ -9,6 +9,7 @@ namespace fc {
# pragma warning(disable:4521) /* multiple copy ctors */ # pragma warning(disable:4521) /* multiple copy ctors */
# pragma warning(disable:4522) /* multiple assignment operators */ # pragma warning(disable:4522) /* multiple assignment operators */
#endif #endif
bool assert_optional(bool is_valid ); // defined in exception.cpp
/** /**
* @brief provides stack-based nullable value similar to boost::optional * @brief provides stack-based nullable value similar to boost::optional
@ -188,17 +189,17 @@ namespace fc {
// casts and comparisons, use valid() or !! // casts and comparisons, use valid() or !!
explicit operator bool()const { return _valid; } explicit operator bool()const { return _valid; }
T& operator*() { assert(_valid); return ref(); } T& operator*() { assert(assert_optional(_valid)); return ref(); }
const T& operator*()const { assert(_valid); return ref(); } const T& operator*()const { assert(assert_optional(_valid)); return ref(); }
T* operator->() T* operator->()
{ {
assert( _valid ); assert( assert_optional(_valid) );
return ptr(); return ptr();
} }
const T* operator->()const const T* operator->()const
{ {
assert( _valid ); assert( assert_optional(_valid) );
return ptr(); return ptr();
} }

View file

@ -11,13 +11,14 @@ namespace fc
(parse_error_exception) (parse_error_exception)
(invalid_arg_exception) (invalid_arg_exception)
(invalid_operation_exception) (invalid_operation_exception)
(unknown_host_exception)
(key_not_found_exception) (key_not_found_exception)
(bad_cast_exception) (bad_cast_exception)
(out_of_range_exception) (out_of_range_exception)
(canceled_exception) (canceled_exception)
(assert_exception) (assert_exception)
(eof_exception) (eof_exception)
(unknown_host_exception)
(null_optional)
) )
namespace detail namespace detail
@ -195,4 +196,11 @@ namespace fc
("key",k)("enum",e) ); ("key",k)("enum",e) );
} }
bool assert_optional(bool is_valid )
{
if( !is_valid )
throw null_optional();
return true;
}
} // fc } // fc