~cosme/ubuntu/precise/freeimage/freeimage-3.15.1

« back to all changes in this revision

Viewing changes to Wrapper/FreeImage.NET/cs/Library/Structs/fi_handle.cs

  • Committer: Bazaar Package Importer
  • Author(s): Cosme Domínguez Díaz
  • Date: 2010-07-20 13:42:15 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100720134215-xt1454zaedv3b604
Tags: 3.13.1-0ubuntu1
* New upstream release. Closes: (LP: #607800)
 - Updated debian/freeimage-get-orig-source script.
 - Removing no longer necessary debian/patches/* and
   the patch system in debian/rules.
 - Updated debian/rules to work with the new Makefiles.
 - Drop from -O3 to -O2 and use lzma compression saves
   ~10 MB of free space. 
* lintian stuff
 - fixed debhelper-but-no-misc-depends
 - fixed ldconfig-symlink-missing-for-shlib

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ==========================================================
 
2
// FreeImage 3 .NET wrapper
 
3
// Original FreeImage 3 functions and .NET compatible derived functions
 
4
//
 
5
// Design and implementation by
 
6
// - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net)
 
7
// - Carsten Klein (cklein05@users.sourceforge.net)
 
8
//
 
9
// Contributors:
 
10
// - David Boland (davidboland@vodafone.ie)
 
11
//
 
12
// Main reference : MSDN Knowlede Base
 
13
//
 
14
// This file is part of FreeImage 3
 
15
//
 
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
 
24
// THIS DISCLAIMER.
 
25
//
 
26
// Use at your own risk!
 
27
// ==========================================================
 
28
 
 
29
// ==========================================================
 
30
// CVS
 
31
// $Revision: 1.7 $
 
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
// ==========================================================
 
35
 
 
36
using System;
 
37
using System.Collections.Generic;
 
38
using System.Runtime.InteropServices;
 
39
 
 
40
namespace FreeImageAPI.IO
 
41
{
 
42
        /// <summary>
 
43
        /// Wrapper for a custom handle.
 
44
        /// </summary>
 
45
        /// <remarks>
 
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.
 
48
        ///
 
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'
 
51
        /// structure.
 
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:
 
63
        /// 
 
64
        /// <code>
 
65
        /// using (fi_handle handle = new fi_handle(object))
 
66
        /// {
 
67
        ///     callSomeFunctions(handle);
 
68
        /// }
 
69
        /// </code>
 
70
        /// 
 
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.
 
78
        ///
 
79
        /// This way we can implement a <b>fi_handle</b> that works with managed an unmanaged
 
80
        /// code.
 
81
        /// </remarks>
 
82
        [Serializable, StructLayout(LayoutKind.Sequential)]
 
83
        public struct fi_handle : IComparable, IComparable<fi_handle>, IEquatable<fi_handle>, IDisposable
 
84
        {
 
85
                /// <summary>
 
86
                /// The handle to wrap.
 
87
                /// </summary>
 
88
                public IntPtr handle;
 
89
 
 
90
                /// <summary>
 
91
                /// Initializes a new instance wrapping a managed object.
 
92
                /// </summary>
 
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)
 
97
                {
 
98
                        if (obj == null)
 
99
                        {
 
100
                                throw new ArgumentNullException("obj");
 
101
                        }
 
102
                        GCHandle gch = GCHandle.Alloc(obj, GCHandleType.Normal);
 
103
                        handle = GCHandle.ToIntPtr(gch);
 
104
                }
 
105
 
 
106
                /// <summary>
 
107
                /// Tests whether two specified <see cref="fi_handle"/> structures are equivalent.
 
108
                /// </summary>
 
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>
 
111
                /// <returns>
 
112
                /// <b>true</b> if the two <see cref="fi_handle"/> structures are equal; otherwise, <b>false</b>.
 
113
                /// </returns>
 
114
                public static bool operator ==(fi_handle left, fi_handle right)
 
115
                {
 
116
                        return (left.handle == right.handle);
 
117
                }
 
118
 
 
119
                /// <summary>
 
120
                /// Tests whether two specified <see cref="fi_handle"/> structures are different.
 
121
                /// </summary>
 
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>
 
124
                /// <returns>
 
125
                /// <b>true</b> if the two <see cref="fi_handle"/> structures are different; otherwise, <b>false</b>.
 
126
                /// </returns>
 
127
                public static bool operator !=(fi_handle left, fi_handle right)
 
128
                {
 
129
                        return (left.handle != right.handle);
 
130
                }
 
131
 
 
132
                /// <summary>
 
133
                /// Gets whether the pointer is a null pointer.
 
134
                /// </summary>
 
135
                public bool IsNull
 
136
                {
 
137
                        get
 
138
                        {
 
139
                                return (handle == IntPtr.Zero);
 
140
                        }
 
141
                }
 
142
 
 
143
                /// <summary>
 
144
                /// Returns the object assigned to the handle in case this instance
 
145
                /// was created by managed code.
 
146
                /// </summary>
 
147
                /// <returns><see cref="Object"/> assigned to this handle or null on failure.</returns>
 
148
                internal object GetObject()
 
149
                {
 
150
                        object result = null;
 
151
                        if (handle != IntPtr.Zero)
 
152
                        {
 
153
                                try
 
154
                                {
 
155
                                        result = GCHandle.FromIntPtr(handle).Target;
 
156
                                }
 
157
                                catch
 
158
                                {
 
159
                                }
 
160
                        }
 
161
                        return result;
 
162
                }
 
163
 
 
164
                /// <summary>
 
165
                /// Converts the numeric value of the <see cref="fi_handle"/> object
 
166
                /// to its equivalent string representation.
 
167
                /// </summary>
 
168
                /// <returns>The string representation of the value of this instance.</returns>
 
169
                public override string ToString()
 
170
                {
 
171
                        return handle.ToString();
 
172
                }
 
173
 
 
174
                /// <summary>
 
175
                /// Returns a hash code for this <see cref="fi_handle"/> structure.
 
176
                /// </summary>
 
177
                /// <returns>An integer value that specifies the hash code for this <see cref="fi_handle"/>.</returns>
 
178
                public override int GetHashCode()
 
179
                {
 
180
                        return handle.GetHashCode();
 
181
                }
 
182
 
 
183
                /// <summary>
 
184
                /// Tests whether the specified object is a <see cref="fi_handle"/> structure
 
185
                /// and is equivalent to this <see cref="fi_handle"/> structure.
 
186
                /// </summary>
 
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)
 
191
                {
 
192
                        return ((obj is fi_handle) && (this == ((fi_handle)obj)));
 
193
                }
 
194
 
 
195
                /// <summary>
 
196
                /// Indicates whether the current object is equal to another object of the same type.
 
197
                /// </summary>
 
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)
 
201
                {
 
202
                        return (this == other);
 
203
                }
 
204
 
 
205
                /// <summary>
 
206
                /// Compares this instance with a specified <see cref="Object"/>.
 
207
                /// </summary>
 
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)
 
212
                {
 
213
                        if (obj == null)
 
214
                        {
 
215
                                return 1;
 
216
                        }
 
217
                        if (!(obj is fi_handle))
 
218
                        {
 
219
                                throw new ArgumentException("obj");
 
220
                        }
 
221
                        return CompareTo((fi_handle)obj);
 
222
                }
 
223
 
 
224
                /// <summary>
 
225
                /// Compares this instance with a specified <see cref="fi_handle"/> object.
 
226
                /// </summary>
 
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)
 
231
                {
 
232
                        return handle.ToInt64().CompareTo(other.handle.ToInt64());
 
233
                }
 
234
 
 
235
                /// <summary>
 
236
                /// Releases all resources used by the instance.
 
237
                /// </summary>
 
238
                public void Dispose()
 
239
                {
 
240
                        if (this.handle != IntPtr.Zero)
 
241
                        {
 
242
                                try
 
243
                                {
 
244
                                        GCHandle.FromIntPtr(handle).Free();
 
245
                                }
 
246
                                catch
 
247
                                {
 
248
                                }
 
249
                                finally
 
250
                                {
 
251
                                        this.handle = IntPtr.Zero;
 
252
                                }
 
253
                        }
 
254
                }
 
255
        }
 
256
}
 
 
b'\\ No newline at end of file'