Skip to content

Commit

Permalink
Fix json Val type check
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalif committed Jan 5, 2016
1 parent b6c0013 commit 4a4e220
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion libgrive/src/json/Val.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ std::string Val::Str() const

Val::operator std::string() const
{
return As<std::string>() ;
return Str();
}

int Val::Int() const
Expand Down
22 changes: 8 additions & 14 deletions libgrive/src/json/Val.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
7 changes: 4 additions & 3 deletions libgrive/test/btest/ValTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "json/Val.hh"
#include <boost/test/unit_test.hpp>
#include <iostream>

using namespace gr ;

Expand All @@ -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 ) ;
}
Expand Down

0 comments on commit 4a4e220

Please sign in to comment.