Return unique_ptr from json routines

This commit is contained in:
Danny Robson 2012-01-04 17:05:40 +11:00
parent af97b70377
commit b302b510c3
2 changed files with 19 additions and 11 deletions

View File

@ -235,16 +235,16 @@ struct parse_context {
*/ */
namespace json { namespace json {
json::node * std::unique_ptr<json::node>
parse (const boost::filesystem::path &path) parse (const boost::filesystem::path &path)
{ return parse ((const char *)slurp (path)); } { return parse ((const char *)slurp (path)); }
json::node * std::unique_ptr<json::node>
parse (const std::string &path) parse (const std::string &path)
{ return parse (path.c_str (), path.c_str () + path.size ()); } { return parse (path.c_str (), path.c_str () + path.size ()); }
node * std::unique_ptr<json::node>
parse (const char *start, parse (const char *start,
const char *stop) { const char *stop) {
bool __success = true; bool __success = true;
@ -265,11 +265,11 @@ namespace json {
//__root->print (cout) << endl; //__root->print (cout) << endl;
assert (*__root == *__root); assert (*__root == *__root);
return __root; return std::unique_ptr<json::node> (__root);
} }
node* std::unique_ptr<json::node>
parse (const char *start) parse (const char *start)
{ return parse (start, start + strlen (start)); } { return parse (start, start + strlen (start)); }
} }
@ -305,6 +305,10 @@ json::node::operator!=(const node &rhs) const
{ return !(*this == rhs); } { return !(*this == rhs); }
bool json::node::operator==(const char *rhs) const
{ return to_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 to_object ()[key]; }

View File

@ -20,9 +20,10 @@
#ifndef __UTIL_JSON_HPP #ifndef __UTIL_JSON_HPP
#define __UTIL_JSON_HPP #define __UTIL_JSON_HPP
#include <map>
#include <string>
#include <iostream> #include <iostream>
#include <map>
#include <memory>
#include <string>
#include <vector> #include <vector>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
@ -37,10 +38,10 @@ namespace json {
class boolean; class boolean;
class null; class null;
extern node* parse (const boost::filesystem::path &path); extern std::unique_ptr<node> parse (const boost::filesystem::path &path);
extern node* parse (const char *start, const char *stop); extern std::unique_ptr<node> parse (const char *start, const char *stop);
extern node* parse (const char *start); extern std::unique_ptr<node> parse (const char *start);
extern node* parse (const std::string&); extern std::unique_ptr<node> parse (const std::string&);
/// Abstract base for all JSON values /// Abstract base for all JSON values
class node { class node {
@ -69,6 +70,9 @@ namespace json {
virtual bool operator==(const boolean &) const { return false; } virtual bool operator==(const boolean &) const { return false; }
virtual bool operator==(const null &) const { return false; } virtual bool operator==(const null &) const { return false; }
virtual bool operator==(const char *rhs) const;
virtual bool operator!=(const char *rhs) const { return !(*this == rhs); }
virtual const node& operator[] (const std::string&) const; virtual const node& operator[] (const std::string&) const;
virtual const node& operator[] (unsigned int) const; virtual const node& operator[] (unsigned int) const;