gstminiobject.h 9.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* GStreamer
 * Copyright (C) 2005 David Schleef <ds@schleef.org>
 *
 * gstminiobject.h: Header for GstMiniObject
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
18 19
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
20 21 22 23 24 25
 */


#ifndef __GST_MINI_OBJECT_H__
#define __GST_MINI_OBJECT_H__

Wim Taymans's avatar
Wim Taymans committed
26 27
#include <gst/gstconfig.h>

28 29 30 31
#include <glib-object.h>

G_BEGIN_DECLS

Wim Taymans's avatar
Wim Taymans committed
32 33 34 35
#define GST_IS_MINI_OBJECT_TYPE(obj,type)  ((obj) && GST_MINI_OBJECT_TYPE(obj) == (type))
#define GST_MINI_OBJECT_CAST(obj)          ((GstMiniObject*)(obj))
#define GST_MINI_OBJECT_CONST_CAST(obj)    ((const GstMiniObject*)(obj))
#define GST_MINI_OBJECT(obj)               (GST_MINI_OBJECT_CAST(obj))
36 37 38

typedef struct _GstMiniObject GstMiniObject;

39 40 41 42
/**
 * GstMiniObjectCopyFunction:
 * @obj: MiniObject to copy
 *
43
 * Function prototype for methods to create copies of instances.
44 45 46 47
 *
 * Returns: reference to cloned instance.
 */
typedef GstMiniObject * (*GstMiniObjectCopyFunction) (const GstMiniObject *obj);
48 49 50 51 52 53 54
/**
 * GstMiniObjectDisposeFunction:
 * @obj: MiniObject to dispose
 *
 * Function prototype for when a miniobject has lost its last refcount.
 * Implementation of the mini object are allowed to revive the
 * passed object by doing a gst_mini_object_ref(). If the object is not
55 56 57 58
 * revived after the dispose function, the function should return %TRUE
 * and the memory associated with the object is freed.
 *
 * Returns: %TRUE if the object should be cleaned up.
59
 */
60
typedef gboolean (*GstMiniObjectDisposeFunction) (GstMiniObject *obj);
61
/**
62 63
 * GstMiniObjectFreeFunction:
 * @obj: MiniObject to free
64
 *
65
 * Virtual function prototype for methods to free resources used by
66
 * mini-objects.
67
 */
68
typedef void (*GstMiniObjectFreeFunction) (GstMiniObject *obj);
69

70
 /**
Wim Taymans's avatar
Wim Taymans committed
71 72 73
 * GstMiniObjectNotify:
 * @user_data: data that was provided when the notify was added
 * @obj: the mini object
Wim Taymans's avatar
Wim Taymans committed
74
 *
Wim Taymans's avatar
Wim Taymans committed
75
 * A #GstMiniObjectNotify function can be added to a mini object as a
76 77
 * callback that gets triggered when gst_mini_object_unref() drops the
 * last ref and @obj is about to be freed.
78
 */
Wim Taymans's avatar
Wim Taymans committed
79
typedef void (*GstMiniObjectNotify) (gpointer user_data, GstMiniObject * obj);
80

81
/**
82 83
 * GST_MINI_OBJECT_TYPE:
 * @obj: MiniObject to return type for.
84
 *
85
 * This macro returns the type of the mini-object.
86 87
 */
#define GST_MINI_OBJECT_TYPE(obj)  (GST_MINI_OBJECT_CAST(obj)->type)
88

Stefan Kost's avatar
Stefan Kost committed
89 90 91 92 93 94
/**
 * GST_MINI_OBJECT_FLAGS:
 * @obj: MiniObject to return flags for.
 *
 * 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)
Stefan Kost's avatar
Stefan Kost committed
96 97 98 99 100 101 102
/**
 * GST_MINI_OBJECT_FLAG_IS_SET:
 * @obj: MiniObject to check for flags.
 * @flag: Flag to check for
 *
 * 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))
Stefan Kost's avatar
Stefan Kost committed
104 105 106 107 108 109 110
/**
 * GST_MINI_OBJECT_FLAG_SET:
 * @obj: MiniObject to set flag in.
 * @flag: Flag to set, can by any number of bits in guint32.
 *
 * This macro sets the given bits.
 */
111
#define GST_MINI_OBJECT_FLAG_SET(obj,flag)           (GST_MINI_OBJECT_FLAGS (obj) |= (flag))
Stefan Kost's avatar
Stefan Kost committed
112 113 114 115 116
/**
 * GST_MINI_OBJECT_FLAG_UNSET:
 * @obj: MiniObject to unset flag in.
 * @flag: Flag to set, must be a single bit in guint32.
 *
117
 * This macro unsets the given bits.
Stefan Kost's avatar
Stefan Kost committed
118
 */
119 120
#define GST_MINI_OBJECT_FLAG_UNSET(obj,flag)         (GST_MINI_OBJECT_FLAGS (obj) &= ~(flag))

121 122
/**
 * GstMiniObjectFlags:
123 124 125 126
 * @GST_MINI_OBJECT_FLAG_LOCKABLE: the object can be locked and unlocked with
 * gst_mini_object_lock() and gst_mini_object_unlock().
 * @GST_MINI_OBJECT_FLAG_LOCK_READONLY: the object is permanently locked in
 * READONLY mode. Only read locks can be performed on the object.
127 128 129
 * @GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED: the object is expected to stay alive
 * even after gst_deinit() has been called and so should be ignored by leak
 * detection tools. (Since 1.10)
130 131
 * @GST_MINI_OBJECT_FLAG_LAST: first flag that can be used by subclasses.
 *
132
 * Flags for the mini object
133
 */
134 135
typedef enum
{
136 137
  GST_MINI_OBJECT_FLAG_LOCKABLE      = (1 << 0),
  GST_MINI_OBJECT_FLAG_LOCK_READONLY = (1 << 1),
138
  GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED = (1 << 2),
139
  /* padding */
140
  GST_MINI_OBJECT_FLAG_LAST          = (1 << 4)
141 142
} GstMiniObjectFlags;

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/**
 * GST_MINI_OBJECT_IS_LOCKABLE:
 * @obj: a #GstMiniObject
 *
 * Check if @obj is lockable. A lockable object can be locked and unlocked with
 * gst_mini_object_lock() and gst_mini_object_unlock().
 */
#define GST_MINI_OBJECT_IS_LOCKABLE(obj)  GST_MINI_OBJECT_FLAG_IS_SET(obj, GST_MINI_OBJECT_FLAG_LOCKABLE)

/**
 * GstLockFlags:
 * @GST_LOCK_FLAG_READ: lock for read access
 * @GST_LOCK_FLAG_WRITE: lock for write access
 * @GST_LOCK_FLAG_EXCLUSIVE: lock for exclusive access
 * @GST_LOCK_FLAG_LAST: first flag that can be used for custom purposes
 *
 * Flags used when locking miniobjects
 */
typedef enum {
  GST_LOCK_FLAG_READ      = (1 << 0),
  GST_LOCK_FLAG_WRITE     = (1 << 1),
  GST_LOCK_FLAG_EXCLUSIVE = (1 << 2),

166
  GST_LOCK_FLAG_LAST      = (1 << 8)
167 168 169
} GstLockFlags;

/**
170
 * GST_LOCK_FLAG_READWRITE: (value 3) (type GstLockFlags)
171 172 173
 *
 * GstLockFlags value alias for GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE
 */
174
#define GST_LOCK_FLAG_READWRITE  ((GstLockFlags) (GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE))
175

176 177
/**
 * GST_MINI_OBJECT_REFCOUNT:
Stefan Kost's avatar
Stefan Kost committed
178
 * @obj: a #GstMiniObject
179 180 181
 *
 * Get access to the reference count field of the mini-object.
 */
182
#define GST_MINI_OBJECT_REFCOUNT(obj)           ((GST_MINI_OBJECT_CAST(obj))->refcount)
183 184
/**
 * GST_MINI_OBJECT_REFCOUNT_VALUE:
Stefan Kost's avatar
Stefan Kost committed
185
 * @obj: a #GstMiniObject
186 187 188
 *
 * 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))
190

191
/**
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)
Wim Taymans's avatar
Wim Taymans committed
193
 * @type: the GType of the object
194
 * @refcount: atomic refcount
195
 * @lockstate: atomic state of the locks
196
 * @flags: extra flags.
197 198 199
 * @copy: a copy function
 * @dispose: a dispose function
 * @free: the free function
Wim Taymans's avatar
Wim Taymans committed
200
 *
201 202
 * Base class for refcounted lightweight objects.
 */
203
struct _GstMiniObject {
204
  GType   type;
205

206 207
  /*< public >*/ /* with COW */
  gint    refcount;
208
  gint    lockstate;
209
  guint   flags;
210 211

  GstMiniObjectCopyFunction copy;
212
  GstMiniObjectDisposeFunction dispose;
213
  GstMiniObjectFreeFunction free;
214 215

  /* < private > */
Wim Taymans's avatar
Wim Taymans committed
216 217
  /* Used to keep track of weak ref notifies and qdata */
  guint n_qdata;
218
  gpointer qdata;
219 220
};

221
GST_API
222 223
void            gst_mini_object_init (GstMiniObject *mini_object,
                                      guint flags, GType type,
224 225 226
                                      GstMiniObjectCopyFunction copy_func,
                                      GstMiniObjectDisposeFunction dispose_func,
                                      GstMiniObjectFreeFunction free_func);
227 228


229
/* refcounting */
230

231
GST_API
Wim Taymans's avatar
Wim Taymans committed
232
GstMiniObject * gst_mini_object_ref		(GstMiniObject *mini_object);
233

234
GST_API
Wim Taymans's avatar
Wim Taymans committed
235
void            gst_mini_object_unref		(GstMiniObject *mini_object);
236

237
GST_API
Wim Taymans's avatar
Wim Taymans committed
238
void            gst_mini_object_weak_ref        (GstMiniObject *object,
Wim Taymans's avatar
Wim Taymans committed
239
					         GstMiniObjectNotify notify,
240
					         gpointer data);
241
GST_API
Wim Taymans's avatar
Wim Taymans committed
242
void            gst_mini_object_weak_unref	(GstMiniObject *object,
Wim Taymans's avatar
Wim Taymans committed
243
					         GstMiniObjectNotify notify,
244
					         gpointer data);
245

246
/* locking */
247

248
GST_API
249
gboolean        gst_mini_object_lock            (GstMiniObject *object, GstLockFlags flags);
250

251
GST_API
252 253
void            gst_mini_object_unlock          (GstMiniObject *object, GstLockFlags flags);

254
GST_API
255
gboolean        gst_mini_object_is_writable     (const GstMiniObject *mini_object);
256

257
GST_API
258
GstMiniObject * gst_mini_object_make_writable	(GstMiniObject *mini_object) G_GNUC_WARN_UNUSED_RESULT;
259 260

/* copy */
261

262
GST_API
263
GstMiniObject * gst_mini_object_copy		(const GstMiniObject *mini_object) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
264 265


266
GST_API
Wim Taymans's avatar
Wim Taymans committed
267 268
void            gst_mini_object_set_qdata       (GstMiniObject *object, GQuark quark,
                                                 gpointer data, GDestroyNotify destroy);
269
GST_API
Wim Taymans's avatar
Wim Taymans committed
270 271
gpointer        gst_mini_object_get_qdata       (GstMiniObject *object, GQuark quark);

272
GST_API
273
gpointer        gst_mini_object_steal_qdata     (GstMiniObject *object, GQuark quark);
Wim Taymans's avatar
Wim Taymans committed
274

275
GST_API
276
gboolean        gst_mini_object_replace         (GstMiniObject **olddata, GstMiniObject *newdata);
277

278
GST_API
279
gboolean        gst_mini_object_take            (GstMiniObject **olddata, GstMiniObject *newdata);
280

281
GST_API
282
GstMiniObject * gst_mini_object_steal           (GstMiniObject **olddata) G_GNUC_WARN_UNUSED_RESULT;
283

284 285 286 287 288 289 290
/**
 * GST_DEFINE_MINI_OBJECT_TYPE:
 * @TypeName: name of the new type in CamelCase
 * @type_name: name of the new type
 *
 * Define a new mini-object type with the given name
 */
291 292 293
#define GST_DEFINE_MINI_OBJECT_TYPE(TypeName,type_name) \
   G_DEFINE_BOXED_TYPE(TypeName,type_name,              \
       (GBoxedCopyFunc) gst_mini_object_ref,            \
Wim Taymans's avatar
Wim Taymans committed
294
       (GBoxedFreeFunc) gst_mini_object_unref)
295

296 297 298 299
G_END_DECLS

#endif