// GLib.Value.cs - GLib Value class implementation // // Author: Mike Kestner // // (c) 2001 Mike Kestner namespace GLib { using System; using System.Runtime.InteropServices; /// /// Value Class /// /// /// /// An arbitrary data type similar to a CORBA Any which is used /// to get and set properties on Objects. /// public class Value { IntPtr _val; // Destructor is required since we are allocating unmananged // heap resources. [DllImport("glib-2.0")] static extern void g_free (IntPtr mem); ~Value () { g_free (_val); } /// /// Value Constructor /// /// /// /// Creates an uninitialized Value on the unmanaged heap. /// Use the Init method prior to attempting to assign a /// value to it. /// [DllImport("glib-2.0", CallingConvention=CallingConvention.Cdecl)] static extern IntPtr g_malloc0 (long n_bytes); public Value () { _val = g_malloc0 (5 * IntPtr.Size); } /// /// Value Constructor /// /// /// /// Creates an initialized Value of the specified type. /// public Value (TypeFundamentals type) : this () { Init (type); } /// /// Value Constructor /// /// /// /// Constructs a Value from a specified boolean. /// [DllImport("gobject-2.0", CallingConvention=CallingConvention.Cdecl)] static extern void g_value_set_boolean (IntPtr val, bool data); public Value (bool val) : this () { g_value_init (_val, TypeFundamentals.TypeBoolean); g_value_set_boolean (_val, val); } /// /// Value Constructor /// /// /// /// Constructs a Value from a specified integer. /// [DllImport("gobject-2.0", CallingConvention=CallingConvention.Cdecl)] static extern void g_value_set_int (IntPtr val, int data); public Value (int val) : this () { g_value_init (_val, TypeFundamentals.TypeInt); g_value_set_int (_val, val); } /// /// Value Constructor /// /// /// /// Constructs a Value from a specified string. /// [DllImport("gobject-2.0", CallingConvention=CallingConvention.Cdecl)] static extern void g_value_set_string (IntPtr val, string data); public Value (string val) : this () { g_value_init (_val, TypeFundamentals.TypeString); g_value_set_string (_val, val); } /// /// Init Method /// /// /// /// Prepares a raw value to hold a specified type. /// [DllImport("gobject-2.0", CallingConvention=CallingConvention.Cdecl)] static extern void g_value_init (IntPtr val, TypeFundamentals type); public void Init (TypeFundamentals type) { g_value_init (_val, type); } /// /// Value to Boolean Conversion /// /// /// /// Extracts a bool from a Value. Note, this method /// will produce an exception if the Value does not hold a /// boolean value. /// [DllImport("gobject-2.0", CallingConvention=CallingConvention.Cdecl)] static extern bool g_value_get_boolean (IntPtr val); public static explicit operator bool (Value val) { // FIXME: Insert an appropriate exception here if // _val.type indicates an error. return g_value_get_boolean (val._val); } /// /// Value to Integer Conversion /// /// /// /// Extracts an int from a Value. Note, this method /// will produce an exception if the Value does not hold a /// integer value. /// [DllImport("gobject-2.0", CallingConvention=CallingConvention.Cdecl)] static extern int g_value_get_int (IntPtr val); public static explicit operator int (Value val) { // FIXME: Insert an appropriate exception here if // _val.type indicates an error. return g_value_get_int (val._val); } /// /// Value to String Conversion /// /// /// /// Extracts a string from a Value. Note, this method /// will produce an exception if the Value does not hold a /// string value. /// [DllImport("gobject-2.0", CallingConvention=CallingConvention.Cdecl)] static extern string g_value_get_string (IntPtr val); public static explicit operator String (Value val) { // FIXME: Insert an appropriate exception here if // _val.type indicates an error. return g_value_get_string (val._val); } /// /// MarshalAs Property /// /// /// /// Read only. Accesses a pointer to the raw GValue. /// public IntPtr MarshalAs { get { return _val; } } } }