aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-object.c
blob: 7d6f38a55be371af7918bc7f72abc7c046846276 (plain) (blame)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-object.c: Base class for Camel */

/*
 * Author:
 *  Dan Winship <danw@ximian.com>
 *
 * Copyright 2000 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include "camel-object.h"

/* I just mashed the keyboard for these... */
#define CAMEL_OBJECT_MAGIC_VALUE           0x77A344EF
#define CAMEL_OBJECT_CLASS_MAGIC_VALUE     0xEE26A990
#define CAMEL_OBJECT_FINALIZED_VALUE       0x84AC3656
#define CAMEL_OBJECT_CLASS_FINALIZED_VALUE 0x7621ABCD

#define DEFAULT_PREALLOCS 8

#define BAST_CASTARD 1      /* Define to return NULL when casts fail */

#define NULL_PREP_VALUE ((gpointer)make_global_classfuncs)  /* See camel_object_class_declare_event */

/* ** Quickie type system ************************************************* */

typedef struct _CamelTypeInfo
{
    CamelType self;
    CamelType parent;
    const gchar *name;

    size_t instance_size;
    GMemChunk *instance_chunk;
    CamelObjectInitFunc instance_init;
    CamelObjectFinalizeFunc instance_finalize;
    GList *free_instances;

    size_t classfuncs_size;
    CamelObjectClassInitFunc class_init;
    CamelObjectClassFinalizeFunc class_finalize;
    CamelObjectClass *global_classfuncs;
}
CamelTypeInfo;

typedef struct _CamelHookPair
{
    CamelObjectEventHookFunc func;
    gpointer user_data;
}
CamelHookPair;

/* ************************************************************************ */

static void camel_type_lock_up (void);
static void camel_type_lock_down (void);

static void obj_init (CamelObject * obj);
static void obj_finalize (CamelObject * obj);
static void obj_class_init (CamelObjectClass * class);
static void obj_class_finalize (CamelObjectClass * class);

static gboolean shared_is_of_type (CamelObjectShared * sh, CamelType ctype,
                   gboolean is_obj);
static void make_global_classfuncs (CamelTypeInfo * type_info);

/* ************************************************************************ */

G_LOCK_DEFINE_STATIC (type_system);
G_LOCK_DEFINE_STATIC (type_system_level);
static GPrivate *type_system_locklevel = NULL;

G_LOCK_DEFINE_STATIC (refcount);

static gboolean type_system_initialized = FALSE;
static GHashTable *ctype_to_typeinfo = NULL;
static const CamelType camel_object_type = 1;
static CamelType cur_max_type = CAMEL_INVALID_TYPE;

/* ************************************************************************ */

#define LOCK_VAL (GPOINTER_TO_INT (g_private_get (type_system_locklevel)))
#define LOCK_SET( val ) g_private_set (type_system_locklevel, GINT_TO_POINTER (val))

static void
camel_type_lock_up (void)
{
    G_LOCK (type_system_level);

    if (type_system_locklevel == NULL)
        type_system_locklevel = g_private_new (GINT_TO_POINTER (0));

    if (LOCK_VAL == 0) {
        G_UNLOCK (type_system_level);
        G_LOCK (type_system);
        G_LOCK (type_system_level);
    }

    LOCK_SET (LOCK_VAL + 1);

    G_UNLOCK (type_system_level);
}

static void
camel_type_lock_down (void)
{
    G_LOCK (type_system_level);

    if (type_system_locklevel == NULL) {
        g_warning
            ("camel_type_lock_down: lock down before a lock up?");
        type_system_locklevel = g_private_new (GINT_TO_POINTER (0));
        G_UNLOCK (type_system_level);
        return;
    }

    LOCK_SET (LOCK_VAL - 1);

    if (LOCK_VAL == 0)
        G_UNLOCK (type_system);

    G_UNLOCK (type_system_level);
}

void
camel_type_init (void)
{
    CamelTypeInfo *obj_info;

    camel_type_lock_up ();

    if (type_system_initialized) {
        g_warning
            ("camel_type_init: type system already initialized.");
        camel_type_lock_down ();
        return;
    }

    type_system_initialized = TRUE;
    ctype_to_typeinfo = g_hash_table_new (g_direct_hash, g_direct_equal);

    obj_info = g_new (CamelTypeInfo, 1);
    obj_info->self = camel_object_type;
    obj_info->parent = CAMEL_INVALID_TYPE;
    obj_info->name = "CamelObject";

    obj_info->instance_size = sizeof (CamelObject);
    obj_info->instance_chunk =
        g_mem_chunk_create (CamelObject, DEFAULT_PREALLOCS,
                    G_ALLOC_ONLY);
    obj_info->instance_init = obj_init;
    obj_info->instance_finalize = obj_finalize;
    obj_info->free_instances = NULL;

    obj_info->classfuncs_size = sizeof (CamelObjectClass);
    obj_info->class_init = obj_class_init;
    obj_info->class_finalize = obj_class_finalize;

    g_hash_table_insert (ctype_to_typeinfo,
                 GINT_TO_POINTER (CAMEL_INVALID_TYPE), NULL);
    g_hash_table_insert (ctype_to_typeinfo,
                 GINT_TO_POINTER (camel_object_type), obj_info);

    /* Sigh. Ugly */
    make_global_classfuncs (obj_info);

    cur_max_type = camel_object_type;

    camel_type_lock_down ();
}

CamelType
camel_type_register (CamelType parent, const gchar * name,
             size_t instance_size, size_t classfuncs_size,
             CamelObjectClassInitFunc class_init,
             CamelObjectClassFinalizeFunc class_finalize,
             CamelObjectInitFunc instance_init,
             CamelObjectFinalizeFunc instance_finalize)
{
    CamelTypeInfo *parent_info;
    CamelTypeInfo *obj_info;
    gchar *chunkname;

    g_return_val_if_fail (parent != CAMEL_INVALID_TYPE,
                  CAMEL_INVALID_TYPE);
    g_return_val_if_fail (name, CAMEL_INVALID_TYPE);
    g_return_val_if_fail (instance_size, CAMEL_INVALID_TYPE);
    g_return_val_if_fail (classfuncs_size, CAMEL_INVALID_TYPE);

    camel_type_lock_up ();

    if (type_system_initialized == FALSE) {
        G_UNLOCK (type_system);
        camel_type_init ();
        G_LOCK (type_system);
    }

    parent_info =
        g_hash_table_lookup (ctype_to_typeinfo,
                     GINT_TO_POINTER (parent));

    if (parent_info == NULL) {
        g_warning
            ("camel_type_register: no such parent type %d of class `%s'",
             parent, name);
        camel_type_lock_down ();
        return CAMEL_INVALID_TYPE;
    }

    if (parent_info->instance_size > instance_size) {
        g_warning
            ("camel_type_register: instance of class `%s' would be smaller than parent `%s'",
             name, parent_info->name);
        camel_type_lock_down ();
        return CAMEL_INVALID_TYPE;
    }

    if (parent_info->classfuncs_size > classfuncs_size) {
        g_warning
            ("camel_type_register: classfuncs of class `%s' would be smaller than parent `%s'",
             name, parent_info->name);
        camel_type_lock_down ();
        return CAMEL_INVALID_TYPE;
    }

    cur_max_type++;

    obj_info = g_new (CamelTypeInfo, 1);
    obj_info->self = cur_max_type;
    obj_info->parent = parent;
    obj_info->name = name;

    obj_info->instance_size = instance_size;
    chunkname =
        g_strdup_printf ("chunk for instances of Camel type `%s'",
                 name);
    obj_info->instance_chunk =
        g_mem_chunk_new (chunkname, instance_size,
                 instance_size * DEFAULT_PREALLOCS,
                 G_ALLOC_ONLY);
    g_free (chunkname);
    obj_info->instance_init = instance_init;
    obj_info->instance_finalize = instance_finalize;
    obj_info->free_instances = NULL;

    obj_info->classfuncs_size = classfuncs_size;
    obj_info->class_init = class_init;
    obj_info->class_finalize = class_finalize;

    g_hash_table_insert (ctype_to_typeinfo,
                 GINT_TO_POINTER (obj_info->self), obj_info);

    /* Sigh. Ugly. */
    make_global_classfuncs (obj_info);

    camel_type_lock_down ();
    return obj_info->self;
}

CamelObjectClass *
camel_type_get_global_classfuncs (CamelType type)
{
    CamelTypeInfo *type_info;

    g_return_val_if_fail (type != CAMEL_INVALID_TYPE, NULL);

    camel_type_lock_up ();
    type_info =
        g_hash_table_lookup (ctype_to_typeinfo,
                     GINT_TO_POINTER (type));
    camel_type_lock_down ();

    g_return_val_if_fail (type_info != NULL, NULL);

    return type_info->global_classfuncs;
}

const gchar *
camel_type_to_name (CamelType type)
{
    CamelTypeInfo *type_info;

    g_return_val_if_fail (type != CAMEL_INVALID_TYPE,
                  "(the invalid type)");

    camel_type_lock_up ();
    type_info =
        g_hash_table_lookup (ctype_to_typeinfo,
                     GINT_TO_POINTER (type));
    camel_type_lock_down ();

    g_return_val_if_fail (type_info != NULL,
                  "(a bad type parameter was specified)");

    return type_info->name;
}

/* ** The CamelObject ***************************************************** */

static void
obj_init (CamelObject * obj)
{
    obj->s.magic = CAMEL_OBJECT_MAGIC_VALUE;
    obj->ref_count = 1;
    obj->event_to_hooklist = NULL;
    obj->in_event = 0;
    obj->destroying = 0;
}

static void
obj_finalize (CamelObject * obj)
{
    g_return_if_fail (obj->s.magic == CAMEL_OBJECT_MAGIC_VALUE);
    g_return_if_fail (obj->ref_count == 0);
    g_return_if_fail (obj->in_event == 0);

    obj->s.magic = CAMEL_OBJECT_FINALIZED_VALUE;

    if (obj->event_to_hooklist) {
#if 0
        g_hash_table_foreach (obj->event_to_hooklist, (GHFunc) g_free,
                      NULL);
#endif
        g_hash_table_destroy (obj->event_to_hooklist);
        obj->event_to_hooklist = NULL;
    }
}

static void
obj_class_init (CamelObjectClass * class)
{
    class->s.magic = CAMEL_OBJECT_CLASS_MAGIC_VALUE;

    camel_object_class_declare_event (class, "finalize", NULL);
}

static void
obj_class_finalize (CamelObjectClass * class)
{
    g_return_if_fail (class->s.magic == CAMEL_OBJECT_CLASS_MAGIC_VALUE);

    class->s.magic = CAMEL_OBJECT_CLASS_FINALIZED_VALUE;

    if (class->event_to_preplist) {
        g_hash_table_foreach (class->event_to_preplist,
                      (GHFunc) g_free, NULL);
        g_hash_table_destroy (class->event_to_preplist);
        class->event_to_preplist = NULL;
    }
}

CamelType
camel_object_get_type (void)
{
    if (type_system_initialized == FALSE)
        camel_type_init ();

    return camel_object_type;
}

CamelObject *
camel_object_new (CamelType type)
{
    CamelTypeInfo *type_info;
    GSList *parents = NULL;
    GSList *head = NULL;
    CamelObject *instance;

    g_return_val_if_fail (type != CAMEL_INVALID_TYPE, NULL);

    /* Look up the type */

    camel_type_lock_up ();

    type_info =
        g_hash_table_lookup (ctype_to_typeinfo,
                     GINT_TO_POINTER (type));

    if (type_info == NULL) {
        g_warning
            ("camel_object_new: trying to create object of invalid type %d",
             type);
        camel_type_lock_down ();
        return NULL;
    }

    /* Grab an instance out of the freed ones if possible, alloc otherwise */

    if (type_info->free_instances) {
        GList *first;

        first = g_list_first (type_info->free_instances);
        instance = first->data;
        type_info->free_instances =
            g_list_remove_link (type_info->free_instances, first);
        g_list_free_1 (first);
    } else {
        instance = g_mem_chunk_alloc0 (type_info->instance_chunk);
    }

    /* Init the instance and classfuncs a bit */

    instance->s.type = type;
    instance->classfuncs = type_info->global_classfuncs;

    /* Loop through the parents in simplest -> most complex order, initing the class and instance.

     * When parent = CAMEL_INVALID_TYPE and we're at the end of the line, _lookup returns NULL
     * because we inserted it as corresponding to CAMEL_INVALID_TYPE. Clever, eh?
     */

    while (type_info) {
        parents = g_slist_prepend (parents, type_info);
        type_info =
            g_hash_table_lookup (ctype_to_typeinfo,
                         GINT_TO_POINTER (type_info->
                                  parent));
    }

    head = parents;

    for (; parents && parents->data; parents = parents->next) {
        CamelTypeInfo *thisinfo;

        thisinfo = parents->data;
        if (thisinfo->instance_init)
            (thisinfo->instance_init) (instance);
    }

    g_slist_free (head);

    camel_type_lock_down ();
    return instance;
}

void
camel_object_ref (CamelObject * obj)
{
    g_return_if_fail (CAMEL_IS_OBJECT (obj));

    G_LOCK (refcount);
    obj->ref_count++;
    G_UNLOCK (refcount);
}

void
camel_object_unref (CamelObject * obj)
{
    CamelTypeInfo *type_info;
    CamelTypeInfo *iter;
    GSList *parents = NULL;
    GSList *head = NULL;

    g_return_if_fail (CAMEL_IS_OBJECT (obj));

    G_LOCK (refcount);
    obj->ref_count--;

    if (obj->ref_count > 0) {
        G_UNLOCK (refcount);
        return;
    }

    G_UNLOCK (refcount);

    /* If the object already had its last unref, do not begin the
     * destruction process again. This can happen if, for example,
     * the object sends an event in its finalize handler (vfolders
     * do this).
     */

    if (obj->destroying)
        return;
    
    obj->destroying = 1;

    /* Send the finalize event */

    camel_object_trigger_event (obj, "finalize", NULL);

    /* Destroy it! hahaha! */

    camel_type_lock_up ();

    type_info =
        g_hash_table_lookup (ctype_to_typeinfo,
                     GINT_TO_POINTER (obj->s.type));

    if (type_info == NULL) {
        g_warning
            ("camel_object_unref: seemingly valid object has a bad type %d",
             obj->s.type);
        camel_type_lock_down ();
        return;
    }

    /* Loop through the parents in most complex -> simplest order, finalizing the class 
     * and instance.
     *
     * When parent = CAMEL_INVALID_TYPE and we're at the end of the line, _lookup returns NULL
     * because we inserted it as corresponding to CAMEL_INVALID_TYPE. Clever, eh?
     *
     * Use iter to preserve type_info for free_{instance,classfunc}s
     */

    iter = type_info;

    while (iter) {
        parents = g_slist_prepend (parents, iter);
        iter =
            g_hash_table_lookup (ctype_to_typeinfo,
                         GINT_TO_POINTER (iter->parent));
    }

    parents = g_slist_reverse (parents);
    head = parents;

    for (; parents && parents->data; parents = parents->next) {
        CamelTypeInfo *thisinfo;

        thisinfo = parents->data;
        if (thisinfo->instance_finalize)
            (thisinfo->instance_finalize) (obj);
    }

    g_slist_free (head);

    /* Sanity check */

    if (obj->ref_count != 0)
        g_warning ("camel_object_unref: destroyed object %s at %p somehow got"
               " referenced in destruction chain.",
               camel_type_to_name (obj->s.type),
               obj);

    /* A little bit of cleaning up.

     * Don't erase the type, so we can peek at it if a finalized object
     * is check_cast'ed somewhere.
     */

    memset (obj, 0, type_info->instance_size);
    obj->s.type = type_info->self;
    obj->s.magic = CAMEL_OBJECT_FINALIZED_VALUE;

    /* Tuck away the pointer for use in a new object */

    type_info->free_instances =
        g_list_prepend (type_info->free_instances, obj);

    camel_type_lock_down ();
}

gboolean
camel_object_is_of_type (CamelObject * obj, CamelType ctype)
{
    return shared_is_of_type ((CamelObjectShared *) obj, ctype, TRUE);
}

gboolean
camel_object_class_is_of_type (CamelObjectClass * class, CamelType ctype)
{
    return shared_is_of_type ((CamelObjectShared *) class, ctype, FALSE);
}

#ifdef BAST_CASTARD
#define ERRVAL NULL
#else
#define ERRVAL obj
#endif

CamelObject *
camel_object_check_cast (CamelObject * obj, CamelType ctype)
{
    if (shared_is_of_type ((CamelObjectShared *) obj, ctype, TRUE))
        return obj;
    return ERRVAL;
}

CamelObjectClass *
camel_object_class_check_cast (CamelObjectClass * class, CamelType ctype)
{
    if (shared_is_of_type ((CamelObjectShared *) class, ctype, FALSE))
        return class;
    return ERRVAL;
}

#undef ERRVAL

gchar *
camel_object_describe (CamelObject * obj)
{
    if (obj == NULL)
        return g_strdup ("a NULL pointer");

    if (obj->s.magic == CAMEL_OBJECT_MAGIC_VALUE) {
        return g_strdup_printf ("an instance of `%s' at %p",
                    camel_type_to_name (obj->s.type),
                    obj);
    } else if (obj->s.magic == CAMEL_OBJECT_FINALIZED_VALUE) {
        return g_strdup_printf ("a finalized instance of `%s' at %p",
                    camel_type_to_name (obj->s.type),
                    obj);
    } else if (obj->s.magic == CAMEL_OBJECT_CLASS_MAGIC_VALUE) {
        return g_strdup_printf ("the classfuncs of `%s' at %p",
                    camel_type_to_name (obj->s.type),
                    obj);
    } else if (obj->s.magic == CAMEL_OBJECT_CLASS_FINALIZED_VALUE) {
        return
            g_strdup_printf
            ("the finalized classfuncs of `%s' at %p",
             camel_type_to_name (obj->s.type), obj);
    }

    return g_strdup ("not a CamelObject");
}

/* This is likely to be called in the class_init callback,
 * and the type will likely be somewhat uninitialized. 
 * Is this a problem? We'll see....
 */
void
camel_object_class_declare_event (CamelObjectClass * class,
                  const gchar * name,
                  CamelObjectEventPrepFunc prep)
{
    g_return_if_fail (CAMEL_IS_OBJECT_CLASS (class));
    g_return_if_fail (name);

    if (class->event_to_preplist == NULL)
        class->event_to_preplist =
            g_hash_table_new (g_str_hash, g_str_equal);
    else if (g_hash_table_lookup (class->event_to_preplist, name) != NULL) {
        g_warning
            ("camel_object_class_declare_event: event `%s' already declared for `%s'",
             name, camel_type_to_name (class->s.type));
        return;
    }

    /* AIEEEEEEEEEEEEEEEEEEEEEE

     * I feel so naughty. Since it's valid to declare an event and not
     * provide a hook, it should be valid to insert a NULL value into
     * the table. However, then our lookup in trigger_event would be
     * ambiguous, not telling us whether the event is undefined or whether
     * it merely has no hook.
     *
     * So we create an 'NULL prep' value that != NULL... specifically, it
     * equals the address of one of our static functions , because that
     * can't possibly be your hook.
     *
     * Just don't forget to check for the 'evil value' and it'll work,
     * I promise.
     */

    if (prep == NULL)
        prep = NULL_PREP_VALUE;

    g_hash_table_insert (class->event_to_preplist, g_strdup (name), prep);
}

void
camel_object_hook_event (CamelObject * obj, const gchar * name,
             CamelObjectEventHookFunc hook, gpointer user_data)
{
    GSList *hooklist;
    CamelHookPair *pair;
    gpointer old_name, old_hooklist;

    g_return_if_fail (CAMEL_IS_OBJECT (obj));
    g_return_if_fail (name);
    g_return_if_fail (hook);

    if (obj->event_to_hooklist == NULL)
        obj->event_to_hooklist =
            g_hash_table_new (g_str_hash, g_str_equal);

    pair = g_new (CamelHookPair, 1);
    pair->func = hook;
    pair->user_data = user_data;

    if (g_hash_table_lookup_extended (obj->event_to_hooklist, name,
                      &old_name, &old_hooklist)) {
        hooklist = g_slist_prepend (old_hooklist, pair);
        g_hash_table_insert (obj->event_to_hooklist, old_name,
                     hooklist);
    } else {
        hooklist = g_slist_prepend (NULL, pair);
        g_hash_table_insert (obj->event_to_hooklist, g_strdup (name),
                     hooklist);
    }
}

void
camel_object_unhook_event (CamelObject * obj, const gchar * name,
               CamelObjectEventHookFunc hook, gpointer user_data)
{
    GSList *hooklist;
    GSList *head;

    g_return_if_fail (CAMEL_IS_OBJECT (obj));
    g_return_if_fail (name);
    g_return_if_fail (hook);

    if (obj->event_to_hooklist == NULL) {
        g_warning
            ("camel_object_unhook_event: trying to unhook `%s' from an instance "
             "of `%s' with no hooks attached", name,
             camel_type_to_name (obj->s.type));
        return;
    }

    hooklist = g_hash_table_lookup (obj->event_to_hooklist, name);

    if (hooklist == NULL) {
        g_warning
            ("camel_object_unhook_event: trying to unhook `%s' from an instance "
             "of `%s' with no hooks attached to that event.",
             name, camel_type_to_name (obj->s.type));
        return;
    }

    head = hooklist;

    while (hooklist) {
        CamelHookPair *pair = (CamelHookPair *) hooklist->data;

        if (pair->func == hook && pair->user_data == user_data) {
            g_free (hooklist->data);
            head = g_slist_remove_link (head, hooklist);
            g_slist_free_1 (hooklist);
            g_hash_table_insert (obj->event_to_hooklist, (char *) name,
                         head);
            return;
        }

        hooklist = hooklist->next;
    }

    g_warning
        ("camel_object_unhook_event: cannot find hook/data pair %p/%p in an "
         "instance of `%s' attached to `%s'", hook, user_data,
         camel_type_to_name (obj->s.type), name);
}

void
camel_object_trigger_event (CamelObject * obj, const gchar * name,
                gpointer event_data)
{
    GSList *hooklist;
    CamelHookPair *pair;
    CamelObjectEventPrepFunc prep;

    g_return_if_fail (CAMEL_IS_OBJECT (obj));
    g_return_if_fail (name);

    if (obj->in_event) {
        g_warning
            ("camel_object_trigger_event: trying to trigger `%s' in class "
             "`%s' while already triggering another event", name,
             camel_type_to_name (obj->s.type));
        return;
    }

    if (obj->classfuncs->event_to_preplist == NULL) {
        g_warning
            ("camel_object_trigger_event: trying to trigger `%s' in class "
             "`%s' with no defined events.", name,
             camel_type_to_name (obj->s.type));
        return;
    }

    prep = g_hash_table_lookup (obj->classfuncs->event_to_preplist, name);

    if (prep == NULL) {
        g_warning
            ("camel_object_trigger_event: trying to trigger undefined "
             "event `%s' in class `%s'.", name,
             camel_type_to_name (obj->s.type));
        return;
    }

    /* Ref so that it can't get destroyed in the event, which would
     * be Bad. And it's a valid ref anyway...
     */

    camel_object_ref (obj);
    obj->in_event = 1;

    if ((prep != NULL_PREP_VALUE && !prep (obj, event_data))
        || obj->event_to_hooklist == NULL) {
        obj->in_event = 0;
        camel_object_unref (obj);
        return;
    }

    hooklist = g_hash_table_lookup (obj->event_to_hooklist, name);

    while (hooklist && hooklist->data) {
        pair = hooklist->data;
        (pair->func) (obj, event_data, pair->user_data);
        hooklist = hooklist->next;
    }

    obj->in_event = 0;
    camel_object_unref (obj);
}

/* ** Static helpers ****************************************************** */

static gboolean
shared_is_of_type (CamelObjectShared * sh, CamelType ctype, gboolean is_obj)
{
    CamelTypeInfo *type_info;
    gchar *targtype;

    if (is_obj)
        targtype = "instance";
    else
        targtype = "classdata";

    if (ctype == CAMEL_INVALID_TYPE) {
        g_warning
            ("shared_is_of_type: trying to cast to CAMEL_INVALID_TYPE");
        return FALSE;
    }

    if (sh == NULL) {
        g_warning
            ("shared_is_of_type: trying to cast NULL to %s of `%s'",
             targtype, camel_type_to_name (ctype));
        return FALSE;
    }

    if (sh->magic == CAMEL_OBJECT_FINALIZED_VALUE) {
        g_warning
            ("shared_is_of_type: trying to cast finalized instance "
             "of `%s' into %s of `%s'",
             camel_type_to_name (sh->type), targtype,
             camel_type_to_name (ctype));
        return FALSE;
    }

    if (sh->magic == CAMEL_OBJECT_CLASS_FINALIZED_VALUE) {
        g_warning
            ("shared_is_of_type: trying to cast finalized classdata "
             "of `%s' into %s of `%s'",
             camel_type_to_name (sh->type), targtype,
             camel_type_to_name (ctype));
        return FALSE;
    }

    if (is_obj) {
        if (sh->magic == CAMEL_OBJECT_CLASS_MAGIC_VALUE) {
            g_warning
                ("shared_is_of_type: trying to cast classdata "
                 "of `%s' into instance of `%s'",
                 camel_type_to_name (sh->type),
                 camel_type_to_name (ctype));
            return FALSE;
        }

        if (sh->magic != CAMEL_OBJECT_MAGIC_VALUE) {
            g_warning
                ("shared_is_of_type: trying to cast junk data "
                 "into instance of `%s'",
                 camel_type_to_name (ctype));
            return FALSE;
        }
    } else {
        if (sh->magic == CAMEL_OBJECT_MAGIC_VALUE) {
            g_warning
                ("shared_is_of_type: trying to cast instance "
                 "of `%s' into classdata of `%s'",
                 camel_type_to_name (sh->type),
                 camel_type_to_name (ctype));
            return FALSE;
        }

        if (sh->magic != CAMEL_OBJECT_CLASS_MAGIC_VALUE) {
            g_warning
                ("shared_is_of_type: trying to cast junk data "
                 "into classdata of `%s'",
                 camel_type_to_name (ctype));
            return FALSE;
        }
    }

    camel_type_lock_up ();

    type_info =
        g_hash_table_lookup (ctype_to_typeinfo,
                     GINT_TO_POINTER (sh->type));

    if (type_info == NULL) {
        g_warning ("shared_is_of_type: seemingly valid %s has "
               "bad type %d.", targtype, sh->type);
        camel_type_lock_down ();
        return FALSE;
    }

    while (type_info) {
        if (type_info->self == ctype) {
            camel_type_lock_down ();
            return TRUE;
        }

        type_info =
            g_hash_table_lookup (ctype_to_typeinfo,
                         GINT_TO_POINTER (type_info->
                                  parent));
    }

    /* this isn't an error, e.g. CAMEL_IS_FOLDER(folder), its upto  the
       caller to handle the false case */
    /*g_warning
        ("shared_is_of_type: %s of `%s' (@%p) is not also %s of `%s'",
         targtype, camel_type_to_name (sh->type), sh, targtype,
         camel_type_to_name (ctype));*/

    camel_type_lock_down ();
    return FALSE;
}

static void
make_global_classfuncs (CamelTypeInfo * type_info)
{
    CamelObjectClass *funcs;
    GSList *parents;
    GSList *head;

    g_assert (type_info);

    funcs = g_malloc0 (type_info->classfuncs_size);
    funcs->s.type = type_info->self;

    type_info->global_classfuncs = funcs;

    parents = NULL;
    while (type_info) {
        parents = g_slist_prepend (parents, type_info);
        type_info =
            g_hash_table_lookup (ctype_to_typeinfo,
                         GINT_TO_POINTER (type_info->
                                  parent));
    }

    head = parents;

    for (; parents && parents->data; parents = parents->next) {
        CamelTypeInfo *thisinfo;

        thisinfo = parents->data;
        if (thisinfo->class_init)
            (thisinfo->class_init) (funcs);
    }

    g_slist_free (head);
}