Remove all instances of g_return_if_fail (foo != NULL); that are
[platform/upstream/atk.git] / atk / atkrelation.c
1 /* ATK -  Accessibility Toolkit
2  * Copyright 2001 Sun Microsystems Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib-object.h>
21 #include "atkobject.h"
22 #include "atkrelation.h"
23
24 static void            atk_relation_class_init       (AtkRelationClass  *klass);
25 static void            atk_relation_finalize         (GObject           *object);
26
27 GType
28 atk_relation_get_type (void)
29 {
30   static GType type = 0;
31
32   if (!type)
33     {
34       static const GTypeInfo typeInfo =
35       {
36         sizeof (AtkRelationClass),
37         (GBaseInitFunc) NULL,
38         (GBaseFinalizeFunc) NULL,
39         (GClassInitFunc) atk_relation_class_init,
40         (GClassFinalizeFunc) NULL,
41         NULL,
42         sizeof (AtkRelation),
43         0,
44         (GInstanceInitFunc) NULL,
45       } ;
46       type = g_type_register_static (G_TYPE_OBJECT, "AtkRelation", &typeInfo, 0) ;
47     }
48   return type;
49 }
50
51 static void
52 atk_relation_class_init (AtkRelationClass *klass)
53 {
54   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
55
56   gobject_class->finalize = atk_relation_finalize;
57 }
58
59 /**
60  * atk_relation_type_register:
61  * @name: a name string
62  *
63  * Associate @name with a new #AtkRelationType
64  *
65  * Returns: an #AtkRelationType associated with @name
66  **/
67 AtkRelationType
68 atk_relation_type_register (const gchar *name)
69 {
70   /* TODO: associate name with new type */
71   static guint type = ATK_RELATION_LAST_DEFINED;
72   return (++type);
73 }
74
75 /**
76  * atk_relation_new:
77  * @targets: an array of pointers to #AtkObjects  
78  * @n_targets: number of #AtkObjects pointed to by @targets
79  * @relationship: an #AtkRelationType with which to create the new
80  *  #AtkRelation
81  *
82  * Create a new relation for the specified key and the specified list
83  * of targets.
84  *
85  * Returns: a pointer to a new #AtkRelation
86  **/
87 AtkRelation*
88 atk_relation_new (AtkObject       **targets,
89                   gint            n_targets,
90                   AtkRelationType relationship)
91 {
92   AtkRelation *relation;
93   int         i;
94   GPtrArray      *array;
95
96   g_return_val_if_fail (targets != NULL, NULL);
97
98   relation = g_object_new (ATK_TYPE_RELATION, NULL);
99   array = g_ptr_array_sized_new (n_targets);
100   for (i = 0; i < n_targets; i++)
101   {
102     /*
103      * Add a reference to AtkObject being added to a relation
104      */
105     g_object_ref (targets[i]);
106     g_ptr_array_add (array, targets[i]);
107   }
108   
109   relation->target = array;
110   relation->relationship = relationship;
111
112   return relation;
113 }
114
115 /**
116  * atk_relation_get_relation_type:
117  * @relation: an #AtkRelation 
118  *
119  * Gets the type of @relation
120  *
121  * Returns: the type of @relation
122  **/
123 AtkRelationType
124 atk_relation_get_relation_type (AtkRelation *relation)
125 {
126   g_return_val_if_fail (ATK_IS_RELATION (relation), 0);
127   
128   return relation->relationship;
129 }
130
131 /**
132  * atk_relation_get_target:
133  * @relation: an #AtkRelation
134  *
135  * Gets the target list of @relation
136  *
137  * Returns: the target list of @relation
138  **/
139 GPtrArray*
140 atk_relation_get_target (AtkRelation *relation)
141 {
142   g_return_val_if_fail (ATK_IS_RELATION (relation), FALSE);
143
144   return relation->target;
145 }
146
147 static void
148 atk_relation_finalize (GObject *object)
149 {
150   AtkRelation        *relation;
151
152   g_return_if_fail (ATK_IS_RELATION (object));
153
154   relation = ATK_RELATION (object);
155
156   if (relation->target)
157   {
158     gint i;
159
160     for (i = 0; i < relation->target->len; i++)
161     {
162       /*
163        * Remove a reference to AtkObject being removed from a relation
164        */
165       g_object_unref (g_ptr_array_index (relation->target, i));
166     }
167     g_ptr_array_free (relation->target, TRUE);
168   } 
169 }