]>
code.delx.au - pulseaudio/blob - src/pulsecore/object.h
1 #ifndef foopulseobjecthfoo
2 #define foopulseobjecthfoo
5 This file is part of PulseAudio.
7 Copyright 2004-2006 Lennart Poettering
8 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
10 PulseAudio is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published
12 by the Free Software Foundation; either version 2 of the License,
13 or (at your option) any later version.
15 PulseAudio is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with PulseAudio; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/types.h>
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/refcnt.h>
31 #include <pulsecore/macro.h>
33 typedef struct pa_object pa_object
;
37 const char *type_name
;
38 void (*free
)(pa_object
*o
);
39 int (*check_type
)(const char *type_name
);
42 pa_object
*pa_object_new_internal(size_t size
, const char *type_name
, int (*check_type
)(const char *type_name
));
43 #define pa_object_new(type) ((type*) pa_object_new_internal(sizeof(type), #type, type##_check_type)
45 #define pa_object_free ((void (*) (pa_object* _obj)) pa_xfree)
47 int pa_object_check_type(const char *type
);
49 static inline int pa_object_isinstance(void *o
) {
50 pa_object
*obj
= (pa_object
*) o
;
51 return obj
? obj
->check_type("pa_object") : 0;
54 pa_object
*pa_object_ref(pa_object
*o
);
55 void pa_object_unref(pa_object
*o
);
57 static inline int pa_object_refcnt(pa_object
*o
) {
58 return o
? PA_REFCNT_VALUE(o
) : 0;
61 static inline pa_object
* pa_object_cast(void *o
) {
62 pa_object
*obj
= (pa_object
*) o
;
63 pa_assert(!obj
|| obj
->check_type("pa_object"));
67 #define pa_object_assert_ref(o) pa_assert(pa_object_refcnt(o) > 0)
69 #define PA_OBJECT(o) pa_object_cast(o)
71 #define PA_DECLARE_CLASS(c) \
72 static inline int c##_isinstance(void *o) { \
73 pa_object *obj = (pa_object*) o; \
74 return obj ? obj->check_type(#c) : 1; \
76 static inline c* c##_cast(void *o) { \
77 pa_assert(c##_isinstance(o)); \
80 static inline c* c##_ref(c *o) { \
81 return (c*) pa_object_ref(PA_OBJECT(o)); \
83 static inline void c##_unref(c* o) { \
84 pa_object_unref(PA_OBJECT(o)); \
86 static inline int c##_refcnt(c* o) { \
87 return pa_object_refcnt(PA_OBJECT(o)); \
89 static inline void c##_assert_ref(c *o) { \
90 pa_object_assert_ref(PA_OBJECT(o)); \
92 struct __stupid_useless_struct_to_allow_trailing_semicolon
94 #define PA_DEFINE_CHECK_TYPE(c, parent) \
95 int c##_check_type(const char *type) { \
97 if (strcmp(type, #c) == 0) \
99 return parent##_check_type(type); \
101 struct __stupid_useless_struct_to_allow_trailing_semicolon