2 * Copyright (C) 2005 David Schleef <ds@schleef.org>
4 * gstminiobject.h: Header for GstMiniObject
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #ifndef __GST_MINI_OBJECT_H__
24 #define __GST_MINI_OBJECT_H__
26 #include <gst/gstconfig.h>
28 #include <glib-object.h>
32 GST_API GType _gst_mini_object_type;
34 #define GST_TYPE_MINI_OBJECT (_gst_mini_object_type)
36 #define GST_IS_MINI_OBJECT_TYPE(obj,type) ((obj) && GST_MINI_OBJECT_TYPE(obj) == (type))
37 #define GST_MINI_OBJECT_CAST(obj) ((GstMiniObject*)(obj))
38 #define GST_MINI_OBJECT_CONST_CAST(obj) ((const GstMiniObject*)(obj))
39 #define GST_MINI_OBJECT(obj) (GST_MINI_OBJECT_CAST(obj))
41 typedef struct _GstMiniObject GstMiniObject;
44 GType gst_mini_object_get_type (void);
47 * GstMiniObjectCopyFunction:
48 * @obj: MiniObject to copy
50 * Function prototype for methods to create copies of instances.
52 * Returns: reference to cloned instance.
54 typedef GstMiniObject * (*GstMiniObjectCopyFunction) (const GstMiniObject *obj);
56 * GstMiniObjectDisposeFunction:
57 * @obj: MiniObject to dispose
59 * Function prototype for when a miniobject has lost its last refcount.
60 * Implementation of the mini object are allowed to revive the
61 * passed object by doing a gst_mini_object_ref(). If the object is not
62 * revived after the dispose function, the function should return %TRUE
63 * and the memory associated with the object is freed.
65 * Returns: %TRUE if the object should be cleaned up.
67 typedef gboolean (*GstMiniObjectDisposeFunction) (GstMiniObject *obj);
69 * GstMiniObjectFreeFunction:
70 * @obj: MiniObject to free
72 * Virtual function prototype for methods to free resources used by
75 typedef void (*GstMiniObjectFreeFunction) (GstMiniObject *obj);
78 * GstMiniObjectNotify:
79 * @user_data: data that was provided when the notify was added
80 * @obj: the mini object
82 * A #GstMiniObjectNotify function can be added to a mini object as a
83 * callback that gets triggered when gst_mini_object_unref() drops the
84 * last ref and @obj is about to be freed.
86 typedef void (*GstMiniObjectNotify) (gpointer user_data, GstMiniObject * obj);
89 * GST_MINI_OBJECT_TYPE:
90 * @obj: MiniObject to return type for.
92 * This macro returns the type of the mini-object.
94 #define GST_MINI_OBJECT_TYPE(obj) (GST_MINI_OBJECT_CAST(obj)->type)
97 * GST_MINI_OBJECT_FLAGS:
98 * @obj: MiniObject to return flags for.
100 * This macro returns the entire set of flags for the mini-object.
102 #define GST_MINI_OBJECT_FLAGS(obj) (GST_MINI_OBJECT_CAST(obj)->flags)
104 * GST_MINI_OBJECT_FLAG_IS_SET:
105 * @obj: MiniObject to check for flags.
106 * @flag: Flag to check for
108 * This macro checks to see if the given flag is set.
110 #define GST_MINI_OBJECT_FLAG_IS_SET(obj,flag) !!(GST_MINI_OBJECT_FLAGS (obj) & (flag))
112 * GST_MINI_OBJECT_FLAG_SET:
113 * @obj: MiniObject to set flag in.
114 * @flag: Flag to set, can by any number of bits in guint32.
116 * This macro sets the given bits.
118 #define GST_MINI_OBJECT_FLAG_SET(obj,flag) (GST_MINI_OBJECT_FLAGS (obj) |= (flag))
120 * GST_MINI_OBJECT_FLAG_UNSET:
121 * @obj: MiniObject to unset flag in.
122 * @flag: Flag to set, must be a single bit in guint32.
124 * This macro unsets the given bits.
126 #define GST_MINI_OBJECT_FLAG_UNSET(obj,flag) (GST_MINI_OBJECT_FLAGS (obj) &= ~(flag))
129 * GstMiniObjectFlags:
130 * @GST_MINI_OBJECT_FLAG_LOCKABLE: the object can be locked and unlocked with
131 * gst_mini_object_lock() and gst_mini_object_unlock().
132 * @GST_MINI_OBJECT_FLAG_LOCK_READONLY: the object is permanently locked in
133 * READONLY mode. Only read locks can be performed on the object.
134 * @GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED: the object is expected to stay alive
135 * even after gst_deinit() has been called and so should be ignored by leak
136 * detection tools. (Since: 1.10)
137 * @GST_MINI_OBJECT_FLAG_LAST: first flag that can be used by subclasses.
139 * Flags for the mini object
143 GST_MINI_OBJECT_FLAG_LOCKABLE = (1 << 0),
144 GST_MINI_OBJECT_FLAG_LOCK_READONLY = (1 << 1),
145 GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED = (1 << 2),
147 GST_MINI_OBJECT_FLAG_LAST = (1 << 4)
148 } GstMiniObjectFlags;
151 * GST_MINI_OBJECT_IS_LOCKABLE:
152 * @obj: a #GstMiniObject
154 * Check if @obj is lockable. A lockable object can be locked and unlocked with
155 * gst_mini_object_lock() and gst_mini_object_unlock().
157 #define GST_MINI_OBJECT_IS_LOCKABLE(obj) GST_MINI_OBJECT_FLAG_IS_SET(obj, GST_MINI_OBJECT_FLAG_LOCKABLE)
161 * @GST_LOCK_FLAG_READ: lock for read access
162 * @GST_LOCK_FLAG_WRITE: lock for write access
163 * @GST_LOCK_FLAG_EXCLUSIVE: lock for exclusive access
164 * @GST_LOCK_FLAG_LAST: first flag that can be used for custom purposes
166 * Flags used when locking miniobjects
169 GST_LOCK_FLAG_READ = (1 << 0),
170 GST_LOCK_FLAG_WRITE = (1 << 1),
171 GST_LOCK_FLAG_EXCLUSIVE = (1 << 2),
173 GST_LOCK_FLAG_LAST = (1 << 8)
177 * GST_LOCK_FLAG_READWRITE: (value 3) (type GstLockFlags)
179 * GstLockFlags value alias for GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE
181 #define GST_LOCK_FLAG_READWRITE ((GstLockFlags) (GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE))
184 * GST_MINI_OBJECT_REFCOUNT:
185 * @obj: a #GstMiniObject
187 * Get access to the reference count field of the mini-object.
189 #define GST_MINI_OBJECT_REFCOUNT(obj) ((GST_MINI_OBJECT_CAST(obj))->refcount)
191 * GST_MINI_OBJECT_REFCOUNT_VALUE:
192 * @obj: a #GstMiniObject
194 * Get the reference count value of the mini-object.
196 #define GST_MINI_OBJECT_REFCOUNT_VALUE(obj) (g_atomic_int_get (&(GST_MINI_OBJECT_CAST(obj))->refcount))
199 * GstMiniObject: (ref-func gst_mini_object_ref) (unref-func gst_mini_object_unref) (set-value-func g_value_set_boxed) (get-value-func g_value_get_boxed)
200 * @type: the GType of the object
201 * @refcount: atomic refcount
202 * @lockstate: atomic state of the locks
203 * @flags: extra flags.
204 * @copy: a copy function
205 * @dispose: a dispose function
206 * @free: the free function
208 * Base class for refcounted lightweight objects.
210 struct _GstMiniObject {
213 /*< public >*/ /* with COW */
218 GstMiniObjectCopyFunction copy;
219 GstMiniObjectDisposeFunction dispose;
220 GstMiniObjectFreeFunction free;
223 /* Used to keep track of parents, weak ref notifies and qdata */
225 gpointer priv_pointer;
229 void gst_mini_object_init (GstMiniObject *mini_object,
230 guint flags, GType type,
231 GstMiniObjectCopyFunction copy_func,
232 GstMiniObjectDisposeFunction dispose_func,
233 GstMiniObjectFreeFunction free_func);
239 GstMiniObject * gst_mini_object_ref (GstMiniObject *mini_object);
242 void gst_mini_object_unref (GstMiniObject *mini_object);
245 void gst_clear_mini_object (GstMiniObject **object_ptr);
246 #define gst_clear_mini_object(object_ptr) g_clear_pointer ((object_ptr), gst_mini_object_unref)
249 void gst_mini_object_weak_ref (GstMiniObject *object,
250 GstMiniObjectNotify notify,
253 void gst_mini_object_weak_unref (GstMiniObject *object,
254 GstMiniObjectNotify notify,
260 gboolean gst_mini_object_lock (GstMiniObject *object, GstLockFlags flags);
263 void gst_mini_object_unlock (GstMiniObject *object, GstLockFlags flags);
266 gboolean gst_mini_object_is_writable (const GstMiniObject *mini_object);
269 GstMiniObject * gst_mini_object_make_writable (GstMiniObject *mini_object) G_GNUC_WARN_UNUSED_RESULT;
274 GstMiniObject * gst_mini_object_copy (const GstMiniObject *mini_object) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
278 void gst_mini_object_set_qdata (GstMiniObject *object, GQuark quark,
279 gpointer data, GDestroyNotify destroy);
281 gpointer gst_mini_object_get_qdata (GstMiniObject *object, GQuark quark);
284 gpointer gst_mini_object_steal_qdata (GstMiniObject *object, GQuark quark);
287 void gst_mini_object_add_parent (GstMiniObject *object, GstMiniObject *parent);
289 void gst_mini_object_remove_parent (GstMiniObject *object, GstMiniObject *parent);
292 gboolean gst_mini_object_replace (GstMiniObject **olddata, GstMiniObject *newdata);
295 gboolean gst_mini_object_take (GstMiniObject **olddata, GstMiniObject *newdata);
298 GstMiniObject * gst_mini_object_steal (GstMiniObject **olddata) G_GNUC_WARN_UNUSED_RESULT;
301 * GST_DEFINE_MINI_OBJECT_TYPE:
302 * @TypeName: name of the new type in CamelCase
303 * @type_name: name of the new type
305 * Define a new mini-object type with the given name
307 #define GST_DEFINE_MINI_OBJECT_TYPE(TypeName,type_name) \
308 G_DEFINE_BOXED_TYPE(TypeName,type_name, \
309 (GBoxedCopyFunc) gst_mini_object_ref, \
310 (GBoxedFreeFunc) gst_mini_object_unref)