j/tree: add as_{uint,float,double}
This commit is contained in:
parent
166b97e0c5
commit
0db3188a49
@ -26,6 +26,7 @@
|
||||
#include "debug.hpp"
|
||||
#include "io.hpp"
|
||||
#include "maths.hpp"
|
||||
#include "types/casts.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
@ -235,6 +236,35 @@ json::tree::node::as_null (void) const
|
||||
{ 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
|
||||
|
||||
|
@ -60,6 +60,10 @@ namespace json { namespace tree {
|
||||
virtual const boolean& as_boolean (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_array (void) const { return false; }
|
||||
virtual bool is_string (void) const { return false; }
|
||||
|
Loading…
Reference in New Issue
Block a user