Debian Codegraph @

gstreamer/src/miniobject.ccg

raw
1 /* gstreamermm - a C++ wrapper for gstreamer
2 *
3 * Copyright 2008 The gstreamermm Development Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <gstreamermm/miniobject.h>
21 #include <gstreamermm/private/miniobject_p.h>
22
23 namespace Gst
24 {
25
26 std::map<GstMiniObject*, MiniObject::SlotFinalizer*> MiniObject::finalizers;
27
28 MiniObject::~MiniObject()
29 {
30 GstMiniObject *const gobject_ = reinterpret_cast<GstMiniObject*>(const_cast<MiniObject*>(this));
31 // The value of the reference count is checked so that if this mini object is
32 // being destroyed as a result of weak reference notification no
33 // unreferencing is done and thus no error is issued on unreferencing a mini
34 // object with a reference of 0.
35 if (gobject_ && GST_MINI_OBJECT_REFCOUNT_VALUE(gobject_) > 0)
36 {
37 gst_mini_object_unref(gobject_);
38 }
39
40 if (finalizers.find(gobject_) != finalizers.end())
41 {
42 delete finalizers[gobject_];
43 finalizers[gobject_] = nullptr;
44 }
45 }
46
47 Glib::RefPtr<Gst::MiniObject> MiniObject::create_writable()
48 {
49 /*
50 * This function is generally used in the following pattern:
51 * RefPtr<Buffer> p = (...);
52 * p = p->create_writable();
53 *
54 * There are two cases:
55 * 1. object is not writable, therefore:
56 * - somebody else may have another reference to the object (but it this might change in the meantime)
57 * - gst_mini_object_make_writable may return a new copy of object
58 * - we have to make additional ref, that will be unreffed in gst_mini_object_make_writable
59 * 2. object is_writable, then:
60 * - our caller has the only one reference to object (therefore nobody can increase refcount in the meantime)
61 * - gst_mini_object_make_writable will return the same object and will not do any reffing/unreffing
62 * - we cannot make any additional refs before calling gst_mini_object_make_writable, since it would enforce unnecessary
63 * copying of the object
64 * - we are supposed to create a new RefPtr with is own reference to the object, therefore we need to "take copy"
65 * - however when caller release the pointer (implicitly, during assignment) the refcount will be 1 again
66 */
67 if(is_writable())
68 {
69 return Glib::wrap(gst_mini_object_make_writable(gobj()), true); // take copy so original object is left to the current owner (and hopefully will be released soon)
70 }
71 else
72 {
73 reference(); // gst_mini_object_make_writable(buf) will unref the old buffer, but our caller is still holding RefPtr to it
74 return Glib::wrap(gst_mini_object_make_writable(gobj()));
75 }
76 }
77
78 void MiniObject::set_qdata(GQuark quark, QuarkData *data)
79 {
80 gst_mini_object_set_qdata(gobj(), quark, static_cast<gpointer>(data), &MiniObject::destroy_qdata);
81 }
82
83 void MiniObject::destroy_qdata(gpointer qdata)
84 {
85 QuarkData *data = static_cast<QuarkData*>(qdata);
86 delete data;
87 }
88
89 QuarkData* MiniObject::get_qdata(GQuark quark) const
90 {
91 gpointer qdata = gst_mini_object_get_qdata(const_cast<GstMiniObject*>(gobj()), quark);
92 return static_cast<QuarkData*>(qdata);
93 }
94
95 void MiniObject::add_finalize_notifier(const SlotFinalizer& slot)
96 {
97 SlotFinalizer *finalizer = new SlotFinalizer(slot);
98 if (finalizers.find(gobj()) != finalizers.end())
99 {
100 delete finalizers[gobj()];
101 finalizers[gobj()] = finalizer;
102 }
103 else
104 {
105 finalizers.insert(std::make_pair(gobj(), finalizer));
106 }
107 gst_mini_object_weak_ref(gobj(), &MiniObject::MiniObject_Finalizer_gstreamermm_callback, static_cast<gpointer>(finalizer));
108 }
109
110 void MiniObject::MiniObject_Finalizer_gstreamermm_callback(gpointer userdata, GstMiniObject*)
111 {
112 (*static_cast<SlotFinalizer*>(userdata))();
113 }
114
115 void MiniObject::remove_finalize_notifier()
116 {
117 if (finalizers.find(gobj()) != finalizers.end())
118 {
119 gst_mini_object_weak_unref(gobj(), &MiniObject::MiniObject_Finalizer_gstreamermm_callback, finalizers[gobj()]);
120 delete finalizers[gobj()];
121 finalizers[gobj()] = nullptr;
122 }
123 }
124
125 } //namespace Gst