]> code.delx.au - pulseaudio/blob - src/pulsecore/object.h
remap: add MMX mono to stereo
[pulseaudio] / src / pulsecore / object.h
1 #ifndef foopulseobjecthfoo
2 #define foopulseobjecthfoo
3
4 /***
5 This file is part of PulseAudio.
6
7 Copyright 2004-2006 Lennart Poettering
8 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9
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.1 of the License,
13 or (at your option) any later version.
14
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.
19
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
23 USA.
24 ***/
25
26 #include <string.h>
27 #include <sys/types.h>
28
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/refcnt.h>
31 #include <pulsecore/macro.h>
32
33 typedef struct pa_object pa_object;
34
35 struct pa_object {
36 PA_REFCNT_DECLARE;
37 const char *type_name;
38 void (*free)(pa_object *o);
39 int (*check_type)(const char *type_name);
40 };
41
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)
44
45 #define pa_object_free ((void (*) (pa_object* _obj)) pa_xfree)
46
47 int pa_object_check_type(const char *type);
48
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;
52 }
53
54 pa_object *pa_object_ref(pa_object *o);
55 void pa_object_unref(pa_object *o);
56
57 static inline int pa_object_refcnt(pa_object *o) {
58 return o ? PA_REFCNT_VALUE(o) : 0;
59 }
60
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"));
64 return obj;
65 }
66
67 #define pa_object_assert_ref(o) pa_assert(pa_object_refcnt(o) > 0)
68
69 #define PA_OBJECT(o) pa_object_cast(o)
70
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; \
75 } \
76 static inline c* c##_cast(void *o) { \
77 pa_assert(c##_isinstance(o)); \
78 return (c*) o; \
79 } \
80 static inline c* c##_ref(c *o) { \
81 return (c*) pa_object_ref(PA_OBJECT(o)); \
82 } \
83 static inline void c##_unref(c* o) { \
84 pa_object_unref(PA_OBJECT(o)); \
85 } \
86 static inline int c##_refcnt(c* o) { \
87 return pa_object_refcnt(PA_OBJECT(o)); \
88 } \
89 static inline void c##_assert_ref(c *o) { \
90 pa_object_assert_ref(PA_OBJECT(o)); \
91 } \
92 struct __stupid_useless_struct_to_allow_trailing_semicolon
93
94 #define PA_DEFINE_CHECK_TYPE(c, parent) \
95 int c##_check_type(const char *type) { \
96 pa_assert(type); \
97 if (strcmp(type, #c) == 0) \
98 return 1; \
99 return parent##_check_type(type); \
100 } \
101 struct __stupid_useless_struct_to_allow_trailing_semicolon
102
103
104 #endif