IntPtr.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // System.IntPtr.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // Maintainer:
  8. // Michael Lambert, [email protected]
  9. //
  10. // (C) Ximian, Inc. http://www.ximian.com
  11. //
  12. // Remarks: Requires '/unsafe' compiler option. This class uses void*,
  13. // in overloaded constructors, conversion, and cast members in
  14. // the public interface. Using pointers is not valid CLS and
  15. // the methods in question have been marked with the
  16. // CLSCompliant attribute that avoid compiler warnings.
  17. //
  18. // FIXME: How do you specify a native int in C#? I am going to have to do some figuring out
  19. //
  20. using System;
  21. using System.Runtime.Serialization;
  22. [
  23. assembly: System.CLSCompliant(true)
  24. ]
  25. namespace System {
  26. [
  27. CLSCompliant(true)
  28. ]
  29. public unsafe struct IntPtr : ISerializable {
  30. private void *value;
  31. public static IntPtr Zero;
  32. static IntPtr ()
  33. {
  34. Zero.value = (void *) 0;
  35. }
  36. public IntPtr (int i32)
  37. {
  38. value = (void *) i32;
  39. }
  40. public IntPtr (long i64)
  41. {
  42. value = (void *) i64;
  43. }
  44. [
  45. CLSCompliant(false)
  46. ]
  47. unsafe public IntPtr (void *ptr)
  48. {
  49. value = ptr;
  50. }
  51. public static int Size {
  52. get {
  53. return sizeof (void *);
  54. }
  55. }
  56. public void GetObjectData (SerializationInfo si, StreamingContext sc)
  57. {
  58. if( si == null )
  59. throw new ArgumentNullException( "si" );
  60. si.AddValue("value", (long) value);
  61. }
  62. public override bool Equals (object o)
  63. {
  64. if (!(o is System.IntPtr))
  65. return false;
  66. return ((IntPtr) o).value == value;
  67. }
  68. public override int GetHashCode ()
  69. {
  70. return (int) value;
  71. }
  72. public int ToInt32 ()
  73. {
  74. return (int) value;
  75. }
  76. public long ToInt64 ()
  77. {
  78. return (long) value;
  79. }
  80. [
  81. CLSCompliant(false)
  82. ]
  83. unsafe public void *ToPointer ()
  84. {
  85. return value;
  86. }
  87. override public string ToString ()
  88. {
  89. if (Size == 4)
  90. return ((int) value).ToString ();
  91. else
  92. return ((long) value).ToString ();
  93. }
  94. public static bool operator == (IntPtr a, IntPtr b)
  95. {
  96. return (a.value == b.value);
  97. }
  98. public static bool operator != (IntPtr a, IntPtr b)
  99. {
  100. return (a.value != b.value);
  101. }
  102. public static explicit operator IntPtr (int value)
  103. {
  104. return new IntPtr (value);
  105. }
  106. public static explicit operator IntPtr (long value)
  107. {
  108. return new IntPtr (value);
  109. }
  110. [
  111. CLSCompliant(false)
  112. ]
  113. unsafe public static explicit operator IntPtr (void *value)
  114. {
  115. return new IntPtr (value);
  116. }
  117. public static explicit operator int (IntPtr value)
  118. {
  119. return (int) value.value;
  120. }
  121. public static explicit operator long (IntPtr value)
  122. {
  123. return (long) value.value;
  124. }
  125. [
  126. CLSCompliant(false)
  127. ]
  128. unsafe public static explicit operator void * (IntPtr value)
  129. {
  130. return value.value;
  131. }
  132. }
  133. }