Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/Role.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 3.4 KB
Line 
1// Copyright (c) 2010-2013 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.Threading;
21
22namespace ICSharpCode.NRefactory
23{
24  /// <summary>
25  /// Represents the role a node plays within its parent.
26  /// </summary>
27  public abstract class Role
28  {
29    public const int RoleIndexBits = 9;
30   
31    static readonly Role[] roles = new Role[1 << RoleIndexBits];
32    static int nextRoleIndex = 0;
33   
34    readonly uint index;
35   
36    [CLSCompliant(false)]
37    public uint Index {
38      get { return index; }
39    }
40   
41    // don't allow NRefactory consumers to derive from Role
42    internal Role()
43    {
44      this.index = (uint)Interlocked.Increment(ref nextRoleIndex);
45      if (this.index >= roles.Length)
46        throw new InvalidOperationException("Too many roles");
47      roles[this.index] = this;
48    }
49   
50    /// <summary>
51    /// Gets whether the specified node is valid in this role.
52    /// </summary>
53    public abstract bool IsValid(object node);
54   
55    /// <summary>
56    /// Gets the role with the specified index.
57    /// </summary>
58    [CLSCompliant(false)]
59    public static Role GetByIndex(uint index)
60    {
61      return roles[index];
62    }
63  }
64 
65  /// <summary>
66  /// Represents the role a node plays within its parent.
67  /// All nodes with this role have type T.
68  /// </summary>
69  public class Role<T> : Role where T : class
70  {
71    readonly string name; // helps with debugging the AST
72    readonly T nullObject;
73   
74    /// <summary>
75    /// Gets the null object used when there's no node with this role.
76    /// Not every role has a null object; this property returns null for roles without a null object.
77    /// </summary>
78    /// <remarks>
79    /// Roles used for non-collections should always have a null object, so that no AST property returns null.
80    /// However, if a role used for collections only, it may leave out the null object.
81    /// </remarks>
82    public T NullObject {
83      get { return nullObject; }
84    }
85   
86    public override bool IsValid(object node)
87    {
88      return node is T;
89    }
90   
91    public Role(string name)
92    {
93      if (name == null)
94        throw new ArgumentNullException("name");
95      this.name = name;
96    }
97   
98    public Role(string name, T nullObject)
99    {
100      if (name == null)
101        throw new ArgumentNullException("name");
102      if (nullObject == null)
103        throw new ArgumentNullException ("nullObject");
104      this.nullObject = nullObject;
105      this.name = name;
106    }
107   
108    public override string ToString()
109    {
110      return name;
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.