diff options
author | Philip Withnall <philip.withnall@collabora.co.uk> | 2015-11-07 14:17:31 +0100 |
---|---|---|
committer | Emmanuele Bassi <ebassi@gnome.org> | 2016-03-01 14:53:00 +0000 |
commit | 58f479b60eb2db4c73605d469d68a8ffd8679327 (patch) | |
tree | b414ce65be7a8d493234fcf325521a69d0ea5bd4 /json-glib/json-value.c | |
parent | a82b93ba60dd0f54660990df86ba0cf7fc74c9a8 (diff) | |
download | json-glib-58f479b60eb2db4c73605d469d68a8ffd8679327.tar.gz |
core: Add immutability support to core objects
Add an immutable mode to JsonNode, JsonObject, JsonArray and JsonValue.
This is an optional mode which objects enter by calling json_*_seal().
It is a one-way transition, which means that we can build and manipulate
objects as much as desired, before sealing them and enjoying the
benefits of immutable objects: no need to take copies when handling
them, persistent hash values (still to be implemented).
https://bugzilla.gnome.org/show_bug.cgi?id=756121
Diffstat (limited to 'json-glib/json-value.c')
-rw-r--r-- | json-glib/json-value.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/json-glib/json-value.c b/json-glib/json-value.c index b6d47e1..148cf8d 100644 --- a/json-glib/json-value.c +++ b/json-glib/json-value.c @@ -2,6 +2,7 @@ * * This file is part of JSON-GLib * Copyright (C) 2012 Emmanuele Bassi <ebassi@gnome.org> + * Copyright (C) 2015 Collabora Ltd. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -18,6 +19,7 @@ * * Author: * Emmanuele Bassi <ebassi@linux.intel.com> + * Philip Withnall <philip.withnall@collabora.co.uk> */ #include "config.h" @@ -165,12 +167,32 @@ json_value_free (JsonValue *value) } } +/** + * json_value_seal: + * @value: a #JsonValue + * + * Seals the #JsonValue, making it immutable to further changes. + * + * If the @value is already immutable, this is a no-op. + * + * Since: UNRELEASED + */ +void +json_value_seal (JsonValue *value) +{ + g_return_if_fail (JSON_VALUE_IS_VALID (value)); + g_return_if_fail (value->ref_count > 0); + + value->immutable = TRUE; +} + #define _JSON_VALUE_DEFINE_SET(Type,EType,CType,VField) \ void \ json_value_set_##Type (JsonValue *value, CType VField) \ { \ g_return_if_fail (JSON_VALUE_IS_VALID (value)); \ g_return_if_fail (JSON_VALUE_HOLDS (value, JSON_VALUE_##EType)); \ + g_return_if_fail (!value->immutable); \ \ value->data.VField = VField; \ \ @@ -202,6 +224,7 @@ json_value_set_string (JsonValue *value, { g_return_if_fail (JSON_VALUE_IS_VALID (value)); g_return_if_fail (JSON_VALUE_HOLDS_STRING (value)); + g_return_if_fail (!value->immutable); g_free (value->data.v_str); value->data.v_str = g_strdup (v_str); |