---
source: boltffi_bindgen/src/render/csharp/templates.rs
expression: template.render().unwrap()
---
// <auto-generated>
// This file was generated by BoltFFI. Do not edit.
// </auto-generated>
#nullable enable

using System;
using System.Threading;

namespace Demo
{
    public sealed class Validator : IDisposable
    {
        private IntPtr _handle;

        // Private handle-adopting ctor; used only by chained `: this(...)` and the static factories below.
        private Validator(IntPtr handle)
        {
            _handle = handle;
        }

        public void Validate()
        {
            ThrowIfDisposed();
            FfiBuf _buf = NativeMethods.ValidatorValidate(_handle);
            try
            {
                var reader = new WireReader(_buf);
                if (reader.ReadU8() != 0) throw new BoltException(reader.ReadString());
            }
            finally
            {
                NativeMethods.FreeBuf(_buf);
            }
        }

        internal IntPtr RawHandle => _handle;

        private void ThrowIfDisposed()
        {
            if (_handle == IntPtr.Zero) throw new ObjectDisposedException(nameof(Validator));
        }

        public void Dispose()
        {
            IntPtr handle = Interlocked.Exchange(ref _handle, IntPtr.Zero);
            if (handle == IntPtr.Zero) return;
            NativeMethods.ValidatorFree(handle);
            GC.SuppressFinalize(this);
        }

        ~Validator()
        {
            Dispose();
        }
    }
}
