The Labs \ Source Viewer \ SSCLI \ System \ IntPtr

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  IntPtr
  18. **
  19. **
  20. ** Purpose: Platform independent integer
  21. **
  22. **
  23. ===========================================================*/
  24. namespace System
  25. {
  26.    
  27.     using System;
  28.     using System.Globalization;
  29.     using System.Runtime.Serialization;
  30.     using System.Runtime.CompilerServices;
  31.     using System.Runtime.ConstrainedExecution;
  32.    
  33.     [Serializable()]
  34.     [System.Runtime.InteropServices.ComVisible(true)]
  35.     public struct IntPtr : ISerializable
  36.     {
  37.        
  38.         unsafe private void* m_value;
  39.         // The compiler treats void* closest to uint hence explicit casts are required to preserve int behavior
  40.         public static readonly IntPtr Zero;
  41.        
  42.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  43.         unsafe internal bool IsNull()
  44.         {
  45.             return (this.m_value == null);
  46.         }
  47.        
  48.         [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
  49.         unsafe public IntPtr(int value)
  50.         {
  51.             #if WIN32
  52.             m_value = (void*)value;
  53.             #else
  54.             m_value = (void*)(long)value;
  55.             #endif
  56.         }
  57.        
  58.         [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
  59.         unsafe public IntPtr(long value)
  60.         {
  61.             #if WIN32
  62.             m_value = (void*)checked((int)value);
  63.             #else
  64.             m_value = (void*)value;
  65.             #endif
  66.         }
  67.        
  68.         [CLSCompliant(false)]
  69.         [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
  70.         unsafe public IntPtr(void* value)
  71.         {
  72.             m_value = value;
  73.         }
  74.        
  75.         unsafe private IntPtr(SerializationInfo info, StreamingContext context)
  76.         {
  77.             long l = info.GetInt64("value");
  78.            
  79.             if (Size == 4 && (l > Int32.MaxValue || l < Int32.MinValue)) {
  80.                 throw new ArgumentException(Environment.GetResourceString("Serialization_InvalidPtrValue"));
  81.             }
  82.            
  83.             m_value = (void*)l;
  84.         }
  85.        
  86.         unsafe void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  87.         {
  88.             if (info == null) {
  89.                 throw new ArgumentNullException("info");
  90.             }
  91.             #if WIN32
  92.             info.AddValue("value", (long)((int)m_value));
  93.             #else
  94.             info.AddValue("value", (long)(m_value));
  95.             #endif
  96.         }
  97.        
  98.         unsafe public override bool Equals(object obj)
  99.         {
  100.             if (obj is IntPtr) {
  101.                 return (m_value == ((IntPtr)obj).m_value);
  102.             }
  103.             return false;
  104.         }
  105.        
  106.         unsafe public override int GetHashCode()
  107.         {
  108.             return unchecked((int)((long)m_value));
  109.         }
  110.        
  111.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  112.         unsafe public int ToInt32()
  113.         {
  114.             #if WIN32
  115.             return (int)m_value;
  116.             #else
  117.             long l = (long)m_value;
  118.             return checked((int)l);
  119.             #endif
  120.         }
  121.        
  122.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  123.         unsafe public long ToInt64()
  124.         {
  125.             #if WIN32
  126.             return (long)(int)m_value;
  127.             #else
  128.             return (long)m_value;
  129.             #endif
  130.         }
  131.        
  132.         unsafe public override string ToString()
  133.         {
  134.             #if WIN32
  135.             return ((int)m_value).ToString(CultureInfo.InvariantCulture);
  136.             #else
  137.             return ((long)m_value).ToString(CultureInfo.InvariantCulture);
  138.             #endif
  139.         }
  140.        
  141.         unsafe public string ToString(string format)
  142.         {
  143.             #if WIN32
  144.             return ((int)m_value).ToString(format, CultureInfo.InvariantCulture);
  145.             #else
  146.             return ((long)m_value).ToString(format, CultureInfo.InvariantCulture);
  147.             #endif
  148.         }
  149.        
  150.        
  151.         [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
  152.         public static explicit operator IntPtr(int value)
  153.         {
  154.             return new IntPtr(value);
  155.         }
  156.        
  157.         [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
  158.         public static explicit operator IntPtr(long value)
  159.         {
  160.             return new IntPtr(value);
  161.         }
  162.        
  163.         [CLSCompliant(false), ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
  164.         unsafe public static explicit operator IntPtr(void* value)
  165.         {
  166.             return new IntPtr(value);
  167.         }
  168.        
  169.         [CLSCompliant(false)]
  170.         unsafe public static explicit operator void*(IntPtr value)
  171.         {
  172.             return value.ToPointer();
  173.         }
  174.        
  175.         unsafe public static explicit operator int(IntPtr value)
  176.         {
  177.             #if WIN32
  178.             return (int)value.m_value;
  179.             #else
  180.             long l = (long)value.m_value;
  181.             return checked((int)l);
  182.             #endif
  183.         }
  184.        
  185.         unsafe public static explicit operator long(IntPtr value)
  186.         {
  187.             #if WIN32
  188.             return (long)(int)value.m_value;
  189.             #else
  190.             return (long)value.m_value;
  191.             #endif
  192.         }
  193.        
  194.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  195.         unsafe public static bool operator ==(IntPtr value1, IntPtr value2)
  196.         {
  197.             return value1.m_value == value2.m_value;
  198.         }
  199.        
  200.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  201.         unsafe public static bool operator !=(IntPtr value1, IntPtr value2)
  202.         {
  203.             return value1.m_value != value2.m_value;
  204.         }
  205.        
  206.         public static int Size {
  207.             [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  208.             get {
  209.                 #if WIN32
  210.                 return 4;
  211.                 #else
  212.                 return 8;
  213.                 #endif
  214.             }
  215.         }
  216.        
  217.        
  218.         [CLSCompliant(false)]
  219.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  220.         unsafe public void* ToPointer()
  221.         {
  222.             return m_value;
  223.         }
  224.     }
  225. }

Developer Fusion