* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
-#include "gboxed.h"
-#include "gbsearcharray.h"
-#include "gvalue.h"
-#include "gvaluearray.h"
-#include "gclosure.h"
-#include "gvaluecollector.h"
+#include "config.h"
#include <string.h>
-/* --- typedefs & structures --- */
-typedef struct
-{
- GType type;
- GBoxedCopyFunc copy;
- GBoxedFreeFunc free;
-} BoxedNode;
-
-
-/* --- prototypes --- */
-static gint boxed_nodes_cmp (gconstpointer p1,
- gconstpointer p2);
+/* for GValueArray */
+#define GLIB_DISABLE_DEPRECATION_WARNINGS
+#include "gboxed.h"
+#include "gclosure.h"
+#include "gtype-private.h"
+#include "gvalue.h"
+#include "gvaluearray.h"
+#include "gvaluecollector.h"
-/* --- variables --- */
-static GBSearchArray *boxed_bsa = NULL;
-static GBSearchConfig boxed_bconfig = G_STATIC_BCONFIG (sizeof (BoxedNode), boxed_nodes_cmp, 0);
-
-
-/* --- functions --- */
-static gint
-boxed_nodes_cmp (gconstpointer p1,
- gconstpointer p2)
-{
- const BoxedNode *node1 = p1, *node2 = p2;
- return G_BSEARCH_ARRAY_CMP (node1->type, node2->type);
-}
+/**
+ * SECTION:gboxed
+ * @short_description: A mechanism to wrap opaque C structures registered
+ * by the type system
+ * @see_also: #GParamSpecBoxed, g_param_spec_boxed()
+ * @title: Boxed Types
+ *
+ * GBoxed is a generic wrapper mechanism for arbitrary C structures. The only
+ * thing the type system needs to know about the structures is how to copy and
+ * free them, beyond that they are treated as opaque chunks of memory.
+ *
+ * Boxed types are useful for simple value-holder structures like rectangles or
+ * points. They can also be used for wrapping structures defined in non-GObject
+ * based libraries.
+ */
static inline void /* keep this function in sync with gvalue.c */
value_meminit (GValue *value,
memset (value->data, 0, sizeof (value->data));
}
-static gpointer
-value_copy (gpointer boxed)
+static GValue *
+value_copy (GValue *src_value)
{
- const GValue *src_value = boxed;
GValue *dest_value = g_new0 (GValue, 1);
if (G_VALUE_TYPE (src_value))
}
static void
-value_free (gpointer boxed)
+value_free (GValue *value)
{
- GValue *value = boxed;
-
if (G_VALUE_TYPE (value))
g_value_unset (value);
g_free (value);
}
-static gpointer
-gstring_copy (gpointer boxed)
-{
- const GString *src_gstring = boxed;
-
- return g_string_new_len (src_gstring->str, src_gstring->len);
-}
-
-static void
-gstring_free (gpointer boxed)
+static GPollFD *
+pollfd_copy (GPollFD *src)
{
- GString *gstring = boxed;
-
- g_string_free (gstring, TRUE);
+ GPollFD *dest = g_new0 (GPollFD, 1);
+ /* just a couple of integers */
+ memcpy (dest, src, sizeof (GPollFD));
+ return dest;
}
void
-g_boxed_type_init (void) /* sync with gtype.c */
+_g_boxed_type_init (void)
{
- static const GTypeInfo info = {
+ const GTypeInfo info = {
0, /* class_size */
NULL, /* base_init */
NULL, /* base_destroy */
const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
GType type;
- boxed_bsa = g_bsearch_array_new (&boxed_bconfig);
-
/* G_TYPE_BOXED
*/
- type = g_type_register_fundamental (G_TYPE_BOXED, "GBoxed", &info, &finfo,
+ type = g_type_register_fundamental (G_TYPE_BOXED, g_intern_static_string ("GBoxed"), &info, &finfo,
G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
g_assert (type == G_TYPE_BOXED);
}
-GType
-g_closure_get_type (void)
+static GDate *
+gdate_copy (GDate *date)
{
- static GType type_id = 0;
-
- if (!type_id)
- type_id = g_boxed_type_register_static ("GClosure",
- (GBoxedCopyFunc) g_closure_ref,
- (GBoxedFreeFunc) g_closure_unref);
- return type_id;
+ return g_date_new_julian (g_date_get_julian (date));
}
-GType
-g_value_get_type (void)
+static GString *
+gstring_copy (GString *src_gstring)
{
- static GType type_id = 0;
+ return g_string_new_len (src_gstring->str, src_gstring->len);
+}
- if (!type_id)
- type_id = g_boxed_type_register_static ("GValue",
- value_copy,
- value_free);
- return type_id;
+static void
+gstring_free (GString *gstring)
+{
+ g_string_free (gstring, TRUE);
}
+G_DEFINE_BOXED_TYPE (GClosure, g_closure, g_closure_ref, g_closure_unref)
+G_DEFINE_BOXED_TYPE (GValue, g_value, value_copy, value_free)
+G_DEFINE_BOXED_TYPE (GValueArray, g_value_array, g_value_array_copy, g_value_array_free)
+G_DEFINE_BOXED_TYPE (GDate, g_date, gdate_copy, g_date_free)
+/* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
+G_DEFINE_BOXED_TYPE (GString, g_gstring, gstring_copy, gstring_free)
+G_DEFINE_BOXED_TYPE (GHashTable, g_hash_table, g_hash_table_ref, g_hash_table_unref)
+G_DEFINE_BOXED_TYPE (GArray, g_array, g_array_ref, g_array_unref)
+G_DEFINE_BOXED_TYPE (GPtrArray, g_ptr_array,g_ptr_array_ref, g_ptr_array_unref)
+G_DEFINE_BOXED_TYPE (GByteArray, g_byte_array, g_byte_array_ref, g_byte_array_unref)
+G_DEFINE_BOXED_TYPE (GBytes, g_bytes, g_bytes_ref, g_bytes_unref);
+
+G_DEFINE_BOXED_TYPE (GRegex, g_regex, g_regex_ref, g_regex_unref)
+G_DEFINE_BOXED_TYPE (GMatchInfo, g_match_info, g_match_info_ref, g_match_info_unref)
+
+#define g_variant_type_get_type g_variant_type_get_gtype
+G_DEFINE_BOXED_TYPE (GVariantType, g_variant_type, g_variant_type_copy, g_variant_type_free)
+#undef g_variant_type_get_type
+
+G_DEFINE_BOXED_TYPE (GVariantBuilder, g_variant_builder, g_variant_builder_ref, g_variant_builder_unref)
+G_DEFINE_BOXED_TYPE (GVariantDict, g_variant_dict, g_variant_dict_ref, g_variant_dict_unref)
+
+G_DEFINE_BOXED_TYPE (GError, g_error, g_error_copy, g_error_free)
+
+G_DEFINE_BOXED_TYPE (GDateTime, g_date_time, g_date_time_ref, g_date_time_unref);
+G_DEFINE_BOXED_TYPE (GTimeZone, g_time_zone, g_time_zone_ref, g_time_zone_unref);
+G_DEFINE_BOXED_TYPE (GKeyFile, g_key_file, g_key_file_ref, g_key_file_unref)
+G_DEFINE_BOXED_TYPE (GMappedFile, g_mapped_file, g_mapped_file_ref, g_mapped_file_unref)
+
+G_DEFINE_BOXED_TYPE (GMainLoop, g_main_loop, g_main_loop_ref, g_main_loop_unref)
+G_DEFINE_BOXED_TYPE (GMainContext, g_main_context, g_main_context_ref, g_main_context_unref)
+G_DEFINE_BOXED_TYPE (GSource, g_source, g_source_ref, g_source_unref)
+G_DEFINE_BOXED_TYPE (GPollFD, g_pollfd, pollfd_copy, g_free)
+G_DEFINE_BOXED_TYPE (GMarkupParseContext, g_markup_parse_context, g_markup_parse_context_ref, g_markup_parse_context_unref)
+
+G_DEFINE_BOXED_TYPE (GThread, g_thread, g_thread_ref, g_thread_unref)
+G_DEFINE_BOXED_TYPE (GChecksum, g_checksum, g_checksum_copy, g_checksum_free)
+
+/* This one can't use G_DEFINE_BOXED_TYPE (GStrv, g_strv, g_strdupv, g_strfreev) */
GType
-g_value_array_get_type (void)
+g_strv_get_type (void)
{
- static GType type_id = 0;
+ static volatile gsize g_define_type_id__volatile = 0;
- if (!type_id)
- type_id = g_boxed_type_register_static ("GValueArray",
- (GBoxedCopyFunc) g_value_array_copy,
- (GBoxedFreeFunc) g_value_array_free);
- return type_id;
+ if (g_once_init_enter (&g_define_type_id__volatile))
+ {
+ GType g_define_type_id =
+ g_boxed_type_register_static (g_intern_static_string ("GStrv"),
+ (GBoxedCopyFunc) g_strdupv,
+ (GBoxedFreeFunc) g_strfreev);
+
+ g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
+ }
+
+ return g_define_type_id__volatile;
}
+/**
+ * g_variant_get_gtype:
+ *
+ * Since: 2.24
+ * Deprecated: 2.26
+ */
GType
-g_gstring_get_type (void)
+g_variant_get_gtype (void)
{
- static GType type_id = 0;
-
- if (!type_id)
- type_id = g_boxed_type_register_static ("GString", /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
- gstring_copy,
- gstring_free);
- return type_id;
+ return G_TYPE_VARIANT;
}
static void
boxed_proxy_value_init (GValue *value)
{
- BoxedNode key, *node;
-
- key.type = G_VALUE_TYPE (value);
- node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
value->data[0].v_pointer = NULL;
}
boxed_proxy_value_free (GValue *value)
{
if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
- {
- BoxedNode key, *node;
-
- key.type = G_VALUE_TYPE (value);
- node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
- node->free (value->data[0].v_pointer);
- }
+ _g_type_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
}
static void
GValue *dest_value)
{
if (src_value->data[0].v_pointer)
- {
- BoxedNode key, *node;
-
- key.type = G_VALUE_TYPE (src_value);
- node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
- dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer);
- }
+ dest_value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (src_value), src_value->data[0].v_pointer);
else
dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
}
GTypeCValue *collect_values,
guint collect_flags)
{
- BoxedNode key, *node;
-
- key.type = G_VALUE_TYPE (value);
- node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
-
if (!collect_values[0].v_pointer)
value->data[0].v_pointer = NULL;
else
value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
}
else
- value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
+ value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (value), collect_values[0].v_pointer);
}
return NULL;
gpointer *boxed_p = collect_values[0].v_pointer;
if (!boxed_p)
- return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
+ return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
if (!value->data[0].v_pointer)
*boxed_p = NULL;
else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
*boxed_p = value->data[0].v_pointer;
else
- {
- BoxedNode key, *node;
-
- key.type = G_VALUE_TYPE (value);
- node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
- *boxed_p = node->copy (value->data[0].v_pointer);
- }
+ *boxed_p = _g_type_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer);
return NULL;
}
+/**
+ * g_boxed_type_register_static:
+ * @name: Name of the new boxed type.
+ * @boxed_copy: Boxed structure copy function.
+ * @boxed_free: Boxed structure free function.
+ *
+ * This function creates a new %G_TYPE_BOXED derived type id for a new
+ * boxed type with name @name. Boxed type handling functions have to be
+ * provided to copy and free opaque boxed structures of this type.
+ *
+ * Returns: New %G_TYPE_BOXED derived type id for @name.
+ */
GType
g_boxed_type_register_static (const gchar *name,
GBoxedCopyFunc boxed_copy,
"p",
boxed_proxy_lcopy_value,
};
- static const GTypeInfo type_info = {
+ GTypeInfo type_info = {
0, /* class_size */
NULL, /* base_init */
NULL, /* base_finalize */
type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
- /* install proxy functions upon successfull registration */
+ /* install proxy functions upon successful registration */
if (type)
- {
- BoxedNode key;
-
- key.type = type;
- key.copy = boxed_copy;
- key.free = boxed_free;
- boxed_bsa = g_bsearch_array_insert (boxed_bsa, &boxed_bconfig, &key, TRUE);
- }
+ _g_type_boxed_init (type, boxed_copy, boxed_free);
return type;
}
+/**
+ * g_boxed_copy:
+ * @boxed_type: The type of @src_boxed.
+ * @src_boxed: The boxed structure to be copied.
+ *
+ * Provide a copy of a boxed structure @src_boxed which is of type @boxed_type.
+ *
+ * Returns: (transfer full): The newly created copy of the boxed structure.
+ */
gpointer
g_boxed_copy (GType boxed_type,
gconstpointer src_boxed)
/* check if our proxying implementation is used, we can short-cut here */
if (value_table->value_copy == boxed_proxy_value_copy)
- {
- BoxedNode key, *node;
-
- key.type = boxed_type;
- node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
- dest_boxed = node->copy ((gpointer) src_boxed);
- }
+ dest_boxed = _g_type_boxed_copy (boxed_type, (gpointer) src_boxed);
else
{
GValue src_value, dest_value;
-
+
/* we heavily rely on third-party boxed type value vtable
* implementations to follow normal boxed value storage
* (data[0].v_pointer is the boxed struct, and
* data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
* rest zero).
- * but then, we can expect that since we layed out the
+ * but then, we can expect that since we laid out the
* g_boxed_*() API.
* data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
* after a copy.
/* double check and grouse if things went wrong */
if (dest_value.data[1].v_ulong)
- g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
+ g_warning ("the copy_value() implementation of type '%s' seems to make use of reserved GValue fields",
g_type_name (boxed_type));
dest_boxed = dest_value.data[0].v_pointer;
return dest_boxed;
}
+/**
+ * g_boxed_free:
+ * @boxed_type: The type of @boxed.
+ * @boxed: The boxed structure to be freed.
+ *
+ * Free the boxed structure @boxed which is of type @boxed_type.
+ */
void
g_boxed_free (GType boxed_type,
gpointer boxed)
/* check if our proxying implementation is used, we can short-cut here */
if (value_table->value_free == boxed_proxy_value_free)
- {
- BoxedNode key, *node;
-
- key.type = boxed_type;
- node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
- node->free (boxed);
- }
+ _g_type_boxed_free (boxed_type, boxed);
else
{
GValue value;
}
}
+/**
+ * g_value_get_boxed:
+ * @value: a valid #GValue of %G_TYPE_BOXED derived type
+ *
+ * Get the contents of a %G_TYPE_BOXED derived #GValue.
+ *
+ * Returns: (transfer none): boxed contents of @value
+ */
gpointer
g_value_get_boxed (const GValue *value)
{
return value->data[0].v_pointer;
}
+/**
+ * g_value_dup_boxed: (skip)
+ * @value: a valid #GValue of %G_TYPE_BOXED derived type
+ *
+ * Get the contents of a %G_TYPE_BOXED derived #GValue. Upon getting,
+ * the boxed value is duplicated and needs to be later freed with
+ * g_boxed_free(), e.g. like: g_boxed_free (G_VALUE_TYPE (@value),
+ * return_value);
+ *
+ * Returns: boxed contents of @value
+ */
gpointer
g_value_dup_boxed (const GValue *value)
{
static inline void
value_set_boxed_internal (GValue *value,
- gconstpointer const_boxed,
+ gconstpointer boxed,
gboolean need_copy,
gboolean need_free)
{
- BoxedNode key, *node;
- gpointer boxed = (gpointer) const_boxed;
-
if (!boxed)
{
/* just resetting to NULL might not be desired, need to
return;
}
- key.type = G_VALUE_TYPE (value);
- node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
-
- if (node)
- {
- /* we proxy this type, free contents and copy right away */
- if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
- node->free (value->data[0].v_pointer);
- value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
- value->data[0].v_pointer = need_copy ? node->copy (boxed) : boxed;
- }
- else
- {
- /* we don't handle this type, free contents and let g_boxed_copy()
- * figure what's required
- */
- if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
- g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
- value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
- value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : boxed;
- }
+ if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
+ g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
+ value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
+ value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : (gpointer) boxed;
}
+/**
+ * g_value_set_boxed:
+ * @value: a valid #GValue of %G_TYPE_BOXED derived type
+ * @v_boxed: (allow-none): boxed value to be set
+ *
+ * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
+ */
void
g_value_set_boxed (GValue *value,
gconstpointer boxed)
value_set_boxed_internal (value, boxed, TRUE, TRUE);
}
+/**
+ * g_value_set_static_boxed:
+ * @value: a valid #GValue of %G_TYPE_BOXED derived type
+ * @v_boxed: (allow-none): static boxed value to be set
+ *
+ * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
+ * The boxed value is assumed to be static, and is thus not duplicated
+ * when setting the #GValue.
+ */
void
g_value_set_static_boxed (GValue *value,
gconstpointer boxed)
value_set_boxed_internal (value, boxed, FALSE, FALSE);
}
+/**
+ * g_value_set_boxed_take_ownership:
+ * @value: a valid #GValue of %G_TYPE_BOXED derived type
+ * @v_boxed: (allow-none): duplicated unowned boxed value to be set
+ *
+ * This is an internal function introduced mainly for C marshallers.
+ *
+ * Deprecated: 2.4: Use g_value_take_boxed() instead.
+ */
void
g_value_set_boxed_take_ownership (GValue *value,
gconstpointer boxed)
{
+ g_value_take_boxed (value, boxed);
+}
+
+/**
+ * g_value_take_boxed:
+ * @value: a valid #GValue of %G_TYPE_BOXED derived type
+ * @v_boxed: (allow-none): duplicated unowned boxed value to be set
+ *
+ * Sets the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed
+ * and takes over the ownership of the callers reference to @v_boxed;
+ * the caller doesn't have to unref it any more.
+ *
+ * Since: 2.4
+ */
+void
+g_value_take_boxed (GValue *value,
+ gconstpointer boxed)
+{
g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));