using System; using System.Runtime.InteropServices; namespace Math.Gmp.Native { /// /// Represents a pointer to a string in unmanaged memory. /// /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1049:TypesThatOwnNativeResourcesShouldBeDisposable")] public struct char_ptr { /// /// Pointer to string in unmanaged memory. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr Pointer; /// /// Creates new string in unmanaged memory and initializes it with . /// /// The value of the new string. /// /// /// When done with the string, unmanaged memory must be released with free. /// /// public char_ptr(string str) { if (str == null) throw new ArgumentNullException("str"); Pointer = gmp_lib.allocate((size_t)(str.Length + 1)).ToIntPtr(); Marshal.Copy(System.Text.Encoding.ASCII.GetBytes(str), 0, Pointer, str.Length); Marshal.Copy(new Byte[] { 0 }, 0, (IntPtr)(Pointer.ToInt64() + str.Length), 1); } /// /// Creates new string using an already allocated string in unmanaged memory. /// /// Pointer to existing string in unmanaged memory. public char_ptr(IntPtr pointer) { this.Pointer = pointer; } /// /// Gets pointer to string in unmanaged memory. /// /// Pointer to string in unmanaged memory. public IntPtr ToIntPtr() { return Pointer; } /// /// Gets a null char_ptr. /// public static readonly char_ptr Zero = new char_ptr(IntPtr.Zero); /// /// Gets the .NET string equivalent of the unmanaged string. /// /// The .NET string equivalent of the unmanaged string. public override string ToString() { return Marshal.PtrToStringAnsi(Pointer); } /// /// Returns a value indicating whether this instance is equal to a specified object. /// /// An object to compare with this instance. /// True if is an instance of char_ptr and equals the value of this instance; otherwise, False. public override bool Equals(object obj) { if (!(obj is char_ptr)) return false; return Equals((char_ptr)obj); } /// /// Returns a value indicating whether this instance is equal to a specified char_ptr value. /// /// A char_ptr value to compare to this instance. /// True if has the same value as this instance; otherwise, False. public bool Equals(char_ptr other) { return Pointer == other.Pointer; } /// /// Returns the hash code for this instance. /// /// A 32-bit signed integer hash code. public override int GetHashCode() { return Pointer.GetHashCode(); } /// /// Gets a value that indicates whether the two argument values are equal. /// /// A char_ptr value. /// A char_ptr value. /// True if the two values are equal, and False otherwise. public static bool operator ==(char_ptr value1, char_ptr value2) { return value1.Equals(value2); } /// /// Gets a value that indicates whether the two argument values are different. /// /// A char_ptr value. /// A char_ptr value. /// True if the two values are different, and False otherwise. public static bool operator !=(char_ptr value1, char_ptr value2) { return !value1.Equals(value2); } } }