gstminiobject.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* GStreamer
  2. * Copyright (C) 2005 David Schleef <ds@schleef.org>
  3. *
  4. * gstminiobject.h: Header for GstMiniObject
  5. *
  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.
  10. *
  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.
  15. *
  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.
  20. */
  21. #ifndef __GST_MINI_OBJECT_H__
  22. #define __GST_MINI_OBJECT_H__
  23. #include <gst/gstconfig.h>
  24. #include <glib-object.h>
  25. G_BEGIN_DECLS
  26. #define GST_IS_MINI_OBJECT_TYPE(obj,type) ((obj) && GST_MINI_OBJECT_TYPE(obj) == (type))
  27. #define GST_MINI_OBJECT_CAST(obj) ((GstMiniObject*)(obj))
  28. #define GST_MINI_OBJECT_CONST_CAST(obj) ((const GstMiniObject*)(obj))
  29. #define GST_MINI_OBJECT(obj) (GST_MINI_OBJECT_CAST(obj))
  30. typedef struct _GstMiniObject GstMiniObject;
  31. /**
  32. * GstMiniObjectCopyFunction:
  33. * @obj: MiniObject to copy
  34. *
  35. * Function prototype for methods to create copies of instances.
  36. *
  37. * Returns: reference to cloned instance.
  38. */
  39. typedef GstMiniObject * (*GstMiniObjectCopyFunction) (const GstMiniObject *obj);
  40. /**
  41. * GstMiniObjectDisposeFunction:
  42. * @obj: MiniObject to dispose
  43. *
  44. * Function prototype for when a miniobject has lost its last refcount.
  45. * Implementation of the mini object are allowed to revive the
  46. * passed object by doing a gst_mini_object_ref(). If the object is not
  47. * revived after the dispose function, the function should return %TRUE
  48. * and the memory associated with the object is freed.
  49. *
  50. * Returns: %TRUE if the object should be cleaned up.
  51. */
  52. typedef gboolean (*GstMiniObjectDisposeFunction) (GstMiniObject *obj);
  53. /**
  54. * GstMiniObjectFreeFunction:
  55. * @obj: MiniObject to free
  56. *
  57. * Virtual function prototype for methods to free resources used by
  58. * mini-objects.
  59. */
  60. typedef void (*GstMiniObjectFreeFunction) (GstMiniObject *obj);
  61. /**
  62. * GstMiniObjectNotify:
  63. * @user_data: data that was provided when the notify was added
  64. * @obj: the mini object
  65. *
  66. * A #GstMiniObjectNotify function can be added to a mini object as a
  67. * callback that gets triggered when gst_mini_object_unref() drops the
  68. * last ref and @obj is about to be freed.
  69. */
  70. typedef void (*GstMiniObjectNotify) (gpointer user_data, GstMiniObject * obj);
  71. /**
  72. * GST_MINI_OBJECT_TYPE:
  73. * @obj: MiniObject to return type for.
  74. *
  75. * This macro returns the type of the mini-object.
  76. */
  77. #define GST_MINI_OBJECT_TYPE(obj) (GST_MINI_OBJECT_CAST(obj)->type)
  78. /**
  79. * GST_MINI_OBJECT_FLAGS:
  80. * @obj: MiniObject to return flags for.
  81. *
  82. * This macro returns the entire set of flags for the mini-object.
  83. */
  84. #define GST_MINI_OBJECT_FLAGS(obj) (GST_MINI_OBJECT_CAST(obj)->flags)
  85. /**
  86. * GST_MINI_OBJECT_FLAG_IS_SET:
  87. * @obj: MiniObject to check for flags.
  88. * @flag: Flag to check for
  89. *
  90. * This macro checks to see if the given flag is set.
  91. */
  92. #define GST_MINI_OBJECT_FLAG_IS_SET(obj,flag) !!(GST_MINI_OBJECT_FLAGS (obj) & (flag))
  93. /**
  94. * GST_MINI_OBJECT_FLAG_SET:
  95. * @obj: MiniObject to set flag in.
  96. * @flag: Flag to set, can by any number of bits in guint32.
  97. *
  98. * This macro sets the given bits.
  99. */
  100. #define GST_MINI_OBJECT_FLAG_SET(obj,flag) (GST_MINI_OBJECT_FLAGS (obj) |= (flag))
  101. /**
  102. * GST_MINI_OBJECT_FLAG_UNSET:
  103. * @obj: MiniObject to unset flag in.
  104. * @flag: Flag to set, must be a single bit in guint32.
  105. *
  106. * This macro unsets the given bits.
  107. */
  108. #define GST_MINI_OBJECT_FLAG_UNSET(obj,flag) (GST_MINI_OBJECT_FLAGS (obj) &= ~(flag))
  109. /**
  110. * GstMiniObjectFlags:
  111. * @GST_MINI_OBJECT_FLAG_LOCKABLE: the object can be locked and unlocked with
  112. * gst_mini_object_lock() and gst_mini_object_unlock().
  113. * @GST_MINI_OBJECT_FLAG_LOCK_READONLY: the object is permanently locked in
  114. * READONLY mode. Only read locks can be performed on the object.
  115. * @GST_MINI_OBJECT_FLAG_LAST: first flag that can be used by subclasses.
  116. *
  117. * Flags for the mini object
  118. */
  119. typedef enum
  120. {
  121. GST_MINI_OBJECT_FLAG_LOCKABLE = (1 << 0),
  122. GST_MINI_OBJECT_FLAG_LOCK_READONLY = (1 << 1),
  123. /* padding */
  124. GST_MINI_OBJECT_FLAG_LAST = (1 << 4)
  125. } GstMiniObjectFlags;
  126. /**
  127. * GST_MINI_OBJECT_IS_LOCKABLE:
  128. * @obj: a #GstMiniObject
  129. *
  130. * Check if @obj is lockable. A lockable object can be locked and unlocked with
  131. * gst_mini_object_lock() and gst_mini_object_unlock().
  132. */
  133. #define GST_MINI_OBJECT_IS_LOCKABLE(obj) GST_MINI_OBJECT_FLAG_IS_SET(obj, GST_MINI_OBJECT_FLAG_LOCKABLE)
  134. /**
  135. * GstLockFlags:
  136. * @GST_LOCK_FLAG_READ: lock for read access
  137. * @GST_LOCK_FLAG_WRITE: lock for write access
  138. * @GST_LOCK_FLAG_EXCLUSIVE: lock for exclusive access
  139. * @GST_LOCK_FLAG_LAST: first flag that can be used for custom purposes
  140. *
  141. * Flags used when locking miniobjects
  142. */
  143. typedef enum {
  144. GST_LOCK_FLAG_READ = (1 << 0),
  145. GST_LOCK_FLAG_WRITE = (1 << 1),
  146. GST_LOCK_FLAG_EXCLUSIVE = (1 << 2),
  147. GST_LOCK_FLAG_LAST = (1 << 8)
  148. } GstLockFlags;
  149. /**
  150. * GST_LOCK_FLAG_READWRITE:
  151. *
  152. * GstLockFlags value alias for GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE
  153. */
  154. #define GST_LOCK_FLAG_READWRITE (GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE)
  155. /**
  156. * GST_MINI_OBJECT_REFCOUNT:
  157. * @obj: a #GstMiniObject
  158. *
  159. * Get access to the reference count field of the mini-object.
  160. */
  161. #define GST_MINI_OBJECT_REFCOUNT(obj) ((GST_MINI_OBJECT_CAST(obj))->refcount)
  162. /**
  163. * GST_MINI_OBJECT_REFCOUNT_VALUE:
  164. * @obj: a #GstMiniObject
  165. *
  166. * Get the reference count value of the mini-object.
  167. */
  168. #define GST_MINI_OBJECT_REFCOUNT_VALUE(obj) (g_atomic_int_get (&(GST_MINI_OBJECT_CAST(obj))->refcount))
  169. /**
  170. * 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)
  171. * @type: the GType of the object
  172. * @refcount: atomic refcount
  173. * @lockstate: atomic state of the locks
  174. * @flags: extra flags.
  175. * @copy: a copy function
  176. * @dispose: a dispose function
  177. * @free: the free function
  178. *
  179. * Base class for refcounted lightweight objects.
  180. */
  181. struct _GstMiniObject {
  182. GType type;
  183. /*< public >*/ /* with COW */
  184. gint refcount;
  185. gint lockstate;
  186. guint flags;
  187. GstMiniObjectCopyFunction copy;
  188. GstMiniObjectDisposeFunction dispose;
  189. GstMiniObjectFreeFunction free;
  190. /* < private > */
  191. /* Used to keep track of weak ref notifies and qdata */
  192. guint n_qdata;
  193. gpointer qdata;
  194. };
  195. void gst_mini_object_init (GstMiniObject *mini_object,
  196. guint flags, GType type,
  197. GstMiniObjectCopyFunction copy_func,
  198. GstMiniObjectDisposeFunction dispose_func,
  199. GstMiniObjectFreeFunction free_func);
  200. /* refcounting */
  201. GstMiniObject * gst_mini_object_ref (GstMiniObject *mini_object);
  202. void gst_mini_object_unref (GstMiniObject *mini_object);
  203. void gst_mini_object_weak_ref (GstMiniObject *object,
  204. GstMiniObjectNotify notify,
  205. gpointer data);
  206. void gst_mini_object_weak_unref (GstMiniObject *object,
  207. GstMiniObjectNotify notify,
  208. gpointer data);
  209. /* locking */
  210. gboolean gst_mini_object_lock (GstMiniObject *object, GstLockFlags flags);
  211. void gst_mini_object_unlock (GstMiniObject *object, GstLockFlags flags);
  212. gboolean gst_mini_object_is_writable (const GstMiniObject *mini_object);
  213. GstMiniObject * gst_mini_object_make_writable (GstMiniObject *mini_object);
  214. /* copy */
  215. GstMiniObject * gst_mini_object_copy (const GstMiniObject *mini_object) G_GNUC_MALLOC;
  216. void gst_mini_object_set_qdata (GstMiniObject *object, GQuark quark,
  217. gpointer data, GDestroyNotify destroy);
  218. gpointer gst_mini_object_get_qdata (GstMiniObject *object, GQuark quark);
  219. gpointer gst_mini_object_steal_qdata (GstMiniObject *object, GQuark quark);
  220. gboolean gst_mini_object_replace (GstMiniObject **olddata, GstMiniObject *newdata);
  221. gboolean gst_mini_object_take (GstMiniObject **olddata, GstMiniObject *newdata);
  222. GstMiniObject * gst_mini_object_steal (GstMiniObject **olddata);
  223. /**
  224. * GST_DEFINE_MINI_OBJECT_TYPE:
  225. * @TypeName: name of the new type in CamelCase
  226. * @type_name: name of the new type
  227. *
  228. * Define a new mini-object type with the given name
  229. */
  230. #define GST_DEFINE_MINI_OBJECT_TYPE(TypeName,type_name) \
  231. G_DEFINE_BOXED_TYPE(TypeName,type_name, \
  232. (GBoxedCopyFunc) gst_mini_object_ref, \
  233. (GBoxedFreeFunc) gst_mini_object_unref)
  234. G_END_DECLS
  235. #endif