// This file is auto-generated by alef. DO NOT EDIT.
#nullable enable

using System;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
{% if has_methods %}
using System.Text.Json;
using System.Text.Json.Serialization;
{% endif %}
{% if needs_list %}
using System.Collections.Generic;
{% endif %}
{% if needs_async %}
using System.Threading.Tasks;
{% endif %}
{% if needs_streaming %}
using System.Threading;
using System.Runtime.CompilerServices;
{% endif %}

namespace {{ namespace }};

internal sealed class {{ class_name }}SafeHandle : SafeHandle
{
    internal {{ class_name }}SafeHandle(IntPtr handle) : base(IntPtr.Zero, true)
    {
        SetHandle(handle);
    }

    public override bool IsInvalid => handle == IntPtr.Zero;

    protected override bool ReleaseHandle()
    {
        NativeMethods.{{ free_method }}(handle);
        return true;
    }
}
{% if doc -%}

/// <summary>
{%- for line in doc_lines %}
/// {{ line }}
{%- endfor %}
/// </summary>
{%- endif %}

public sealed class {{ class_name }} : IDisposable
{
{%- if has_methods %}
    private static readonly JsonSerializerOptions JsonOptions = new()
    {
        Converters = { new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseLower) },
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
    };
{%- endif %}

    private readonly {{ class_name }}SafeHandle _safeHandle;

    internal {{ class_name }}(IntPtr handle)
    {
        _safeHandle = new {{ class_name }}SafeHandle(handle);
    }

    internal IntPtr Handle => _safeHandle.DangerousGetHandle();

    public void Dispose() => _safeHandle.Dispose();
