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

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

        public int Divide(int b)
        {
            ThrowIfDisposed();
            FfiBuf _buf = NativeMethods.CalculatorDivide(_handle, b);
            try
            {
                var reader = new WireReader(_buf);
                if (reader.ReadU8() != 0) throw new MathErrorException(MathErrorWire.Decode(reader));
                return reader.ReadI32();
            }
            finally
            {
                NativeMethods.FreeBuf(_buf);
            }
        }

        internal IntPtr RawHandle => _handle;

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

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

        ~Calculator()
        {
            Dispose();
        }
    }
}
