[glibmm/glibmm-2-18] Allow dynamic GType registration, using g_type_module_register_type().
- From: Murray Cumming <murrayc src gnome org>
- To: svn-commits-list gnome org
- Subject: [glibmm/glibmm-2-18] Allow dynamic GType registration, using g_type_module_register_type().
- Date: Mon, 22 Jun 2009 04:51:26 -0400 (EDT)
commit b279b472c2221011c6420652d5a5122efa66f4bd
Author: Murray Cumming <murrayc murrayc com>
Date: Fri Jun 19 15:30:00 2009 +0200
Allow dynamic GType registration, using g_type_module_register_type().
* glib/glibmm/class.[h|cc]: Added a register_derived_type() overload that
takes an extra GTypeModule* parameter, and which calls
g_type_module_register_type() instead of g_type_register_static().
* tools/m4/class_gobject.m4:
* tools/m4/class_gtkobject.m4:
* tools/m4/class_shared.m4: Added a _DYNAMIC_GTYPE_REGISTRATION macro to
be used in the class in the hg file, to add a *_Class::init(GTypeModule)
method, and a get_type(GTypeModule*) method that calls it.
ChangeLog | 13 ++++++++++
glib/glibmm/class.cc | 35 ++++++++++++++++++++++-----
glib/glibmm/class.h | 15 +++++++++---
tools/m4/class_gobject.m4 | 5 ++++
tools/m4/class_gtkobject.m4 | 5 ++++
tools/m4/class_shared.m4 | 53 +++++++++++++++++++++++++++++++++++++++++++
6 files changed, 115 insertions(+), 11 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 73d1b49..9232123 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2009-06-19 Murray Cumming <murrayc murrayc com>
+
+ Allow dynamic GType registration, using g_type_module_register_type().
+
+ * glib/glibmm/class.[h|cc]: Added a register_derived_type() overload that
+ takes an extra GTypeModule* parameter, and which calls
+ g_type_module_register_type() instead of g_type_register_static().
+ * tools/m4/class_gobject.m4:
+ * tools/m4/class_gtkobject.m4:
+ * tools/m4/class_shared.m4: Added a _DYNAMIC_GTYPE_REGISTRATION macro to
+ be used in the class in the hg file, to add a *_Class::init(GTypeModule)
+ method, and a get_type(GTypeModule*) method that calls it.
+
2009-06-16 José Alburquerque <jaalburqu svn gnome org>
* tools/extra_defs_gen/generate_extra_defs.cc:
diff --git a/glib/glibmm/class.cc b/glib/glibmm/class.cc
index be71fa4..af28517 100644
--- a/glib/glibmm/class.cc
+++ b/glib/glibmm/class.cc
@@ -4,16 +4,16 @@
/* Copyright (C) 1998-2002 The gtkmm Development Team
*
* This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
*
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
@@ -29,9 +29,20 @@ namespace Glib
void Class::register_derived_type(GType base_type)
{
+ return register_derived_type(base_type, 0);
+}
+
+void Class::register_derived_type(GType base_type, GTypeModule* module)
+{
if(gtype_)
return; // already initialized
+ //0 is not a valid GType.
+ //It would lead to a crash later.
+ //We allow this, failing silently, to make life easier for gstreamermm.
+ if(base_type == 0)
+ return; // already initialized
+
GTypeQuery base_query = { 0, 0, 0, 0, };
g_type_query(base_type, &base_query);
@@ -49,10 +60,20 @@ void Class::register_derived_type(GType base_type)
0, // value_table
};
- Glib::ustring derived_name = "gtkmm__";
- derived_name += base_query.type_name;
+ if(!(base_query.type_name))
+ {
+ g_critical("Class::register_derived_type(): base_query.type_name is NULL.");
+ return;
+ }
+
+ gchar* derived_name = g_strconcat("gtkmm__", base_query.type_name, NULL);
+
+ if(module)
+ gtype_ = g_type_module_register_type(module, base_type, derived_name, &derived_info, GTypeFlags(0));
+ else
+ gtype_ = g_type_register_static(base_type, derived_name, &derived_info, GTypeFlags(0));
- gtype_ = g_type_register_static(base_type, derived_name.c_str(), &derived_info, GTypeFlags(0));
+ g_free(derived_name);
}
GType Class::clone_custom_type(const char* custom_type_name) const
diff --git a/glib/glibmm/class.h b/glib/glibmm/class.h
index 2610d75..2e7345d 100644
--- a/glib/glibmm/class.h
+++ b/glib/glibmm/class.h
@@ -8,16 +8,16 @@
* Copyright (C) 1998-2002 The gtkmm Development Team
*
* This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
*
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
@@ -57,8 +57,15 @@ protected:
GType gtype_;
GClassInitFunc class_init_func_;
+ /** Register a GType, derived from the @a base_type.
+ */
void register_derived_type(GType base_type);
+ /** Register a GType, derived from the @a base_type.
+ * @param module If this is not 0 then g_type_module_register_type() will be used. Otherwise g_type_register_static() will be used.
+ */
+ void register_derived_type(GType base_type, GTypeModule* module);
+
private:
static void custom_class_init_function(void* g_class, void* class_data);
};
diff --git a/tools/m4/class_gobject.m4 b/tools/m4/class_gobject.m4
index 50743d9..12cbec6 100644
--- a/tools/m4/class_gobject.m4
+++ b/tools/m4/class_gobject.m4
@@ -247,6 +247,11 @@ public:
#ifndef DOXYGEN_SHOULD_SKIP_THIS
static GType get_type() G_GNUC_CONST;
+
+ifdef(`__BOOL_DYNAMIC_GTYPE_REGISTRATION__',`
+ static GType get_type(GTypeModule* module) G_GNUC_CONST;
+',`')
+
static GType get_base_type() G_GNUC_CONST;
#endif
diff --git a/tools/m4/class_gtkobject.m4 b/tools/m4/class_gtkobject.m4
index cbe9529..9c6e3e1 100644
--- a/tools/m4/class_gtkobject.m4
+++ b/tools/m4/class_gtkobject.m4
@@ -210,6 +210,11 @@ protected:
public:
#ifndef DOXYGEN_SHOULD_SKIP_THIS
static GType get_type() G_GNUC_CONST;
+
+ifdef(`__BOOL_DYNAMIC_GTYPE_REGISTRATION__',`
+ static GType get_type(GTypeModule* module) G_GNUC_CONST;
+',`')
+
static GType get_base_type() G_GNUC_CONST;
#endif
diff --git a/tools/m4/class_shared.m4 b/tools/m4/class_shared.m4
index 13f718c..a57ff7d 100644
--- a/tools/m4/class_shared.m4
+++ b/tools/m4/class_shared.m4
@@ -50,6 +50,17 @@ define(`__BOOL_DO_NOT_DERIVE_GTYPE__',`$1')
_POP()
')
+dnl GVolumeMonitor can be broken/impeded by defining a sub-type.
+define(`_DYNAMIC_GTYPE_REGISTRATION',`dnl
+_PUSH()
+dnl Define this macro to be tested for later.
+define(`__BOOL_DYNAMIC_GTYPE_REGISTRATION__',`$1')
+_POP()
+')
+
+
+
+
dnl
dnl
dnl
@@ -73,6 +84,10 @@ ifdef(`__BOOL_DO_NOT_DERIVE_GTYPE__',`dnl
const Glib::Class& init();
+ifdef(`__BOOL_DYNAMIC_GTYPE_REGISTRATION__',`
+ const Glib::Class& init(GTypeModule* module);
+',`')
+
ifdef(`__BOOL_DO_NOT_DERIVE_GTYPE__',`dnl
',`dnl
static void class_init_function(void* g_class, void* class_data);
@@ -126,6 +141,35 @@ _IMPORT(SECTION_CC_IMPLEMENTS_INTERFACES)
return *this;
}
+
+ifdef(`__BOOL_DYNAMIC_GTYPE_REGISTRATION__',`
+const Glib::Class& __CPPNAME__`'_Class::init(GTypeModule* module)
+{
+ if(!gtype_) // create the GType if necessary
+ {
+ifdef(`__BOOL_DO_NOT_DERIVE_GTYPE__',`dnl
+ // Do not derive a GType, or use a derived klass:
+ gtype_ = CppClassParent::CppObjectType::get_type();
+',`dnl
+ // Glib::Class has to know the class init function to clone custom types.
+ class_init_func_ = &__CPPNAME__`'_Class::class_init_function;
+
+ // This is actually just optimized away, apparently with no harm.
+ // Make sure that the parent type has been created.
+ //CppClassParent::CppObjectType::get_type();
+
+ // Create the wrapper type, with the same class/instance size as the base type.
+ register_derived_type(_LOWER(__CCAST__)_get_type(), module);
+
+ // Add derived versions of interfaces, if the C type implements any interfaces:
+_IMPORT(SECTION_CC_IMPLEMENTS_INTERFACES)
+')
+ }
+
+ return *this;
+}
+',`')
+
ifdef(`__BOOL_DO_NOT_DERIVE_GTYPE__',`dnl
',`dnl
@@ -166,6 +210,15 @@ GType __CPPNAME__::get_type()
return __BASE__`'_class_.init().get_type();
}
+ifdef(`__BOOL_DYNAMIC_GTYPE_REGISTRATION__',`
+GType __CPPNAME__::get_type(GTypeModule* module)
+{
+ return __BASE__`'_class_.init(module).get_type();
+}
+'dnl
+,`'dnl
+)
+
GType __CPPNAME__::get_base_type()
{
return _LOWER(__CCAST__)_get_type();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]