1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.Collections.Generic;
22
using System.Threading;
24
namespace ICSharpCode.NRefactory
27
/// Provides an interface to handle annotations in an object.
29
public interface IAnnotatable
32
/// Gets all annotations stored on this IAnnotatable.
34
IEnumerable<object> Annotations {
39
/// Gets the first annotation of the specified type.
40
/// Returns null if no matching annotation exists.
42
/// <typeparam name='T'>
43
/// The type of the annotation.
45
T Annotation<T> () where T: class;
48
/// Gets the first annotation of the specified type.
49
/// Returns null if no matching annotation exists.
51
/// <param name='type'>
52
/// The type of the annotation.
54
object Annotation (Type type);
57
/// Adds an annotation to this instance.
59
/// <param name='annotation'>
60
/// The annotation to add.
62
void AddAnnotation (object annotation);
65
/// Removes all annotations of the specified type.
67
/// <typeparam name='T'>
68
/// The type of the annotations to remove.
70
void RemoveAnnotations<T> () where T : class;
73
/// Removes all annotations of the specified type.
75
/// <param name='type'>
76
/// The type of the annotations to remove.
78
void RemoveAnnotations(Type type);
82
public abstract class AbstractAnnotatable : IAnnotatable
84
// Annotations: points either null (no annotations), to the single annotation,
85
// or to an AnnotationList.
86
// Once it is pointed at an AnnotationList, it will never change (this allows thread-safety support by locking the list)
91
/// Clones all annotations.
92
/// This method is intended to be called by Clone() implementations in derived classes.
94
/// AstNode copy = (AstNode)MemberwiseClone();
95
/// copy.CloneAnnotations();
98
protected void CloneAnnotations()
100
ICloneable cloneable = annotations as ICloneable;
101
if (cloneable != null)
102
annotations = cloneable.Clone();
105
sealed class AnnotationList : List<object>, ICloneable
107
// There are two uses for this custom list type:
108
// 1) it's private, and thus (unlike List<object>) cannot be confused with real annotations
109
// 2) It allows us to simplify the cloning logic by making the list behave the same as a clonable annotation.
110
public AnnotationList (int initialCapacity) : base(initialCapacity)
114
public object Clone ()
117
AnnotationList copy = new AnnotationList (this.Count);
118
for (int i = 0; i < this.Count; i++) {
119
object obj = this [i];
120
ICloneable c = obj as ICloneable;
121
copy.Add (c != null ? c.Clone () : obj);
128
public virtual void AddAnnotation (object annotation)
130
if (annotation == null)
131
throw new ArgumentNullException ("annotation");
132
retry: // Retry until successful
133
object oldAnnotation = Interlocked.CompareExchange (ref this.annotations, annotation, null);
134
if (oldAnnotation == null) {
135
return; // we successfully added a single annotation
137
AnnotationList list = oldAnnotation as AnnotationList;
139
// we need to transform the old annotation into a list
140
list = new AnnotationList (4);
141
list.Add (oldAnnotation);
142
list.Add (annotation);
143
if (Interlocked.CompareExchange (ref this.annotations, list, oldAnnotation) != oldAnnotation) {
144
// the transformation failed (some other thread wrote to this.annotations first)
148
// once there's a list, use simple locking
150
list.Add (annotation);
155
public virtual void RemoveAnnotations<T> () where T : class
157
retry: // Retry until successful
158
object oldAnnotations = this.annotations;
159
AnnotationList list = oldAnnotations as AnnotationList;
162
list.RemoveAll (obj => obj is T);
163
} else if (oldAnnotations is T) {
164
if (Interlocked.CompareExchange (ref this.annotations, null, oldAnnotations) != oldAnnotations) {
165
// Operation failed (some other thread wrote to this.annotations first)
171
public virtual void RemoveAnnotations (Type type)
174
throw new ArgumentNullException ("type");
175
retry: // Retry until successful
176
object oldAnnotations = this.annotations;
177
AnnotationList list = oldAnnotations as AnnotationList;
180
list.RemoveAll (obj => type.IsInstanceOfType (obj));
181
} else if (type.IsInstanceOfType (oldAnnotations)) {
182
if (Interlocked.CompareExchange (ref this.annotations, null, oldAnnotations) != oldAnnotations) {
183
// Operation failed (some other thread wrote to this.annotations first)
189
public T Annotation<T> () where T: class
191
object annotations = this.annotations;
192
AnnotationList list = annotations as AnnotationList;
195
foreach (object obj in list) {
203
return annotations as T;
207
public object Annotation (Type type)
210
throw new ArgumentNullException ("type");
211
object annotations = this.annotations;
212
AnnotationList list = annotations as AnnotationList;
215
foreach (object obj in list) {
216
if (type.IsInstanceOfType (obj))
221
if (type.IsInstanceOfType (annotations))
228
/// Gets all annotations stored on this AstNode.
230
public IEnumerable<object> Annotations {
232
object annotations = this.annotations;
233
AnnotationList list = annotations as AnnotationList;
236
return list.ToArray ();
239
if (annotations != null)
240
return new object[] { annotations };
242
return Enumerable.Empty<object> ();