summaryrefslogtreecommitdiff
path: root/atk/atkregistry.c
blob: 8ad98478279dd0929245dfa3397fb56878256388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* ATK - Accessibility Toolkit
 * Copyright 2001 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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 "atkregistry.h"
#include "atknoopobjectfactory.h"

static AtkRegistry *default_registry = NULL;;

static void              atk_registry_init           (AtkRegistry      *instance,
                                                      AtkRegistryClass *klass);
static void              atk_registry_finalize       (GObject          *instance);
static void              atk_registry_class_init     (AtkRegistryClass *klass);

static AtkRegistry*      atk_registry_new            ();
static GType           atk_registry_get_factory_type (AtkRegistry      *registry,
                                                      GType            type);

GType
atk_registry_get_type (void)
{
  static GType type = 0;

  if (!type)
    {
      static const GTypeInfo info =
      {
        sizeof (AtkRegistryClass),
        (GBaseInitFunc) NULL,                             /* base_init */
        (GBaseFinalizeFunc) NULL,                         /* base_finalize */
        (GClassInitFunc) atk_registry_class_init,         /* class_init */
        (GClassFinalizeFunc) NULL,                        /* class_finalize */
        NULL,                                             /* class_data */
        sizeof (AtkRegistry),                             /* instance size */
        0,                                                /* n_preallocs */
        (GInstanceInitFunc) atk_registry_init,            /* instance init */
        NULL                                              /* value table */
      };

      type = g_type_register_static (G_TYPE_OBJECT, "AtkRegistry", &info, 0);
    }

  return type;
}

static void
atk_registry_class_init (AtkRegistryClass *klass)
{
  GObjectClass *object_class;

  /* is paranoia appropriate in a class initializer ? */
  g_return_if_fail (G_IS_OBJECT_CLASS (klass));

  object_class = G_OBJECT_CLASS (klass);
  object_class->finalize = atk_registry_finalize;
  default_registry = atk_registry_new ();
}

#if 0
/*
 * Cannot define a class_finalize function when calling
 * g_type_register_static()
 */
static void
atk_registry_class_finalize (GObjectClass *klass)
{
  g_return_if_fail (ATK_IS_REGISTRY_CLASS (klass));

  g_free (default_registry);
}
#endif

static void
atk_registry_init (AtkRegistry *instance, AtkRegistryClass *klass)
{
  instance->factory_type_registry = g_hash_table_new (NULL, NULL);
  instance->factory_singleton_cache = g_hash_table_new (NULL, NULL);
}

static AtkRegistry*
atk_registry_new ()
{
  GObject *object;

  object = g_object_new (ATK_TYPE_REGISTRY, NULL);

  g_return_val_if_fail ((object != NULL), NULL);
  g_return_val_if_fail (ATK_IS_REGISTRY (object), NULL);

  return (AtkRegistry *) object;
}

static void
atk_registry_finalize (GObject *instance)
{
  AtkRegistry *registry;

  g_return_if_fail (ATK_IS_REGISTRY (instance));
  registry = ATK_REGISTRY (instance);
  g_free (registry->factory_type_registry);
  g_free (registry->factory_singleton_cache);
}


void
atk_registry_set_factory_type (AtkRegistry *registry,
                               GType type,
                               GType factory_type)
{
  GType old_type;
  gpointer value;
  AtkObjectFactory *old_factory;

  g_return_if_fail (ATK_IS_REGISTRY (registry));

  value = g_hash_table_lookup (registry->factory_type_registry, 
                                  GUINT_TO_POINTER (type));
  old_type = GPOINTER_TO_UINT (value);
  if (old_type && old_type != factory_type)
    {
      g_hash_table_remove (registry->factory_type_registry, 
                           GUINT_TO_POINTER (type));
      /*
       * If the old factory was created, notify it that it has
       * been replaced, then free it.
       */
      old_factory = g_hash_table_lookup (registry->factory_singleton_cache, 
                                         GUINT_TO_POINTER (old_type));
      if (old_factory)
        {
          atk_object_factory_invalidate (old_factory);
          g_type_free_instance ((GTypeInstance *) old_factory);
        }
    }
  g_hash_table_insert (registry->factory_type_registry, 
                       GUINT_TO_POINTER (type), 
                       GUINT_TO_POINTER (factory_type));
}

GType
atk_registry_get_factory_type (AtkRegistry *registry,
                               GType type)
{
  GType factory_type;
  gpointer value;

  /*
   * look up factory type in first hash;
   * if there isn't an explicitly registered factory type,
   * try inheriting one...
   */
  do {
    value =
        g_hash_table_lookup (registry->factory_type_registry, 
                             GUINT_TO_POINTER (type));
    type = g_type_parent (type);
    if (type == G_TYPE_INVALID)
      {
        break;
      }
  } while (value == NULL);

  factory_type = GPOINTER_TO_UINT (value);
  return factory_type;
}


AtkObjectFactory*
atk_registry_get_factory (AtkRegistry *registry,
                          GType type)
{
  gpointer factory_pointer = NULL;
  GType factory_type;

  factory_type = atk_registry_get_factory_type (registry, type);

  if (factory_type == G_TYPE_INVALID)
  {
  /* Factory type has not been specified for this object type */
    static AtkObjectFactory* default_factory = NULL;

    if (!default_factory)
      default_factory = atk_no_op_object_factory_new ();

    return default_factory;
  }

  /* ask second hashtable for instance of factory type */
  factory_pointer =
        g_hash_table_lookup (registry->factory_singleton_cache, 
        GUINT_TO_POINTER (factory_type));

  /* if there isn't one already, create one and save it */
  if (factory_pointer == NULL)
    {
      factory_pointer = g_type_create_instance (factory_type);
      g_hash_table_insert (registry->factory_singleton_cache,
                           GUINT_TO_POINTER (factory_type),
                           factory_pointer);
    }

  return ATK_OBJECT_FACTORY (factory_pointer);
}


AtkRegistry*
atk_get_default_registry ()
{
  if (!default_registry)
  {
    default_registry = atk_registry_new();
  }
  return default_registry;
}