---
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 Counter : IDisposable
    {
        private IntPtr _handle;

        internal Counter(IntPtr handle)
        {
            _handle = handle;
        }

        public int TryGetPositive()
        {
            ThrowIfDisposed();
            FfiBuf _buf = NativeMethods.CounterTryGetPositive(_handle);
            try
            {
                var reader = new WireReader(_buf);
                if (reader.ReadU8() != 0) throw new BoltException(reader.ReadString());
                return reader.ReadI32();
            }
            finally
            {
                NativeMethods.FreeBuf(_buf);
            }
        }

        internal IntPtr RawHandle => _handle;

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

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

        ~Counter()
        {
            Dispose();
        }
    }
}
