/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Xshd/XshdReference.cs

http://github.com/icsharpcode/ILSpy · C# · 142 lines · 74 code · 15 blank · 53 comment · 13 complexity · 4880815260936a7513f1fd3e5dc77a4b MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
  20. {
  21. /// <summary>
  22. /// A reference to an xshd color, or an inline xshd color.
  23. /// </summary>
  24. [Serializable]
  25. public struct XshdReference<T> : IEquatable<XshdReference<T>> where T : XshdElement
  26. {
  27. string referencedDefinition;
  28. string referencedElement;
  29. T inlineElement;
  30. /// <summary>
  31. /// Gets the reference.
  32. /// </summary>
  33. public string ReferencedDefinition {
  34. get { return referencedDefinition; }
  35. }
  36. /// <summary>
  37. /// Gets the reference.
  38. /// </summary>
  39. public string ReferencedElement {
  40. get { return referencedElement; }
  41. }
  42. /// <summary>
  43. /// Gets the inline element.
  44. /// </summary>
  45. public T InlineElement {
  46. get { return inlineElement; }
  47. }
  48. /// <summary>
  49. /// Creates a new XshdReference instance.
  50. /// </summary>
  51. public XshdReference(string referencedDefinition, string referencedElement)
  52. {
  53. if (referencedElement == null)
  54. throw new ArgumentNullException("referencedElement");
  55. this.referencedDefinition = referencedDefinition;
  56. this.referencedElement = referencedElement;
  57. this.inlineElement = null;
  58. }
  59. /// <summary>
  60. /// Creates a new XshdReference instance.
  61. /// </summary>
  62. public XshdReference(T inlineElement)
  63. {
  64. if (inlineElement == null)
  65. throw new ArgumentNullException("inlineElement");
  66. this.referencedDefinition = null;
  67. this.referencedElement = null;
  68. this.inlineElement = inlineElement;
  69. }
  70. /// <summary>
  71. /// Applies the visitor to the inline element, if there is any.
  72. /// </summary>
  73. public object AcceptVisitor(IXshdVisitor visitor)
  74. {
  75. if (inlineElement != null)
  76. return inlineElement.AcceptVisitor(visitor);
  77. else
  78. return null;
  79. }
  80. #region Equals and GetHashCode implementation
  81. // The code in this region is useful if you want to use this structure in collections.
  82. // If you don't need it, you can just remove the region and the ": IEquatable<XshdColorReference>" declaration.
  83. /// <inheritdoc/>
  84. public override bool Equals(object obj)
  85. {
  86. if (obj is XshdReference<T>)
  87. return Equals((XshdReference<T>)obj); // use Equals method below
  88. else
  89. return false;
  90. }
  91. /// <summary>
  92. /// Equality operator.
  93. /// </summary>
  94. public bool Equals(XshdReference<T> other)
  95. {
  96. // add comparisions for all members here
  97. return this.referencedDefinition == other.referencedDefinition
  98. && this.referencedElement == other.referencedElement
  99. && this.inlineElement == other.inlineElement;
  100. }
  101. /// <inheritdoc/>
  102. public override int GetHashCode()
  103. {
  104. // combine the hash codes of all members here (e.g. with XOR operator ^)
  105. return GetHashCode(referencedDefinition) ^ GetHashCode(referencedElement) ^ GetHashCode(inlineElement);
  106. }
  107. static int GetHashCode(object o)
  108. {
  109. return o != null ? o.GetHashCode() : 0;
  110. }
  111. /// <summary>
  112. /// Equality operator.
  113. /// </summary>
  114. public static bool operator ==(XshdReference<T> left, XshdReference<T> right)
  115. {
  116. return left.Equals(right);
  117. }
  118. /// <summary>
  119. /// Inequality operator.
  120. /// </summary>
  121. public static bool operator !=(XshdReference<T> left, XshdReference<T> right)
  122. {
  123. return !left.Equals(right);
  124. }
  125. #endregion
  126. }
  127. }