forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object.cs
81 lines (69 loc) · 2.31 KB
/
Object.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
namespace Intel.RealSense.Base
{
using System;
using System.Diagnostics;
/// <summary>
/// Base class for disposable objects with native resources
/// </summary>
public abstract class Object : IDisposable
{
// TODO: rename, kept for backwards compatiblity
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal RefCountedDeleterHandle m_instance;
/// <summary>
/// Initializes a new instance of the <see cref="Object"/> class.
/// </summary>
/// <param name="ptr">native pointer</param>
/// <param name="deleter">optional deleter</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="ptr"/> is null</exception>
protected Object(IntPtr ptr, Deleter deleter)
{
if (ptr == IntPtr.Zero)
{
throw new ArgumentNullException(nameof(ptr));
}
m_instance = new RefCountedDeleterHandle(ptr, deleter);
}
protected Object(Object other)
{
if (other.m_instance == null)
{
throw new ArgumentNullException(nameof(other));
}
m_instance = other.m_instance;
m_instance.Retain();
}
/// <summary>
/// Gets the native handle
/// </summary>
/// <exception cref="ObjectDisposedException">Thrown when <see cref="DeleterHandle.IsInvalid"/></exception>
public IntPtr Handle
{
get
{
if (m_instance.IsInvalid)
{
throw new ObjectDisposedException(GetType().Name);
}
return m_instance.Handle;
}
}
/// <inheritdoc/>
public void Dispose()
{
this.Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
m_instance.Dispose();
//Reset the instance ref to an invalid handle
m_instance = new RefCountedDeleterHandle();
}
internal void Reset(IntPtr ptr, Deleter deleter)
{
m_instance.Reset(ptr, deleter);
}
}
}