j/tree: add templated type conversions

This commit is contained in:
Danny Robson 2015-09-08 14:33:39 +10:00
parent e6b15d2a85
commit c2589abc8e
2 changed files with 51 additions and 0 deletions

View File

@ -321,6 +321,54 @@ json::tree::node::as_chars (void) const
} }
///////////////////////////////////////////////////////////////////////////////
namespace json { namespace tree {
template <>
bool node::as (void) const
{
return as_bool ();
}
} }
//-----------------------------------------------------------------------------
namespace json { namespace tree {
template <>
float json::tree::node::as (void) const
{
return as_float ();
}
} }
//-----------------------------------------------------------------------------
namespace json { namespace tree {
template <>
double
json::tree::node::as (void) const
{
return as_double ();
}
} }
//-----------------------------------------------------------------------------
#define AS_INTEGRAL(T) \
namespace json { namespace tree { \
template <> \
T \
node::as (void) const \
{ \
return static_cast<T> (as_double ()); \
} \
} }
AS_INTEGRAL(uint8_t)
AS_INTEGRAL(uint16_t)
AS_INTEGRAL(uint32_t)
AS_INTEGRAL(uint64_t)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Global operatoers // Global operatoers
bool bool

View File

@ -75,6 +75,9 @@ namespace json { namespace tree {
virtual size_t as_uint (void) const; virtual size_t as_uint (void) const;
virtual const char* as_chars (void) const; virtual const char* as_chars (void) const;
template <typename T>
T as (void) const;
virtual bool is_object (void) const { return false; } virtual bool is_object (void) const { return false; }
virtual bool is_array (void) const { return false; } virtual bool is_array (void) const { return false; }
virtual bool is_string (void) const { return false; } virtual bool is_string (void) const { return false; }