123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- namespace UTJ.FbxExporter
- {
- public class PinnedArray<T> : IDisposable, IEnumerable<T>, IEnumerable
- {
- public PinnedArray(int size = 0)
- {
- this.m_data = new T[size];
- this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
- }
- public PinnedArray(T[] data, bool clone = false)
- {
- this.m_data = ((!clone) ? data : ((T[])data.Clone()));
- this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned);
- }
- public int Length
- {
- get
- {
- return this.m_data.Length;
- }
- }
- public T this[int i]
- {
- get
- {
- return this.m_data[i];
- }
- set
- {
- this.m_data[i] = value;
- }
- }
- public T[] Array
- {
- get
- {
- return this.m_data;
- }
- }
- public IntPtr Pointer
- {
- get
- {
- return (this.m_data.Length != 0) ? this.m_gch.AddrOfPinnedObject() : IntPtr.Zero;
- }
- }
- public PinnedArray<T> Clone()
- {
- return new PinnedArray<T>((T[])this.m_data.Clone(), false);
- }
- public bool Assign(T[] source)
- {
- if (source != null && this.m_data.Length == source.Length)
- {
- System.Array.Copy(source, this.m_data, this.m_data.Length);
- return true;
- }
- return false;
- }
- public void Dispose()
- {
- this.Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (disposing && this.m_gch.IsAllocated)
- {
- this.m_gch.Free();
- }
- }
- public IEnumerator<T> GetEnumerator()
- {
- return (IEnumerator<T>)this.m_data.GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- public static implicit operator IntPtr(PinnedArray<T> v)
- {
- return (v != null) ? v.Pointer : IntPtr.Zero;
- }
- public static implicit operator T[](PinnedArray<T> v)
- {
- return (v != null) ? v.Array : null;
- }
- private T[] m_data;
- private GCHandle m_gch;
- }
- }
|