Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gstreamer.git] / gst / gstminiobject.h
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #ifndef __GST_MINI_OBJECT_H__
24 #define __GST_MINI_OBJECT_H__
25
26 #include <gst/gstconfig.h>
27
28 #include <glib-object.h>
29
30 G_BEGIN_DECLS
31
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))
36
37 typedef struct _GstMiniObject GstMiniObject;
38
39 /**
40  * GstMiniObjectCopyFunction:
41  * @obj: MiniObject to copy
42  *
43  * Function prototype for methods to create copies of instances.
44  *
45  * Returns: reference to cloned instance.
46  */
47 typedef GstMiniObject * (*GstMiniObjectCopyFunction) (const GstMiniObject *obj);
48 /**
49  * GstMiniObjectDisposeFunction:
50  * @obj: MiniObject to dispose
51  *
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.
57  *
58  * Returns: %TRUE if the object should be cleaned up.
59  */
60 typedef gboolean (*GstMiniObjectDisposeFunction) (GstMiniObject *obj);
61 /**
62  * GstMiniObjectFreeFunction:
63  * @obj: MiniObject to free
64  *
65  * Virtual function prototype for methods to free ressources used by
66  * mini-objects.
67  */
68 typedef void (*GstMiniObjectFreeFunction) (GstMiniObject *obj);
69
70  /**
71  * GstMiniObjectWeakNotify:
72  * @data: data that was provided when the weak reference was established
73  * @where_the_mini_object_was: the mini object being finalized
74  *
75  * A #GstMiniObjectWeakNotify function can be added to a mini object as a
76  * callback that gets triggered when the mini object is finalized. Since the
77  * mini object is already being finalized when the #GstMiniObjectWeakNotify is
78  * called, there's not much you could do with the object, apart from e.g. using
79  * its adress as hash-index or the like.
80  *
81  * Since: 0.10.35
82  */
83 typedef void (*GstMiniObjectWeakNotify) (gpointer data,
84     GstMiniObject * where_the_mini_object_was);
85
86 /**
87  * GST_MINI_OBJECT_FLAGS:
88  * @obj: MiniObject to return flags for.
89  *
90  * This macro returns the entire set of flags for the mini-object.
91  */
92 #define GST_MINI_OBJECT_TYPE(obj)  (GST_MINI_OBJECT_CAST(obj)->type)
93 /**
94  * GST_MINI_OBJECT_FLAGS:
95  * @obj: MiniObject to return flags for.
96  *
97  * This macro returns the entire set of flags for the mini-object.
98  */
99 #define GST_MINI_OBJECT_FLAGS(obj)  (GST_MINI_OBJECT_CAST(obj)->flags)
100 /**
101  * GST_MINI_OBJECT_FLAG_IS_SET:
102  * @obj: MiniObject to check for flags.
103  * @flag: Flag to check for
104  *
105  * This macro checks to see if the given flag is set.
106  */
107 #define GST_MINI_OBJECT_FLAG_IS_SET(obj,flag)        !!(GST_MINI_OBJECT_FLAGS (obj) & (flag))
108 /**
109  * GST_MINI_OBJECT_FLAG_SET:
110  * @obj: MiniObject to set flag in.
111  * @flag: Flag to set, can by any number of bits in guint32.
112  *
113  * This macro sets the given bits.
114  */
115 #define GST_MINI_OBJECT_FLAG_SET(obj,flag)           (GST_MINI_OBJECT_FLAGS (obj) |= (flag))
116 /**
117  * GST_MINI_OBJECT_FLAG_UNSET:
118  * @obj: MiniObject to unset flag in.
119  * @flag: Flag to set, must be a single bit in guint32.
120  *
121  * This macro usets the given bits.
122  */
123 #define GST_MINI_OBJECT_FLAG_UNSET(obj,flag)         (GST_MINI_OBJECT_FLAGS (obj) &= ~(flag))
124
125 /**
126  * GstMiniObjectFlags:
127  * @GST_MINI_OBJECT_FLAG_LAST: first flag that can be used by subclasses.
128  *
129  * Flags for the mini object
130  */
131 typedef enum
132 {
133   /* padding */
134   GST_MINI_OBJECT_FLAG_LAST = (1<<4)
135 } GstMiniObjectFlags;
136
137 /**
138  * GST_MINI_OBJECT_REFCOUNT:
139  * @obj: a #GstMiniObject
140  *
141  * Get access to the reference count field of the mini-object.
142  */
143 #define GST_MINI_OBJECT_REFCOUNT(obj)           ((GST_MINI_OBJECT_CAST(obj))->refcount)
144 /**
145  * GST_MINI_OBJECT_REFCOUNT_VALUE:
146  * @obj: a #GstMiniObject
147  *
148  * Get the reference count value of the mini-object.
149  */
150 #define GST_MINI_OBJECT_REFCOUNT_VALUE(obj)     (g_atomic_int_get (&(GST_MINI_OBJECT_CAST(obj))->refcount))
151
152 /**
153  * GST_MINI_OBJECT_SIZE:
154  * @obj: a #GstMiniObject
155  *
156  * Get the allocated size of @obj.
157  */
158 #define GST_MINI_OBJECT_SIZE(obj)              ((GST_MINI_OBJECT_CAST(obj))->size)
159
160 /**
161  * GstMiniObject:
162  * @type: the GType of the object
163  * @refcount: atomic refcount
164  * @flags: extra flags.
165  * @size: the size of the structure
166  * @copy: a copy function
167  * @dispose: a dispose function
168  * @free: the free function
169  *
170  * Base class for refcounted lightweight objects.
171  * Ref Func: gst_mini_object_ref
172  * Unref Func: gst_mini_object_unref
173  * Set Value Func: g_value_set_boxed
174  * Get Value Func: g_value_get_boxed
175  */
176 struct _GstMiniObject {
177   GType   type;
178
179   /*< public >*/ /* with COW */
180   gint    refcount;
181   guint   flags;
182   gsize   size;
183
184   GstMiniObjectCopyFunction copy;
185   GstMiniObjectDisposeFunction dispose;
186   GstMiniObjectFreeFunction free;
187
188   /* < private > */
189   /* Used to keep track of weak ref notifies */
190   guint n_weak_refs;
191   struct
192   {
193     GstMiniObjectWeakNotify notify;
194     gpointer data;
195   } *weak_refs;
196 };
197
198 void            gst_mini_object_init            (GstMiniObject *mini_object,
199                                                  GType type, gsize size);
200
201 GstMiniObject * gst_mini_object_copy            (const GstMiniObject *mini_object) G_GNUC_MALLOC;
202 gboolean        gst_mini_object_is_writable     (const GstMiniObject *mini_object);
203 GstMiniObject * gst_mini_object_make_writable   (GstMiniObject *mini_object);
204
205 /* refcounting */
206 GstMiniObject * gst_mini_object_ref             (GstMiniObject *mini_object);
207 void            gst_mini_object_unref           (GstMiniObject *mini_object);
208
209 void            gst_mini_object_weak_ref        (GstMiniObject *object,
210                                                  GstMiniObjectWeakNotify notify,
211                                                  gpointer data);
212 void            gst_mini_object_weak_unref      (GstMiniObject *object,
213                                                  GstMiniObjectWeakNotify notify,
214                                                  gpointer data);
215
216 gboolean        gst_mini_object_replace         (GstMiniObject **olddata, GstMiniObject *newdata);
217 gboolean        gst_mini_object_take            (GstMiniObject **olddata, GstMiniObject *newdata);
218 GstMiniObject * gst_mini_object_steal           (GstMiniObject **olddata);
219
220 #define GST_DEFINE_MINI_OBJECT_TYPE(TypeName,type_name) \
221    G_DEFINE_BOXED_TYPE(TypeName,type_name,              \
222        (GBoxedCopyFunc) gst_mini_object_ref,            \
223        (GBoxedFreeFunc)gst_mini_object_unref)
224
225 G_END_DECLS
226
227 #endif
228