Rename "to_<type>" functions to "as_<type>"
'to' implies a copy, 'as' implies reinterpret.
This commit is contained in:
parent
e6ad6c2db3
commit
45d8cfffbf
@ -36,12 +36,12 @@ colour::operator*= (double v) {
|
|||||||
|
|
||||||
const json::node&
|
const json::node&
|
||||||
operator>> (const json::node &node, colour &c) {
|
operator>> (const json::node &node, colour &c) {
|
||||||
c.red = node[0].to_number ();
|
c.red = node[0].as_number ();
|
||||||
c.green = node[1].to_number ();
|
c.green = node[1].as_number ();
|
||||||
c.blue = node[2].to_number ();
|
c.blue = node[2].as_number ();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
c.alpha = node[3].to_number ();
|
c.alpha = node[3].as_number ();
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
c.alpha = 1.0;
|
c.alpha = 1.0;
|
||||||
}
|
}
|
||||||
|
16
json.cpp.rl
16
json.cpp.rl
@ -281,27 +281,27 @@ json::write (const json::node &node, std::ostream &os)
|
|||||||
|
|
||||||
|
|
||||||
const json::object&
|
const json::object&
|
||||||
json::node::to_object (void) const
|
json::node::as_object (void) const
|
||||||
{ throw parse_error ("node is not an object"); }
|
{ throw parse_error ("node is not an object"); }
|
||||||
|
|
||||||
|
|
||||||
const json::array&
|
const json::array&
|
||||||
json::node::to_array (void) const
|
json::node::as_array (void) const
|
||||||
{ throw parse_error ("node is not an array"); }
|
{ throw parse_error ("node is not an array"); }
|
||||||
|
|
||||||
|
|
||||||
const json::string&
|
const json::string&
|
||||||
json::node::to_string (void) const
|
json::node::as_string (void) const
|
||||||
{ throw parse_error ("node is not a string"); }
|
{ throw parse_error ("node is not a string"); }
|
||||||
|
|
||||||
|
|
||||||
const json::number&
|
const json::number&
|
||||||
json::node::to_number (void) const
|
json::node::as_number (void) const
|
||||||
{ throw parse_error ("node is not a number"); }
|
{ throw parse_error ("node is not a number"); }
|
||||||
|
|
||||||
|
|
||||||
const json::boolean&
|
const json::boolean&
|
||||||
json::node::to_boolean (void) const
|
json::node::as_boolean (void) const
|
||||||
{ throw parse_error ("node is not a boolean"); }
|
{ throw parse_error ("node is not a boolean"); }
|
||||||
|
|
||||||
|
|
||||||
@ -311,17 +311,17 @@ json::node::operator!=(const node &rhs) const
|
|||||||
|
|
||||||
|
|
||||||
bool json::node::operator==(const char *rhs) const
|
bool json::node::operator==(const char *rhs) const
|
||||||
{ return to_string () == rhs; }
|
{ return as_string () == rhs; }
|
||||||
|
|
||||||
|
|
||||||
const json::node&
|
const json::node&
|
||||||
json::node::operator[] (const std::string &key) const
|
json::node::operator[] (const std::string &key) const
|
||||||
{ return to_object ()[key]; }
|
{ return as_object ()[key]; }
|
||||||
|
|
||||||
|
|
||||||
const json::node&
|
const json::node&
|
||||||
json::node::operator[] (unsigned int idx) const
|
json::node::operator[] (unsigned int idx) const
|
||||||
{ return to_array()[idx]; }
|
{ return as_array()[idx]; }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Object
|
* Object
|
||||||
|
20
json.hpp
20
json.hpp
@ -50,11 +50,11 @@ namespace json {
|
|||||||
public:
|
public:
|
||||||
virtual ~node () { ; }
|
virtual ~node () { ; }
|
||||||
|
|
||||||
virtual const object& to_object (void) const;
|
virtual const object& as_object (void) const;
|
||||||
virtual const array& to_array (void) const;
|
virtual const array& as_array (void) const;
|
||||||
virtual const string& to_string (void) const;
|
virtual const string& as_string (void) const;
|
||||||
virtual const number& to_number (void) const;
|
virtual const number& as_number (void) const;
|
||||||
virtual const boolean& to_boolean (void) const;
|
virtual const boolean& as_boolean (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; }
|
||||||
@ -91,7 +91,7 @@ namespace json {
|
|||||||
object () { ; }
|
object () { ; }
|
||||||
virtual ~object ();
|
virtual ~object ();
|
||||||
|
|
||||||
virtual const object& to_object (void) const { return *this; }
|
virtual const object& as_object (void) const { return *this; }
|
||||||
virtual bool is_object (void) const { return true; }
|
virtual bool is_object (void) const { return true; }
|
||||||
virtual bool operator==(const object &rhs) const;
|
virtual bool operator==(const object &rhs) const;
|
||||||
virtual bool operator==(const node &rhs) const
|
virtual bool operator==(const node &rhs) const
|
||||||
@ -116,7 +116,7 @@ namespace json {
|
|||||||
public:
|
public:
|
||||||
virtual ~array();
|
virtual ~array();
|
||||||
|
|
||||||
virtual const array& to_array (void) const { return *this; }
|
virtual const array& as_array (void) const { return *this; }
|
||||||
virtual bool is_array (void) const { return true; }
|
virtual bool is_array (void) const { return true; }
|
||||||
virtual bool operator==(const array &rhs) const;
|
virtual bool operator==(const array &rhs) const;
|
||||||
virtual bool operator==(const node &rhs) const
|
virtual bool operator==(const node &rhs) const
|
||||||
@ -147,7 +147,7 @@ namespace json {
|
|||||||
string (const std::string &_value): m_value (_value) { ; }
|
string (const std::string &_value): m_value (_value) { ; }
|
||||||
string (const char *_value): m_value (_value) { ; }
|
string (const char *_value): m_value (_value) { ; }
|
||||||
|
|
||||||
virtual const string& to_string (void) const { return *this; }
|
virtual const string& as_string (void) const { return *this; }
|
||||||
virtual bool is_string (void) const { return true; }
|
virtual bool is_string (void) const { return true; }
|
||||||
virtual bool operator==(const string &rhs) const;
|
virtual bool operator==(const string &rhs) const;
|
||||||
virtual bool operator==(const node &rhs) const
|
virtual bool operator==(const node &rhs) const
|
||||||
@ -169,7 +169,7 @@ namespace json {
|
|||||||
number (double _value): m_value (_value) { ; }
|
number (double _value): m_value (_value) { ; }
|
||||||
number (int _value): m_value (_value) { ; }
|
number (int _value): m_value (_value) { ; }
|
||||||
|
|
||||||
virtual const number& to_number (void) const { return *this; }
|
virtual const number& as_number (void) const { return *this; }
|
||||||
virtual bool is_number (void) const { return true; }
|
virtual bool is_number (void) const { return true; }
|
||||||
virtual bool operator==(const number &rhs) const;
|
virtual bool operator==(const number &rhs) const;
|
||||||
virtual bool operator==(const node &rhs) const
|
virtual bool operator==(const node &rhs) const
|
||||||
@ -190,7 +190,7 @@ namespace json {
|
|||||||
public:
|
public:
|
||||||
boolean (bool _value): m_value (_value) { ; }
|
boolean (bool _value): m_value (_value) { ; }
|
||||||
|
|
||||||
virtual const boolean& to_boolean (void) const { return *this; }
|
virtual const boolean& as_boolean (void) const { return *this; }
|
||||||
virtual bool is_boolean (void) const { return true; }
|
virtual bool is_boolean (void) const { return true; }
|
||||||
virtual bool operator==(const boolean &rhs) const;
|
virtual bool operator==(const boolean &rhs) const;
|
||||||
virtual bool operator==(const node &rhs) const
|
virtual bool operator==(const node &rhs) const
|
||||||
|
12
range.cpp
12
range.cpp
@ -18,17 +18,17 @@ using namespace util;
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
range<T>::range (const json::node &node) {
|
range<T>::range (const json::node &node) {
|
||||||
if (node.is_string () && (node.to_string () == "UNIT" ||
|
if (node.is_string () && (node.as_string () == "UNIT" ||
|
||||||
node.to_string () == "unit")) {
|
node.as_string () == "unit")) {
|
||||||
min = UNIT.min;
|
min = UNIT.min;
|
||||||
max = UNIT.max;
|
max = UNIT.max;
|
||||||
} else if (node.is_string () && (node.to_string () == "UNLIMITED" ||
|
} else if (node.is_string () && (node.as_string () == "UNLIMITED" ||
|
||||||
node.to_string () == "unlimited")) {
|
node.as_string () == "unlimited")) {
|
||||||
min = UNLIMITED.min;
|
min = UNLIMITED.min;
|
||||||
max = UNLIMITED.max;
|
max = UNLIMITED.max;
|
||||||
} else {
|
} else {
|
||||||
min = node[0].to_number ();
|
min = node[0].as_number ();
|
||||||
max = node[1].to_number ();
|
max = node[1].as_number ();
|
||||||
}
|
}
|
||||||
|
|
||||||
sanity ();
|
sanity ();
|
||||||
|
@ -206,9 +206,9 @@ operator<< (std::ostream &os, const util::vector &v) {
|
|||||||
|
|
||||||
const json::node&
|
const json::node&
|
||||||
operator>> (const json::node &node, util::vector &v) {
|
operator>> (const json::node &node, util::vector &v) {
|
||||||
v.x = node[0].to_number ();
|
v.x = node[0].as_number ();
|
||||||
v.y = node[1].to_number ();
|
v.y = node[1].as_number ();
|
||||||
v.z = node[2].to_number ();
|
v.z = node[2].as_number ();
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user