1 /*****************************************************************************
2 * vlc_objects.h: vlc_object_t definition and manipulation methods
3 *****************************************************************************
4 * Copyright (C) 2002-2008 VLC authors and VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
24 * \defgroup vlc_object VLC objects
28 * Common VLC object defintions
32 struct vlc_object_internals
;
33 struct vlc_object_marker
;
36 * VLC object common members
38 * Common public properties for all VLC objects.
39 * Object also have private properties maintained by the core, see
40 * \ref vlc_object_internals_t
44 struct vlc_logger
*logger
;
46 struct vlc_object_internals
*priv
;
47 struct vlc_object_marker
*obj
;
54 * A boolean during module probing when the probe is "forced".
55 * See \ref module_need().
61 * Type-safe vlc_object_t cast
63 * This macro attempts to cast a pointer to a compound type to a
64 * \ref vlc_object_t pointer in a type-safe manner.
65 * It checks if the compound type actually starts with an embedded
66 * \ref vlc_object_t structure.
68 #if !defined(__cplusplus)
69 # define VLC_OBJECT(x) \
71 struct vlc_object_marker *: (x), \
72 default: (&((x)->obj)) \
74 # define vlc_object_cast(t)
76 static inline vlc_object_t
*VLC_OBJECT(vlc_object_t
*o
)
81 # define vlc_object_cast(t) \
83 static inline struct vlc_object_t *VLC_OBJECT(struct t *d) \
85 return (struct vlc_object_t *)d; \
89 vlc_object_cast(libvlc_int_t
)
90 vlc_object_cast(intf_thread_t
)
91 vlc_object_cast(vlc_player_t
)
92 vlc_object_cast(stream_t
)
93 vlc_object_cast(stream_directory_t
)
94 vlc_object_cast(stream_extractor_t
)
95 vlc_object_cast(decoder_t
)
96 vlc_object_cast(filter_t
)
97 vlc_object_cast(audio_output
)
98 vlc_object_cast(vout_thread_t
)
99 vlc_object_cast(vout_display_t
)
100 vlc_object_cast(vout_window_t
)
101 vlc_object_cast(sout_instance_t
)
102 vlc_object_cast(sout_stream_t
)
103 vlc_object_cast(sout_access_out_t
)
104 vlc_object_cast(extensions_manager_t
)
105 vlc_object_cast(fingerprinter_thread_t
)
106 vlc_object_cast(demux_meta_t
)
107 vlc_object_cast(xml_t
)
108 vlc_object_cast(services_discovery_t
)
109 vlc_object_cast(vlc_renderer_discovery_t
)
110 vlc_object_cast(vlc_medialibrary_module_t
)
112 /* The root object */
115 struct vlc_object_t obj
;
119 * Allocates and initializes a vlc object.
121 * @param i_size object byte size
123 * @return the new object, or NULL on error.
125 VLC_API
void *vlc_object_create( vlc_object_t
*, size_t ) VLC_MALLOC VLC_USED
;
128 * Drops the strong reference to an object.
130 * This removes the initial strong reference to a given object. This must be
131 * called exactly once per allocated object after it is no longer needed,
132 * matching vlc_object_create() or vlc_custom_create().
134 VLC_API
void vlc_object_delete(vlc_object_t
*obj
);
135 #define vlc_object_delete(obj) vlc_object_delete(VLC_OBJECT(obj))
137 VLC_API
size_t vlc_list_children(vlc_object_t
*, vlc_object_t
**, size_t) VLC_USED
;
140 * Returns the object type name.
142 * This returns a nul-terminated string identifying the object type.
143 * The string is valid for at least as long as the object reference.
145 * \param obj object whose type name to get
147 VLC_API
const char *vlc_object_typename(const vlc_object_t
*obj
) VLC_USED
;
150 * Gets the parent of an object.
152 * \return the parent object (NULL if none)
154 * \note The returned parent object pointer is valid as long as the child is.
156 VLC_API vlc_object_t
*vlc_object_parent(vlc_object_t
*obj
) VLC_USED
;
157 #define vlc_object_parent(o) vlc_object_parent(VLC_OBJECT(o))
159 static inline struct vlc_logger
*vlc_object_logger(vlc_object_t
*obj
)
163 #define vlc_object_logger(o) vlc_object_logger(VLC_OBJECT(o))
166 * Tries to get the name of module bound to an object.
168 * \warning This function is intrinsically race-prone, as a module may be
169 * bound or unbound asynchronously by another thread.
170 * Do not trust the result for any purpose other than debugging/tracing.
172 * \return Normally, this returns a heap-allocated nul-terminated string
173 * which is the name of the module. If no module are bound to the object, it
174 * returns NULL. It also returns NULL on error.
176 #define vlc_object_get_name(obj) var_GetString(obj, "module-name")
178 #define vlc_object_create(a,b) vlc_object_create( VLC_OBJECT(a), b )
180 #define vlc_object_find_name(a,b) \
181 vlc_object_find_name( VLC_OBJECT(a),b)
184 static inline libvlc_int_t
*vlc_object_instance(vlc_object_t
*obj
)
186 vlc_object_t
*parent
;
190 while ((obj
= vlc_object_parent(obj
)) != NULL
);
192 return (libvlc_int_t
*)parent
;
194 #define vlc_object_instance(o) vlc_object_instance(VLC_OBJECT(o))
196 /* Here for backward compatibility. TODO: Move to <vlc_vout.h>! */
197 VLC_API vout_thread_t
*vout_Hold(vout_thread_t
*vout
);
198 VLC_API
void vout_Release(vout_thread_t
*vout
);
200 /* Here for backward compatibility. TODO: Move to <vlc_aout.h>! */
201 VLC_API audio_output_t
*aout_Hold(audio_output_t
*aout
);
202 VLC_API
void aout_Release(audio_output_t
*aout
);
204 /* TODO: remove vlc_object_hold/_release() for GUIs, remove this */
205 VLC_DEPRECATED
static inline void *vlc_object_hold(vlc_object_t
*o
)
207 const char *tn
= vlc_object_typename(o
);
209 if (!strcmp(tn
, "audio output"))
210 aout_Hold((audio_output_t
*)o
);
211 if (!strcmp(tn
, "video output"))
212 vout_Hold((vout_thread_t
*)o
);
216 static inline void vlc_object_release(vlc_object_t
*o
)
218 const char *tn
= vlc_object_typename(o
);
220 if (!strcmp(tn
, "audio output"))
221 aout_Release((audio_output_t
*)o
);
222 if (!strcmp(tn
, "video output"))
223 vout_Release((vout_thread_t
*)o
);
227 * @defgroup objres Object resources
229 * The object resource functions tie resource allocation to an instance of
230 * a module through a VLC object.
231 * Such resource will be automatically freed, in first in last out order,
232 * when the module instance associated with the VLC object is terminated.
234 * Specifically, if the module instance activation/probe function fails, the
235 * resource will be freed immediately after the failure. If the activation
236 * succeeds, the resource will be freed when the module instance is terminated.
238 * This is a convenience mechanism to save explicit clean-up function calls
245 * Allocates memory for a module.
247 * This function allocates memory from the heap for a module instance.
248 * The memory is uninitialized.
250 * @param obj VLC object to tie the memory allocation to
251 * @param size byte size of the memory allocation
253 * @return a pointer to the allocated memory, or NULL on error (errno is set).
255 VLC_API VLC_MALLOC
void *vlc_obj_malloc(vlc_object_t
*obj
, size_t size
);
258 * Allocates a zero-initialized table for a module.
260 * This function allocates a table from the heap for a module instance.
261 * The memory is initialized to all zeroes.
263 * @param obj VLC object to tie the memory allocation to
264 * @param nmemb number of table entries
265 * @param size byte size of a table entry
267 * @return a pointer to the allocated memory, or NULL on error (errno is set).
269 VLC_API VLC_MALLOC
void *vlc_obj_calloc(vlc_object_t
*obj
, size_t nmemb
,
273 * Duplicates a string for a module.
275 * This function allocates a copy of a nul-terminated string for a module
278 * @param obj VLC object to tie the memory allocation to
279 * @param str string to copy
281 * @return a pointer to the copy, or NULL on error (errno is set).
283 VLC_API VLC_MALLOC
char *vlc_obj_strdup(vlc_object_t
*obj
, const char *str
);
286 * Manually frees module memory.
288 * This function manually frees a resource allocated with vlc_obj_malloc(),
289 * vlc_obj_calloc() or vlc_obj_strdup() before the module instance is
290 * terminated. This is seldom necessary.
292 * @param obj VLC object that the allocation was tied to
293 * @param ptr pointer to the allocated resource
295 VLC_API
void vlc_obj_free(vlc_object_t
*obj
, void *ptr
);