]> code.delx.au - pulseaudio/blob - src/pulsecore/object.h
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / object.h
1 #ifndef foopulseobjecthfoo
2 #define foopulseobjecthfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of PulseAudio.
8
9 Copyright 2004-2006 Lennart Poettering
10 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
11
12 PulseAudio is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as published
14 by the Free Software Foundation; either version 2 of the License,
15 or (at your option) any later version.
16
17 PulseAudio is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU Lesser General Public License
23 along with PulseAudio; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 USA.
26 ***/
27
28 #include <string.h>
29 #include <sys/types.h>
30
31 #include <pulse/xmalloc.h>
32 #include <pulsecore/refcnt.h>
33 #include <pulsecore/macro.h>
34
35 typedef struct pa_object pa_object;
36
37 struct pa_object {
38 PA_REFCNT_DECLARE;
39 const char *type_name;
40 void (*free)(pa_object *o);
41 int (*check_type)(const char *type_name);
42 };
43
44 pa_object *pa_object_new_internal(size_t size, const char *type_name, int (*check_type)(const char *type_name));
45 #define pa_object_new(type) ((type*) pa_object_new_internal(sizeof(type), #type, type##_check_type)
46
47 #define pa_object_free ((void (*) (pa_object* o)) pa_xfree)
48
49 int pa_object_check_type(const char *type);
50
51 static inline int pa_object_isinstance(void *o) {
52 pa_object *obj = (pa_object*) o;
53 return obj ? obj->check_type("pa_object") : 0;
54 }
55
56 pa_object *pa_object_ref(pa_object *o);
57 void pa_object_unref(pa_object *o);
58
59 static inline int pa_object_refcnt(pa_object *o) {
60 return o ? PA_REFCNT_VALUE(o) : 0;
61 }
62
63 static inline pa_object* pa_object_cast(void *o) {
64 pa_object *obj = (pa_object*) o;
65 pa_assert(!obj || obj->check_type("pa_object"));
66 return obj;
67 }
68
69 #define pa_object_assert_ref(o) pa_assert(pa_object_refcnt(o) > 0)
70
71 #define PA_OBJECT(o) pa_object_cast(o)
72
73 #define PA_DECLARE_CLASS(c) \
74 static inline int c##_isinstance(void *o) { \
75 pa_object *obj = (pa_object*) o; \
76 return obj ? obj->check_type(#c) : 1; \
77 } \
78 static inline c* c##_cast(void *o) { \
79 pa_assert(c##_isinstance(o)); \
80 return (c*) o; \
81 } \
82 static inline c* c##_ref(c *o) { \
83 return (c*) pa_object_ref(PA_OBJECT(o)); \
84 } \
85 static inline void c##_unref(c* o) { \
86 pa_object_unref(PA_OBJECT(o)); \
87 } \
88 static inline int c##_refcnt(c* o) { \
89 return pa_object_refcnt(PA_OBJECT(o)); \
90 } \
91 static inline void c##_assert_ref(c *o) { \
92 pa_object_assert_ref(PA_OBJECT(o)); \
93 } \
94 struct __stupid_useless_struct_to_allow_trailing_semicolon
95
96 #define PA_DEFINE_CHECK_TYPE(c, parent) \
97 int c##_check_type(const char *type) { \
98 pa_assert(type); \
99 if (strcmp(type, #c) == 0) \
100 return 1; \
101 return parent##_check_type(type); \
102 } \
103 struct __stupid_useless_struct_to_allow_trailing_semicolon
104
105
106 #endif