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 #define GST_IS_MINI_OBJECT_TYPE(obj,type) ((obj) && GST_MINI_OBJECT_TYPE(obj) == (type))
33 #define GST_MINI_OBJECT_CAST(obj) ((GstMiniObject*)(obj))
34 #define GST_MINI_OBJECT_CONST_CAST(obj) ((const GstMiniObject*)(obj))
35 #define GST_MINI_OBJECT(obj) (GST_MINI_OBJECT_CAST(obj))
37 typedef struct _GstMiniObject GstMiniObject;
40 * GstMiniObjectCopyFunction:
41 * @obj: MiniObject to copy
43 * Function prototype for methods to create copies of instances.
45 * Returns: reference to cloned instance.
47 typedef GstMiniObject * (*GstMiniObjectCopyFunction) (const GstMiniObject *obj);
49 * GstMiniObjectDisposeFunction:
50 * @obj: MiniObject to dispose
52 * Function prototype for when a miniobject has lost its last refcount.
53 * Implementation of the mini object are allowed to revive the
54 * passed object by doing a gst_mini_object_ref(). If the object is not
55 * revived after the dispose function, the function should return %TRUE
56 * and the memory associated with the object is freed.
58 * Returns: %TRUE if the object should be cleaned up.
60 typedef gboolean (*GstMiniObjectDisposeFunction) (GstMiniObject *obj);
62 * GstMiniObjectFreeFunction:
63 * @obj: MiniObject to free
65 * Virtual function prototype for methods to free resources used by
68 typedef void (*GstMiniObjectFreeFunction) (GstMiniObject *obj);
71 * GstMiniObjectNotify:
72 * @user_data: data that was provided when the notify was added
73 * @obj: the mini object
75 * A #GstMiniObjectNotify function can be added to a mini object as a
76 * callback that gets triggered when gst_mini_object_unref() drops the
77 * last ref and @obj is about to be freed.
79 typedef void (*GstMiniObjectNotify) (gpointer user_data, GstMiniObject * obj);
82 * GST_MINI_OBJECT_TYPE:
83 * @obj: MiniObject to return type for.
85 * This macro returns the type of the mini-object.
87 #define GST_MINI_OBJECT_TYPE(obj) (GST_MINI_OBJECT_CAST(obj)->type)
90 * GST_MINI_OBJECT_FLAGS:
91 * @obj: MiniObject to return flags for.
93 * This macro returns the entire set of flags for the mini-object.
95 #define GST_MINI_OBJECT_FLAGS(obj) (GST_MINI_OBJECT_CAST(obj)->flags)
97 * GST_MINI_OBJECT_FLAG_IS_SET:
98 * @obj: MiniObject to check for flags.
99 * @flag: Flag to check for
101 * This macro checks to see if the given flag is set.
103 #define GST_MINI_OBJECT_FLAG_IS_SET(obj,flag) !!(GST_MINI_OBJECT_FLAGS (obj) & (flag))
105 * GST_MINI_OBJECT_FLAG_SET:
106 * @obj: MiniObject to set flag in.
107 * @flag: Flag to set, can by any number of bits in guint32.
109 * This macro sets the given bits.
111 #define GST_MINI_OBJECT_FLAG_SET(obj,flag) (GST_MINI_OBJECT_FLAGS (obj) |= (flag))
113 * GST_MINI_OBJECT_FLAG_UNSET:
114 * @obj: MiniObject to unset flag in.
115 * @flag: Flag to set, must be a single bit in guint32.
117 * This macro unsets the given bits.
119 #define GST_MINI_OBJECT_FLAG_UNSET(obj,flag) (GST_MINI_OBJECT_FLAGS (obj) &= ~(flag))
122 * GstMiniObjectFlags:
123 * @GST_MINI_OBJECT_FLAG_LOCKABLE: the object can be locked and unlocked with
124 * gst_mini_object_lock() and gst_mini_object_unlock().
125 * @GST_MINI_OBJECT_FLAG_LOCK_READONLY: the object is permanently locked in
126 * READONLY mode. Only read locks can be performed on the object.
127 * @GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED: the object is expected to stay alive
128 * even after gst_deinit() has been called and so should be ignored by leak
129 * detection tools. (Since 1.10)
130 * @GST_MINI_OBJECT_FLAG_LAST: first flag that can be used by subclasses.
132 * Flags for the mini object
136 GST_MINI_OBJECT_FLAG_LOCKABLE = (1 << 0),
137 GST_MINI_OBJECT_FLAG_LOCK_READONLY = (1 << 1),
138 GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED = (1 << 2),
140 GST_MINI_OBJECT_FLAG_LAST = (1 << 4)
141 } GstMiniObjectFlags;
144 * GST_MINI_OBJECT_IS_LOCKABLE:
145 * @obj: a #GstMiniObject
147 * Check if @obj is lockable. A lockable object can be locked and unlocked with
148 * gst_mini_object_lock() and gst_mini_object_unlock().
150 #define GST_MINI_OBJECT_IS_LOCKABLE(obj) GST_MINI_OBJECT_FLAG_IS_SET(obj, GST_MINI_OBJECT_FLAG_LOCKABLE)
154 * @GST_LOCK_FLAG_READ: lock for read access
155 * @GST_LOCK_FLAG_WRITE: lock for write access
156 * @GST_LOCK_FLAG_EXCLUSIVE: lock for exclusive access
157 * @GST_LOCK_FLAG_LAST: first flag that can be used for custom purposes
159 * Flags used when locking miniobjects
162 GST_LOCK_FLAG_READ = (1 << 0),
163 GST_LOCK_FLAG_WRITE = (1 << 1),
164 GST_LOCK_FLAG_EXCLUSIVE = (1 << 2),
166 GST_LOCK_FLAG_LAST = (1 << 8)
170 * GST_LOCK_FLAG_READWRITE:
172 * GstLockFlags value alias for GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE
174 #define GST_LOCK_FLAG_READWRITE (GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE)
177 * GST_MINI_OBJECT_REFCOUNT:
178 * @obj: a #GstMiniObject
180 * Get access to the reference count field of the mini-object.
182 #define GST_MINI_OBJECT_REFCOUNT(obj) ((GST_MINI_OBJECT_CAST(obj))->refcount)
184 * GST_MINI_OBJECT_REFCOUNT_VALUE:
185 * @obj: a #GstMiniObject
187 * Get the reference count value of the mini-object.
189 #define GST_MINI_OBJECT_REFCOUNT_VALUE(obj) (g_atomic_int_get (&(GST_MINI_OBJECT_CAST(obj))->refcount))
192 * 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)
193 * @type: the GType of the object
194 * @refcount: atomic refcount
195 * @lockstate: atomic state of the locks
196 * @flags: extra flags.
197 * @copy: a copy function
198 * @dispose: a dispose function
199 * @free: the free function
201 * Base class for refcounted lightweight objects.
203 struct _GstMiniObject {
206 /*< public >*/ /* with COW */
211 GstMiniObjectCopyFunction copy;
212 GstMiniObjectDisposeFunction dispose;
213 GstMiniObjectFreeFunction free;
216 /* Used to keep track of weak ref notifies and qdata */
221 void gst_mini_object_init (GstMiniObject *mini_object,
222 guint flags, GType type,
223 GstMiniObjectCopyFunction copy_func,
224 GstMiniObjectDisposeFunction dispose_func,
225 GstMiniObjectFreeFunction free_func);
229 GstMiniObject * gst_mini_object_ref (GstMiniObject *mini_object);
230 void gst_mini_object_unref (GstMiniObject *mini_object);
232 void gst_mini_object_weak_ref (GstMiniObject *object,
233 GstMiniObjectNotify notify,
235 void gst_mini_object_weak_unref (GstMiniObject *object,
236 GstMiniObjectNotify notify,
240 gboolean gst_mini_object_lock (GstMiniObject *object, GstLockFlags flags);
241 void gst_mini_object_unlock (GstMiniObject *object, GstLockFlags flags);
243 gboolean gst_mini_object_is_writable (const GstMiniObject *mini_object);
244 GstMiniObject * gst_mini_object_make_writable (GstMiniObject *mini_object) G_GNUC_WARN_UNUSED_RESULT;
247 GstMiniObject * gst_mini_object_copy (const GstMiniObject *mini_object) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
250 void gst_mini_object_set_qdata (GstMiniObject *object, GQuark quark,
251 gpointer data, GDestroyNotify destroy);
252 gpointer gst_mini_object_get_qdata (GstMiniObject *object, GQuark quark);
253 gpointer gst_mini_object_steal_qdata (GstMiniObject *object, GQuark quark);
256 gboolean gst_mini_object_replace (GstMiniObject **olddata, GstMiniObject *newdata);
257 gboolean gst_mini_object_take (GstMiniObject **olddata, GstMiniObject *newdata);
258 GstMiniObject * gst_mini_object_steal (GstMiniObject **olddata) G_GNUC_WARN_UNUSED_RESULT;
261 * GST_DEFINE_MINI_OBJECT_TYPE:
262 * @TypeName: name of the new type in CamelCase
263 * @type_name: name of the new type
265 * Define a new mini-object type with the given name
267 #define GST_DEFINE_MINI_OBJECT_TYPE(TypeName,type_name) \
268 G_DEFINE_BOXED_TYPE(TypeName,type_name, \
269 (GBoxedCopyFunc) gst_mini_object_ref, \
270 (GBoxedFreeFunc) gst_mini_object_unref)