Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ResourcesTracker.cs #1110

Merged
merged 1 commit into from
Dec 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/OpenCvSharp/Util/ResourcesTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;

namespace OpenCvSharp.Util
{
/// <summary>
/// Used for manage the resources of OpenCVSharp, like Mat, MatExpr, etc.
/// </summary>
public class ResourcesTracker : IDisposable
{
private ISet<DisposableObject> trackedObjects = new HashSet<DisposableObject>();
private object asyncLock = new object();

/// <summary>
/// Trace the object obj, and return it
/// </summary>
/// <typeparam name="TCV"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public TCV T<TCV>(TCV obj) where TCV : DisposableObject
{
if (obj == null)
{
return obj;
}
lock (asyncLock)
{
trackedObjects.Add(obj);
}
return obj;
}

/// <summary>
/// Trace an array of objects , and return them
/// </summary>
/// <typeparam name="TCV"></typeparam>
/// <param name="objs"></param>
/// <returns></returns>

public TCV[] T<TCV>(TCV[] objs) where TCV : DisposableObject
{
foreach (var obj in objs)
{
T(obj);
}
return objs;
}

/// <summary>
/// Create a new Mat instance, and trace it
/// </summary>
/// <returns></returns>

public Mat NewMat()
{
return T(new Mat());
}

/// <summary>
/// Create a new Mat instance, and trace it
/// </summary>
/// <param name="size">size</param>
/// <param name="matType">matType</param>
/// <param name="scalar">scalar</param>
/// <returns></returns>

public Mat NewMat(Size size, MatType matType, Scalar scalar)
{
return T(new Mat(size, matType, scalar));
}

/// <summary>
/// Dispose all traced objects
/// </summary>

public void Dispose()
{
lock (asyncLock)
{
foreach (var obj in trackedObjects)
{
if (obj.IsDisposed == false)
{
obj.Dispose();
}
}
trackedObjects.Clear();
}
}
}
}