using System.Runtime.InteropServices; using OpenCvSharp.Internal.Util; namespace OpenCvSharp.Internal.Vectors; /// /// public class VectorOfRect : DisposableCvObject, IStdVector { /// /// Constructor /// public VectorOfRect() { ptr = NativeMethods.vector_Rect_new1(); } /// /// Constructor /// /// public VectorOfRect(nuint size) { if (size < 0) throw new ArgumentOutOfRangeException(nameof(size)); ptr = NativeMethods.vector_Rect_new2(size); } /// /// Constructor /// /// public VectorOfRect(IEnumerable data) { if (data is null) throw new ArgumentNullException(nameof(data)); var array = data.ToArray(); ptr = NativeMethods.vector_Rect_new3(array, (nuint)array.Length); } /// /// Releases unmanaged resources /// protected override void DisposeUnmanaged() { NativeMethods.vector_Rect_delete(ptr); base.DisposeUnmanaged(); } /// /// vector.size() /// public int Size { get { var res = NativeMethods.vector_Rect_getSize(ptr); GC.KeepAlive(this); return (int)res; } } /// /// &vector[0] /// public IntPtr ElemPtr { get { var res = NativeMethods.vector_Rect_getPointer(ptr); GC.KeepAlive(this); return res; } } /// /// Converts std::vector to managed array /// /// public Rect[] ToArray() { var size = Size; if (size == 0) { return Array.Empty(); } var dst = new Rect[size]; using (var dstPtr = new ArrayAddress1(dst)) { long bytesToCopy = Marshal.SizeOf() * dst.Length; unsafe { Buffer.MemoryCopy(ElemPtr.ToPointer(), dstPtr.Pointer.ToPointer(), bytesToCopy, bytesToCopy); } } GC.KeepAlive(this); // ElemPtr is IntPtr to memory held by this object, so // make sure we are not disposed until finished with copy. return dst; } }