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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
// ==========================================================
// FreeImage 3 .NET wrapper
// Original FreeImage 3 functions and .NET compatible derived functions
//
// Design and implementation by
// - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net)
// - Carsten Klein (cklein05@users.sourceforge.net)
//
// Contributors:
// - David Boland (davidboland@vodafone.ie)
//
// Main reference : MSDN Knowlede Base
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
// ==========================================================
// CVS
// $Revision: 1.7 $
// $Date: 2009/02/20 07:41:08 $
// $Id: fi_handle.cs,v 1.7 2009/02/20 07:41:08 cklein05 Exp $
// ==========================================================
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace FreeImageAPI.IO
{
/// <summary>
/// Wrapper for a custom handle.
/// </summary>
/// <remarks>
/// The <b>fi_handle</b> of FreeImage in C++ is a simple pointer, but in .NET
/// it's not that simple. This wrapper uses fi_handle in two different ways.
///
/// We implement a new plugin and FreeImage gives us a handle (pointer) that
/// we can simply pass through to the given functions in a 'FreeImageIO'
/// structure.
/// But when we want to use LoadFromhandle or SaveToHandle we need
/// a fi_handle (that we receive again in our own functions).
/// This handle is for example a stream (see LoadFromStream / SaveToStream)
/// that we want to work with. To know which stream a read/write is meant for
/// we could use a hash value that the wrapper itself handles or we can
/// go the unmanaged way of using a handle.
/// Therefor we use a <see cref="GCHandle"/> to receive a unique pointer that we can
/// convert back into a .NET object.
/// When the <b>fi_handle</b> instance is no longer needed the instance must be disposed
/// by the creater manually! It is recommended to use the <c>using</c> statement to
/// be sure the instance is always disposed:
///
/// <code>
/// using (fi_handle handle = new fi_handle(object))
/// {
/// callSomeFunctions(handle);
/// }
/// </code>
///
/// What does that mean?
/// If we get a <b>fi_handle</b> from unmanaged code we get a pointer to unmanaged
/// memory that we do not have to care about, and just pass ist back to FreeImage.
/// If we have to create a handle our own we use the standard constructur
/// that fills the <see cref="IntPtr"/> with an pointer that represents the given object.
/// With calling <see cref="GetObject"/> the <see cref="IntPtr"/> is used to retrieve the original
/// object we passed through the constructor.
///
/// This way we can implement a <b>fi_handle</b> that works with managed an unmanaged
/// code.
/// </remarks>
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct fi_handle : IComparable, IComparable<fi_handle>, IEquatable<fi_handle>, IDisposable
{
/// <summary>
/// The handle to wrap.
/// </summary>
public IntPtr handle;
/// <summary>
/// Initializes a new instance wrapping a managed object.
/// </summary>
/// <param name="obj">The object to wrap.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="obj"/> is null.</exception>
public fi_handle(object obj)
{
if (obj == null)
{
throw new ArgumentNullException("obj");
}
GCHandle gch = GCHandle.Alloc(obj, GCHandleType.Normal);
handle = GCHandle.ToIntPtr(gch);
}
/// <summary>
/// Tests whether two specified <see cref="fi_handle"/> structures are equivalent.
/// </summary>
/// <param name="left">The <see cref="fi_handle"/> that is to the left of the equality operator.</param>
/// <param name="right">The <see cref="fi_handle"/> that is to the right of the equality operator.</param>
/// <returns>
/// <b>true</b> if the two <see cref="fi_handle"/> structures are equal; otherwise, <b>false</b>.
/// </returns>
public static bool operator ==(fi_handle left, fi_handle right)
{
return (left.handle == right.handle);
}
/// <summary>
/// Tests whether two specified <see cref="fi_handle"/> structures are different.
/// </summary>
/// <param name="left">The <see cref="fi_handle"/> that is to the left of the inequality operator.</param>
/// <param name="right">The <see cref="fi_handle"/> that is to the right of the inequality operator.</param>
/// <returns>
/// <b>true</b> if the two <see cref="fi_handle"/> structures are different; otherwise, <b>false</b>.
/// </returns>
public static bool operator !=(fi_handle left, fi_handle right)
{
return (left.handle != right.handle);
}
/// <summary>
/// Gets whether the pointer is a null pointer.
/// </summary>
public bool IsNull
{
get
{
return (handle == IntPtr.Zero);
}
}
/// <summary>
/// Returns the object assigned to the handle in case this instance
/// was created by managed code.
/// </summary>
/// <returns><see cref="Object"/> assigned to this handle or null on failure.</returns>
internal object GetObject()
{
object result = null;
if (handle != IntPtr.Zero)
{
try
{
result = GCHandle.FromIntPtr(handle).Target;
}
catch
{
}
}
return result;
}
/// <summary>
/// Converts the numeric value of the <see cref="fi_handle"/> object
/// to its equivalent string representation.
/// </summary>
/// <returns>The string representation of the value of this instance.</returns>
public override string ToString()
{
return handle.ToString();
}
/// <summary>
/// Returns a hash code for this <see cref="fi_handle"/> structure.
/// </summary>
/// <returns>An integer value that specifies the hash code for this <see cref="fi_handle"/>.</returns>
public override int GetHashCode()
{
return handle.GetHashCode();
}
/// <summary>
/// Tests whether the specified object is a <see cref="fi_handle"/> structure
/// and is equivalent to this <see cref="fi_handle"/> structure.
/// </summary>
/// <param name="obj">The object to test.</param>
/// <returns><b>true</b> if <paramref name="obj"/> is a <see cref="fi_handle"/> structure
/// equivalent to this <see cref="fi_handle"/> structure; otherwise, <b>false</b>.</returns>
public override bool Equals(object obj)
{
return ((obj is fi_handle) && (this == ((fi_handle)obj)));
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>True if the current object is equal to the other parameter; otherwise, <b>false</b>.</returns>
public bool Equals(fi_handle other)
{
return (this == other);
}
/// <summary>
/// Compares this instance with a specified <see cref="Object"/>.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>A 32-bit signed integer indicating the lexical relationship between the two comparands.</returns>
/// <exception cref="ArgumentException"><paramref name="obj"/> is not a <see cref="fi_handle"/>.</exception>
public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
if (!(obj is fi_handle))
{
throw new ArgumentException("obj");
}
return CompareTo((fi_handle)obj);
}
/// <summary>
/// Compares this instance with a specified <see cref="fi_handle"/> object.
/// </summary>
/// <param name="other">A <see cref="fi_handle"/> to compare.</param>
/// <returns>A signed number indicating the relative values of this instance
/// and <paramref name="other"/>.</returns>
public int CompareTo(fi_handle other)
{
return handle.ToInt64().CompareTo(other.handle.ToInt64());
}
/// <summary>
/// Releases all resources used by the instance.
/// </summary>
public void Dispose()
{
if (this.handle != IntPtr.Zero)
{
try
{
GCHandle.FromIntPtr(handle).Free();
}
catch
{
}
finally
{
this.handle = IntPtr.Zero;
}
}
}
}
}
|