j/tree: add as_{uint,float,double}

This commit is contained in:
Danny Robson 2015-02-16 23:34:50 +11:00
parent 166b97e0c5
commit 0db3188a49
2 changed files with 34 additions and 0 deletions

View File

@ -26,6 +26,7 @@
#include "debug.hpp" #include "debug.hpp"
#include "io.hpp" #include "io.hpp"
#include "maths.hpp" #include "maths.hpp"
#include "types/casts.hpp"
#include <algorithm> #include <algorithm>
#include <cstdlib> #include <cstdlib>
@ -235,6 +236,35 @@ json::tree::node::as_null (void) const
{ throw json::type_error ("node is not a null"); } { throw json::type_error ("node is not a null"); }
///////////////////////////////////////////////////////////////////////////////
float
json::tree::node::as_float (void) const
{
return static_cast<float> (as_number ().native ());
}
//-----------------------------------------------------------------------------
double
json::tree::node::as_double (void) const
{
return as_number ().native ();
}
//-----------------------------------------------------------------------------
size_t
json::tree::node::as_uint (void) const
{
auto val = as_number ().native ();
if (!is_integer (val))
throw json::type_error ("cast fractional value to uint");
// TODO: use trunc_cast
return static_cast<size_t> (val);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Global operatoers // Global operatoers

View File

@ -60,6 +60,10 @@ namespace json { namespace tree {
virtual const boolean& as_boolean (void) const; virtual const boolean& as_boolean (void) const;
virtual const null& as_null (void) const; virtual const null& as_null (void) const;
virtual float as_float (void) const;
virtual double as_double (void) const;
virtual size_t as_uint (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; }