예제 #1
0
        public Comparator(string name, Comparison <NativeArray> comparison)
        {
            GCHandle selfHandle = default(GCHandle);

            try
            {
                var utf = Encoding.UTF8.GetBytes(name);
                NativeName = Marshal.AllocHGlobal(utf.Length + 1);
                Marshal.Copy(utf, 0, NativeName, utf.Length);

                Comparison = comparison;

                selfHandle = GCHandle.Alloc(this);

                _handle = LevelDBInterop.leveldb_comparator_create(
                    GCHandle.ToIntPtr(selfHandle),
                    Marshal.GetFunctionPointerForDelegate(Destructor),
                    Marshal.GetFunctionPointerForDelegate(Compare),
                    Marshal.GetFunctionPointerForDelegate(GetName));
                if (_handle == IntPtr.Zero)
                {
                    throw new ApplicationException("Failed to initialize LevelDB comparator");
                }
            }
            catch
            {
                if (selfHandle != default(GCHandle))
                {
                    selfHandle.Free();
                }
                if (NativeName != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(NativeName);
                }
                throw;
            }
        }