PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultAccessor.cs

http://github.com/icsharpcode/ILSpy
C# | 123 lines | 87 code | 13 blank | 23 comment | 19 complexity | 7fddb6a20db2fd865b5881e45812a9b9 MD5 | raw file
  1// Copyright (c) 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
 19using System;
 20using System.Collections.Generic;
 21using System.Linq;
 22
 23namespace ICSharpCode.NRefactory.TypeSystem.Implementation
 24{
 25	/// <summary>
 26	/// Default implementation of <see cref="IAccessor"/>.
 27	/// </summary>
 28	[Serializable]
 29	public sealed class DefaultAccessor : AbstractFreezable, IAccessor, ISupportsInterning
 30	{
 31		static readonly DefaultAccessor[] defaultAccessors = CreateDefaultAccessors();
 32		
 33		static DefaultAccessor[] CreateDefaultAccessors()
 34		{
 35			DefaultAccessor[] accessors = new DefaultAccessor[(int)Accessibility.ProtectedAndInternal + 1];
 36			for (int i = 0; i < accessors.Length; i++) {
 37				accessors[i] = new DefaultAccessor();
 38				accessors[i].accessibility = (Accessibility)i;
 39				accessors[i].Freeze();
 40			}
 41			return accessors;
 42		}
 43		
 44		/// <summary>
 45		/// Gets the default accessor with the specified accessibility (and without attributes or region).
 46		/// </summary>
 47		public static IAccessor GetFromAccessibility(Accessibility accessibility)
 48		{
 49			int index = (int)accessibility;
 50			if (index >= 0 && index < defaultAccessors.Length) {
 51				return defaultAccessors[index];
 52			} else {
 53				DefaultAccessor a = new DefaultAccessor();
 54				a.accessibility = accessibility;
 55				a.Freeze();
 56				return a;
 57			}
 58		}
 59		
 60		Accessibility accessibility;
 61		DomRegion region;
 62		IList<IAttribute> attributes;
 63		IList<IAttribute> returnTypeAttributes;
 64		
 65		protected override void FreezeInternal()
 66		{
 67			base.FreezeInternal();
 68			this.attributes = FreezeList(this.attributes);
 69		}
 70		
 71		public Accessibility Accessibility {
 72			get { return accessibility; }
 73			set {
 74				CheckBeforeMutation();
 75				accessibility = value;
 76			}
 77		}
 78		
 79		public DomRegion Region {
 80			get { return region; }
 81			set {
 82				CheckBeforeMutation();
 83				region = value;
 84			}
 85		}
 86		
 87		public IList<IAttribute> Attributes {
 88			get {
 89				if (attributes == null)
 90					attributes = new List<IAttribute>();
 91				return attributes;
 92			}
 93		}
 94		
 95		public IList<IAttribute> ReturnTypeAttributes {
 96			get {
 97				if (returnTypeAttributes == null)
 98					returnTypeAttributes = new List<IAttribute>();
 99				return returnTypeAttributes;
100			}
101		}
102		
103		void ISupportsInterning.PrepareForInterning(IInterningProvider provider)
104		{
105			attributes = provider.InternList(attributes);
106			returnTypeAttributes = provider.InternList(returnTypeAttributes);
107		}
108		
109		int ISupportsInterning.GetHashCodeForInterning()
110		{
111			return (attributes != null ? attributes.GetHashCode() : 0)
112				^ (returnTypeAttributes != null ? returnTypeAttributes.GetHashCode() : 0)
113				^ region.GetHashCode() ^ (int)accessibility;
114		}
115		
116		bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
117		{
118			DefaultAccessor a = other as DefaultAccessor;
119			return a != null && (attributes == a.attributes && returnTypeAttributes == a.returnTypeAttributes 
120			                     && accessibility == a.accessibility && region == a.region);
121		}
122	}
123}