From a76f60df81531552e0ad6abf270da202b3dd62b5 Mon Sep 17 00:00:00 2001 From: Martin Kupec Date: Tue, 1 Sep 2015 23:55:41 +0200 Subject: [PATCH] Add possibility to pick glib type name for custom classes This patch adds TypeName attribute which can be used on custom class derived from GLib.Object in order to set GLib type name of the class. --- glib/GType.cs | 21 +++++++++++++++++--- glib/Makefile.am | 1 + glib/Object.cs | 2 +- glib/TypeNameAttribute.cs | 41 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 glib/TypeNameAttribute.cs diff --git a/glib/GType.cs b/glib/GType.cs index 7f63e2bae..32e5eb878 100644 --- a/glib/GType.cs +++ b/glib/GType.cs @@ -95,19 +95,32 @@ namespace GLib { public static readonly GType Variant = new GType ((IntPtr) TypeFundamentals.TypeVariant); + static HashSet managedTypes = new HashSet (); static IDictionary types = new Dictionary (); static IDictionary gtypes = new Dictionary (); public static void Register (GType native_type, System.Type type) + { + Register (native_type, type, false); + } + + public static void Register (GType native_type, System.Type type, bool managed) { lock (types) { if (native_type != GType.Pointer && native_type != GType.Boxed && native_type != ManagedValue.GType) types[native_type.Val] = type; if (type != null) gtypes[type] = native_type; + if (managed) + managedTypes.Add(native_type); } } + public static bool IsManaged (GType gtype) + { + return managedTypes.Contains(gtype); + } + static GType () { g_type_init (); @@ -318,7 +331,7 @@ namespace GLib { public GType GetThresholdType () { GType curr_type = this; - while (curr_type.ToString ().StartsWith ("__gtksharp_")) + while (IsManaged (curr_type)) curr_type = curr_type.GetBaseType (); return curr_type; } @@ -364,7 +377,9 @@ namespace GLib { internal static GType RegisterGObjectType (Object.ClassInitializer gobject_class_initializer) { GType parent_gtype = LookupGObjectType (gobject_class_initializer.Type.BaseType); - string name = BuildEscapedName (gobject_class_initializer.Type); + + TypeNameAttribute nattr = (TypeNameAttribute)Attribute.GetCustomAttribute (gobject_class_initializer.Type, typeof (TypeNameAttribute), false); + string name = nattr != null ? nattr.Name : BuildEscapedName (gobject_class_initializer.Type); IntPtr native_name = GLib.Marshaller.StringToPtrGStrdup (name); GTypeQuery query; @@ -376,7 +391,7 @@ namespace GLib { GType gtype = new GType (g_type_register_static (parent_gtype.Val, native_name, ref info, 0)); GLib.Marshaller.Free (native_name); - Register (gtype, gobject_class_initializer.Type); + Register (gtype, gobject_class_initializer.Type, true); return gtype; } diff --git a/glib/Makefile.am b/glib/Makefile.am index 4502f44b5..eb840d20c 100644 --- a/glib/Makefile.am +++ b/glib/Makefile.am @@ -90,6 +90,7 @@ sources = \ ToggleRef.cs \ TypeFundamentals.cs \ TypeInitializerAttribute.cs \ + TypeNameAttribute.cs \ ValueArray.cs \ Value.cs \ Variant.cs \ diff --git a/glib/Object.cs b/glib/Object.cs index 50a2ce6ea..3f444eeab 100644 --- a/glib/Object.cs +++ b/glib/Object.cs @@ -619,7 +619,7 @@ namespace GLib { protected virtual void CreateNativeObject (string[] names, GLib.Value[] vals) { GType gtype = LookupGType (); - bool is_managed_subclass = gtype.ToString ().StartsWith ("__gtksharp"); + bool is_managed_subclass = GType.IsManaged (gtype); GParameter[] parms = new GParameter [is_managed_subclass ? names.Length + 1 : names.Length]; for (int i = 0; i < names.Length; i++) { parms [i].name = GLib.Marshaller.StringToPtrGStrdup (names [i]); diff --git a/glib/TypeNameAttribute.cs b/glib/TypeNameAttribute.cs new file mode 100644 index 000000000..edadff098 --- /dev/null +++ b/glib/TypeNameAttribute.cs @@ -0,0 +1,41 @@ +// TypeNameAttribute.cs +// +// Copyright (c) 2015 Martin Kupec +// Copyright (c) 2015 Ales Kurecka +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of version 2 of the Lesser GNU General +// Public License as published by the Free Software Foundation. +// +// This program 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., 59 Temple Place - Suite 330, +// Boston, MA 02111-1307, USA. + + +namespace GLib { + + using System; + + [AttributeUsage (AttributeTargets.Class, AllowMultiple = false, Inherited = false)] + public sealed class TypeNameAttribute : Attribute { + private readonly string name; + + public TypeNameAttribute (string name) + { + this.name = name; + } + + public string Name + { + get { + return name; + } + } + } +}