Fix json Val type check

pull/59/head
Vitaliy Filippov 2016-01-06 02:21:33 +03:00
parent b6c0013052
commit 4a4e22026b
3 changed files with 13 additions and 18 deletions

View File

@ -123,7 +123,7 @@ std::string Val::Str() const
Val::operator std::string() const
{
return As<std::string>() ;
return Str();
}
int Val::Int() const

View File

@ -194,35 +194,29 @@ Val& Val::Assign( const T& t )
template <typename T>
const T& Val::As() const
{
try
{
const Impl<T> *impl = dynamic_cast<const Impl<T> *>( m_base.get() ) ;
return impl->val ;
}
catch ( std::exception& e )
const Impl<T> *impl = dynamic_cast<const Impl<T> *>( m_base.get() ) ;
if ( !impl )
{
TypeEnum dest = Type2Enum<T>::type ;
BOOST_THROW_EXCEPTION(
Error() << SrcType_(Type()) << DestType_(dest)
Error() << SrcType_( Type() ) << DestType_( dest )
) ;
}
return impl->val ;
}
template <typename T>
T& Val::As()
{
try
{
Impl<T> *impl = dynamic_cast<Impl<T> *>( m_base.get() ) ;
return impl->val ;
}
catch ( std::exception& e )
Impl<T> *impl = dynamic_cast<Impl<T> *>( m_base.get() ) ;
if ( !impl )
{
TypeEnum dest = Type2Enum<T>::type ;
BOOST_THROW_EXCEPTION(
Error() << SrcType_(Type()) << DestType_(dest)
Error() << SrcType_( Type() ) << DestType_( dest )
) ;
}
return impl->val ;
}
template <typename T>

View File

@ -19,6 +19,7 @@
#include "json/Val.hh"
#include <boost/test/unit_test.hpp>
#include <iostream>
using namespace gr ;
@ -33,11 +34,11 @@ BOOST_FIXTURE_TEST_SUITE( ValTest, Fixture )
BOOST_AUTO_TEST_CASE( TestSimpleTypes )
{
Val null ;
BOOST_CHECK_EQUAL( null.Type(), Val::null_type ) ;
BOOST_CHECK( null.Is<void>() ) ;
BOOST_CHECK_EQUAL( Val::Null().Type(), Val::null_type ) ;
BOOST_CHECK( Val::Null().Is<void>() ) ;
Val i( 100 ) ;
BOOST_CHECK_EQUAL( i.Str(), "100" );
BOOST_CHECK_EQUAL( i.As<long long>(), 100 ) ;
BOOST_CHECK_EQUAL( i.Type(), Val::int_type ) ;
}