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(playlist_t
)
93 vlc_object_cast(stream_t
)
94 vlc_object_cast(stream_directory_t
)
95 vlc_object_cast(stream_extractor_t
)
96 vlc_object_cast(decoder_t
)
97 vlc_object_cast(filter_t
)
98 vlc_object_cast(audio_output
)
99 vlc_object_cast(vout_thread_t
)
100 vlc_object_cast(vout_display_t
)
101 vlc_object_cast(vout_window_t
)
102 vlc_object_cast(sout_instance_t
)
103 vlc_object_cast(sout_stream_t
)
104 vlc_object_cast(sout_access_out_t
)
105 vlc_object_cast(extensions_manager_t
)
106 vlc_object_cast(fingerprinter_thread_t
)
107 vlc_object_cast(demux_meta_t
)
108 vlc_object_cast(xml_t
)
109 vlc_object_cast(services_discovery_t
)
110 vlc_object_cast(vlc_renderer_discovery_t
)
111 vlc_object_cast(vlc_medialibrary_module_t
)
113 /* The root object */
116 struct vlc_object_t obj
;
120 * Allocates and initializes a vlc object.
122 * @param i_size object byte size
124 * @return the new object, or NULL on error.
126 VLC_API
void *vlc_object_create( vlc_object_t
*, size_t ) VLC_MALLOC VLC_USED
;
129 * Drops the strong reference to an object.
131 * This removes the initial strong reference to a given object. This must be
132 * called exactly once per allocated object after it is no longer needed,
133 * matching vlc_object_create() or vlc_custom_create().
135 VLC_API
void vlc_object_delete(vlc_object_t
*obj
);
136 #define vlc_object_delete(obj) vlc_object_delete(VLC_OBJECT(obj))
138 VLC_API
size_t vlc_list_children(vlc_object_t
*, vlc_object_t
**, size_t) VLC_USED
;
141 * Returns the object type name.
143 * This returns a nul-terminated string identifying the object type.
144 * The string is valid for at least as long as the object reference.
146 * \param obj object whose type name to get
148 VLC_API
const char *vlc_object_typename(const vlc_object_t
*obj
) VLC_USED
;
151 * Gets the parent of an object.
153 * \return the parent object (NULL if none)
155 * \note The returned parent object pointer is valid as long as the child is.
157 VLC_API vlc_object_t
*vlc_object_parent(vlc_object_t
*obj
) VLC_USED
;
158 #define vlc_object_parent(o) vlc_object_parent(VLC_OBJECT(o))
160 static inline struct vlc_logger
*vlc_object_logger(vlc_object_t
*obj
)
164 #define vlc_object_logger(o) vlc_object_logger(VLC_OBJECT(o))
167 * Tries to get the name of module bound to an object.
169 * \warning This function is intrinsically race-prone, as a module may be
170 * bound or unbound asynchronously by another thread.
171 * Do not trust the result for any purpose other than debugging/tracing.
173 * \return Normally, this returns a heap-allocated nul-terminated string
174 * which is the name of the module. If no module are bound to the object, it
175 * returns NULL. It also returns NULL on error.
177 #define vlc_object_get_name(obj) var_GetString(obj, "module-name")
179 #define vlc_object_create(a,b) vlc_object_create( VLC_OBJECT(a), b )
181 #define vlc_object_find_name(a,b) \
182 vlc_object_find_name( VLC_OBJECT(a),b)
185 static inline libvlc_int_t
*vlc_object_instance(vlc_object_t
*obj
)
187 vlc_object_t
*parent
;
191 while ((obj
= vlc_object_parent(obj
)) != NULL
);
193 return (libvlc_int_t
*)parent
;
195 #define vlc_object_instance(o) vlc_object_instance(VLC_OBJECT(o))
197 /* Here for backward compatibility. TODO: Move to <vlc_vout.h>! */
198 VLC_API vout_thread_t
*vout_Hold(vout_thread_t
*vout
);
199 VLC_API
void vout_Release(vout_thread_t
*vout
);
201 /* Here for backward compatibility. TODO: Move to <vlc_aout.h>! */
202 VLC_API audio_output_t
*aout_Hold(audio_output_t
*aout
);
203 VLC_API
void aout_Release(audio_output_t
*aout
);
205 /* TODO: remove vlc_object_hold/_release() for GUIs, remove this */
206 VLC_DEPRECATED
static inline void *vlc_object_hold(vlc_object_t
*o
)
208 const char *tn
= vlc_object_typename(o
);
210 if (!strcmp(tn
, "audio output"))
211 aout_Hold((audio_output_t
*)o
);
212 if (!strcmp(tn
, "video output"))
213 vout_Hold((vout_thread_t
*)o
);
217 static inline void vlc_object_release(vlc_object_t
*o
)
219 const char *tn
= vlc_object_typename(o
);
221 if (!strcmp(tn
, "audio output"))
222 aout_Release((audio_output_t
*)o
);
223 if (!strcmp(tn
, "video output"))
224 vout_Release((vout_thread_t
*)o
);
228 * @defgroup objres Object resources
230 * The object resource functions tie resource allocation to an instance of
231 * a module through a VLC object.
232 * Such resource will be automatically freed, in first in last out order,
233 * when the module instance associated with the VLC object is terminated.
235 * Specifically, if the module instance activation/probe function fails, the
236 * resource will be freed immediately after the failure. If the activation
237 * succeeds, the resource will be freed when the module instance is terminated.
239 * This is a convenience mechanism to save explicit clean-up function calls
246 * Allocates memory for a module.
248 * This function allocates memory from the heap for a module instance.
249 * The memory is uninitialized.
251 * @param obj VLC object to tie the memory allocation to
252 * @param size byte size of the memory allocation
254 * @return a pointer to the allocated memory, or NULL on error (errno is set).
256 VLC_API VLC_MALLOC
void *vlc_obj_malloc(vlc_object_t
*obj
, size_t size
);
259 * Allocates a zero-initialized table for a module.
261 * This function allocates a table from the heap for a module instance.
262 * The memory is initialized to all zeroes.
264 * @param obj VLC object to tie the memory allocation to
265 * @param nmemb number of table entries
266 * @param size byte size of a table entry
268 * @return a pointer to the allocated memory, or NULL on error (errno is set).
270 VLC_API VLC_MALLOC
void *vlc_obj_calloc(vlc_object_t
*obj
, size_t nmemb
,
274 * Duplicates a string for a module.
276 * This function allocates a copy of a nul-terminated string for a module
279 * @param obj VLC object to tie the memory allocation to
280 * @param str string to copy
282 * @return a pointer to the copy, or NULL on error (errno is set).
284 VLC_API VLC_MALLOC
char *vlc_obj_strdup(vlc_object_t
*obj
, const char *str
);
287 * Manually frees module memory.
289 * This function manually frees a resource allocated with vlc_obj_malloc(),
290 * vlc_obj_calloc() or vlc_obj_strdup() before the module instance is
291 * terminated. This is seldom necessary.
293 * @param obj VLC object that the allocation was tied to
294 * @param ptr pointer to the allocated resource
296 VLC_API
void vlc_obj_free(vlc_object_t
*obj
, void *ptr
);