-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathCGALObject.cs
More file actions
162 lines (140 loc) · 4.32 KB
/
CGALObject.cs
File metadata and controls
162 lines (140 loc) · 4.32 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace CGALDotNet
{
/// <summary>
/// Base class for objects that referrence a CGAL object.
/// </summary>
public abstract class CGALObject : IDisposable
{
protected const string DLL_NAME = "CGALWrapper.dll";
protected const CallingConvention CDECL = CallingConvention.Cdecl;
/// <summary>
/// The pointer to the unmanged CGAL object.
/// </summary>
private IntPtr m_ptr;
/// <summary>
/// Default constructor.
/// </summary>
internal CGALObject()
{
m_ptr = IntPtr.Zero;
}
/// <summary>
/// Constructor taking a referrence to a CGAL object.
/// </summary>
/// <param name="ptr">A pointer to a CGAL object.</param>
internal CGALObject(IntPtr ptr)
{
m_ptr = ptr;
}
/// <summary>
/// The destuctor.
/// </summary>
~CGALObject()
{
Release();
}
/// <summary>
/// Has the object been disposed.
/// </summary>
public bool IsDisposed { get; private set; }
/// <summary>
/// Get the ptr to the CGAL object.
/// </summary>
internal IntPtr Ptr
{
get
{
CheckPtr();
return m_ptr;
}
private protected set
{
m_ptr = value;
}
}
/// <summary>
/// Dispose the CGAL object.
/// </summary>
public void Dispose()
{
Release();
GC.SuppressFinalize(this);
}
/// <summary>
/// Print some information about the object.
/// </summary>
/// <returns>Print some information about the object.</returns>
public override string ToString()
{
return String.Format("[CGALObject: Ptr={0}, IsDiposed={1}]", Ptr.ToInt64(), IsDisposed);
}
/// <summary>
/// Print the object into the console.
/// </summary>
public void Print()
{
var buider = new StringBuilder();
Print(buider);
Console.WriteLine(buider.ToString());
}
/// <summary>
/// Print the object into a string builder.
/// </summary>
/// <param name="builder">The builder to print into.</param>
public virtual void Print(StringBuilder builder)
{
builder.AppendLine(ToString());
}
/// <summary>
/// Release the CGAL object.
/// </summary>
private void Release()
{
if (!IsDisposed)
{
if(m_ptr != IntPtr.Zero)
ReleasePtr();
m_ptr = IntPtr.Zero;
IsDisposed = true;
}
}
/// <summary>
/// Swap the unmanaged pointer with another.
/// The old ptr will be released first.
/// </summary>
/// <param name="ptr">The new ptr. The old ptr will be released first.</param>
internal void Swap(IntPtr ptr)
{
if(IsDisposed)
throw new CGALUnmanagedResourcesReleasedExeception("Can not swap when object has beed disposed.");
if (m_ptr != IntPtr.Zero)
ReleasePtr(Ptr);
Ptr = ptr;
}
/// <summary>
/// Allow derived class to release the unmanaged memory.
/// </summary>
protected abstract void ReleasePtr();
/// <summary>
/// Allow derived class to release the unmanaged memory.
/// </summary>
protected virtual void ReleasePtr(IntPtr ptr)
{
throw new CGALUnmanagedResourcesNotReleasedExeception("ReleasePtr not implemented.");
}
/// <summary>
/// Check if the object is still valid.
/// </summary>
protected void CheckPtr()
{
if(IsDisposed)
throw new CGALUnmanagedResourcesReleasedExeception("Unmanaged resources have been released.");
if (m_ptr == IntPtr.Zero)
throw new CGALUnmanagedResourcesReleasedExeception("Unmanaged resources have not been created.");
}
}
}