Debian Codegraph @

libmowgli-2/src/libmowgli/object/class.h

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * class.h: Object class and type management, cast checking.
4 *
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #ifndef MOWGLI_SRC_LIBMOWGLI_OBJECT_CLASS_H_INCLUDE_GUARD
25 #define MOWGLI_SRC_LIBMOWGLI_OBJECT_CLASS_H_INCLUDE_GUARD 1
26
27 #include "container/list.h"
28 #include "core/stdinc.h"
29
30 typedef void (*mowgli_destructor_t)(void *);
31
32 typedef struct
33 {
34 char *name;
35 mowgli_list_t derivitives;
36 mowgli_destructor_t destructor;
37 mowgli_boolean_t dynamic;
38 mowgli_list_t message_handlers;
39 } mowgli_object_class_t;
40
41 extern void mowgli_object_class_init(mowgli_object_class_t *klass, const char *name, mowgli_destructor_t des, mowgli_boolean_t dynamic);
42 extern int mowgli_object_class_check_cast(mowgli_object_class_t *klass1, mowgli_object_class_t *klass2);
43 extern void mowgli_object_class_set_derivitive(mowgli_object_class_t *klass, mowgli_object_class_t *parent);
44 extern void *mowgli_object_class_reinterpret_impl( /* mowgli_object_t */ void *object, mowgli_object_class_t *klass);
45 extern mowgli_object_class_t *mowgli_object_class_find_by_name(const char *name);
46 extern void mowgli_object_class_destroy(mowgli_object_class_t *klass);
47
48 #define MOWGLI_REINTERPRET_CAST(object, klass) (klass *) mowgli_object_class_reinterpret_impl(object, mowgli_object_class_find_by_name(#klass))
49
50 #define mowgli_forced_cast(from_type, to_type, from, to) \
51 do \
52 { \
53 union cast_union \
54 { \
55 to_type out; \
56 from_type in; \
57 } u; \
58 typedef int cant_use_union_cast[ \
59 sizeof(from_type) == sizeof(u) \
60 && sizeof(from_type) == sizeof(to_type) ? 1 : -1]; \
61 u.in = from; \
62 to = u.out; \
63 } while (0)
64
65 #endif /* MOWGLI_SRC_LIBMOWGLI_OBJECT_CLASS_H_INCLUDE_GUARD */