PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython.Modules/_ctypes/Pointer.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 137 lines | 101 code | 19 blank | 17 comment | 35 complexity | 160564e006c866277b76cc41af8535b1 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Text;
  17. using IronPython.Runtime;
  18. using IronPython.Runtime.Operations;
  19. using IronPython.Runtime.Types;
  20. #if !SILVERLIGHT
  21. namespace IronPython.Modules {
  22. /// <summary>
  23. /// Provides support for interop with native code from Python code.
  24. /// </summary>
  25. public static partial class CTypes {
  26. [PythonType("_Pointer")]
  27. public abstract class Pointer : CData {
  28. private CData _object;
  29. public Pointer() {
  30. _memHolder = new MemoryHolder(IntPtr.Size);
  31. }
  32. public Pointer(CData value) {
  33. _object = value; // Keep alive the object, more to do here.
  34. _memHolder = new MemoryHolder(IntPtr.Size);
  35. _memHolder.WriteIntPtr(0, value._memHolder);
  36. _memHolder.AddObject("1", value);
  37. if (value._objects != null) {
  38. _memHolder.AddObject("0", value._objects);
  39. }
  40. }
  41. public object contents {
  42. get {
  43. PythonType elementType = (PythonType)((PointerType)NativeType)._type;
  44. CData res = (CData)elementType.CreateInstance(elementType.Context.SharedContext);
  45. res._memHolder = _memHolder.ReadMemoryHolder(0);
  46. if(res._memHolder.UnsafeAddress == IntPtr.Zero) {
  47. throw PythonOps.ValueError("NULL value access");
  48. }
  49. return res;
  50. }
  51. set {
  52. }
  53. }
  54. public object this[int index] {
  55. get {
  56. INativeType type = ((PointerType)NativeType)._type;
  57. MemoryHolder address = _memHolder.ReadMemoryHolder(0);
  58. return type.GetValue(address, this, checked(type.Size * index), false);
  59. }
  60. set {
  61. MemoryHolder address = _memHolder.ReadMemoryHolder(0);
  62. INativeType type = ((PointerType)NativeType)._type;
  63. object keepAlive = type.SetValue(address, checked(type.Size * index), value);
  64. if (keepAlive != null) {
  65. _memHolder.AddObject(index.ToString(), keepAlive);
  66. }
  67. }
  68. }
  69. public bool __nonzero__() {
  70. return _memHolder.ReadIntPtr(0) != IntPtr.Zero;
  71. }
  72. public object this[Slice index] {
  73. get {
  74. if (index.stop == null) {
  75. throw PythonOps.ValueError("slice stop is required");
  76. }
  77. int start = index.start != null ? (int)index.start : 0;
  78. int stop = index.stop != null ? (int)index.stop : 0;
  79. int step = index.step != null ? (int)index.step : 1;
  80. if (step < 0 && index.start == null) {
  81. throw PythonOps.ValueError("slice start is required for step < 0");
  82. }
  83. if (start < 0) {
  84. start = 0;
  85. }
  86. INativeType type = ((PointerType)NativeType)._type;
  87. SimpleType elemType = type as SimpleType;
  88. if ((stop < start && step > 0) || (start < stop && step < 0)) {
  89. if (elemType != null && (elemType._type == SimpleTypeKind.WChar || elemType._type == SimpleTypeKind.Char)) {
  90. return String.Empty;
  91. }
  92. return new List();
  93. }
  94. MemoryHolder address = _memHolder.ReadMemoryHolder(0);
  95. if (elemType != null && (elemType._type == SimpleTypeKind.WChar || elemType._type == SimpleTypeKind.Char)) {
  96. int elmSize = ((INativeType)elemType).Size;
  97. StringBuilder res = new StringBuilder();
  98. for (int i = start; stop > start ? i < stop : i > stop; i += step) {
  99. res.Append(
  100. elemType.ReadChar(address, checked(i * elmSize))
  101. );
  102. }
  103. return res.ToString();
  104. } else {
  105. List res = new List((stop - start) / step);
  106. for (int i = start; stop > start ? i < stop : i > stop; i += step) {
  107. res.AddNoLock(
  108. type.GetValue(address, this, checked(type.Size * i), false)
  109. );
  110. }
  111. return res;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. #endif