using System;
using System.Runtime.InteropServices;
using NetVips.Internal;
namespace NetVips;
///
/// Manage a .
///
public class VipsObject : GObject
{
///
/// Attach a post-close delegate. This is called on finalization.
///
///
/// Useful for e.g. deleting the file associated with a temp image.
///
public event Action OnPostClose
{
add => SignalConnect("postclose", value);
remove => SignalHandlersDisconnectByFunc(value);
}
///
internal VipsObject(nint pointer) : base(pointer)
{
}
///
/// Print a table of all active libvips objects. Handy for debugging.
///
internal static void PrintAll()
{
GC.Collect();
Internal.VipsObject.PrintAll();
}
///
/// slow! eeeeew.
///
/// Arg to fetch.
/// The pspec for this arg.
private GParamSpec.Struct? GetPspec(string name)
{
var argument = Internal.VipsObject.GetArgument(this, name, out var pspec, out _, out _);
return argument != 0
? default(GParamSpec.Struct?)
: Marshal.PtrToStructure(pspec);
}
///
/// Get a GObject property.
///
///
/// The value of the property is converted to a C# value.
///
/// Arg to fetch.
/// The GObject property.
internal object Get(string name)
{
var pspec = GetPspec(name);
if (!pspec.HasValue)
{
throw new VipsException("Property not found.");
}
var gtype = pspec.Value.ValueType;
using var gv = new GValue();
gv.SetType(gtype);
// this will add a reference for GObject properties, that ref will be
// unreferenced when the GValue is finalized
Internal.GObject.GetProperty(this, name, ref gv.Struct);
return gv.Get();
}
///
/// Set a GObject property. The value is converted to the property type, if possible.
///
/// The name of the property to set.
/// The value.
/// The GType of the property.
internal void Set(nint gtype, string name, object value)
{
using var gv = new GValue();
gv.SetType(gtype);
gv.Set(value);
Internal.GObject.SetProperty(this, name, in gv.Struct);
}
///
/// Set a series of properties using a string.
///
///
/// For example:
/// "fred=12, tile"
/// "[fred=12]"
///
/// Arguments as a string.
/// on success; otherwise, .
internal bool SetString(string stringOptions)
{
var result = Internal.VipsObject.SetFromString(this, stringOptions);
return result == 0;
}
///
/// Get the GType of a GObject property.
///
/// The name of the GType to get the type of.
/// A new instance of initialized to the GType or
/// if the property does not exist.
public nint GetTypeOf(string name)
{
var pspec = GetPspec(name);
if (!pspec.HasValue)
{
// need to clear any error, this is horrible
Vips.ErrorClear();
return IntPtr.Zero;
}
return pspec.Value.ValueType;
}
///
/// Get the blurb for a GObject property.
///
/// Arg to fetch.
/// The blurb.
public string GetBlurb(string name)
{
var pspec = GetPspec(name);
if (!pspec.HasValue)
{
return null;
}
var pspecValue = pspec.Value;
return Marshal.PtrToStringAnsi(GParamSpec.GetBlurb(in pspecValue));
}
///
/// Get the description of a GObject.
///
/// The description of a GObject.
public string GetDescription()
{
return Marshal.PtrToStringAnsi(Internal.VipsObject.GetDescription(this));
}
}