changeset 322:0e80e4589533

Json: make sure m_handle is never empty
author David Demelier <markand@malikania.fr>
date Fri, 27 Feb 2015 18:15:33 +0100
parents 0eb59106f700
children c5dd79aaa216 d52a69f9f029
files C++/Json.h
diffstat 1 files changed, 37 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/C++/Json.h	Fri Feb 27 08:48:40 2015 +0100
+++ b/C++/Json.h	Fri Feb 27 18:15:33 2015 +0100
@@ -20,7 +20,9 @@
 #define _JSON_H_
 
 #include <algorithm>
+#include <cerrno>
 #include <cstdlib>
+#include <cstring>
 #include <initializer_list>
 #include <fstream>
 #include <iterator>
@@ -179,26 +181,37 @@
 	 */
 	Handle m_handle;
 
+	inline void check() const
+	{
+		if (m_handle == nullptr)
+			throw JsonError(std::strerror(errno));
+	}
+
 public:
 	/**
 	 * Deep copy of that element.
 	 *
 	 * @param value the other value
+	 * @throw JsonError on allocation error
 	 */
 	inline JsonValue(const JsonValue &value)
 		: m_handle(json_deep_copy(value.m_handle.get()), json_decref)
 	{
+		check();
 	}
 
 	/**
 	 * Assign a deep copy of the other element.
 	 *
 	 * @return *this
+	 * @throw JsonError on allocation error
 	 */
 	inline JsonValue &operator=(const JsonValue &value)
 	{
 		m_handle = Handle(json_deep_copy(value.m_handle.get()), json_decref);
 
+		check();
+
 		return *this;
 	}
 
@@ -207,7 +220,7 @@
 	 *
 	 * @param other the other value
 	 */
-	inline JsonValue(JsonValue &&other)
+	inline JsonValue(JsonValue &&other) noexcept
 		: m_handle(std::move(other.m_handle))
 	{
 		other.m_handle = Handle(json_null(), json_decref);
@@ -218,7 +231,7 @@
 	 *
 	 * @param other the other value
 	 */
-	inline JsonValue &operator=(JsonValue &&other)
+	inline JsonValue &operator=(JsonValue &&other) noexcept
 	{
 		m_handle = std::move(other.m_handle);
 		other.m_handle = Handle(json_null(), json_decref);
@@ -248,7 +261,7 @@
 	/**
 	 * Create an empty value (JsonType::Null).
 	 */
-	inline JsonValue()
+	inline JsonValue() noexcept
 		: m_handle(json_null(), json_decref)
 	{
 	}
@@ -258,7 +271,7 @@
 	 *
 	 * @param value the value
 	 */
-	inline JsonValue(bool value)
+	inline JsonValue(bool value) noexcept
 		: m_handle(json_boolean(value), json_decref)
 	{
 	}
@@ -267,50 +280,61 @@
 	 * Create a integer value (JsonType::Integer).
 	 *
 	 * @param value the value
+	 * @throw JsonError on allocation error
 	 */
 	inline JsonValue(int value)
 		: m_handle(json_integer(value), json_decref)
 	{
+		check();
 	}
 
 	/**
 	 * Create a real value (JsonType::Real).
 	 *
 	 * @param value the value
+	 * @throw JsonError on allocation error
 	 */
 	inline JsonValue(double value)
 		: m_handle(json_real(value), json_decref)
 	{
+		check();
 	}
 
 	/**
 	 * Create a string value (JsonType::String).
-	 * @param value
+	 *
+	 * @param value the value
+	 * @throw JsonError on allocation error
 	 */
 	inline JsonValue(std::string value)
 		: m_handle(json_string(value.c_str()), json_decref)
 	{
+		check();
 	}
 
 	/**
 	 * Create from a C string (JsonType::String).
 	 *
 	 * @param value the string
+	 * @throw JsonError on allocation error
 	 */
 	inline JsonValue(const char *value)
 		: m_handle(json_string(value), json_decref)
 	{
+		check();
 	}
 
 	/**
 	 * Create from a string literal (JsonType::String).
 	 *
 	 * @param value the value
+	 * @throw JsonError on allocation error
 	 */
 	template <size_t Size>
 	inline JsonValue(char (&value)[Size])
 		: m_handle(json_string(value), json_decref)
 	{
+		check();
 	}
 
 	/**
@@ -894,16 +918,20 @@
 public:
 	/**
 	 * Create an empty array.
+	 *
+	 * @throw JsonError on allocation error
 	 */
 	inline JsonArray()
 		: JsonValue(json_array())
 	{
+		check();
 	}
 
 	/**
 	 * Create an array from a list of values.
 	 *
 	 * @param list the list
+	 * @throw JsonError on allocation error
 	 */
 	inline JsonArray(std::initializer_list<value_type> list)
 		: JsonArray()
@@ -1322,16 +1350,20 @@
 public:
 	/**
 	 * Create empty object.
+	 *
+	 * @throw JsonError on allocation error
 	 */
 	inline JsonObject()
 		: JsonValue(json_object())
 	{
+		check();
 	}
 
 	/**
 	 * Create a JsonObject from an initializer_list.
 	 *
 	 * @param list the list of key-value pairs
+	 * @throw JsonError on allocation error
 	 */
 	inline JsonObject(std::initializer_list<value_type> list)
 		: JsonObject()