2.13.1
[platform/upstream/glib.git] / gobject / gboxed.c
index 5356513..cab8164 100644 (file)
 #include       "gclosure.h"
 #include       "gvaluecollector.h"
 
+#include       "gobjectalias.h"
+
 #include <string.h>
 
 /* --- typedefs & structures --- */
 typedef struct
 {
   GType                 type;
-  GBoxedInitFunc init;
   GBoxedCopyFunc copy;
   GBoxedFreeFunc free;
-  gboolean      is_refcounted;
 } BoxedNode;
 
 
@@ -43,7 +43,12 @@ static gint  boxed_nodes_cmp         (gconstpointer  p1,
 
 
 /* --- variables --- */
-static GBSearchArray boxed_bsa = { boxed_nodes_cmp, sizeof (BoxedNode), 0, 0, NULL };
+static GBSearchArray *boxed_bsa = NULL;
+static const GBSearchConfig boxed_bconfig = {
+  sizeof (BoxedNode),
+  boxed_nodes_cmp,
+  0,
+};
 
 
 /* --- functions --- */
@@ -65,13 +70,31 @@ value_meminit (GValue *value,
 }
 
 static gpointer
-value_array_init (void)
+value_copy (gpointer boxed)
+{
+  const GValue *src_value = boxed;
+  GValue *dest_value = g_new0 (GValue, 1);
+
+  if (G_VALUE_TYPE (src_value))
+    {
+      g_value_init (dest_value, G_VALUE_TYPE (src_value));
+      g_value_copy (src_value, dest_value);
+    }
+  return dest_value;
+}
+
+static void
+value_free (gpointer boxed)
 {
-  return g_value_array_new (0);
+  GValue *value = boxed;
+
+  if (G_VALUE_TYPE (value))
+    g_value_unset (value);
+  g_free (value);
 }
 
 void
-g_boxed_type_init (void)  /* sync with gtype.c */
+g_boxed_type_init (void) 
 {
   static const GTypeInfo info = {
     0,                          /* class_size */
@@ -88,39 +111,140 @@ g_boxed_type_init (void)  /* sync with gtype.c */
   const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
   GType type;
 
+  boxed_bsa = g_bsearch_array_create (&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);
+}
 
-  /* 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);
+GType
+g_closure_get_type (void)
+{
+  static GType type_id = 0;
+
+  if (!type_id)
+    type_id = g_boxed_type_register_static (g_intern_static_string ("GClosure"),
+                                           (GBoxedCopyFunc) g_closure_ref,
+                                           (GBoxedFreeFunc) g_closure_unref);
+  return type_id;
+}
+
+GType
+g_value_get_type (void)
+{
+  static GType type_id = 0;
+
+  if (!type_id)
+    type_id = g_boxed_type_register_static (g_intern_static_string ("GValue"),
+                                           value_copy,
+                                           value_free);
+  return type_id;
+}
+
+GType
+g_value_array_get_type (void)
+{
+  static GType type_id = 0;
+
+  if (!type_id)
+    type_id = g_boxed_type_register_static (g_intern_static_string ("GValueArray"),
+                                           (GBoxedCopyFunc) g_value_array_copy,
+                                           (GBoxedFreeFunc) g_value_array_free);
+  return type_id;
+}
+
+static gpointer
+gdate_copy (gpointer boxed)
+{
+  const GDate *date = (const GDate*) boxed;
+
+  return g_date_new_julian (g_date_get_julian (date));
+}
+
+GType
+g_date_get_type (void)
+{
+  static GType type_id = 0;
+
+  if (!type_id)
+    type_id = g_boxed_type_register_static (g_intern_static_string ("GDate"),
+                                           (GBoxedCopyFunc) gdate_copy,
+                                           (GBoxedFreeFunc) g_date_free);
+  return type_id;
+}
+
+GType
+g_strv_get_type (void)
+{
+  static GType type_id = 0;
+
+  if (!type_id)
+    type_id = g_boxed_type_register_static (g_intern_static_string ("GStrv"),
+                                           (GBoxedCopyFunc) g_strdupv,
+                                           (GBoxedFreeFunc) g_strfreev);
+  return type_id;
+}
+
+static gpointer
+gstring_copy (gpointer boxed)
+{
+  const GString *src_gstring = boxed;
+
+  return g_string_new_len (src_gstring->str, src_gstring->len);
 }
 
 static void
-boxed_proxy_value_init (GValue *value)
+gstring_free (gpointer boxed)
 {
-  BoxedNode key, *node;
+  GString *gstring = boxed;
 
-  key.type = G_VALUE_TYPE (value);
-  node = g_bsearch_array_lookup (&boxed_bsa, &key);
-  value->data[0].v_pointer = node->init ? node->init () : NULL;
+  g_string_free (gstring, TRUE);
+}
+
+GType
+g_gstring_get_type (void)
+{
+  static GType type_id = 0;
+
+  if (!type_id)
+    type_id = g_boxed_type_register_static (g_intern_static_string ("GString"),
+                                            /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
+                                           gstring_copy,
+                                           gstring_free);
+  return type_id;
+}
+
+static gpointer
+hash_table_copy (gpointer boxed)
+{
+  GHashTable *hash_table = boxed;
+  return g_hash_table_ref (hash_table);
+}
+
+static void
+hash_table_free (gpointer boxed)
+{
+  GHashTable *hash_table = boxed;
+  g_hash_table_unref (hash_table);
+}
+
+GType
+g_hash_table_get_type (void)
+{
+  static GType type_id = 0;
+  if (!type_id)
+    type_id = g_boxed_type_register_static (g_intern_static_string ("GHashTable"),
+                                           hash_table_copy, hash_table_free);
+  return type_id;
+}
+
+static void
+boxed_proxy_value_init (GValue *value)
+{
+  value->data[0].v_pointer = NULL;
 }
 
 static void
@@ -131,7 +255,7 @@ boxed_proxy_value_free (GValue *value)
       BoxedNode key, *node;
 
       key.type = G_VALUE_TYPE (value);
-      node = g_bsearch_array_lookup (&boxed_bsa, &key);
+      node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
       node->free (value->data[0].v_pointer);
     }
 }
@@ -145,7 +269,7 @@ boxed_proxy_value_copy (const GValue *src_value,
       BoxedNode key, *node;
 
       key.type = G_VALUE_TYPE (src_value);
-      node = g_bsearch_array_lookup (&boxed_bsa, &key);
+      node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
       dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer);
     }
   else
@@ -167,15 +291,13 @@ boxed_proxy_collect_value (GValue      *value,
   BoxedNode key, *node;
 
   key.type = G_VALUE_TYPE (value);
-  node = g_bsearch_array_lookup (&boxed_bsa, &key);
+  node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &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;
+    value->data[0].v_pointer = NULL;
   else
     {
-      /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
-      if (!node->is_refcounted && (collect_flags & G_VALUE_NOCOPY_CONTENTS))
+      if (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;
@@ -207,7 +329,7 @@ boxed_proxy_lcopy_value (const GValue *value,
       BoxedNode key, *node;
 
       key.type = G_VALUE_TYPE (value);
-      node = g_bsearch_array_lookup (&boxed_bsa, &key);
+      node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
       *boxed_p = node->copy (value->data[0].v_pointer);
     }
 
@@ -216,10 +338,8 @@ boxed_proxy_lcopy_value (const GValue *value,
 
 GType
 g_boxed_type_register_static (const gchar   *name,
-                             GBoxedInitFunc boxed_init,
                              GBoxedCopyFunc boxed_copy,
-                             GBoxedFreeFunc boxed_free,
-                             gboolean       is_refcounted)
+                             GBoxedFreeFunc boxed_free)
 {
   static const GTypeValueTable vtable = {
     boxed_proxy_value_init,
@@ -258,17 +378,15 @@ g_boxed_type_register_static (const gchar   *name,
       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);
+      boxed_bsa = g_bsearch_array_insert (boxed_bsa, &boxed_bconfig, &key);
     }
 
   return type;
 }
 
-GBoxed*
+gpointer
 g_boxed_copy (GType         boxed_type,
              gconstpointer src_boxed)
 {
@@ -289,14 +407,14 @@ g_boxed_copy (GType         boxed_type,
       BoxedNode key, *node;
 
       key.type = boxed_type;
-      node = g_bsearch_array_lookup (&boxed_bsa, &key);
+      node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
       dest_boxed = node->copy ((gpointer) src_boxed);
     }
   else
     {
       GValue src_value, dest_value;
       
-      /* we heavil rely on third-party boxed type value vtable
+      /* 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,
@@ -311,14 +429,12 @@ g_boxed_copy (GType         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 */
+      /* call third-party code copy function, 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)
+      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_type_name (boxed_type));
 
@@ -348,7 +464,7 @@ g_boxed_free (GType    boxed_type,
       BoxedNode key, *node;
 
       key.type = boxed_type;
-      node = g_bsearch_array_lookup (&boxed_bsa, &key);
+      node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
       node->free (boxed);
     }
   else
@@ -372,7 +488,7 @@ g_value_get_boxed (const GValue *value)
 }
 
 gpointer
-g_value_dup_boxed (GValue *value)
+g_value_dup_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);
@@ -401,7 +517,7 @@ value_set_boxed_internal (GValue       *value,
     }
 
   key.type = G_VALUE_TYPE (value);
-  node = g_bsearch_array_lookup (&boxed_bsa, &key);
+  node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
 
   if (node)
     {
@@ -447,8 +563,18 @@ void
 g_value_set_boxed_take_ownership (GValue       *value,
                                  gconstpointer boxed)
 {
+  g_value_take_boxed (value, boxed);
+}
+
+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)));
 
   value_set_boxed_internal (value, boxed, FALSE, TRUE);
 }
+
+#define __G_BOXED_C__
+#include "gobjectaliasdef.c"