glib/gobject/gboxed.c
Tim Janik 37e4b8c87e changed prototype of g_boxed_type_register_static() to contain an optional
Wed Mar  7 09:36:33 2001  Tim Janik  <timj@gtk.org>

	* gboxed.[hc]: changed prototype of g_boxed_type_register_static()
	to contain an optional init function and a hint at whether the
	boxed structure uses ref counting internally.
	added g_value_set_boxed_take_ownership().
	made G_TYPE_BOXED an abstract value type.

	* genums.[hc]: made G_TYPE_ENUM and G_TYPE_FLAGS abstract value
	types.

	* glib-genmarshal.c: argument type changes, preparation for third-party
	arg specification.

	* gobject.[hc]: cleaned up get/set property code.
	added g_strdup_value_contents() to improve warnings.

	* gparam.[hc]: added g_param_value_convert(), taking over responsibility
	of the old g_value_convert(). added G_PARAM_LAX_VALIDATION flag so
	validation alterations may be valid a part of the property setting
	process.

	* gparamspecs.[hc]: made value comparisons stable (for sort applications).
	added GParamSpecValueArray, a param spec for value arrays and
	GParamSpecClosure. nuked the value exchange functions and
	GParamSpecCCallback.

	* gtype.[hc]: catch unintialized usages of the type system with
	g_return_val_if_uninitialized(). introduced G_TYPE_FLAG_VALUE_ABSTRACT
	to flag types that introduce a value table, but can't be used for
	g_value_init(). cleaned up reserved type ids.

	* gvalue.[hc]: code cleanups and saner checking.
	nuked the value exchange API. implemented value transformations, we
	can't really "convert" values, rather transforms are an anylogy to
	C casts, real conversions need a param spec for validation, which is
	why g_param_value_convert() does real conversions now.

	* gvaluearray.[hc]: new files that implement a GValueArray, a struct
	that can hold inhomogeneous arrays of value (to that extend that it
	also allowes undefined values, i.e. G_VALUE_TYPE(value)==0).
	this is exposed to the type system as a boxed type.

	* gvaluetransform.c: new file implementing most of the former value
	exchange functions as single-sided transformations.

	* gvaluetypes.[hc]: nuked G_TYPE_CCALLBACK, added
	g_value_set_string_take_ownership().

	* *.h: s/G_IS_VALUE_/G_VALUE_HOLDS_/.

	* *.[hc]: many fixes and cleanups.

	* many warning improvements.

Tue Feb 27 18:35:15 2001  Tim Janik  <timj@gtk.org>

	* gobject.c (g_object_get_valist): urg, pass G_VALUE_NOCOPY_CONTENTS
	into G_VALUE_LCOPY(), this needs proper documenting.

	* gparam.c: fixed G_PARAM_USER_MASK.

	* gtype.c (type_data_make_W):
	(type_data_last_unref_Wm): fixed invalid memory freeing.

	* gobject.c (g_object_last_unref): destroy signal handlers associated
	with object, right before finalization.

	* gsignal.c (g_signal_parse_name): catch destroyed nodes or signals
	that don't actually support details.

	* gobject.[hc]: got rid of property trailers. nuked GObject
	properties "data" and the "signal" variants.
	(g_object_connect): new convenience function to do multiple
	signal connections at once.
	(g_object_disconnect): likewise, for disconnections.

	* gparam.[hc] (g_param_spec_pool_lookup): took out trailer support.

	* gvalue.[hc]: marked g_value_fits_pointer() and g_value_peek_pointer()
	as private (the latter got renamed from g_value_get_as_pointer()).

Wed Mar  7 09:32:06 2001  Tim Janik  <timj@gtk.org>

        * glib-object.h: add gvaluearray.h.

        * gstring.[hc]: fixup naming of g_string_sprint*.

        * gtypes.h: fixed GCompareDataFunc naming.

Wed Mar  7 09:33:27 2001  Tim Janik  <timj@gtk.org>

        * gobject/Makefile.am: shuffled rules to avoid excessive
        rebuilds.

        * gobject/gobject-sections.txt: updates.

        * gobject/tmpl/*: bunch of updates, added another patch
        from Eric Lemings <eric.b.lemings@lmco.com>.
2001-03-07 14:46:45 +00:00

455 lines
13 KiB
C

/* GObject - GLib Type, Object, Parameter and Signal Library
* Copyright (C) 2000-2001 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* 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.
*/
#include "gboxed.h"
#include "gbsearcharray.h"
#include "gvalue.h"
#include "gvaluearray.h"
#include "gclosure.h"
#include "gvaluecollector.h"
#include <string.h>
/* --- typedefs & structures --- */
typedef struct
{
GType type;
GBoxedInitFunc init;
GBoxedCopyFunc copy;
GBoxedFreeFunc free;
gboolean is_refcounted;
} BoxedNode;
/* --- prototypes --- */
static gint boxed_nodes_cmp (gconstpointer p1,
gconstpointer p2);
/* --- variables --- */
static GBSearchArray boxed_bsa = { boxed_nodes_cmp, sizeof (BoxedNode), 0, 0, NULL };
/* --- 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);
}
static inline void /* keep this function in sync with gvalue.c */
value_meminit (GValue *value,
GType value_type)
{
value->g_type = value_type;
memset (value->data, 0, sizeof (value->data));
}
static gpointer
value_array_init (void)
{
return g_value_array_new (0);
}
void
g_boxed_type_init (void) /* sync with gtype.c */
{
static const GTypeInfo info = {
0, /* class_size */
NULL, /* base_init */
NULL, /* base_destroy */
NULL, /* class_init */
NULL, /* class_destroy */
NULL, /* class_data */
0, /* instance_size */
0, /* n_preallocs */
NULL, /* instance_init */
NULL, /* value_table */
};
const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
GType type;
/* G_TYPE_BOXED
*/
type = g_type_register_fundamental (G_TYPE_BOXED, "GBoxed", &info, &finfo,
G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
g_assert (type == G_TYPE_BOXED);
/* boxed: G_TYPE_CLOSURE
*/
type = g_boxed_type_register_static ("GClosure",
(GBoxedInitFunc) NULL,
(GBoxedCopyFunc) g_closure_ref,
(GBoxedFreeFunc) g_closure_unref,
TRUE);
g_assert (type == G_TYPE_CLOSURE);
/* boxed: G_TYPE_VALUE_ARRAY
*/
type = g_boxed_type_register_static ("GValueArray",
value_array_init, /* don't allow NULL values */
(GBoxedCopyFunc) g_value_array_copy,
(GBoxedFreeFunc) g_value_array_free,
FALSE);
g_assert (type == G_TYPE_VALUE_ARRAY);
}
static void
boxed_proxy_value_init (GValue *value)
{
BoxedNode key, *node;
key.type = G_VALUE_TYPE (value);
node = g_bsearch_array_lookup (&boxed_bsa, &key);
value->data[0].v_pointer = node->init ? node->init () : NULL;
}
static void
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, &key);
node->free (value->data[0].v_pointer);
}
}
static void
boxed_proxy_value_copy (const GValue *src_value,
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, &key);
dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer);
}
else
dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
}
static gpointer
boxed_proxy_value_peek_pointer (const GValue *value)
{
return value->data[0].v_pointer;
}
static gchar*
boxed_proxy_collect_value (GValue *value,
guint n_collect_values,
GTypeCValue *collect_values,
guint collect_flags)
{
BoxedNode key, *node;
key.type = G_VALUE_TYPE (value);
node = g_bsearch_array_lookup (&boxed_bsa, &key);
/* for NULL values, we have to call GBoxedInitFunc */
if (!collect_values[0].v_pointer)
value->data[0].v_pointer = node->init ? node->init () : NULL;
else
{
/* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
if (!node->is_refcounted && (collect_flags & G_VALUE_NOCOPY_CONTENTS))
{
value->data[0].v_pointer = collect_values[0].v_pointer;
value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
}
else
value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
}
return NULL;
}
static gchar*
boxed_proxy_lcopy_value (const GValue *value,
guint n_collect_values,
GTypeCValue *collect_values,
guint collect_flags)
{
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));
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, &key);
*boxed_p = node->copy (value->data[0].v_pointer);
}
return NULL;
}
GType
g_boxed_type_register_static (const gchar *name,
GBoxedInitFunc boxed_init,
GBoxedCopyFunc boxed_copy,
GBoxedFreeFunc boxed_free,
gboolean is_refcounted)
{
static const GTypeValueTable vtable = {
boxed_proxy_value_init,
boxed_proxy_value_free,
boxed_proxy_value_copy,
boxed_proxy_value_peek_pointer,
"p",
boxed_proxy_collect_value,
"p",
boxed_proxy_lcopy_value,
};
static const GTypeInfo type_info = {
0, /* class_size */
NULL, /* base_init */
NULL, /* base_finalize */
NULL, /* class_init */
NULL, /* class_finalize */
NULL, /* class_data */
0, /* instance_size */
0, /* n_preallocs */
NULL, /* instance_init */
&vtable, /* value_table */
};
GType type;
g_return_val_if_fail (name != NULL, 0);
g_return_val_if_fail (boxed_copy != NULL, 0);
g_return_val_if_fail (boxed_free != NULL, 0);
g_return_val_if_fail (g_type_from_name (name) == 0, 0);
type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
/* install proxy functions upon successfull registration */
if (type)
{
BoxedNode key;
key.type = type;
key.init = boxed_init;
key.copy = boxed_copy;
key.free = boxed_free;
key.is_refcounted = is_refcounted != FALSE;
g_bsearch_array_insert (&boxed_bsa, &key, TRUE);
}
return type;
}
GBoxed*
g_boxed_copy (GType boxed_type,
gconstpointer src_boxed)
{
GTypeValueTable *value_table;
gpointer dest_boxed;
g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
g_return_val_if_fail (src_boxed != NULL, NULL);
value_table = g_type_value_table_peek (boxed_type);
if (!value_table)
g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
/* 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, &key);
dest_boxed = node->copy ((gpointer) src_boxed);
}
else
{
GValue src_value, dest_value;
/* we heavil 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
* g_boxed_*() API.
* data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
* after a copy.
*/
/* equiv. to g_value_set_static_boxed() */
value_meminit (&src_value, boxed_type);
src_value.data[0].v_pointer = (gpointer) src_boxed;
src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
/* call third-party code copy fucntion, fingers-crossed */
value_meminit (&dest_value, boxed_type);
value_table->value_copy (&src_value, &dest_value);
/* double check and grouse if things went wrong */
if (dest_value.data[1].v_ulong ||
dest_value.data[2].v_ulong ||
dest_value.data[3].v_ulong)
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;
}
void
g_boxed_free (GType boxed_type,
gpointer boxed)
{
GTypeValueTable *value_table;
g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
g_return_if_fail (boxed != NULL);
value_table = g_type_value_table_peek (boxed_type);
if (!value_table)
g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
/* 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, &key);
node->free (boxed);
}
else
{
GValue value;
/* see g_boxed_copy() on why we think we can do this */
value_meminit (&value, boxed_type);
value.data[0].v_pointer = boxed;
value_table->value_free (&value);
}
}
gpointer
g_value_get_boxed (const GValue *value)
{
g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
return value->data[0].v_pointer;
}
gpointer
g_value_dup_boxed (GValue *value)
{
g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
}
static inline void
value_set_boxed_internal (GValue *value,
gconstpointer const_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
* have value reinitialized also (for values defaulting
* to other default value states than a NULL data pointer),
* g_value_reset() will handle this
*/
g_value_reset (value);
return;
}
key.type = G_VALUE_TYPE (value);
node = g_bsearch_array_lookup (&boxed_bsa, &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;
}
}
void
g_value_set_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)));
value_set_boxed_internal (value, boxed, TRUE, TRUE);
}
void
g_value_set_static_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)));
value_set_boxed_internal (value, boxed, FALSE, FALSE);
}
void
g_value_set_boxed_take_ownership (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)));
value_set_boxed_internal (value, boxed, FALSE, TRUE);
}