mirror of https://github.com/vitalif/grive2
Fix json Val type check
parent
b6c0013052
commit
4a4e22026b
|
@ -123,7 +123,7 @@ std::string Val::Str() const
|
||||||
|
|
||||||
Val::operator std::string() const
|
Val::operator std::string() const
|
||||||
{
|
{
|
||||||
return As<std::string>() ;
|
return Str();
|
||||||
}
|
}
|
||||||
|
|
||||||
int Val::Int() const
|
int Val::Int() const
|
||||||
|
|
|
@ -194,35 +194,29 @@ Val& Val::Assign( const T& t )
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const T& Val::As() const
|
const T& Val::As() const
|
||||||
{
|
{
|
||||||
try
|
const Impl<T> *impl = dynamic_cast<const Impl<T> *>( m_base.get() ) ;
|
||||||
{
|
if ( !impl )
|
||||||
const Impl<T> *impl = dynamic_cast<const Impl<T> *>( m_base.get() ) ;
|
|
||||||
return impl->val ;
|
|
||||||
}
|
|
||||||
catch ( std::exception& e )
|
|
||||||
{
|
{
|
||||||
TypeEnum dest = Type2Enum<T>::type ;
|
TypeEnum dest = Type2Enum<T>::type ;
|
||||||
BOOST_THROW_EXCEPTION(
|
BOOST_THROW_EXCEPTION(
|
||||||
Error() << SrcType_(Type()) << DestType_(dest)
|
Error() << SrcType_( Type() ) << DestType_( dest )
|
||||||
) ;
|
) ;
|
||||||
}
|
}
|
||||||
|
return impl->val ;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T& Val::As()
|
T& Val::As()
|
||||||
{
|
{
|
||||||
try
|
Impl<T> *impl = dynamic_cast<Impl<T> *>( m_base.get() ) ;
|
||||||
{
|
if ( !impl )
|
||||||
Impl<T> *impl = dynamic_cast<Impl<T> *>( m_base.get() ) ;
|
|
||||||
return impl->val ;
|
|
||||||
}
|
|
||||||
catch ( std::exception& e )
|
|
||||||
{
|
{
|
||||||
TypeEnum dest = Type2Enum<T>::type ;
|
TypeEnum dest = Type2Enum<T>::type ;
|
||||||
BOOST_THROW_EXCEPTION(
|
BOOST_THROW_EXCEPTION(
|
||||||
Error() << SrcType_(Type()) << DestType_(dest)
|
Error() << SrcType_( Type() ) << DestType_( dest )
|
||||||
) ;
|
) ;
|
||||||
}
|
}
|
||||||
|
return impl->val ;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include "json/Val.hh"
|
#include "json/Val.hh"
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using namespace gr ;
|
using namespace gr ;
|
||||||
|
|
||||||
|
@ -33,11 +34,11 @@ BOOST_FIXTURE_TEST_SUITE( ValTest, Fixture )
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE( TestSimpleTypes )
|
BOOST_AUTO_TEST_CASE( TestSimpleTypes )
|
||||||
{
|
{
|
||||||
Val null ;
|
BOOST_CHECK_EQUAL( Val::Null().Type(), Val::null_type ) ;
|
||||||
BOOST_CHECK_EQUAL( null.Type(), Val::null_type ) ;
|
BOOST_CHECK( Val::Null().Is<void>() ) ;
|
||||||
BOOST_CHECK( null.Is<void>() ) ;
|
|
||||||
|
|
||||||
Val i( 100 ) ;
|
Val i( 100 ) ;
|
||||||
|
BOOST_CHECK_EQUAL( i.Str(), "100" );
|
||||||
BOOST_CHECK_EQUAL( i.As<long long>(), 100 ) ;
|
BOOST_CHECK_EQUAL( i.As<long long>(), 100 ) ;
|
||||||
BOOST_CHECK_EQUAL( i.Type(), Val::int_type ) ;
|
BOOST_CHECK_EQUAL( i.Type(), Val::int_type ) ;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue