2011-01-22 18:52:38 +00:00
|
|
|
// Copyright (c) 2011 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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace GLib {
|
|
|
|
|
|
|
|
public class Variant : IDisposable {
|
|
|
|
|
|
|
|
IntPtr handle;
|
|
|
|
public IntPtr Handle {
|
|
|
|
get { return handle; }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Docs say that GVariant is threadsafe.
|
|
|
|
~Variant ()
|
|
|
|
{
|
|
|
|
Dispose (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
Dispose (true);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern void g_variant_unref (IntPtr handle);
|
|
|
|
|
|
|
|
void Dispose (bool disposing)
|
|
|
|
{
|
|
|
|
if (handle == IntPtr.Zero)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_variant_unref (handle);
|
|
|
|
handle = IntPtr.Zero;
|
|
|
|
if (disposing)
|
|
|
|
GC.SuppressFinalize (this);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_ref_sink (IntPtr handle);
|
|
|
|
|
|
|
|
public Variant (IntPtr handle)
|
|
|
|
{
|
|
|
|
this.handle = g_variant_ref_sink (handle);
|
|
|
|
}
|
|
|
|
|
2014-05-15 19:48:59 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
static extern IntPtr g_variant_new_variant (IntPtr val);
|
|
|
|
|
|
|
|
public static Variant NewVariant (Variant val) {
|
|
|
|
return new Variant (g_variant_new_variant (val.Handle));
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_new_boolean (bool val);
|
|
|
|
|
|
|
|
public Variant (bool val) : this (g_variant_new_boolean (val)) {}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_new_byte (byte val);
|
|
|
|
|
|
|
|
public Variant (byte val) : this (g_variant_new_byte (val)) {}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_new_int16 (short val);
|
|
|
|
|
|
|
|
public Variant (short val) : this (g_variant_new_int16 (val)) {}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_new_uint16 (ushort val);
|
|
|
|
|
|
|
|
public Variant (ushort val) : this (g_variant_new_uint16 (val)) {}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_new_int32 (int val);
|
|
|
|
|
|
|
|
public Variant (int val) : this (g_variant_new_int32 (val)) {}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_new_uint32 (uint val);
|
|
|
|
|
|
|
|
public Variant (uint val) : this (g_variant_new_uint32 (val)) {}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_new_int64 (long val);
|
|
|
|
|
|
|
|
public Variant (long val) : this (g_variant_new_int64 (val)) {}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_new_uint64 (ulong val);
|
|
|
|
|
|
|
|
public Variant (ulong val) : this (g_variant_new_uint64 (val)) {}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_new_double (double val);
|
|
|
|
|
|
|
|
public Variant (double val) : this (g_variant_new_double (val)) {}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_new_string (IntPtr val);
|
|
|
|
|
|
|
|
public Variant (string val)
|
|
|
|
{
|
|
|
|
IntPtr native_val = Marshaller.StringToPtrGStrdup (val);
|
|
|
|
handle = g_variant_ref_sink (g_variant_new_string (native_val));
|
|
|
|
Marshaller.Free (native_val);
|
|
|
|
}
|
|
|
|
|
2014-05-15 19:50:53 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
static extern IntPtr g_variant_new_strv (IntPtr[] strv, IntPtr length);
|
|
|
|
|
|
|
|
public Variant (string[] strv)
|
|
|
|
{
|
|
|
|
IntPtr[] native = Marshaller.StringArrayToNullTermPointer (strv);
|
|
|
|
handle = g_variant_ref_sink (g_variant_new_strv (native, new IntPtr ((long) strv.Length)));
|
|
|
|
Marshaller.Free (native);
|
|
|
|
}
|
|
|
|
|
2014-05-15 19:52:46 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
static extern IntPtr g_variant_new_tuple (IntPtr[] children, UIntPtr n_children);
|
|
|
|
|
|
|
|
public static Variant NewTuple (Variant[] children)
|
|
|
|
{
|
|
|
|
if (children == null)
|
|
|
|
return new Variant (g_variant_new_tuple (null, new UIntPtr (0ul)));
|
|
|
|
|
|
|
|
IntPtr[] native = new IntPtr[children.Length];
|
|
|
|
for (int i = 0; i < children.Length; i++)
|
|
|
|
native[i] = children[i].Handle;
|
|
|
|
|
|
|
|
return new Variant (g_variant_new_tuple (native, new UIntPtr ((ulong) children.Length)));
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern bool g_variant_get_boolean (IntPtr handle);
|
|
|
|
|
|
|
|
public static explicit operator bool (Variant val)
|
|
|
|
{
|
|
|
|
return g_variant_get_boolean (val.Handle);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern byte g_variant_get_byte (IntPtr handle);
|
|
|
|
|
|
|
|
public static explicit operator byte (Variant val)
|
|
|
|
{
|
|
|
|
return g_variant_get_byte (val.Handle);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern short g_variant_get_int16 (IntPtr handle);
|
|
|
|
|
|
|
|
public static explicit operator short (Variant val)
|
|
|
|
{
|
|
|
|
return g_variant_get_int16 (val.Handle);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern ushort g_variant_get_uint16 (IntPtr handle);
|
|
|
|
|
|
|
|
public static explicit operator ushort (Variant val)
|
|
|
|
{
|
|
|
|
return g_variant_get_uint16 (val.Handle);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern int g_variant_get_int32 (IntPtr handle);
|
|
|
|
|
|
|
|
public static explicit operator int (Variant val)
|
|
|
|
{
|
|
|
|
return g_variant_get_int32 (val.Handle);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern uint g_variant_get_uint32 (IntPtr handle);
|
|
|
|
|
|
|
|
public static explicit operator uint (Variant val)
|
|
|
|
{
|
|
|
|
return g_variant_get_uint32 (val.Handle);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern long g_variant_get_int64 (IntPtr handle);
|
|
|
|
|
|
|
|
public static explicit operator long (Variant val)
|
|
|
|
{
|
|
|
|
return g_variant_get_int64 (val.Handle);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern ulong g_variant_get_uint64 (IntPtr handle);
|
|
|
|
|
|
|
|
public static explicit operator ulong (Variant val)
|
|
|
|
{
|
|
|
|
return g_variant_get_uint64 (val.Handle);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern double g_variant_get_double (IntPtr handle);
|
|
|
|
|
|
|
|
public static explicit operator double (Variant val)
|
|
|
|
{
|
|
|
|
return g_variant_get_double (val.Handle);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2011-01-22 18:52:38 +00:00
|
|
|
static extern IntPtr g_variant_get_string (IntPtr handle);
|
|
|
|
|
|
|
|
public static explicit operator string (Variant val)
|
|
|
|
{
|
|
|
|
IntPtr str = g_variant_get_string (val.Handle);
|
|
|
|
return str == IntPtr.Zero ? null : GLib.Marshaller.Utf8PtrToString (str);
|
|
|
|
}
|
2014-05-15 19:51:32 +00:00
|
|
|
|
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
static extern IntPtr g_variant_print (IntPtr variant, bool type_annotate);
|
|
|
|
|
|
|
|
public string Print (bool type_annotate)
|
|
|
|
{
|
|
|
|
IntPtr str = g_variant_print(handle, type_annotate);
|
|
|
|
return Marshaller.PtrToStringGFree (str);
|
|
|
|
}
|
2011-01-22 18:52:38 +00:00
|
|
|
}
|
|
|
|
}
|