VLC 4.0.0-dev
vlc_objects.h
Go to the documentation of this file.
1/*****************************************************************************
2 * vlc_objects.h: vlc_object_t definition and manipulation methods
3 *****************************************************************************
4 * Copyright (C) 2002-2008 VLC authors and VideoLAN
5 *
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 *
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.
12 *
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.
17 *
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 *****************************************************************************/
22
23/**
24 * \defgroup vlc_object VLC objects
25 * \ingroup vlc
26 * @{
27 * \file
28 * Common VLC object definitions
29 */
30
31struct vlc_logger;
32struct vlc_tracer;
34struct vlc_object_marker;
35
36/**
37 * VLC object common members
38 *
39 * Common public properties for all VLC objects.
40 * Object also have private properties maintained by the core, see
41 * \ref vlc_object_internals_t
42 */
43struct vlc_object_t
45 struct vlc_logger *logger;
46 union {
48 struct vlc_object_marker *obj;
49 };
50
51 bool no_interact;
53 /** Module probe flag
54 *
55 * A boolean during module probing when the probe is "forced".
56 * See \ref module_need().
57 */
58 bool force;
59};
60
61/**
62 * Type-safe vlc_object_t cast
63 *
64 * This macro attempts to cast a pointer to a compound type to a
65 * \ref vlc_object_t pointer in a type-safe manner.
66 * It checks if the compound type actually starts with an embedded
67 * \ref vlc_object_t structure.
68 */
69#if !defined(__cplusplus)
70# define VLC_OBJECT(x) \
71 _Generic((x)->obj, \
72 struct vlc_object_marker *: (x), \
73 default: (&((x)->obj)) \
74 )
75#else
76static inline vlc_object_t *VLC_OBJECT(vlc_object_t *o)
77 { return o; }
78
79template<typename T>
80static inline vlc_object_t *VLC_OBJECT(T *d)
81 { return &d->obj; }
82#endif
83
84#ifdef __cplusplus
85extern "C" {
86#endif
87
88/* The root object */
89struct libvlc_int_t
91 struct vlc_object_t obj;
92};
93
94/**
95 * Allocates and initializes a vlc object.
96 *
97 * @param i_size object byte size
98 *
99 * @return the new object, or NULL on error.
100 */
103/**
104 * Drops the strong reference to an object.
105 *
106 * This removes the initial strong reference to a given object. This must be
107 * called exactly once per allocated object after it is no longer needed,
108 * matching vlc_object_create() or vlc_custom_create().
109 */
111#define vlc_object_delete(obj) vlc_object_delete(VLC_OBJECT(obj))
115/**
116 * Returns the object type name.
117 *
118 * This returns a nul-terminated string identifying the object type.
119 * The string is valid for at least as long as the object reference.
120 *
121 * \param obj object whose type name to get
122 */
124
125/**
126 * Gets the parent of an object.
127 *
128 * \return the parent object (NULL if none)
129 *
130 * \note The returned parent object pointer is valid as long as the child is.
131 */
133#define vlc_object_parent(o) vlc_object_parent(VLC_OBJECT(o))
135static inline struct vlc_logger *vlc_object_logger(vlc_object_t *obj)
137 return obj->logger;
138}
139#define vlc_object_logger(o) vlc_object_logger(VLC_OBJECT(o))
141 /**
142 * Get tracer of a vlc instance from an object.
143 *
144 * \return the tracer of a vlc instance from an object (NULL if none).
145 */
147
148/**
149 * Tries to get the name of module bound to an object.
150 *
151 * \warning This function is intrinsically race-prone, as a module may be
152 * bound or unbound asynchronously by another thread.
153 * Do not trust the result for any purpose other than debugging/tracing.
154 *
155 * \return Normally, this returns a heap-allocated nul-terminated string
156 * which is the name of the module. If no module are bound to the object, it
157 * returns NULL. It also returns NULL on error.
158 */
159#define vlc_object_get_name(obj) var_GetString(obj, "module-name")
161#define vlc_object_create(a,b) vlc_object_create( VLC_OBJECT(a), b )
163#define vlc_object_find_name(a,b) \
164 vlc_object_find_name( VLC_OBJECT(a),b)
165
169 vlc_object_t *parent;
170
171 do
172 parent = obj;
173 while ((obj = vlc_object_parent(obj)) != NULL);
174
175 return (libvlc_int_t *)parent;
176}
177#define vlc_object_instance(o) vlc_object_instance(VLC_OBJECT(o))
179/* Here for backward compatibility. TODO: Move to <vlc_vout.h>! */
182
183/* Here for backward compatibility. TODO: Move to <vlc_aout.h>! */
186
187/* TODO: remove vlc_object_hold/_release() for GUIs, remove this */
188VLC_DEPRECATED static inline void *vlc_object_hold(vlc_object_t *o)
190 const char *tn = vlc_object_typename(o);
191
192 if (!strcmp(tn, "audio output"))
194 if (!strcmp(tn, "video output"))
196 return o;
197}
198
199static inline void vlc_object_release(vlc_object_t *o)
201 const char *tn = vlc_object_typename(o);
202
203 if (!strcmp(tn, "audio output"))
205 if (!strcmp(tn, "video output"))
207}
208#define vlc_object_release(o) vlc_object_release(VLC_OBJECT(o))
210/**
211 * @defgroup objres Object resources
212 *
213 * The object resource functions tie resource allocation to an instance of
214 * a module through a VLC object.
215 * Such resource will be automatically freed, in first in last out order,
216 * when the module instance associated with the VLC object is terminated.
217 *
218 * Specifically, if the module instance activation/probe function fails, the
219 * resource will be freed immediately after the failure. If the activation
220 * succeeds, the resource will be freed when the module instance is terminated.
221 *
222 * This is a convenience mechanism to save explicit clean-up function calls
223 * in modules.
224 *
225 * @{
226 */
227
228/**
229 * Allocates memory for a module.
230 *
231 * This function allocates memory from the heap for a module instance.
232 * The memory is uninitialized.
233 *
234 * @param obj VLC object to tie the memory allocation to
235 * @param size byte size of the memory allocation
236 *
237 * @return a pointer to the allocated memory, or NULL on error (errno is set).
238 */
239VLC_API VLC_MALLOC void *vlc_obj_malloc(vlc_object_t *obj, size_t size);
240#define vlc_obj_malloc(o, s) vlc_obj_malloc(VLC_OBJECT(o), s)
242/**
243 * Allocates a zero-initialized table for a module.
244 *
245 * This function allocates a table from the heap for a module instance.
246 * The memory is initialized to all zeroes.
247 *
248 * @param obj VLC object to tie the memory allocation to
249 * @param nmemb number of table entries
250 * @param size byte size of a table entry
251 *
252 * @return a pointer to the allocated memory, or NULL on error (errno is set).
253 */
254VLC_API VLC_MALLOC void *vlc_obj_calloc(vlc_object_t *obj, size_t nmemb,
255 size_t size);
256#define vlc_obj_calloc(o, n, s) vlc_obj_calloc(VLC_OBJECT(o), n, s)
258/**
259 * Duplicates a string for a module.
260 *
261 * This function allocates a copy of a nul-terminated string for a module
262 * instance.
263 *
264 * @param obj VLC object to tie the memory allocation to
265 * @param str string to copy
266 *
267 * @return a pointer to the copy, or NULL on error (errno is set).
268 */
269VLC_API VLC_MALLOC char *vlc_obj_strdup(vlc_object_t *obj, const char *str);
270#define vlc_obj_strdup(o, s) vlc_obj_strdup(VLC_OBJECT(o), s)
272/**
273 * Manually frees module memory.
274 *
275 * This function manually frees a resource allocated with vlc_obj_malloc(),
276 * vlc_obj_calloc() or vlc_obj_strdup() before the module instance is
277 * terminated. This is seldom necessary.
278 *
279 * @param obj VLC object that the allocation was tied to
280 * @param ptr pointer to the allocated resource
281 */
282VLC_API void vlc_obj_free(vlc_object_t *obj, void *ptr);
283#define vlc_obj_free(o, p) vlc_obj_free(VLC_OBJECT(o), p)
285#ifdef __cplusplus
286} /* extern "C" */
287
288# include <type_traits>
289
290#undef vlc_object_create
291
292template <typename O> VLC_MALLOC VLC_USED
293static inline void* vlc_object_create(O *obj, size_t size)
294{
295 return vlc_object_create(VLC_OBJECT(obj), size);
296}
297
298template<typename T, typename O> VLC_MALLOC VLC_USED
299static inline T* vlc_object_create(O *obj)
300{
301 static_assert(std::is_pointer<T>::value == false, "vlc_object_create can only create objects");
302 return static_cast<T*>(vlc_object_create(VLC_OBJECT(obj), sizeof(T)));
303}
304#endif
305
306/** @} */
307/** @} */
#define VLC_USED
Definition: fourcc_gen.c:32
#define VLC_API
Definition: fourcc_gen.c:31
#define VLC_MALLOC
Definition: vlc_common.h:164
#define VLC_DEPRECATED
Definition: vlc_common.h:165
#define vlc_obj_calloc(o, n, s)
Definition: vlc_objects.h:257
#define vlc_obj_malloc(o, s)
Definition: vlc_objects.h:241
#define vlc_obj_free(o, p)
Definition: vlc_objects.h:284
#define vlc_obj_strdup(o, s)
Definition: vlc_objects.h:271
#define VLC_OBJECT(x)
Type-safe vlc_object_t cast.
Definition: vlc_objects.h:71
#define vlc_object_create(a, b)
Definition: vlc_objects.h:162
vout_thread_t * vout_Hold(vout_thread_t *vout)
Definition: video_output.c:2080
struct vlc_tracer * vlc_object_get_tracer(vlc_object_t *obj)
Get tracer of a vlc instance from an object.
Definition: objects.c:120
#define vlc_object_release(o)
Definition: vlc_objects.h:209
#define vlc_object_delete(obj)
Definition: vlc_objects.h:112
#define vlc_object_logger(o)
Definition: vlc_objects.h:140
void aout_Release(audio_output_t *aout)
Definition: output.c:434
const char * vlc_object_typename(const vlc_object_t *obj)
Returns the object type name.
Definition: objects.c:110
audio_output_t * aout_Hold(audio_output_t *aout)
Definition: output.c:390
static void * vlc_object_hold(vlc_object_t *o)
Definition: vlc_objects.h:189
size_t vlc_list_children(vlc_object_t *, vlc_object_t **, size_t)
#define vlc_object_parent(o)
Definition: vlc_objects.h:134
#define vlc_object_instance(o)
Definition: vlc_objects.h:178
void vout_Release(vout_thread_t *vout)
Definition: video_output.c:1928
Audio output object.
Definition: vlc_aout.h:155
Definition: vlc_objects.h:91
struct vlc_object_t obj
Definition: vlc_objects.h:92
Definition: messages.c:85
Definition: variables.h:36
VLC object common members.
Definition: vlc_objects.h:45
struct vlc_logger * logger
Definition: vlc_objects.h:46
struct vlc_object_marker * obj
Definition: vlc_objects.h:49
bool no_interact
Definition: vlc_objects.h:52
bool force
Module probe flag.
Definition: vlc_objects.h:59
struct vlc_object_internals * priv
Definition: vlc_objects.h:48
Definition: tracer.c:36
Video output thread descriptor.
Definition: vlc_vout.h:55
This file is a collection of common definitions and types.