miniobject: add new methods to manage miniobject pointers
[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 memory associated with the
56  * object is freed.
57  */
58 typedef void (*GstMiniObjectDisposeFunction) (GstMiniObject *obj);
59 /**
60  * GstMiniObjectFreeFunction:
61  * @obj: MiniObject to free
62  *
63  * Virtual function prototype for methods to free ressources used by
64  * mini-objects.
65  */
66 typedef void (*GstMiniObjectFreeFunction) (GstMiniObject *obj);
67
68  /**
69  * GstMiniObjectWeakNotify:
70  * @data: data that was provided when the weak reference was established
71  * @where_the_mini_object_was: the mini object being finalized
72  * 
73  * A #GstMiniObjectWeakNotify function can be added to a mini object as a
74  * callback that gets triggered when the mini object is finalized. Since the
75  * mini object is already being finalized when the #GstMiniObjectWeakNotify is
76  * called, there's not much you could do with the object, apart from e.g. using
77  * its adress as hash-index or the like.
78  *
79  * Since: 0.10.35
80  */
81 typedef void (*GstMiniObjectWeakNotify) (gpointer data,
82     GstMiniObject * where_the_mini_object_was);
83
84 /**
85  * GST_MINI_OBJECT_FLAGS:
86  * @obj: MiniObject to return flags for.
87  *
88  * This macro returns the entire set of flags for the mini-object.
89  */
90 #define GST_MINI_OBJECT_TYPE(obj)  (GST_MINI_OBJECT_CAST(obj)->type)
91 /**
92  * GST_MINI_OBJECT_FLAGS:
93  * @obj: MiniObject to return flags for.
94  *
95  * This macro returns the entire set of flags for the mini-object.
96  */
97 #define GST_MINI_OBJECT_FLAGS(obj)  (GST_MINI_OBJECT_CAST(obj)->flags)
98 /**
99  * GST_MINI_OBJECT_FLAG_IS_SET:
100  * @obj: MiniObject to check for flags.
101  * @flag: Flag to check for
102  *
103  * This macro checks to see if the given flag is set.
104  */
105 #define GST_MINI_OBJECT_FLAG_IS_SET(obj,flag)        !!(GST_MINI_OBJECT_FLAGS (obj) & (flag))
106 /**
107  * GST_MINI_OBJECT_FLAG_SET:
108  * @obj: MiniObject to set flag in.
109  * @flag: Flag to set, can by any number of bits in guint32.
110  *
111  * This macro sets the given bits.
112  */
113 #define GST_MINI_OBJECT_FLAG_SET(obj,flag)           (GST_MINI_OBJECT_FLAGS (obj) |= (flag))
114 /**
115  * GST_MINI_OBJECT_FLAG_UNSET:
116  * @obj: MiniObject to unset flag in.
117  * @flag: Flag to set, must be a single bit in guint32.
118  *
119  * This macro usets the given bits.
120  */
121 #define GST_MINI_OBJECT_FLAG_UNSET(obj,flag)         (GST_MINI_OBJECT_FLAGS (obj) &= ~(flag))
122
123 /**
124  * GstMiniObjectFlags:
125  * @GST_MINI_OBJECT_FLAG_LAST: first flag that can be used by subclasses.
126  *
127  * Flags for the mini object
128  */
129 typedef enum
130 {
131   /* padding */
132   GST_MINI_OBJECT_FLAG_LAST = (1<<4)
133 } GstMiniObjectFlags;
134
135 /**
136  * GST_MINI_OBJECT_REFCOUNT:
137  * @obj: a #GstMiniObject
138  *
139  * Get access to the reference count field of the mini-object.
140  */
141 #define GST_MINI_OBJECT_REFCOUNT(obj)           ((GST_MINI_OBJECT_CAST(obj))->refcount)
142 /**
143  * GST_MINI_OBJECT_REFCOUNT_VALUE:
144  * @obj: a #GstMiniObject
145  *
146  * Get the reference count value of the mini-object.
147  */
148 #define GST_MINI_OBJECT_REFCOUNT_VALUE(obj)     (g_atomic_int_get (&(GST_MINI_OBJECT_CAST(obj))->refcount))
149
150 /**
151  * GST_MINI_OBJECT_SIZE:
152  * @obj: a #GstMiniObject
153  *
154  * Get the allocated size of @obj.
155  */
156 #define GST_MINI_OBJECT_SIZE(obj)              ((GST_MINI_OBJECT_CAST(obj))->size)
157
158 /**
159  * GstMiniObject:
160  * @refcount: atomic refcount
161  * @flags: extra flags.
162  * @copy: a copy function
163  * @dispose: a dispose function
164  * @free: the free function
165  *
166  * Base class for refcounted lightweight objects.
167  * Ref Func: gst_mini_object_ref
168  * Unref Func: gst_mini_object_unref
169  * Set Value Func: g_value_set_boxed
170  * Get Value Func: g_value_get_boxed
171  */
172 struct _GstMiniObject {
173   GType   type;
174
175   /*< public >*/ /* with COW */
176   gint    refcount;
177   guint   flags;
178   gsize   size;
179
180   GstMiniObjectCopyFunction copy;
181   GstMiniObjectDisposeFunction dispose;
182   GstMiniObjectFreeFunction free;
183
184   /* < private > */
185   /* Used to keep track of weak ref notifies */
186   guint n_weak_refs;
187   struct
188   {
189     GstMiniObjectWeakNotify notify;
190     gpointer data;
191   } *weak_refs;
192 };
193
194 GType           gst_mini_object_register        (const gchar *name);
195
196 void            gst_mini_object_init            (GstMiniObject *mini_object,
197                                                  GType type, gsize size);
198
199 GstMiniObject*  gst_mini_object_copy            (const GstMiniObject *mini_object);
200 gboolean        gst_mini_object_is_writable     (const GstMiniObject *mini_object);
201 GstMiniObject*  gst_mini_object_make_writable   (GstMiniObject *mini_object);
202
203 /* refcounting */
204 GstMiniObject*  gst_mini_object_ref             (GstMiniObject *mini_object);
205 void            gst_mini_object_unref           (GstMiniObject *mini_object);
206
207 void            gst_mini_object_weak_ref        (GstMiniObject *object,
208                                                  GstMiniObjectWeakNotify notify,
209                                                  gpointer data);
210 void            gst_mini_object_weak_unref      (GstMiniObject *object,
211                                                  GstMiniObjectWeakNotify notify,
212                                                  gpointer data);
213
214 gboolean        gst_mini_object_replace         (GstMiniObject **olddata, GstMiniObject *newdata);
215 gboolean        gst_mini_object_take            (GstMiniObject **olddata, GstMiniObject *newdata);
216 GstMiniObject * gst_mini_object_steal           (GstMiniObject **olddata);
217
218
219 G_END_DECLS
220
221 #endif
222