Rename "to_<type>" functions to "as_<type>"

'to' implies a copy, 'as' implies reinterpret.
This commit is contained in:
Danny Robson 2012-04-12 16:08:06 +10:00
parent e6ad6c2db3
commit 45d8cfffbf
5 changed files with 31 additions and 31 deletions

View File

@ -36,12 +36,12 @@ colour::operator*= (double v) {
const json::node&
operator>> (const json::node &node, colour &c) {
c.red = node[0].to_number ();
c.green = node[1].to_number ();
c.blue = node[2].to_number ();
c.red = node[0].as_number ();
c.green = node[1].as_number ();
c.blue = node[2].as_number ();
try {
c.alpha = node[3].to_number ();
c.alpha = node[3].as_number ();
} catch (...) {
c.alpha = 1.0;
}

View File

@ -281,27 +281,27 @@ json::write (const json::node &node, std::ostream &os)
const json::object&
json::node::to_object (void) const
json::node::as_object (void) const
{ throw parse_error ("node is not an object"); }
const json::array&
json::node::to_array (void) const
json::node::as_array (void) const
{ throw parse_error ("node is not an array"); }
const json::string&
json::node::to_string (void) const
json::node::as_string (void) const
{ throw parse_error ("node is not a string"); }
const json::number&
json::node::to_number (void) const
json::node::as_number (void) const
{ throw parse_error ("node is not a number"); }
const json::boolean&
json::node::to_boolean (void) const
json::node::as_boolean (void) const
{ 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
{ return to_string () == rhs; }
{ return as_string () == rhs; }
const json::node&
json::node::operator[] (const std::string &key) const
{ return to_object ()[key]; }
{ return as_object ()[key]; }
const json::node&
json::node::operator[] (unsigned int idx) const
{ return to_array()[idx]; }
{ return as_array()[idx]; }
/*
* Object

View File

@ -50,11 +50,11 @@ namespace json {
public:
virtual ~node () { ; }
virtual const object& to_object (void) const;
virtual const array& to_array (void) const;
virtual const string& to_string (void) const;
virtual const number& to_number (void) const;
virtual const boolean& to_boolean (void) const;
virtual const object& as_object (void) const;
virtual const array& as_array (void) const;
virtual const string& as_string (void) const;
virtual const number& as_number (void) const;
virtual const boolean& as_boolean (void) const;
virtual bool is_object (void) const { return false; }
virtual bool is_array (void) const { return false; }
@ -91,7 +91,7 @@ namespace json {
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 operator==(const object &rhs) const;
virtual bool operator==(const node &rhs) const
@ -116,7 +116,7 @@ namespace json {
public:
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 operator==(const array &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 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 operator==(const string &rhs) const;
virtual bool operator==(const node &rhs) const
@ -169,7 +169,7 @@ namespace json {
number (double _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 operator==(const number &rhs) const;
virtual bool operator==(const node &rhs) const
@ -190,7 +190,7 @@ namespace json {
public:
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 operator==(const boolean &rhs) const;
virtual bool operator==(const node &rhs) const

View File

@ -18,17 +18,17 @@ using namespace util;
template <typename T>
range<T>::range (const json::node &node) {
if (node.is_string () && (node.to_string () == "UNIT" ||
node.to_string () == "unit")) {
if (node.is_string () && (node.as_string () == "UNIT" ||
node.as_string () == "unit")) {
min = UNIT.min;
max = UNIT.max;
} else if (node.is_string () && (node.to_string () == "UNLIMITED" ||
node.to_string () == "unlimited")) {
} else if (node.is_string () && (node.as_string () == "UNLIMITED" ||
node.as_string () == "unlimited")) {
min = UNLIMITED.min;
max = UNLIMITED.max;
} else {
min = node[0].to_number ();
max = node[1].to_number ();
min = node[0].as_number ();
max = node[1].as_number ();
}
sanity ();

View File

@ -206,9 +206,9 @@ operator<< (std::ostream &os, const util::vector &v) {
const json::node&
operator>> (const json::node &node, util::vector &v) {
v.x = node[0].to_number ();
v.y = node[1].to_number ();
v.z = node[2].to_number ();
v.x = node[0].as_number ();
v.y = node[1].as_number ();
v.z = node[2].as_number ();
return node;
}