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_LAST: first flag that can be used by subclasses.
129 * Flags for the mini object
133 GST_MINI_OBJECT_FLAG_LOCKABLE = (1 << 0),
134 GST_MINI_OBJECT_FLAG_LOCK_READONLY = (1 << 1),
136 GST_MINI_OBJECT_FLAG_LAST = (1 << 4)
137 } GstMiniObjectFlags;
140 * GST_MINI_OBJECT_IS_LOCKABLE:
141 * @obj: a #GstMiniObject
143 * Check if @obj is lockable. A lockable object can be locked and unlocked with
144 * gst_mini_object_lock() and gst_mini_object_unlock().
146 #define GST_MINI_OBJECT_IS_LOCKABLE(obj) GST_MINI_OBJECT_FLAG_IS_SET(obj, GST_MINI_OBJECT_FLAG_LOCKABLE)
150 * @GST_LOCK_FLAG_READ: lock for read access
151 * @GST_LOCK_FLAG_WRITE: lock for write access
152 * @GST_LOCK_FLAG_EXCLUSIVE: lock for exclusive access
153 * @GST_LOCK_FLAG_LAST: first flag that can be used for custom purposes
155 * Flags used when locking miniobjects
158 GST_LOCK_FLAG_READ = (1 << 0),
159 GST_LOCK_FLAG_WRITE = (1 << 1),
160 GST_LOCK_FLAG_EXCLUSIVE = (1 << 2),
162 GST_LOCK_FLAG_LAST = (1 << 8)
166 * GST_LOCK_FLAG_READWRITE:
168 * GstLockFlags value alias for GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE
170 #define GST_LOCK_FLAG_READWRITE (GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE)
173 * GST_MINI_OBJECT_REFCOUNT:
174 * @obj: a #GstMiniObject
176 * Get access to the reference count field of the mini-object.
178 #define GST_MINI_OBJECT_REFCOUNT(obj) ((GST_MINI_OBJECT_CAST(obj))->refcount)
180 * GST_MINI_OBJECT_REFCOUNT_VALUE:
181 * @obj: a #GstMiniObject
183 * Get the reference count value of the mini-object.
185 #define GST_MINI_OBJECT_REFCOUNT_VALUE(obj) (g_atomic_int_get (&(GST_MINI_OBJECT_CAST(obj))->refcount))
188 * 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)
189 * @type: the GType of the object
190 * @refcount: atomic refcount
191 * @lockstate: atomic state of the locks
192 * @flags: extra flags.
193 * @copy: a copy function
194 * @dispose: a dispose function
195 * @free: the free function
197 * Base class for refcounted lightweight objects.
199 struct _GstMiniObject {
202 /*< public >*/ /* with COW */
207 GstMiniObjectCopyFunction copy;
208 GstMiniObjectDisposeFunction dispose;
209 GstMiniObjectFreeFunction free;
212 /* Used to keep track of weak ref notifies and qdata */
217 void gst_mini_object_init (GstMiniObject *mini_object,
218 guint flags, GType type,
219 GstMiniObjectCopyFunction copy_func,
220 GstMiniObjectDisposeFunction dispose_func,
221 GstMiniObjectFreeFunction free_func);
225 GstMiniObject * gst_mini_object_ref (GstMiniObject *mini_object);
226 void gst_mini_object_unref (GstMiniObject *mini_object);
228 void gst_mini_object_weak_ref (GstMiniObject *object,
229 GstMiniObjectNotify notify,
231 void gst_mini_object_weak_unref (GstMiniObject *object,
232 GstMiniObjectNotify notify,
236 gboolean gst_mini_object_lock (GstMiniObject *object, GstLockFlags flags);
237 void gst_mini_object_unlock (GstMiniObject *object, GstLockFlags flags);
239 gboolean gst_mini_object_is_writable (const GstMiniObject *mini_object);
240 GstMiniObject * gst_mini_object_make_writable (GstMiniObject *mini_object);
243 GstMiniObject * gst_mini_object_copy (const GstMiniObject *mini_object) G_GNUC_MALLOC;
246 void gst_mini_object_set_qdata (GstMiniObject *object, GQuark quark,
247 gpointer data, GDestroyNotify destroy);
248 gpointer gst_mini_object_get_qdata (GstMiniObject *object, GQuark quark);
249 gpointer gst_mini_object_steal_qdata (GstMiniObject *object, GQuark quark);
252 gboolean gst_mini_object_replace (GstMiniObject **olddata, GstMiniObject *newdata);
253 gboolean gst_mini_object_take (GstMiniObject **olddata, GstMiniObject *newdata);
254 GstMiniObject * gst_mini_object_steal (GstMiniObject **olddata);
257 * GST_DEFINE_MINI_OBJECT_TYPE:
258 * @TypeName: name of the new type in CamelCase
259 * @type_name: name of the new type
261 * Define a new mini-object type with the given name
263 #define GST_DEFINE_MINI_OBJECT_TYPE(TypeName,type_name) \
264 G_DEFINE_BOXED_TYPE(TypeName,type_name, \
265 (GBoxedCopyFunc) gst_mini_object_ref, \
266 (GBoxedFreeFunc) gst_mini_object_unref)