Debian Codegraph @

gstreamer/gstreamermm/register.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 #ifndef REGISTER_H_
21 #define REGISTER_H_
22
23 #include <glib-object.h>
24 #include <glibmm/property.h>
25 #include <glibmm/init.h>
26 #include <gstreamermm/padtemplate.h>
27
28 namespace Gst
29 {
30
31 template<class DerivedCppType>
32 static GType
33 register_mm_type(const gchar * type_name=typeid(DerivedCppType).name());
34
35 template<typename DerivedCppType>
36 class ElementClass
37 {
38 friend GType register_mm_type<DerivedCppType>(const gchar * type_name);
39 GstElementClass* klass;
40 ElementClass(typename DerivedCppType::BaseClassType* klass): klass((GstElementClass*) klass){}
41 ElementClass(ElementClass const&);
42 void operator=(ElementClass const&);
43 public:
44 void add_pad_template(const Glib::RefPtr<Gst::PadTemplate>& tpl)
45 {
46 gst_element_class_add_pad_template(klass, tpl->gobj());
47 }
48 void set_metadata(const Glib::ustring& longname, const Glib::ustring& classification,
49 const Glib::ustring& description, const Glib::ustring& author)
50 {
51 gst_element_class_set_metadata(klass, longname.c_str(), classification.c_str(), description.c_str(), author.c_str());
52 }
53
54 void add_metadata(const Glib::ustring& key, const Glib::ustring& value)
55 {
56 gst_element_class_add_metadata(klass ,key.c_str(), value.c_str());
57 }
58
59 const GstElementClass* gobj() const { return klass; }
60 GstElementClass* gobj() { return klass; }
61 };
62
63 template<class DerivedCppType>
64 static GType
65 register_mm_type(const gchar * type_name)
66 {
67 struct GlibCppType
68 {
69 typename DerivedCppType::BaseObjectType parent;
70 DerivedCppType *self;
71 static void init(GlibCppType *instance, gpointer /* g_class */)
72 {
73 //instance->parent will be passed to C++ base of DerivedCppType; this will cause registerging "self" as MM wrapper of "parent"
74 Gst::init();
75 instance->self = new DerivedCppType(&instance->parent);
76 }
77
78 static void finalize(GObject *object)
79 {
80 //the following will destroy q_data, among which MM wrapper to this "object" is stored. This will cause implicit delete on "self", since it is registered as wrapper of "object".
81 (G_OBJECT_CLASS(g_type_class_peek_parent(G_OBJECT_GET_CLASS(object))))->finalize(object);
82 }
83 };
84
85 struct GlibCppTypeClass
86 {
87 typename DerivedCppType::BaseClassType parent_class;
88 /* nothing more */
89 static void init (GlibCppTypeClass * klass, gpointer data)
90 {
91 DerivedCppType::CppClassType::class_init_function((void*)klass, (void*)data);
92 GObjectClass *gobject_class;
93
94 gobject_class = (GObjectClass *) klass;
95
96 gobject_class->get_property = &Glib::custom_get_property_callback;
97 gobject_class->set_property = &Glib::custom_set_property_callback;
98 gobject_class->finalize = &GlibCppType::finalize;
99
100 Gst::ElementClass<DerivedCppType> element_class((typename DerivedCppType::BaseClassType*) klass);
101 DerivedCppType::class_init(&element_class);
102 }
103 };
104
105 Glib::init();
106
107 // the "most derived" pure glib type
108 GType parent_type = DerivedCppType::get_base_type();
109
110 /* The typedef for GType may be gulong or gsize, depending on the
111 * system and whether the compiler is c++ or not. The g_once_init_*
112 * functions always take a gsize * though ... */
•113 static gsize gonce_data = 0;
114 if (g_once_init_enter (&gonce_data)) {
115 GTypeInfo info;
116
117 info.class_size = sizeof(GlibCppTypeClass);
118 info.base_init = nullptr;
119 info.base_finalize = nullptr;
120 info.class_init = (GClassInitFunc) &GlibCppTypeClass::init;
121 info.class_finalize = nullptr;
122 info.class_data = nullptr;
123 info.instance_size = sizeof(GlibCppType);
124 info.n_preallocs = 0;
125 info.instance_init = (GInstanceInitFunc) &GlibCppType::init;
126 info.value_table = nullptr;
127
128 GType _type = g_type_register_static(parent_type, type_name, &info, (GTypeFlags)0);
129 g_once_init_leave(&gonce_data, (gsize) _type);
130 }
131 return (GType) gonce_data;
132 }
133
134 } /*namespace Gst*/
135
136
137 #endif /* REGISTER_H_ */