GtkSharp/glib/Value.cs

442 lines
13 KiB
C#
Raw Normal View History

// GLib.Value.cs - GLib Value class implementation
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// Copyright (c) 2001 Mike Kestner
// Copyright (c) 2003-2004 Novell, Inc.
//
// 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;
using System.Collections;
using System.Runtime.InteropServices;
[StructLayout (LayoutKind.Sequential)]
public struct Value : IDisposable {
IntPtr type;
long pad_1;
long pad_2;
public static Value Empty;
public Value (GLib.GType gtype)
{
type = IntPtr.Zero;
pad_1 = pad_2 = 0;
g_value_init (ref this, gtype.Val);
}
public Value (object obj)
{
type = IntPtr.Zero;
pad_1 = pad_2 = 0;
* parser/gapi2xml.pl: make note of _get_type methods for enums * */*-api.xml: Regen, adding gtype="..." to many enum types * generator/EnumGen.cs (Generate): if the enum has the "gtype" property, add a GTypeAttribute pointing to an internal FooGType class whose GType property can be used to get the enum's GType. * generator/ObjectGen.cs: s/ObjectManager.RegisterType/GType.Register/ * glib/GTypeAttribute.cs: attribute for indicating a property that will return the GType of a type (particularly for enums, which can't have GType properties added to them). * glib/GType.cs: renamed from Type.cs to match the type name (public static readonly GType ...): add a few missing types. (Register): moved from ObjectManager.RegisterType (LookupGType): moved from TypeConverter.LookupType and extended to handle GTypeAttribute. Also, fix mappings for sbyte/byte/char, and return specific GTypes for Object subclasses rather than always returning GType.Object. [Fixes #74699] (LookupType): moved from ObjectWrapper.LookupType (ToString): return the type name * glib/Object.cs (RegisterGType): s/ObjectManager.Register/GType.Register/ (LookupGType): Make this protected internal so GType can access it. * glib/ObjectManager.cs (RegisterType): deprecate in favor of GType.Register. (LookupType): moved to GType * glib/TypeConverter.cs (LookupType): now a deprecated wrapper around GType.LookupGType. * glib/Value.cs: Use GType casts rather than TypeConverter * gtk/NodeStore.cs (ScanType): * gtk/ListStore.custom (ListStore): * gtk/TreeStore.custom (TreeStore): Use (GType) cast rather than TypeConverter. Remove the error check and exception, since the cast never returns GType.Invalid. (The check probably predates GLib.ManagedValue.) * gnome/PanelAppletFactory.cs (Register): Use a GType cast rather than GLib.Object.LookupGType (which is no longer accessible after an mcs bugfix) * sample/GtkDemo/DemoIconView.cs (CreateStore): use the Type[] constructor rather than the GType[] constructor, since it translates typeof(Gdk.Pixbuf) correctly now. svn path=/trunk/gtk-sharp/; revision=44038
2005-05-04 16:54:24 +00:00
GType gtype = (GType) obj.GetType ();
g_value_init (ref this, gtype.Val);
Val = obj;
2002-07-25 Rachel Hestilow <hestilow@ximian.com> [about 60% of the marshalling patch that I lost. The rest to come tomorrow.] * generator/BoxedGen.cs, StructGen.cs: Move most of this to StructBase, delete large chunks duplicated from ClassBase. * generator/IGeneratable.cs: Add MarshalReturnType, FromNativeReturn. * generator/ClassBase.cs: Move ctor stuff here. Add a CallByName overload with no parameters for the "self" reference. * generator/EnumGen.cs, CallbackGen.cs: Implement new MarshalReturnType, FromNativeReturn. * generator/Method.cs: Use container_type.MarshalType, CallByName, and SymbolTable.FromNativeReturn when generating call and import sigs. * generator/OpaqueGen.cs: Added. * generator/Property.cs: Handle boxed and opaques differently. * generator/SymbolTable.cs: Update for the opaque stuff and the new Return methods. Also change GetClassGen to simply call the as operator. * glib/Boxed.cs: Update for struct usage -- this is now a wrapper for the purposes of using with Value. * glib/Opaque.cs: Added. New base class for opaque structs. * glue/textiter.c, gtk/TextIter.custom: Remove. * gnome/Program.cs: Update for new struct marshalling. * parser/Metadata.pm: Use our own getChildrenByTagName. * parser/README: Update for new requirements (was out of sync with build.pl) * parser/gapi2xml.pl: Hide struct like const in field elements. * parser/gapi_pp.pl: Handle embedded union fields (poorly). * sample/test/TestColorSelection.cs: Comment out null color tests for now. svn path=/trunk/gtk-sharp/; revision=6186
2002-07-26 06:08:52 +00:00
}
public Value (bool val) : this (GType.Boolean)
{
g_value_set_boolean (ref this, val);
}
public Value (int val) : this (GType.Int)
{
g_value_set_int (ref this, val);
}
public Value (uint val) : this (GType.UInt)
{
g_value_set_uint (ref this, val);
}
public Value (ushort val) : this (GType.UInt)
{
g_value_set_uint (ref this, val);
}
public Value (long val) : this (GType.Int64)
{
g_value_set_int64 (ref this, val);
}
public Value (ulong val) : this (GType.UInt64)
{
g_value_set_uint64 (ref this, val);
}
public Value (EnumWrapper wrap, string type_name)
{
type = IntPtr.Zero;
pad_1 = pad_2 = 0;
IntPtr native = GLib.Marshaller.StringToPtrGStrdup (type_name);
gtksharp_value_create_from_type_name (ref this, native);
GLib.Marshaller.Free (native);
if (wrap.flags)
g_value_set_flags (ref this, (uint) (int) wrap);
else
g_value_set_enum (ref this, (int) wrap);
}
public Value (float val) : this (GType.Float)
{
g_value_set_float (ref this, val);
}
public Value (double val) : this (GType.Double)
{
g_value_set_double (ref this, val);
}
public Value (string val) : this (GType.String)
{
IntPtr native_val = GLib.Marshaller.StringToPtrGStrdup (val);
g_value_set_string (ref this, native_val);
GLib.Marshaller.Free (native_val);
}
public Value (IntPtr val) : this (GType.Pointer)
{
g_value_set_pointer (ref this, val);
}
public Value (Opaque val, string type_name)
{
type = IntPtr.Zero;
pad_1 = pad_2 = 0;
IntPtr native = GLib.Marshaller.StringToPtrGStrdup (type_name);
gtksharp_value_create_from_type_name (ref this, native);
GLib.Marshaller.Free (native);
g_value_set_boxed (ref this, val.Handle);
}
public Value (GLib.Object val) : this (val == null ? GType.Object : val.NativeType)
{
g_value_set_object (ref this, val == null ? IntPtr.Zero : val.Handle);
}
public Value (GLib.Object obj, string prop_name)
{
type = IntPtr.Zero;
pad_1 = pad_2 = 0;
IntPtr prop = GLib.Marshaller.StringToPtrGStrdup (prop_name);
gtksharp_value_create_from_property (ref this, obj.Handle, prop);
GLib.Marshaller.Free (prop);
}
[Obsolete]
public Value (GLib.Object obj, string prop_name, EnumWrapper wrap)
{
type = IntPtr.Zero;
pad_1 = pad_2 = 0;
IntPtr native = GLib.Marshaller.StringToPtrGStrdup (prop_name);
gtksharp_value_create_from_type_and_property (ref this, obj.NativeType.Val, native);
GLib.Marshaller.Free (native);
if (wrap.flags)
g_value_set_flags (ref this, (uint) (int) wrap);
else
g_value_set_enum (ref this, (int) wrap);
}
[Obsolete]
public Value (IntPtr obj, string prop_name, Opaque val)
{
type = IntPtr.Zero;
pad_1 = pad_2 = 0;
IntPtr native = GLib.Marshaller.StringToPtrGStrdup (prop_name);
gtksharp_value_create_from_property (ref this, obj, native);
GLib.Marshaller.Free (native);
g_value_set_boxed (ref this, val.Handle);
}
public void Dispose ()
{
g_value_unset (ref this);
}
public void Init (GLib.GType gtype)
2002-07-25 Rachel Hestilow <hestilow@ximian.com> [about 60% of the marshalling patch that I lost. The rest to come tomorrow.] * generator/BoxedGen.cs, StructGen.cs: Move most of this to StructBase, delete large chunks duplicated from ClassBase. * generator/IGeneratable.cs: Add MarshalReturnType, FromNativeReturn. * generator/ClassBase.cs: Move ctor stuff here. Add a CallByName overload with no parameters for the "self" reference. * generator/EnumGen.cs, CallbackGen.cs: Implement new MarshalReturnType, FromNativeReturn. * generator/Method.cs: Use container_type.MarshalType, CallByName, and SymbolTable.FromNativeReturn when generating call and import sigs. * generator/OpaqueGen.cs: Added. * generator/Property.cs: Handle boxed and opaques differently. * generator/SymbolTable.cs: Update for the opaque stuff and the new Return methods. Also change GetClassGen to simply call the as operator. * glib/Boxed.cs: Update for struct usage -- this is now a wrapper for the purposes of using with Value. * glib/Opaque.cs: Added. New base class for opaque structs. * glue/textiter.c, gtk/TextIter.custom: Remove. * gnome/Program.cs: Update for new struct marshalling. * parser/Metadata.pm: Use our own getChildrenByTagName. * parser/README: Update for new requirements (was out of sync with build.pl) * parser/gapi2xml.pl: Hide struct like const in field elements. * parser/gapi_pp.pl: Handle embedded union fields (poorly). * sample/test/TestColorSelection.cs: Comment out null color tests for now. svn path=/trunk/gtk-sharp/; revision=6186
2002-07-26 06:08:52 +00:00
{
g_value_init (ref this, gtype.Val);
2002-07-25 Rachel Hestilow <hestilow@ximian.com> [about 60% of the marshalling patch that I lost. The rest to come tomorrow.] * generator/BoxedGen.cs, StructGen.cs: Move most of this to StructBase, delete large chunks duplicated from ClassBase. * generator/IGeneratable.cs: Add MarshalReturnType, FromNativeReturn. * generator/ClassBase.cs: Move ctor stuff here. Add a CallByName overload with no parameters for the "self" reference. * generator/EnumGen.cs, CallbackGen.cs: Implement new MarshalReturnType, FromNativeReturn. * generator/Method.cs: Use container_type.MarshalType, CallByName, and SymbolTable.FromNativeReturn when generating call and import sigs. * generator/OpaqueGen.cs: Added. * generator/Property.cs: Handle boxed and opaques differently. * generator/SymbolTable.cs: Update for the opaque stuff and the new Return methods. Also change GetClassGen to simply call the as operator. * glib/Boxed.cs: Update for struct usage -- this is now a wrapper for the purposes of using with Value. * glib/Opaque.cs: Added. New base class for opaque structs. * glue/textiter.c, gtk/TextIter.custom: Remove. * gnome/Program.cs: Update for new struct marshalling. * parser/Metadata.pm: Use our own getChildrenByTagName. * parser/README: Update for new requirements (was out of sync with build.pl) * parser/gapi2xml.pl: Hide struct like const in field elements. * parser/gapi_pp.pl: Handle embedded union fields (poorly). * sample/test/TestColorSelection.cs: Comment out null color tests for now. svn path=/trunk/gtk-sharp/; revision=6186
2002-07-26 06:08:52 +00:00
}
public static explicit operator bool (Value val)
{
return g_value_get_boolean (ref val);
}
public static explicit operator int (Value val)
{
return g_value_get_int (ref val);
}
public static explicit operator uint (Value val)
{
return g_value_get_uint (ref val);
}
public static explicit operator ushort (Value val)
{
return (ushort) g_value_get_uint (ref val);
}
public static explicit operator long (Value val)
{
return g_value_get_int64 (ref val);
}
public static explicit operator ulong (Value val)
{
return g_value_get_uint64 (ref val);
}
public static explicit operator EnumWrapper (Value val)
{
if (glibsharp_value_holds_flags (ref val))
return new EnumWrapper ((int)g_value_get_flags (ref val), true);
else
return new EnumWrapper (g_value_get_enum (ref val), false);
}
public static explicit operator float (Value val)
{
return g_value_get_float (ref val);
}
public static explicit operator double (Value val)
{
return g_value_get_double (ref val);
}
public static explicit operator string (Value val)
{
IntPtr str = g_value_get_string (ref val);
return str == IntPtr.Zero ? null : GLib.Marshaller.Utf8PtrToString (str);
}
public static explicit operator IntPtr (Value val)
{
return g_value_get_pointer (ref val);
}
public static explicit operator GLib.Opaque (Value val)
{
return GLib.Opaque.GetOpaque (g_value_get_boxed (ref val));
}
public static explicit operator GLib.Boxed (Value val)
{
return new GLib.Boxed (g_value_get_boxed (ref val));
}
public static explicit operator GLib.Object (Value val)
{
return GLib.Object.GetObject (g_value_get_object (ref val), false);
}
public static explicit operator GLib.UnwrappedObject (Value val)
{
return new UnwrappedObject (g_value_get_object (ref val));
}
public object Val
{
get {
GLib.GType type = new GLib.GType (gtksharp_value_get_value_type (ref this));
if (type == GType.Char)
return (char) this;
else if (type == GType.Boolean)
return (bool) this;
else if (type == GType.Int)
return (int) this;
else if (type == GType.UInt)
return (uint) this;
else if (type == GType.Int64)
return (long) this;
else if (type == GType.UInt64)
return (ulong) this;
else if (type == GType.Float)
return (float) this;
else if (type == GType.Double)
return (double) this;
else if (type == GType.String)
return (string) this;
else if (type == ManagedValue.GType)
return ManagedValue.ObjectForWrapper (g_value_get_boxed (ref this));
else if (g_type_is_a (type.Val, GType.Object.Val))
return (GLib.Object) this;
else
throw new Exception ("Unknown type");
}
set {
IntPtr buf;
* parser/gapi2xml.pl: make note of _get_type methods for enums * */*-api.xml: Regen, adding gtype="..." to many enum types * generator/EnumGen.cs (Generate): if the enum has the "gtype" property, add a GTypeAttribute pointing to an internal FooGType class whose GType property can be used to get the enum's GType. * generator/ObjectGen.cs: s/ObjectManager.RegisterType/GType.Register/ * glib/GTypeAttribute.cs: attribute for indicating a property that will return the GType of a type (particularly for enums, which can't have GType properties added to them). * glib/GType.cs: renamed from Type.cs to match the type name (public static readonly GType ...): add a few missing types. (Register): moved from ObjectManager.RegisterType (LookupGType): moved from TypeConverter.LookupType and extended to handle GTypeAttribute. Also, fix mappings for sbyte/byte/char, and return specific GTypes for Object subclasses rather than always returning GType.Object. [Fixes #74699] (LookupType): moved from ObjectWrapper.LookupType (ToString): return the type name * glib/Object.cs (RegisterGType): s/ObjectManager.Register/GType.Register/ (LookupGType): Make this protected internal so GType can access it. * glib/ObjectManager.cs (RegisterType): deprecate in favor of GType.Register. (LookupType): moved to GType * glib/TypeConverter.cs (LookupType): now a deprecated wrapper around GType.LookupGType. * glib/Value.cs: Use GType casts rather than TypeConverter * gtk/NodeStore.cs (ScanType): * gtk/ListStore.custom (ListStore): * gtk/TreeStore.custom (TreeStore): Use (GType) cast rather than TypeConverter. Remove the error check and exception, since the cast never returns GType.Invalid. (The check probably predates GLib.ManagedValue.) * gnome/PanelAppletFactory.cs (Register): Use a GType cast rather than GLib.Object.LookupGType (which is no longer accessible after an mcs bugfix) * sample/GtkDemo/DemoIconView.cs (CreateStore): use the Type[] constructor rather than the GType[] constructor, since it translates typeof(Gdk.Pixbuf) correctly now. svn path=/trunk/gtk-sharp/; revision=44038
2005-05-04 16:54:24 +00:00
GType type = (GType) value.GetType();
if (type == GType.Char)
g_value_set_char (ref this, (char) value);
else if (type == GType.Boolean)
g_value_set_boolean (ref this, (bool) value);
else if (type == GType.Int)
g_value_set_int (ref this, (int) value);
else if (type == GType.UInt)
g_value_set_uint (ref this, (uint) value);
else if (type == GType.Int64)
g_value_set_int64 (ref this, (long) value);
else if (type == GType.UInt64)
g_value_set_uint64 (ref this, (ulong) value);
else if (type == GType.Float)
g_value_set_float (ref this, (float) value);
else if (type == GType.Double)
g_value_set_double (ref this, (double) value);
else if (type == GType.String) {
IntPtr native = GLib.Marshaller.StringToPtrGStrdup ((string)value);
g_value_set_string (ref this, native);
GLib.Marshaller.Free (native);
} else if (type == GType.Pointer) {
if (value is IWrapper) {
g_value_set_pointer (ref this, ((IWrapper)value).Handle);
return;
}
buf = Marshal.AllocHGlobal (Marshal.SizeOf (value.GetType()));
Marshal.StructureToPtr (value, buf, false);
g_value_set_pointer (ref this, buf);
} else if (type == ManagedValue.GType) {
g_value_set_boxed (ref this, ManagedValue.WrapObject (value));
} else if (g_type_is_a (type.Val, GLib.GType.Boxed.Val)) {
if (value is IWrapper) {
g_value_set_boxed (ref this, ((IWrapper)value).Handle);
return;
}
buf = Marshal.AllocHGlobal (Marshal.SizeOf (value.GetType()));
Marshal.StructureToPtr (value, buf, false);
g_value_set_boxed (ref this, buf);
Marshal.FreeHGlobal (buf);
* parser/gapi2xml.pl: make note of _get_type methods for enums * */*-api.xml: Regen, adding gtype="..." to many enum types * generator/EnumGen.cs (Generate): if the enum has the "gtype" property, add a GTypeAttribute pointing to an internal FooGType class whose GType property can be used to get the enum's GType. * generator/ObjectGen.cs: s/ObjectManager.RegisterType/GType.Register/ * glib/GTypeAttribute.cs: attribute for indicating a property that will return the GType of a type (particularly for enums, which can't have GType properties added to them). * glib/GType.cs: renamed from Type.cs to match the type name (public static readonly GType ...): add a few missing types. (Register): moved from ObjectManager.RegisterType (LookupGType): moved from TypeConverter.LookupType and extended to handle GTypeAttribute. Also, fix mappings for sbyte/byte/char, and return specific GTypes for Object subclasses rather than always returning GType.Object. [Fixes #74699] (LookupType): moved from ObjectWrapper.LookupType (ToString): return the type name * glib/Object.cs (RegisterGType): s/ObjectManager.Register/GType.Register/ (LookupGType): Make this protected internal so GType can access it. * glib/ObjectManager.cs (RegisterType): deprecate in favor of GType.Register. (LookupType): moved to GType * glib/TypeConverter.cs (LookupType): now a deprecated wrapper around GType.LookupGType. * glib/Value.cs: Use GType casts rather than TypeConverter * gtk/NodeStore.cs (ScanType): * gtk/ListStore.custom (ListStore): * gtk/TreeStore.custom (TreeStore): Use (GType) cast rather than TypeConverter. Remove the error check and exception, since the cast never returns GType.Invalid. (The check probably predates GLib.ManagedValue.) * gnome/PanelAppletFactory.cs (Register): Use a GType cast rather than GLib.Object.LookupGType (which is no longer accessible after an mcs bugfix) * sample/GtkDemo/DemoIconView.cs (CreateStore): use the Type[] constructor rather than the GType[] constructor, since it translates typeof(Gdk.Pixbuf) correctly now. svn path=/trunk/gtk-sharp/; revision=44038
2005-05-04 16:54:24 +00:00
} else if (g_type_is_a (type.Val, GType.Object.Val))
g_value_set_object (ref this, ((GLib.Object) value).Handle);
else
throw new Exception ("Unknown type");
}
}
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_init (ref GLib.Value val, IntPtr gtype);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_unset (ref GLib.Value val);
[DllImport("glibsharpglue-2")]
static extern IntPtr gtksharp_value_create_from_property(ref GLib.Value val, IntPtr obj, IntPtr name);
[DllImport("glibsharpglue-2")]
static extern IntPtr gtksharp_value_create_from_type_and_property(ref GLib.Value val, IntPtr gtype, IntPtr name);
[DllImport("glibsharpglue-2")]
static extern IntPtr gtksharp_value_create_from_type_name(ref GLib.Value val, IntPtr type_name);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_boolean (ref Value val, bool data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_boxed (ref Value val, IntPtr data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_double (ref Value val, double data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_float (ref Value val, float data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_int (ref Value val, int data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_int64 (ref Value val, long data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_uint64 (ref Value val, ulong data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_object (ref Value val, IntPtr data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_pointer (ref Value val, IntPtr data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_string (ref Value val, IntPtr data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_uint (ref Value val, uint data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_enum (ref Value val, int data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_flags (ref Value val, uint data);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_value_set_char (ref Value val, char data);
[DllImport("libgobject-2.0-0.dll")]
static extern bool g_value_get_boolean (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern IntPtr g_value_get_boxed (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern double g_value_get_double (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern float g_value_get_float (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern int g_value_get_int (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern long g_value_get_int64 (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern ulong g_value_get_uint64 (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern IntPtr g_value_get_object (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern IntPtr g_value_get_pointer (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern IntPtr g_value_get_string (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern uint g_value_get_uint (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern int g_value_get_enum (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern uint g_value_get_flags (ref Value val);
[DllImport("glibsharpglue-2")]
static extern bool glibsharp_value_holds_flags (ref Value val);
[DllImport("glibsharpglue-2")]
static extern IntPtr gtksharp_value_get_value_type (ref Value val);
[DllImport("libgobject-2.0-0.dll")]
static extern bool g_type_is_a (IntPtr type, IntPtr is_a_type);
}
}