1
// ==========================================================
2
// FreeImage 3 .NET wrapper
3
// Original FreeImage 3 functions and .NET compatible derived functions
5
// Design and implementation by
6
// - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net)
7
// - Carsten Klein (cklein05@users.sourceforge.net)
10
// - David Boland (davidboland@vodafone.ie)
12
// Main reference : MSDN Knowlede Base
14
// This file is part of FreeImage 3
16
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
17
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
18
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
19
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
20
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
21
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
22
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
23
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
26
// Use at your own risk!
27
// ==========================================================
29
// ==========================================================
32
// $Date: 2009/02/20 07:41:08 $
33
// $Id: fi_handle.cs,v 1.7 2009/02/20 07:41:08 cklein05 Exp $
34
// ==========================================================
37
using System.Collections.Generic;
38
using System.Runtime.InteropServices;
40
namespace FreeImageAPI.IO
43
/// Wrapper for a custom handle.
46
/// The <b>fi_handle</b> of FreeImage in C++ is a simple pointer, but in .NET
47
/// it's not that simple. This wrapper uses fi_handle in two different ways.
49
/// We implement a new plugin and FreeImage gives us a handle (pointer) that
50
/// we can simply pass through to the given functions in a 'FreeImageIO'
52
/// But when we want to use LoadFromhandle or SaveToHandle we need
53
/// a fi_handle (that we receive again in our own functions).
54
/// This handle is for example a stream (see LoadFromStream / SaveToStream)
55
/// that we want to work with. To know which stream a read/write is meant for
56
/// we could use a hash value that the wrapper itself handles or we can
57
/// go the unmanaged way of using a handle.
58
/// Therefor we use a <see cref="GCHandle"/> to receive a unique pointer that we can
59
/// convert back into a .NET object.
60
/// When the <b>fi_handle</b> instance is no longer needed the instance must be disposed
61
/// by the creater manually! It is recommended to use the <c>using</c> statement to
62
/// be sure the instance is always disposed:
65
/// using (fi_handle handle = new fi_handle(object))
67
/// callSomeFunctions(handle);
71
/// What does that mean?
72
/// If we get a <b>fi_handle</b> from unmanaged code we get a pointer to unmanaged
73
/// memory that we do not have to care about, and just pass ist back to FreeImage.
74
/// If we have to create a handle our own we use the standard constructur
75
/// that fills the <see cref="IntPtr"/> with an pointer that represents the given object.
76
/// With calling <see cref="GetObject"/> the <see cref="IntPtr"/> is used to retrieve the original
77
/// object we passed through the constructor.
79
/// This way we can implement a <b>fi_handle</b> that works with managed an unmanaged
82
[Serializable, StructLayout(LayoutKind.Sequential)]
83
public struct fi_handle : IComparable, IComparable<fi_handle>, IEquatable<fi_handle>, IDisposable
86
/// The handle to wrap.
91
/// Initializes a new instance wrapping a managed object.
93
/// <param name="obj">The object to wrap.</param>
94
/// <exception cref="ArgumentNullException">
95
/// <paramref name="obj"/> is null.</exception>
96
public fi_handle(object obj)
100
throw new ArgumentNullException("obj");
102
GCHandle gch = GCHandle.Alloc(obj, GCHandleType.Normal);
103
handle = GCHandle.ToIntPtr(gch);
107
/// Tests whether two specified <see cref="fi_handle"/> structures are equivalent.
109
/// <param name="left">The <see cref="fi_handle"/> that is to the left of the equality operator.</param>
110
/// <param name="right">The <see cref="fi_handle"/> that is to the right of the equality operator.</param>
112
/// <b>true</b> if the two <see cref="fi_handle"/> structures are equal; otherwise, <b>false</b>.
114
public static bool operator ==(fi_handle left, fi_handle right)
116
return (left.handle == right.handle);
120
/// Tests whether two specified <see cref="fi_handle"/> structures are different.
122
/// <param name="left">The <see cref="fi_handle"/> that is to the left of the inequality operator.</param>
123
/// <param name="right">The <see cref="fi_handle"/> that is to the right of the inequality operator.</param>
125
/// <b>true</b> if the two <see cref="fi_handle"/> structures are different; otherwise, <b>false</b>.
127
public static bool operator !=(fi_handle left, fi_handle right)
129
return (left.handle != right.handle);
133
/// Gets whether the pointer is a null pointer.
139
return (handle == IntPtr.Zero);
144
/// Returns the object assigned to the handle in case this instance
145
/// was created by managed code.
147
/// <returns><see cref="Object"/> assigned to this handle or null on failure.</returns>
148
internal object GetObject()
150
object result = null;
151
if (handle != IntPtr.Zero)
155
result = GCHandle.FromIntPtr(handle).Target;
165
/// Converts the numeric value of the <see cref="fi_handle"/> object
166
/// to its equivalent string representation.
168
/// <returns>The string representation of the value of this instance.</returns>
169
public override string ToString()
171
return handle.ToString();
175
/// Returns a hash code for this <see cref="fi_handle"/> structure.
177
/// <returns>An integer value that specifies the hash code for this <see cref="fi_handle"/>.</returns>
178
public override int GetHashCode()
180
return handle.GetHashCode();
184
/// Tests whether the specified object is a <see cref="fi_handle"/> structure
185
/// and is equivalent to this <see cref="fi_handle"/> structure.
187
/// <param name="obj">The object to test.</param>
188
/// <returns><b>true</b> if <paramref name="obj"/> is a <see cref="fi_handle"/> structure
189
/// equivalent to this <see cref="fi_handle"/> structure; otherwise, <b>false</b>.</returns>
190
public override bool Equals(object obj)
192
return ((obj is fi_handle) && (this == ((fi_handle)obj)));
196
/// Indicates whether the current object is equal to another object of the same type.
198
/// <param name="other">An object to compare with this object.</param>
199
/// <returns>True if the current object is equal to the other parameter; otherwise, <b>false</b>.</returns>
200
public bool Equals(fi_handle other)
202
return (this == other);
206
/// Compares this instance with a specified <see cref="Object"/>.
208
/// <param name="obj">An object to compare with this instance.</param>
209
/// <returns>A 32-bit signed integer indicating the lexical relationship between the two comparands.</returns>
210
/// <exception cref="ArgumentException"><paramref name="obj"/> is not a <see cref="fi_handle"/>.</exception>
211
public int CompareTo(object obj)
217
if (!(obj is fi_handle))
219
throw new ArgumentException("obj");
221
return CompareTo((fi_handle)obj);
225
/// Compares this instance with a specified <see cref="fi_handle"/> object.
227
/// <param name="other">A <see cref="fi_handle"/> to compare.</param>
228
/// <returns>A signed number indicating the relative values of this instance
229
/// and <paramref name="other"/>.</returns>
230
public int CompareTo(fi_handle other)
232
return handle.ToInt64().CompareTo(other.handle.ToInt64());
236
/// Releases all resources used by the instance.
238
public void Dispose()
240
if (this.handle != IntPtr.Zero)
244
GCHandle.FromIntPtr(handle).Free();
251
this.handle = IntPtr.Zero;
b'\\ No newline at end of file'