]>
code.delx.au - pulseaudio/blob - src/pulsecore/object.h
1 #ifndef foopulseobjecthfoo
2 #define foopulseobjecthfoo
7 This file is part of PulseAudio.
9 Copyright 2004-2006 Lennart Poettering
10 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
29 #include <sys/types.h>
31 #include <pulse/xmalloc.h>
32 #include <pulsecore/refcnt.h>
33 #include <pulsecore/macro.h>
35 typedef struct pa_object pa_object
;
39 const char *type_name
;
40 void (*free
)(pa_object
*o
);
41 int (*check_type
)(const char *type_name
);
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)
47 #define pa_object_free ((void (*) (pa_object* o)) pa_xfree)
49 int pa_object_check_type(const char *type
);
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;
56 pa_object
*pa_object_ref(pa_object
*o
);
57 void pa_object_unref(pa_object
*o
);
59 static inline int pa_object_refcnt(pa_object
*o
) {
60 return o
? PA_REFCNT_VALUE(o
) : 0;
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"));
69 #define pa_object_assert_ref(o) pa_assert(pa_object_refcnt(o) > 0)
71 #define PA_OBJECT(o) pa_object_cast(o)
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; \
78 static inline c* c##_cast(void *o) { \
79 pa_assert(c##_isinstance(o)); \
82 static inline c* c##_ref(c *o) { \
83 return (c*) pa_object_ref(PA_OBJECT(o)); \
85 static inline void c##_unref(c* o) { \
86 pa_object_unref(PA_OBJECT(o)); \
88 static inline int c##_refcnt(c* o) { \
89 return pa_object_refcnt(PA_OBJECT(o)); \
91 static inline void c##_assert_ref(c *o) { \
92 pa_object_assert_ref(PA_OBJECT(o)); \
94 struct __stupid_useless_struct_to_allow_trailing_semicolon
96 #define PA_DEFINE_CHECK_TYPE(c, parent) \
97 int c##_check_type(const char *type) { \
99 if (strcmp(type, #c) == 0) \
101 return parent##_check_type(type); \
103 struct __stupid_useless_struct_to_allow_trailing_semicolon