PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Runtime/ScopeDictionaryStorage.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 117 lines | 80 code | 20 blank | 17 comment | 16 complexity | 4bca2b6c752e0277fbde7c58b679dfbd 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.Collections.Generic;
  17. using Microsoft.Scripting;
  18. using Microsoft.Scripting.Runtime;
  19. using Microsoft.Scripting.Utils;
  20. using IronPython.Runtime.Operations;
  21. namespace IronPython.Runtime {
  22. /// <summary>
  23. /// Provides dictionary based storage which is backed by a Scope object.
  24. /// </summary>
  25. internal class ScopeDictionaryStorage : DictionaryStorage {
  26. private readonly Scope/*!*/ _scope;
  27. private readonly PythonContext/*!*/ _context;
  28. public ScopeDictionaryStorage(PythonContext/*!*/ context, Scope/*!*/ scope) {
  29. Assert.NotNull(context, scope);
  30. _scope = scope;
  31. _context = context;
  32. }
  33. public override void Add(object key, object value) {
  34. string strKey = key as string;
  35. if (strKey != null) {
  36. PythonOps.ScopeSetMember(_context.SharedContext, _scope, strKey, value);
  37. } else {
  38. PythonScopeExtension ext = (PythonScopeExtension)_context.EnsureScopeExtension(_scope);
  39. ext.EnsureObjectKeys().Add(key, value);
  40. }
  41. }
  42. public override bool Contains(object key) {
  43. object dummy;
  44. return TryGetValue(key, out dummy);
  45. }
  46. public override bool Remove(object key) {
  47. string strKey = key as string;
  48. if (strKey != null) {
  49. if (Contains(key)) {
  50. return PythonOps.ScopeDeleteMember(_context.SharedContext, Scope, strKey);
  51. }
  52. } else {
  53. PythonScopeExtension ext = (PythonScopeExtension)_context.EnsureScopeExtension(_scope);
  54. return ext.ObjectKeys != null && ext.ObjectKeys.Remove(key);
  55. }
  56. return false;
  57. }
  58. public override bool TryGetValue(object key, out object value) {
  59. string strKey = key as string;
  60. if (strKey != null) {
  61. return PythonOps.ScopeTryGetMember(_context.SharedContext, _scope, strKey, out value);
  62. } else {
  63. PythonScopeExtension ext = (PythonScopeExtension)_context.EnsureScopeExtension(_scope);
  64. if (ext.ObjectKeys != null && ext.ObjectKeys.TryGetValue(key, out value)) {
  65. return true;
  66. }
  67. }
  68. value = null;
  69. return false;
  70. }
  71. public override int Count {
  72. get {
  73. return GetItems().Count;
  74. }
  75. }
  76. public override void Clear() {
  77. foreach (var item in GetItems()) {
  78. Remove(item.Key);
  79. }
  80. }
  81. public override List<KeyValuePair<object, object>> GetItems() {
  82. List<KeyValuePair<object, object>> res = new List<KeyValuePair<object, object>>();
  83. foreach (object name in PythonOps.ScopeGetMemberNames(_context.SharedContext, _scope)) {
  84. object value;
  85. if (TryGetValue(name, out value)) {
  86. res.Add(new KeyValuePair<object, object>(name, value));
  87. }
  88. }
  89. return res;
  90. }
  91. internal Scope/*!*/ Scope {
  92. get {
  93. return _scope;
  94. }
  95. }
  96. }
  97. }