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

/DICK.B1/IronPython/Runtime/CustomDictionaryStorage.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 102 lines | 54 code | 19 blank | 29 comment | 9 complexity | bd5f357e550a0869fc07199414fc24e8 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. namespace IronPython.Runtime {
  20. abstract class CustomDictionaryStorage : DictionaryStorage {
  21. private readonly CommonDictionaryStorage/*!*/ _storage = new CommonDictionaryStorage();
  22. public override void Add(object key, object value) {
  23. if (key is string && TrySetExtraValue((string)key, value)) {
  24. return;
  25. }
  26. _storage.Add(key, value);
  27. }
  28. public override bool Contains(object key) {
  29. object dummy;
  30. if (key is string && TryGetExtraValue((string)key, out dummy)) {
  31. return dummy != Uninitialized.Instance;
  32. }
  33. return _storage.Contains(key);
  34. }
  35. public override bool Remove(object key) {
  36. if (key is string) {
  37. return TryRemoveExtraValue((string)key) ?? _storage.Remove(key);
  38. }
  39. return _storage.Remove(key);
  40. }
  41. public override bool TryGetValue(object key, out object value) {
  42. if (key is string && TryGetExtraValue((string)key, out value)) {
  43. return value != Uninitialized.Instance;
  44. }
  45. return _storage.TryGetValue(key, out value);
  46. }
  47. public override int Count {
  48. get { return GetItems().Count; }
  49. }
  50. public override void Clear() {
  51. _storage.Clear();
  52. foreach (var item in GetExtraItems()) {
  53. TryRemoveExtraValue(item.Key);
  54. }
  55. }
  56. public override List<KeyValuePair<object, object>> GetItems() {
  57. List<KeyValuePair<object, object>> res = _storage.GetItems();
  58. foreach (var item in GetExtraItems()) {
  59. res.Add(new KeyValuePair<object, object>(item.Key, item.Value));
  60. }
  61. return res;
  62. }
  63. /// <summary>
  64. /// Gets all of the extra names and values stored in the dictionary.
  65. /// </summary>
  66. protected abstract IEnumerable<KeyValuePair<string, object>> GetExtraItems();
  67. /// <summary>
  68. /// Attemps to sets a value in the extra keys. Returns true if the value is set, false if
  69. /// the value is not an extra key.
  70. /// </summary>
  71. protected abstract bool TrySetExtraValue(string key, object value);
  72. /// <summary>
  73. /// Attempts to get a value from the extra keys. Returns true if the value is an extra
  74. /// key and has a value. False if it is not an extra key or doesn't have a value.
  75. /// </summary>
  76. protected abstract bool TryGetExtraValue(string key, out object value);
  77. /// <summary>
  78. /// Attempts to remove the key. Returns true if the key is removed, false
  79. /// if the key was not removed, or null if the key is not an extra key.
  80. /// </summary>
  81. protected abstract bool? TryRemoveExtraValue(string key);
  82. }
  83. }