move static functions out of Value, add test driver to ensure linking works properly
authorSean Middleditch <sean@middleditch.us>
Tue, 19 Jan 2010 02:50:13 +0000 (18:50 -0800)
committerSean Middleditch <sean@middleditch.us>
Tue, 19 Jan 2010 02:50:13 +0000 (18:50 -0800)
Makefile
driver.cpp [new file with mode: 0644]
jansson-impl.hpp
jansson.hpp
test.cpp

index 7eb4984..7d4931c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,8 +3,8 @@ JANSSON_LIBS := $(shell pkg-config --libs jansson)
 
 all: test
 
-test-bin: test.cpp jansson.hpp jansson-impl.hpp Makefile
-       $(CXX) -o $@ -g -O0 -Wall $(JANSSON_CFLAGS) $< $(JANSSON_LIBS)
+test-bin: driver.cpp test.cpp jansson.hpp jansson-impl.hpp Makefile
+       $(CXX) -o $@ -g -O0 -Wall $(JANSSON_CFLAGS) driver.cpp test.cpp $(JANSSON_LIBS)
 
 test: test-bin
        ./test-bin
diff --git a/driver.cpp b/driver.cpp
new file mode 100644 (file)
index 0000000..f42831e
--- /dev/null
@@ -0,0 +1,7 @@
+#include "jansson.hpp"
+
+extern int json_cpp_tests();
+
+int main() {
+       return json_cpp_tests();
+}
index 6c2e134..6242ed9 100644 (file)
@@ -300,7 +300,7 @@ namespace json {
                }
 
                // take ownership of a json_t (does not increase reference count)
-               Basic Basic::_take(json_t* json) {
+               Basic Basic::take_ownership(json_t* json) {
                        Basic v;
                        v._value = json;
                        return v;
@@ -328,121 +328,121 @@ namespace json {
                }
 
        } // namespace json::_private
-
-       // construct Value from input
-       Value Value::from(const char* value) {
-               return Value::_take(json_string(value));
+       
+       // construct a new iterator for a given object
+       Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
+               _iter = json_object_iter(_object.as_json());
        }
 
-       Value Value::from(const std::string& value) {
-               return Value::from(value.c_str());
+       // construct a new iterator for a given object
+       Iterator::Iterator(const _private::ValueBase<_private::PropertyProxy>& value) :
+                       _object(value.as_json()), _iter(0) {
+               _iter = json_object_iter(_object.as_json());
        }
 
-       Value Value::from(bool value) {
-               return Value::_take(value ? json_true() : json_false());
+       // increment iterator
+       void Iterator::next() {
+               _iter = json_object_iter_next(_object.as_json(), _iter);
        }
 
-       Value Value::from(signed int value) {
-               return Value::_take(json_integer(value));
-       }
+       Iterator& Iterator::operator++() { next(); return *this; }
 
-       Value Value::from(unsigned int value) {
-               return Value::_take(json_integer(value));
+       // test if iterator is still valid
+       bool Iterator::valid() const {
+               return _iter != 0;
        }
 
-       Value Value::from(signed short value) {
-               return Value::_take(json_integer(value));
+       Iterator::operator bool() const {
+               return valid();
        }
 
-       Value Value::from(unsigned short value) {
-               return Value::_take(json_integer(value));
+       // get key
+       const char* Iterator::ckey() const {
+               return json_object_iter_key(_iter);
        }
 
-       Value Value::from(signed long value) {
-               return Value::_take(json_integer(value));
+       std::string Iterator::key() const {
+               return ckey();
        }
 
-       Value Value::from(unsigned long value) {
-               return Value::_take(json_integer(value));
+       // get value
+       const Value Iterator::value() const {
+               return Value(json_object_iter_value(_iter));
        }
 
-       Value Value::from(float value) {
-               return Value::_take(json_real(value));
+       // dereference value
+       const Value Iterator::operator*() const {
+               return value();
        }
 
-       Value Value::from(double value) {
-               return Value::_take(json_real(value));
+       // construct Value from input
+       Value from(const char* value) {
+               return Value::take_ownership(json_string(value));
        }
 
-       // create a new empty object
-       Value Value::object() {
-               return Value::_take(json_object());
+       Value from(const std::string& value) {
+               return from(value.c_str());
        }
 
-       // create a new empty array
-       Value Value::array() {
-               return Value::_take(json_array());
+       Value from(bool value) {
+               return Value::take_ownership(value ? json_true() : json_false());
        }
 
-       // create a new null value
-       Value Value::null() {
-               return Value::_take(json_null());
+       Value from(signed int value) {
+               return Value::take_ownership(json_integer(value));
        }
 
-       // load a file as a JSON value
-       Value Value::load_file(const char* path, json_error_t* error) {
-               return Value::_take(json_load_file(path, error));
+       Value from(unsigned int value) {
+               return Value::take_ownership(json_integer(value));
        }
 
-       // load a string as a JSON value
-       Value Value::load_string(const char* string, json_error_t* error) {
-               return Value::_take(json_loads(string, error));
+       Value from(signed short value) {
+               return Value::take_ownership(json_integer(value));
        }
-       
-       // construct a new iterator for a given object
-       Iterator::Iterator(const Value& value) : _object(value), _iter(0) {
-               _iter = json_object_iter(_object.as_json());
+
+       Value from(unsigned short value) {
+               return Value::take_ownership(json_integer(value));
        }
 
-       // construct a new iterator for a given object
-       Iterator::Iterator(const _private::ValueBase<_private::PropertyProxy>& value) :
-                       _object(value.as_json()), _iter(0) {
-               _iter = json_object_iter(_object.as_json());
+       Value from(signed long value) {
+               return Value::take_ownership(json_integer(value));
        }
 
-       // increment iterator
-       void Iterator::next() {
-               _iter = json_object_iter_next(_object.as_json(), _iter);
+       Value from(unsigned long value) {
+               return Value::take_ownership(json_integer(value));
        }
 
-       Iterator& Iterator::operator++() { next(); return *this; }
+       Value from(float value) {
+               return Value::take_ownership(json_real(value));
+       }
 
-       // test if iterator is still valid
-       bool Iterator::valid() const {
-               return _iter != 0;
+       Value from(double value) {
+               return Value::take_ownership(json_real(value));
        }
 
-       Iterator::operator bool() const {
-               return valid();
+       // create a new empty object
+       Value object() {
+               return Value::take_ownership(json_object());
        }
 
-       // get key
-       const char* Iterator::ckey() const {
-               return json_object_iter_key(_iter);
+       // create a new empty array
+       Value array() {
+               return Value::take_ownership(json_array());
        }
 
-       std::string Iterator::key() const {
-               return ckey();
+       // create a new null value
+       Value null() {
+               return Value::take_ownership(json_null());
        }
 
-       // get value
-       const Value Iterator::value() const {
-               return Value(json_object_iter_value(_iter));
+       // load a file as a JSON value
+       Value load_file(const char* path, json_error_t* error) {
+               return Value::take_ownership(json_load_file(path, error));
        }
 
-       // dereference value
-       const Value Iterator::operator*() const {
-               return value();
+       // load a string as a JSON value
+       Value load_string(const char* string, json_error_t* error) {
+               return Value::take_ownership(json_loads(string, error));
        }
 
 } // namespace json
@@ -466,6 +466,6 @@ std::istream& operator>>(std::istream& is, json::Value& value) {
        while (is)
                tmp << static_cast<char>(is.get());
        // parse the buffered string
-       value = json::Value::load_string(tmp.str().c_str());
+       value = json::load_string(tmp.str().c_str());
        return is;
 }
index 9df3961..fc81d98 100644 (file)
@@ -150,9 +150,8 @@ namespace json {
                        // get the underlying json_t
                        inline json_t* as_json() const;
 
-               protected:
                        // take ownership of a json_t (does not increase reference count)
-                       static inline Basic _take(json_t* json);
+                       static inline Basic take_ownership(json_t* json);
 
                private:
                        // internal value pointer
@@ -218,34 +217,6 @@ namespace json {
 
                // create reference to value
                explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
-
-               // construct Value from input
-               static inline Value from(const char* value);
-               static inline Value from(const std::string& value);
-               static inline Value from(bool value);
-               static inline Value from(signed int value);
-               static inline Value from(unsigned int value);
-               static inline Value from(signed short value);
-               static inline Value from(unsigned short value);
-               static inline Value from(signed long value);
-               static inline Value from(unsigned long value);
-               static inline Value from(float value);
-               static inline Value from(double value);
-
-               // create a new empty object
-               static inline Value object();
-
-               // create a new empty array
-               static inline Value array();
-
-               // create a new null value
-               static inline Value null();
-
-               // load a file as a JSON value
-               static inline Value load_file(const char* path, json_error_t* error = 0);
-
-               // load a string as a JSON value
-               static inline Value load_string(const char* string, json_error_t* error = 0);
        };
 
        // iterators over a JSON object
@@ -290,6 +261,34 @@ namespace json {
                void* _iter;
        };
 
+       // construct Value from input
+       inline Value from(const char* value);
+       inline Value from(const std::string& value);
+       inline Value from(bool value);
+       inline Value from(signed int value);
+       inline Value from(unsigned int value);
+       inline Value from(signed short value);
+       inline Value from(unsigned short value);
+       inline Value from(signed long value);
+       inline Value from(unsigned long value);
+       inline Value from(float value);
+       inline Value from(double value);
+
+       // create a new empty object
+       inline Value object();
+
+       // create a new empty array
+       inline Value array();
+
+       // create a new null value
+       inline Value null();
+
+       // load a file as a JSON value
+       inline Value load_file(const char* path, json_error_t* error = 0);
+
+       // load a string as a JSON value
+       inline Value load_string(const char* string, json_error_t* error = 0);
+
 } // namespace json 
 
 // stream JSON value out -- inefficient and not recommended for production use
index cc10689..cfadd1c 100644 (file)
--- a/test.cpp
+++ b/test.cpp
@@ -4,8 +4,6 @@
 
 #include "jansson.hpp"
 
-using namespace std;
-
 #define ASSERT_OP(lhs, rhs, op, m) \
        do { \
                if(!((lhs) op (rhs))) { \
@@ -21,11 +19,11 @@ using namespace std;
 #define ASSERT_TRUE(p, m) ASSERT_OP(p, true, ==, m)
 #define ASSERT_FALSE(p, m) ASSERT_OP(p, true, !=, m)
 
-int main() {
-       json::Value e1(json::Value::load_file("test.json"));
+int json_cpp_tests() {
+       json::Value e1(json::load_file("test.json"));
        json::Value e2(e1);
        json::Value e3;
-       json::Value e4(json::Value::load_string("{\"foo\": true, \"bar\": \"test\"}"));
+       json::Value e4(json::load_string("{\"foo\": true, \"bar\": \"test\"}"));
 
        ASSERT_TRUE(e1.is_object(), "e1 is not an object");
        ASSERT_TRUE(e2.is_object(), "e2 is not an object");
@@ -51,46 +49,46 @@ int main() {
        i.next();
        ASSERT_FALSE(i.valid(), "iterator has more values than expected");
 
-       json::Value e5(json::Value::from(12.34));
+       json::Value e5(json::from(12.34));
        ASSERT_TRUE(e5.is_number(), "e5 is not a number after assignment");
        ASSERT_EQ(e5.as_real(), 12.34, "e5 has incorrect value after assignment");
 
-       json::Value e6(json::Value::from(true));
+       json::Value e6(json::from(true));
        ASSERT_TRUE(e6.is_boolean(), "e6 is not a boolean after assignment");
        ASSERT_EQ(e6.as_boolean(), true, "e6 has incorrect value after assignment");
 
-       json::Value e7(json::Value::from("foobar"));
+       json::Value e7(json::from("foobar"));
        ASSERT_TRUE(e7.is_string(), "e7 is not a string after assignment");
        ASSERT_EQ(e7.as_string(), "foobar", "e7 has incorrect value after assignment");
 
-       json::Value e8(json::Value::object());
+       json::Value e8(json::object());
        ASSERT_TRUE(e8.is_object(), "e8 is not an object after assignment");
 
-       json::Value e9(json::Value::null());
+       json::Value e9(json::null());
        ASSERT_TRUE(e9.is_null(), "e9 is not null after assignment");
 
-       json::Value e10(json::Value::array());
+       json::Value e10(json::array());
        ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment");
 
-       e10.set_at(0, json::Value::from("foobar"));
+       e10.set_at(0, json::from("foobar"));
        ASSERT_EQ(e10.size(), 1, "e10 has incorrect number of elements after assignment");
        ASSERT_EQ(e10[0].as_string(), "foobar", "e10[0] has incorrect value after assignment");
 
-       e10.set_at(1, json::Value::from("foobar"));
+       e10.set_at(1, json::from("foobar"));
        ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment");
        ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment");
        ASSERT_EQ(e10[1].as_string(), "foobar", "e10[0] has incorrect value after assignment");
 
-       e10.set_at(0, json::Value::from("barfoo"));
+       e10.set_at(0, json::from("barfoo"));
        ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment");
        ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment");
        ASSERT_EQ(e10[0].as_string(), "barfoo", "e10[0] has incorrect value after assignment");
 
-       e10.set_at(100, json::Value::null());
+       e10.set_at(100, json::null());
        ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment");
        ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment");
 
-       e10.insert_at(1, json::Value::from("new"));
+       e10.insert_at(1, json::from("new"));
        ASSERT_EQ(e10.size(), 3, "e10 has incorrect size after insert");
        ASSERT_EQ(e10[1].as_string(), "new", "e10[1] has incorrect value after insert");
        ASSERT_EQ(e10[2].as_string(), "foobar", "e10[2] has incorrect value after insert");
@@ -102,19 +100,19 @@ int main() {
        e10.clear();
        ASSERT_EQ(e10.size(), 0, "e10 has incorrect number of elements after clear");
 
-       json::Value e11(json::Value::object());
+       json::Value e11(json::object());
        ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment");
 
-       e11.set_key("foo", json::Value::from("test"));
+       e11.set_key("foo", json::from("test"));
        ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment");
        ASSERT_EQ(e11["foo"].as_string(), "test", "e11.foo has incorrect value after assignment");
 
-       e11.set_key("foo", json::Value::from("again"));
+       e11.set_key("foo", json::from("again"));
        ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment");
        ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment");
        ASSERT_EQ(e11["foo"].as_string(), "again", "e11.foo has incorrect value after assignment");
 
-       e11.set_key("bar", json::Value::from("test"));
+       e11.set_key("bar", json::from("test"));
        ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment");
        ASSERT_EQ(e11.size(), 2, "e11 has incorrect number of properties after assignment");
        ASSERT_EQ(e11["bar"].as_string(), "test", "e11.foo has incorrect value after assignment");
@@ -122,11 +120,11 @@ int main() {
        e11.clear();
        ASSERT_EQ(e11.size(), 0, "e11 has incorrect number of properties after clear");
 
-       json::Value e12(json::Value::object());
-       e12.set_key("foo", json::Value::from("test"));
-       e12.set_key("bar", json::Value::from(3));
+       json::Value e12(json::object());
+       e12.set_key("foo", json::from("test"));
+       e12.set_key("bar", json::from(3));
        char* out_cstr = e12.save_string(0);
-       string out(out_cstr);
+       std::string out(out_cstr);
        free(out_cstr);
        ASSERT_EQ(out, "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected");
 
@@ -144,18 +142,18 @@ int main() {
        const json::Value e13(e12);
        ASSERT_EQ(e13["bar"].as_integer(), 3, "e13.bar has incorrect value after copy");
 
-       json::Value e14(json::Value::object());
+       json::Value e14(json::object());
        ASSERT_TRUE(e14.is_object(), "e14 is not an object after construction");
-       e14.set_key("foo", json::Value::object());
+       e14.set_key("foo", json::object());
        ASSERT_TRUE(e14["foo"].is_object(), "e14.foo is not an object after assignment");
-       e14["foo"]["bar"] = json::Value::from(42);
+       e14["foo"]["bar"] = json::from(42);
        ASSERT_EQ(e14["foo"]["bar"].as_integer(), 42, "e14.foo.bar has incorrect value after assignment");
 
-       json::Value e15(json::Value::array());
+       json::Value e15(json::array());
        ASSERT_TRUE(e15.is_array(), "e15 is not an array after construction");
-       e15.set_at(0, json::Value::from(42));
+       e15.set_at(0, json::from(42));
        ASSERT_EQ(e15[0].as_integer(), 42, "e15[0] has incorrect value after assignment");
-       e15[0] = json::Value::from("foo");
+       e15[0] = json::from("foo");
        ASSERT_EQ(e15[0].as_string(), "foo", "e15[0] has incorrecy value after assignment");
        return 0;
 }