IOptionContainer.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. namespace SRDebugger
  4. {
  5. /// <summary>
  6. /// You can implement this interface to create a dynamic "options container".
  7. /// Add the container to SRDebugger via the SRDebug API.
  8. /// </summary>
  9. /// <remarks>
  10. /// When the container is added via the API, the initial set of options will be fetched via <see cref="GetOptions"/>.
  11. /// Options that are added or removed after this point must fire the <see cref="OptionAdded"/> and <see cref="OptionRemoved"/> events in order
  12. /// for those options to be added/removed from the debug panel.
  13. /// If you do not intend to fire any events (i.e. this is a static container) then <see cref="IsDynamic"/> should return false.
  14. /// </remarks>
  15. public interface IOptionContainer
  16. {
  17. /// <summary>
  18. /// Get the initial set of options contained in this object.
  19. /// </summary>
  20. IEnumerable<OptionDefinition> GetOptions();
  21. /// <summary>
  22. /// Will the options collection be changed via events?
  23. /// If true, changes to the option set can be provided via the events <see cref="OptionAdded"/> and <see cref="OptionRemoved"/>.
  24. /// If false, the events will be ignored.
  25. /// </summary>
  26. bool IsDynamic { get; }
  27. event Action<OptionDefinition> OptionAdded;
  28. event Action<OptionDefinition> OptionRemoved;
  29. }
  30. public sealed class DynamicOptionContainer : IOptionContainer
  31. {
  32. public IList<OptionDefinition> Options
  33. {
  34. get { return _optionsReadOnly; }
  35. }
  36. private readonly List<OptionDefinition> _options = new List<OptionDefinition>();
  37. private readonly IList<OptionDefinition> _optionsReadOnly;
  38. public DynamicOptionContainer()
  39. {
  40. _optionsReadOnly = _options.AsReadOnly();
  41. }
  42. public void AddOption(OptionDefinition option)
  43. {
  44. _options.Add(option);
  45. if (OptionAdded != null)
  46. {
  47. OptionAdded(option);
  48. }
  49. }
  50. public bool RemoveOption(OptionDefinition option)
  51. {
  52. if (_options.Remove(option))
  53. {
  54. if (OptionRemoved != null)
  55. {
  56. OptionRemoved(option);
  57. }
  58. return true;
  59. }
  60. return false;
  61. }
  62. IEnumerable<OptionDefinition> IOptionContainer.GetOptions()
  63. {
  64. return _options;
  65. }
  66. public bool IsDynamic
  67. {
  68. get { return true; }
  69. }
  70. public event Action<OptionDefinition> OptionAdded;
  71. public event Action<OptionDefinition> OptionRemoved;
  72. }
  73. }