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.
This commit is contained in:
Martin Kupec 2015-09-01 23:55:41 +02:00 committed by Harol Alfonso Reina
parent 5a7a0f7a3c
commit a76f60df81
4 changed files with 61 additions and 4 deletions

View file

@ -95,19 +95,32 @@ namespace GLib {
public static readonly GType Variant = new GType ((IntPtr) TypeFundamentals.TypeVariant);
static HashSet<GType> managedTypes = new HashSet<GType> ();
static IDictionary<IntPtr, Type> types = new Dictionary<IntPtr, Type> ();
static IDictionary<Type, GType> gtypes = new Dictionary<Type, GType> ();
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;
}

View file

@ -90,6 +90,7 @@ sources = \
ToggleRef.cs \
TypeFundamentals.cs \
TypeInitializerAttribute.cs \
TypeNameAttribute.cs \
ValueArray.cs \
Value.cs \
Variant.cs \

View file

@ -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]);

41
glib/TypeNameAttribute.cs Normal file
View file

@ -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;
}
}
}
}