File: object.c

package info (click to toggle)
pcb 20110918-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 22,304 kB
  • sloc: ansic: 121,422; sh: 7,232; yacc: 5,088; pascal: 4,136; makefile: 1,535; perl: 564; lex: 438; awk: 158; lisp: 86; tcl: 63; xml: 20
file content (345 lines) | stat: -rw-r--r-- 8,586 bytes parent folder | download | duplicates (14)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* GTS - Library for the manipulation of triangulated surfaces
 * Copyright (C) 1999 Stphane Popinet
 *
 * 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 <string.h>
#include "gts.h"
#include "gts-private.h"

static GHashTable * class_table = NULL;

static void gts_object_class_init (GtsObjectClass * klass,
				   GtsObjectClass * parent_class)
{
  if (parent_class) {
    gts_object_class_init (klass, parent_class->parent_class);
    if (parent_class->info.class_init_func)
      (*parent_class->info.class_init_func) (klass);
  }
}

/**
 * gts_object_class_new:
 * @parent_class: a #GtsObjectClass.
 * @info: a #GtsObjectClassInfo, description of the new class to create.
 *
 * Returns: a new #GtsObjectClass derived from @parent_class and described by
 * @info.
 */
gpointer gts_object_class_new (GtsObjectClass * parent_class,
			       GtsObjectClassInfo * info)
{
  GtsObjectClass * klass;

  g_return_val_if_fail (info != NULL, NULL);
  g_return_val_if_fail (parent_class == NULL ||
			info->object_size >= parent_class->info.object_size,
			NULL);
  g_return_val_if_fail (parent_class == NULL ||
			info->class_size >= parent_class->info.class_size,
			NULL);

  klass = g_malloc0 (info->class_size);
  klass->info = *info;
  klass->parent_class = parent_class;
  gts_object_class_init (klass, klass);

  if (!class_table)
    class_table = g_hash_table_new (g_str_hash, g_str_equal);
  g_hash_table_insert (class_table, klass->info.name, klass);

  return klass;
}

/**
 * gts_object_class_from_name:
 * @name: the name of a #GtsObjectClass.
 *
 * Returns: the #GtsObjectClass with name @name or %NULL if it hasn't been 
 * instantiated yet.
 */
GtsObjectClass * gts_object_class_from_name (const gchar * name)
{
  g_return_val_if_fail (name != NULL, NULL);

  if (!class_table)
    return NULL;
  return g_hash_table_lookup (class_table, name);
}

static void object_destroy (GtsObject * object)
{
#ifdef DEBUG_IDENTITY
#ifdef DEBUG_LEAKS
  fprintf (stderr, "destroy %s %p->%d\n", 
	   object->klass->info.name,
	   object, 
	   id (object));
#endif
  id_remove (object);
#endif
  object->klass = NULL;
  g_free (object);
}

static void object_clone (GtsObject * clone, GtsObject * object)
{
  memcpy (clone, object, object->klass->info.object_size);
  clone->reserved = NULL;
}

static void object_class_init (GtsObjectClass * klass)
{
  klass->clone = object_clone;
  klass->destroy = object_destroy;
  klass->read = NULL;
  klass->write = NULL;
  klass->color = NULL;  
  klass->attributes = NULL;
}

static void object_init (GtsObject * object)
{
  object->reserved = NULL;
  object->flags = 0;
}

/**
 * gts_object_class:
 *
 * Returns: the #GtsObjectClass.
 */
GtsObjectClass * gts_object_class (void)
{
  static GtsObjectClass * klass = NULL;

  if (klass == NULL) {
    GtsObjectClassInfo object_info = {
      "GtsObject",
      sizeof (GtsObject),
      sizeof (GtsObjectClass),
      (GtsObjectClassInitFunc) object_class_init,
      (GtsObjectInitFunc) object_init,
      (GtsArgSetFunc) NULL,
      (GtsArgGetFunc) NULL
    };
    klass = gts_object_class_new (NULL, &object_info);
  }

  return klass;
}

/**
 * gts_object_check_cast:
 * @object: a #GtsObject.
 * @klass: a #GtsObjectClass.
 *
 * Returns: @object while emitting warnings if @object is not of class @klass.
 */
gpointer gts_object_check_cast (gpointer object, 
				gpointer klass)
{
  if (!object) {
    g_warning ("invalid cast from (NULL) pointer to `%s'",
	       GTS_OBJECT_CLASS (klass)->info.name);
    return object;
  }
  if (!((GtsObject *) object)->klass) {
    g_warning ("invalid unclassed pointer in cast to `%s'",
	       GTS_OBJECT_CLASS (klass)->info.name);
    return object;
  }
  if (!gts_object_is_from_class (object, klass)) {
    g_warning ("invalid cast from `%s' to `%s'",
	       ((GtsObject *) object)->klass->info.name,
	       GTS_OBJECT_CLASS (klass)->info.name);
    return object;
  }
  return object;
}

/**
 * gts_object_class_check_cast:
 * @klass: a #GtsObjectClass.
 * @from: a #GtsObjectClass.
 *
 * Returns: @klass while emitting warnings if @klass is not derived from
 * @from.
 */
gpointer gts_object_class_check_cast (gpointer klass, 
				      gpointer from)
{
  if (!klass) {
    g_warning ("invalid cast from (NULL) pointer to `%s'",
	       GTS_OBJECT_CLASS (from)->info.name);
    return klass;
  }
  if (!gts_object_class_is_from_class (klass, from)) {
    g_warning ("invalid cast from `%s' to `%s'",
	       GTS_OBJECT_CLASS (klass)->info.name,
	       GTS_OBJECT_CLASS (from)->info.name);
    return klass;
  }
  return klass;
}

/**
 * gts_object_init:
 * @object: a #GtsObject.
 * @klass: a #GtsObjectClass.
 *
 * Calls the init method of @klass with @object as argument. This is done 
 * recursively in the correct order (from the base class to the top). You
 * should rarely need this function as it is called automatically by the
 * constructor for each class.
 */
void gts_object_init (GtsObject * object, GtsObjectClass * klass)
{
  GtsObjectClass * parent_class;

  g_return_if_fail (object != NULL);
  g_return_if_fail (klass != NULL);

  parent_class = klass->parent_class;
  if (parent_class)
    gts_object_init (object, parent_class);
  if (klass->info.object_init_func)
    (*klass->info.object_init_func) (object);
}

/**
 * gts_object_new:
 * @klass: a #GtsObjectClass.
 *
 * Returns: a new initialized object of class @klass.
 */
GtsObject * gts_object_new (GtsObjectClass * klass)
{
  GtsObject * object;

  g_return_val_if_fail (klass != NULL, NULL);

  object = g_malloc0 (klass->info.object_size);
  object->klass = klass;
  gts_object_init (object, klass);

#ifdef DEBUG_IDENTITY
  id_insert (object);
#ifdef DEBUG_LEAKS
  fprintf (stderr, "new %s %p->%d\n", klass->info.name, 
	   object, 
	   id (object));
#endif
#endif

  return object;
}

/**
 * gts_object_clone:
 * @object: a #GtsObject.
 *
 * Calls the clone method of @object. The call to this function will fail
 * if no clone method exists for the given object.
 *
 * Returns: a new object clone of @object.
 */
GtsObject * gts_object_clone (GtsObject * object)
{
  GtsObject * clone;

  g_return_val_if_fail (object != NULL, NULL);
  g_return_val_if_fail (object->klass->clone, NULL);

  clone = g_malloc0 (object->klass->info.object_size);
  clone->klass = object->klass;
  object_init (clone);
  (* object->klass->clone) (clone, object);

#ifdef DEBUG_IDENTITY
  id_insert (clone);
#ifdef DEBUG_LEAKS
  fprintf (stderr, "clone %s %p->%d\n", clone->klass->info.name, 
	   clone, 
	   id (clone));
#endif
#endif

  return clone;
}

/**
 * gts_object_destroy:
 * @object: a #GtsObject.
 *
 * Calls the destroy method of @object, freeing all memory allocated for it.
 */
void gts_object_destroy (GtsObject * object)
{
  g_assert (object->klass->destroy);
  GTS_OBJECT_SET_FLAGS (object, GTS_DESTROYED);
  (* object->klass->destroy) (object);
}

/**
 * gts_object_reset_reserved:
 * @object: a #GtsObject.
 *
 * Reset the reserved field of @object.
 */
void gts_object_reset_reserved (GtsObject * object)
{
  g_return_if_fail (object != NULL);

  object->reserved = NULL;
}

/**
 * gts_object_attributes:
 * @object: a #GtsObject.
 * @from: a #GtsObject.
 *
 * Calls the attributes() method of @object using @from as source.
 */
void gts_object_attributes (GtsObject * object, GtsObject * from)
{
  g_return_if_fail (object != NULL);

  if (object->klass->attributes)
    (* object->klass->attributes) (object, from);
}

static void free_class (gchar * name, GtsObjectClass * klass)
{
  g_free (klass);
}

/**
 * gts_finalize:
 *
 * Free all the memory allocated by the object system of GTS. No other
 * GTS function can be called after this function has been called.
 */
void gts_finalize (void)
{
  if (class_table) {
    g_hash_table_foreach (class_table, (GHFunc) free_class, NULL);
    g_hash_table_destroy (class_table);
    class_table = NULL;
  }
}