PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Runtime/AttributesDictionaryStorage.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 122 lines | 90 code | 18 blank | 14 comment | 21 complexity | 74e98754d7ada5152af8abd06df2b824 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 System.Diagnostics;
  18. using Microsoft.Scripting;
  19. using Microsoft.Scripting.Runtime;
  20. using System.Threading;
  21. namespace IronPython.Runtime {
  22. [Serializable]
  23. internal class AttributesDictionaryStorage : DictionaryStorage {
  24. private IAttributesCollection _data;
  25. private readonly CommonDictionaryStorage _hidden;
  26. public AttributesDictionaryStorage(IAttributesCollection data) {
  27. Debug.Assert(data != null);
  28. _hidden = new CommonDictionaryStorage();
  29. foreach (var key in data.Keys) {
  30. string strKey = key as string;
  31. if (strKey != null && strKey.Length > 0 && strKey[0] == '$') {
  32. _hidden.Add(strKey, null);
  33. }
  34. }
  35. _data = data;
  36. }
  37. public override void Add(object key, object value) {
  38. AddNoLock(key, value);
  39. }
  40. public override void AddNoLock(object key, object value) {
  41. _hidden.Remove(key);
  42. string strKey = key as string;
  43. if (strKey != null) {
  44. _data[SymbolTable.StringToId(strKey)] = value;
  45. } else {
  46. _data.AddObjectKey(key, value);
  47. }
  48. }
  49. public override bool Contains(object key) {
  50. if (_hidden.Contains(key)) {
  51. return false;
  52. }
  53. string strKey = key as string;
  54. if (strKey != null) {
  55. return _data.ContainsKey(SymbolTable.StringToId(strKey));
  56. } else {
  57. return _data.ContainsObjectKey(key);
  58. }
  59. }
  60. public override bool Remove(object key) {
  61. if (_hidden.Contains(key)) {
  62. return false;
  63. }
  64. string strKey = key as string;
  65. if (strKey != null) {
  66. return _data.Remove(SymbolTable.StringToId(strKey));
  67. } else {
  68. return _data.RemoveObjectKey(key);
  69. }
  70. }
  71. public override bool TryGetValue(object key, out object value) {
  72. if (_hidden.Contains(key)) {
  73. value = null;
  74. return false;
  75. }
  76. string strKey = key as string;
  77. if (strKey != null) {
  78. return _data.TryGetValue(SymbolTable.StringToId(strKey), out value);
  79. }
  80. return _data.TryGetObjectValue(key, out value);
  81. }
  82. public override int Count {
  83. get {
  84. return _data.Count - _hidden.Count;
  85. }
  86. }
  87. public override void Clear() {
  88. _data = new SymbolDictionary();
  89. _hidden.Clear();
  90. }
  91. public override List<KeyValuePair<object, object>> GetItems() {
  92. List<KeyValuePair<object, object>> res = new List<KeyValuePair<object, object>>(Count);
  93. foreach (var kvp in _data) {
  94. if (!_hidden.Contains(kvp.Key)) {
  95. res.Add(kvp);
  96. }
  97. }
  98. return res;
  99. }
  100. public override bool HasNonStringAttributes() {
  101. return true;
  102. }
  103. }
  104. }