Permalink
Browse files

Addressed constness issues in the API.

  • Loading branch information...
1 parent 977dec3 commit 26ffa58e5f6b4129f9343464c037a310412f5644 @pibara committed Jul 5, 2011
View
4 JsonMeLib.cpp
@@ -17,10 +17,10 @@ namespace jsonme {
JsonMeLib::~JsonMeLib() {
delete mLibImpl;
}
- Node JsonMeLib::parse(std::string jsonstring){
+ Node JsonMeLib::parse(std::string const & jsonstring) const {
return mLibImpl->parse(jsonstring);
}
- Node JsonMeLib::parseFile(std::string path) {
+ Node JsonMeLib::parseFile(std::string const & path) const {
return mLibImpl->parseFile( path);
}
}
View
24 Node.cpp
@@ -3,45 +3,45 @@
namespace jsonme {
Node::Node():mNode(new NullNode()){}
Node::Node(AbstractNode *node):mNode(node){}
- jsonme::nodetype Node::nodetype() {
+ jsonme::nodetype Node::nodetype() const {
return mNode->nodetype();
}
- Node Node::operator[](std::string name){
+ Node Node::operator[](std::string const & name) const {
return (*mNode)[name];
}
- Node Node::operator[](const char *name) {
+ Node Node::operator[](char const * const name) const {
return (*mNode)[std::string(name)];
}
- size_t Node::size(){
+ size_t Node::size() const {
return mNode->size();
}
- Node Node::operator[](size_t index){
+ Node Node::operator[](size_t index) const {
return (*mNode)[index];
}
- Node::operator Scalar() {
+ Node::operator Scalar() const {
return (*mNode);
}
- jsonme::scalartype Node::scalartype(){
+ jsonme::scalartype Node::scalartype() const{
Scalar s(*mNode);
return s.scalartype();
}
- Node::operator long double() {
+ Node::operator long double() const {
Scalar s(*mNode);
return s;
}
- Node::operator long long() {
+ Node::operator long long() const {
Scalar s(*mNode);
return s;
}
- Node::operator std::string() {
+ Node::operator std::string() const {
Scalar s(*mNode);
return s;
}
- Node::operator bool() {
+ Node::operator bool() const {
Scalar s(*mNode);
return s;
}
- bool Node::isNull() {
+ bool Node::isNull() const {
Scalar s(*mNode);
return s.isNull();
}
View
12 Scalar.cpp
@@ -3,22 +3,22 @@
namespace jsonme {
Scalar::Scalar():mScalar(new NullScalar()){}
Scalar::Scalar(AbstractScalar *scalar):mScalar(scalar){}
- jsonme::scalartype Scalar::scalartype() {
+ jsonme::scalartype Scalar::scalartype() const {
return mScalar->scalartype();
}
- Scalar::operator long double() {
+ Scalar::operator long double() const {
return (*mScalar);
}
- Scalar::operator long long() {
+ Scalar::operator long long() const {
return (*mScalar);
}
- Scalar::operator std::string() {
+ Scalar::operator std::string() const {
return (*mScalar);
}
- Scalar::operator bool() {
+ Scalar::operator bool() const {
return (*mScalar);
}
- bool Scalar::isNull() {
+ bool Scalar::isNull() const {
return mScalar->isNull();
}
}
View
12 gobj/GobjectImplFsTopNode.cpp
@@ -4,7 +4,7 @@
#include "GobjectImplScalar.hpp"
namespace jsonme {
namespace impl {
- GobjectImplFsTopNode::GobjectImplFsTopNode(std::string path):mRoot(0) {
+ GobjectImplFsTopNode::GobjectImplFsTopNode(std::string const & path):mRoot(0) {
GobjectImplError lerror;
json_parser_load_from_file (mParser,path.c_str(), lerror.errorp());
if (lerror.error()) {
@@ -15,19 +15,19 @@ namespace jsonme {
GobjectImplFsTopNode::~GobjectImplFsTopNode() {
delete mRoot;
}
- jsonme::nodetype GobjectImplFsTopNode::nodetype() {
+ jsonme::nodetype GobjectImplFsTopNode::nodetype() const {
return mRoot->nodetype();
}
- Node GobjectImplFsTopNode::operator[](std::string name) {
+ Node GobjectImplFsTopNode::operator[](std::string const & name) const {
return (*mRoot)[name];
}
- size_t GobjectImplFsTopNode::size() {
+ size_t GobjectImplFsTopNode::size() const {
return mRoot->size();
}
- Node GobjectImplFsTopNode::operator[](size_t index) {
+ Node GobjectImplFsTopNode::operator[](size_t index) const {
return (*mRoot)[index];
}
- GobjectImplFsTopNode::operator Scalar() {
+ GobjectImplFsTopNode::operator Scalar() const {
return (*mRoot);
}
}
View
12 gobj/GobjectImplFsTopNode.hpp
@@ -9,13 +9,13 @@ namespace jsonme {
GobjectImplParser mParser;
GobjectImplNode *mRoot;
public:
- GobjectImplFsTopNode(std::string jsonstring);
+ GobjectImplFsTopNode(std::string const & jsonstring);
~GobjectImplFsTopNode();
- jsonme::nodetype nodetype();
- Node operator[](std::string name);
- size_t size();
- Node operator[](size_t index);
- operator Scalar();
+ jsonme::nodetype nodetype() const;
+ Node operator[](std::string const & name) const;
+ size_t size() const;
+ Node operator[](size_t index) const;
+ operator Scalar() const;
};
}
}
View
12 gobj/GobjectImplNode.cpp
@@ -4,9 +4,9 @@
#include "GobjectImplScalar.hpp"
namespace jsonme {
namespace impl {
- GobjectImplNode::GobjectImplNode(JsonNode *node):mNode(node) {
+ GobjectImplNode::GobjectImplNode(JsonNode * const node):mNode(node) {
}
- jsonme::nodetype GobjectImplNode::nodetype() {
+ jsonme::nodetype GobjectImplNode::nodetype() const {
if (!mNode) {
return jsonme::INVALID;
}
@@ -18,7 +18,7 @@ namespace jsonme {
}
return jsonme::INVALID;
}
- Node GobjectImplNode::operator[](std::string name) {
+ Node GobjectImplNode::operator[](std::string const & name) const {
if (!mNode) {
return Node();
}
@@ -34,7 +34,7 @@ namespace jsonme {
return Node();
}
}
- size_t GobjectImplNode::size() {
+ size_t GobjectImplNode::size() const {
if (!mNode) {
return 0;
}
@@ -45,7 +45,7 @@ namespace jsonme {
return 0;
}
}
- Node GobjectImplNode::operator[](size_t index) {
+ Node GobjectImplNode::operator[](size_t index) const {
if (!mNode) {
return Node(new GobjectImplNode(mNode));
}
@@ -61,7 +61,7 @@ namespace jsonme {
return Node();
}
}
- GobjectImplNode::operator Scalar() {
+ GobjectImplNode::operator Scalar() const {
if (this->nodetype() == jsonme::SCALAR) {
return Scalar(new GobjectImplScalar(mNode));
} else {
View
14 gobj/GobjectImplNode.hpp
@@ -6,14 +6,14 @@
namespace jsonme {
namespace impl {
class GobjectImplNode: public AbstractNode {
- JsonNode *mNode;
+ JsonNode * const mNode;
public:
- GobjectImplNode(JsonNode *node);
- jsonme::nodetype nodetype();
- Node operator[](std::string name);
- size_t size();
- Node operator[](size_t index);
- operator Scalar();
+ GobjectImplNode(JsonNode * const node);
+ jsonme::nodetype nodetype() const ;
+ Node operator[](std::string const & name) const ;
+ size_t size() const;
+ Node operator[](size_t index) const ;
+ operator Scalar() const;
};
}
}
View
14 gobj/GobjectImplScalar.cpp
@@ -2,12 +2,12 @@
#include <boost/lexical_cast.hpp>
namespace jsonme {
namespace impl {
- GobjectImplScalar::GobjectImplScalar(JsonNode *node):mNode(node),mValue(){
+ GobjectImplScalar::GobjectImplScalar(JsonNode * const node):mNode(node),mValue(){
if (mNode && JSON_NODE_HOLDS_VALUE(mNode)) {
json_node_get_value(mNode,&mValue);
}
}
- jsonme::scalartype GobjectImplScalar::scalartype() {
+ jsonme::scalartype GobjectImplScalar::scalartype() const {
switch (json_node_get_value_type(mNode)) {
case G_TYPE_BOOLEAN :
return jsonme::BOOL;
@@ -26,7 +26,7 @@ namespace jsonme {
return jsonme::NULLVAL;
}
}
- GobjectImplScalar::operator long double(){
+ GobjectImplScalar::operator long double() const {
switch (json_node_get_value_type(mNode)) {
case G_TYPE_FLOAT: return g_value_get_float(&mValue);
case G_TYPE_DOUBLE: return g_value_get_double(&mValue);
@@ -40,7 +40,7 @@ namespace jsonme {
default: return 0.0;
}
}
- GobjectImplScalar::operator long long(){
+ GobjectImplScalar::operator long long() const {
switch (json_node_get_value_type(mNode)) {
case G_TYPE_FLOAT: return 0; //Fixme, probably an exception is in place.
case G_TYPE_DOUBLE: return 0; //Fixme, probably an exception is in place.
@@ -54,7 +54,7 @@ namespace jsonme {
default: return 0;
}
}
- GobjectImplScalar::operator std::string(){
+ GobjectImplScalar::operator std::string() const {
if (! mNode) {
return "";
}
@@ -71,7 +71,7 @@ namespace jsonme {
default: return "";
}
}
- GobjectImplScalar::operator bool(){
+ GobjectImplScalar::operator bool() const {
switch (json_node_get_value_type(mNode)) {
case G_TYPE_FLOAT: return false;
case G_TYPE_DOUBLE: return false;
@@ -85,7 +85,7 @@ namespace jsonme {
default: return false;
}
}
- bool GobjectImplScalar::isNull(){
+ bool GobjectImplScalar::isNull() const {
if (json_node_is_null(mNode)) {
return true;
}
View
16 gobj/GobjectImplScalar.hpp
@@ -6,16 +6,16 @@
namespace jsonme {
namespace impl {
class GobjectImplScalar: public AbstractScalar {
- JsonNode *mNode;
+ JsonNode * const mNode;
GValue mValue;
public:
- GobjectImplScalar(JsonNode *node);
- jsonme::scalartype scalartype();
- operator long double();
- operator long long();
- operator std::string();
- operator bool();
- bool isNull();
+ GobjectImplScalar(JsonNode * const node);
+ jsonme::scalartype scalartype() const;
+ operator long double() const;
+ operator long long() const;
+ operator std::string() const;
+ operator bool() const;
+ bool isNull() const;
};
}
}
View
12 gobj/GobjectImplStringTopNode.cpp
@@ -4,7 +4,7 @@
#include "GobjectImplScalar.hpp"
namespace jsonme {
namespace impl {
- GobjectImplStringTopNode::GobjectImplStringTopNode(std::string jsonstring):mRoot(0) {
+ GobjectImplStringTopNode::GobjectImplStringTopNode(std::string const & jsonstring):mRoot(0) {
GobjectImplError lerror;
json_parser_load_from_data (mParser,jsonstring.c_str(),jsonstring.size(),lerror.errorp());
if (lerror.error()) {
@@ -15,19 +15,19 @@ namespace jsonme {
GobjectImplStringTopNode::~GobjectImplStringTopNode() {
delete mRoot;
}
- jsonme::nodetype GobjectImplStringTopNode::nodetype() {
+ jsonme::nodetype GobjectImplStringTopNode::nodetype() const {
return mRoot->nodetype();
}
- Node GobjectImplStringTopNode::operator[](std::string name) {
+ Node GobjectImplStringTopNode::operator[](std::string const & name) const {
return (*mRoot)[name];
}
- size_t GobjectImplStringTopNode::size() {
+ size_t GobjectImplStringTopNode::size() const {
return mRoot->size();
}
- Node GobjectImplStringTopNode::operator[](size_t index) {
+ Node GobjectImplStringTopNode::operator[](size_t index) const {
return (*mRoot)[index];
}
- GobjectImplStringTopNode::operator Scalar() {
+ GobjectImplStringTopNode::operator Scalar() const {
return (*mRoot);
}
}
View
14 gobj/GobjectImplStringTopNode.hpp
@@ -8,15 +8,15 @@ namespace jsonme {
namespace impl {
class GobjectImplStringTopNode: public jsonme::AbstractNode {
GobjectImplParser mParser;
- GobjectImplNode *mRoot;
+ GobjectImplNode const * mRoot;
public:
- GobjectImplStringTopNode(std::string jsonstring);
+ GobjectImplStringTopNode(std::string const & jsonstring);
~GobjectImplStringTopNode();
- jsonme::nodetype nodetype();
- Node operator[](std::string name);
- size_t size();
- Node operator[](size_t index);
- operator Scalar();
+ jsonme::nodetype nodetype() const ;
+ Node operator[](std::string const & name) const ;
+ size_t size() const;
+ Node operator[](size_t index) const;
+ operator Scalar() const;
};
}
}
View
5 gobj/GobjectLibImpl.cpp
@@ -8,11 +8,10 @@ namespace jsonme {
GobjectLibImpl::GobjectLibImpl() {
g_type_init ();
}
- AbstractNode *GobjectLibImpl::parse(std::string jsonstring) {
+ AbstractNode *GobjectLibImpl::parse(std::string const & jsonstring) const {
return new GobjectImplStringTopNode(jsonstring);
}
- AbstractNode *GobjectLibImpl::parseFile(std::string path) {
-
+ AbstractNode *GobjectLibImpl::parseFile(std::string const & path) const {
return new GobjectImplFsTopNode(path);
}
}
View
4 gobj/impl.hpp
@@ -8,8 +8,8 @@ namespace jsonme {
class GobjectLibImpl: public AbstractLibImpl {
public:
GobjectLibImpl();
- AbstractNode *parse(std::string jsonstring);
- AbstractNode *parseFile(std::string path);
+ AbstractNode *parse(std::string const & jsonstring) const;
+ AbstractNode *parseFile(std::string const & path) const ;
};
}
}
View
88 json-me.hpp
@@ -24,35 +24,35 @@ namespace jsonme {
class AbstractScalar {
public:
virtual ~AbstractScalar(){}
- virtual jsonme::scalartype scalartype()=0;
- virtual operator long double()=0;
- virtual operator long long()=0;
- virtual operator std::string()=0;
- virtual operator bool()=0;
- virtual bool isNull()=0;
+ virtual jsonme::scalartype scalartype() const=0;
+ virtual operator long double() const=0;
+ virtual operator long long() const=0;
+ virtual operator std::string() const=0;
+ virtual operator bool() const=0;
+ virtual bool isNull() const=0;
};
//A value semantics proxy to the implementation specific scalar.
class Scalar;
class NullScalar: public AbstractScalar {
public:
- jsonme::scalartype scalartype(){ return NULLVAL;}
- operator long double(){ return 0;}
- operator long long(){ return 0;}
- operator std::string(){ return "";}
- operator bool(){ return false;}
- bool isNull(){ return true;}
+ jsonme::scalartype scalartype() const { return NULLVAL;}
+ operator long double() const { return 0;}
+ operator long long() const { return 0;}
+ operator std::string() const { return "";}
+ operator bool() const { return false;}
+ bool isNull() const { return true;}
};
class Scalar: public AbstractScalar {
boost::shared_ptr<AbstractScalar> mScalar;
public:
Scalar();
Scalar(AbstractScalar *scalar);
- jsonme::scalartype scalartype();
- operator long double();
- operator long long();
- operator std::string();
- operator bool();
- bool isNull();
+ jsonme::scalartype scalartype() const;
+ operator long double() const;
+ operator long long() const;
+ operator std::string() const;
+ operator bool() const;
+ bool isNull() const;
};
//A json node is either an object, an array or a scalar.
typedef enum {OBJECT,ARRAY,SCALAR,INVALID} nodetype;
@@ -65,11 +65,11 @@ namespace jsonme {
class AbstractNode {
public:
virtual ~AbstractNode(){}
- virtual jsonme::nodetype nodetype()=0;
- virtual Node operator[](std::string name)=0;
- virtual size_t size()=0;
- virtual Node operator[](size_t index)=0;
- virtual operator Scalar()=0;
+ virtual jsonme::nodetype nodetype() const=0;
+ virtual Node operator[](std::string const & name) const=0;
+ virtual size_t size() const=0;
+ virtual Node operator[](size_t index) const=0;
+ virtual operator Scalar() const=0;
};
//A value semantics proxy to the implementation specific node.
class Node: public AbstractNode, public AbstractScalar {
@@ -78,35 +78,35 @@ namespace jsonme {
private:
class NullNode: public AbstractNode {
public:
- jsonme::nodetype nodetype() {return INVALID;}
- Node operator[](std::string name) { return Node();}
- size_t size() { return 0;}
- Node operator[](size_t index) { return Node();}
- operator Scalar() { return Scalar(); }
+ jsonme::nodetype nodetype() const {return INVALID;}
+ Node operator[](std::string const & name) const { return Node();}
+ size_t size() const { return 0;}
+ Node operator[](size_t index) const { return Node();}
+ operator Scalar() const { return Scalar(); }
};
boost::shared_ptr<AbstractNode> mNode;
public:
Node(AbstractNode *node);
- jsonme::nodetype nodetype();
- Node operator[](std::string name);
- Node operator[](const char *);
- size_t size();
- Node operator[](size_t index);
- jsonme::scalartype scalartype();
- operator Scalar();
- operator long double();
- operator long long();
- operator std::string();
- operator bool();
- bool isNull();
+ jsonme::nodetype nodetype() const;
+ Node operator[](std::string const & name) const;
+ Node operator[](char const * const) const;
+ size_t size() const;
+ Node operator[](size_t index) const ;
+ jsonme::scalartype scalartype() const;
+ operator Scalar() const;
+ operator long double() const;
+ operator long long() const;
+ operator std::string() const;
+ operator bool() const;
+ bool isNull() const;
};
//The impl namespace should be non of your concern.
namespace impl {
class AbstractLibImpl {
public:
virtual ~AbstractLibImpl(){}
- virtual AbstractNode *parse(std::string jsonstring)=0;
- virtual AbstractNode *parseFile(std::string jsonstring)=0;
+ virtual AbstractNode *parse(std::string const & jsonstring) const =0;
+ virtual AbstractNode *parseFile(std::string const & jsonstring) const=0;
};
};
//Wrapper class for an available low level json library.
@@ -115,8 +115,8 @@ namespace jsonme {
public:
JsonMeLib();
~JsonMeLib();
- Node parse(std::string jsonstring);
- Node parseFile(std::string path);
+ Node parse(std::string const & jsonstring) const;
+ Node parseFile(std::string const & path) const;
};
}

0 comments on commit 26ffa58

Please sign in to comment.