~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory/IAnnotatable.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
19
 
using System;
20
 
using System.Collections.Generic;
21
 
using System.Linq;
22
 
using System.Threading;
23
 
 
24
 
namespace ICSharpCode.NRefactory
25
 
{
26
 
        /// <summary>
27
 
        /// Provides an interface to handle annotations in an object.
28
 
        /// </summary>
29
 
        public interface IAnnotatable
30
 
        {
31
 
                /// <summary>
32
 
                /// Gets all annotations stored on this IAnnotatable.
33
 
                /// </summary>
34
 
                IEnumerable<object> Annotations {
35
 
                        get;
36
 
                }
37
 
                
38
 
                /// <summary>
39
 
                /// Gets the first annotation of the specified type.
40
 
                /// Returns null if no matching annotation exists.
41
 
                /// </summary>
42
 
                /// <typeparam name='T'>
43
 
                /// The type of the annotation.
44
 
                /// </typeparam>
45
 
                T Annotation<T> () where T: class;
46
 
                
47
 
                /// <summary>
48
 
                /// Gets the first annotation of the specified type.
49
 
                /// Returns null if no matching annotation exists.
50
 
                /// </summary>
51
 
                /// <param name='type'>
52
 
                /// The type of the annotation.
53
 
                /// </param>
54
 
                object Annotation (Type type);
55
 
                
56
 
                /// <summary>
57
 
                /// Adds an annotation to this instance.
58
 
                /// </summary>
59
 
                /// <param name='annotation'>
60
 
                /// The annotation to add.
61
 
                /// </param>
62
 
                void AddAnnotation (object annotation);
63
 
                
64
 
                /// <summary>
65
 
                /// Removes all annotations of the specified type.
66
 
                /// </summary>
67
 
                /// <typeparam name='T'>
68
 
                /// The type of the annotations to remove.
69
 
                /// </typeparam>
70
 
                void RemoveAnnotations<T> () where T : class;
71
 
                
72
 
                /// <summary>
73
 
                /// Removes all annotations of the specified type.
74
 
                /// </summary>
75
 
                /// <param name='type'>
76
 
                /// The type of the annotations to remove.
77
 
                /// </param>
78
 
                void RemoveAnnotations(Type type);
79
 
        }
80
 
        
81
 
        [Serializable]
82
 
        public abstract class AbstractAnnotatable : IAnnotatable
83
 
        {
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)
87
 
                
88
 
                object annotations;
89
 
                
90
 
                /// <summary>
91
 
                /// Clones all annotations.
92
 
                /// This method is intended to be called by Clone() implementations in derived classes.
93
 
                /// <code>
94
 
                /// AstNode copy = (AstNode)MemberwiseClone();
95
 
                /// copy.CloneAnnotations();
96
 
                /// </code>
97
 
                /// </summary>
98
 
                protected void CloneAnnotations()
99
 
                {
100
 
                        ICloneable cloneable = annotations as ICloneable;
101
 
                        if (cloneable != null)
102
 
                                annotations = cloneable.Clone();
103
 
                }
104
 
                
105
 
                sealed class AnnotationList : List<object>, ICloneable
106
 
                {
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)
111
 
                        {
112
 
                        }
113
 
                        
114
 
                        public object Clone ()
115
 
                        {
116
 
                                lock (this) {
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);
122
 
                                        }
123
 
                                        return copy;
124
 
                                }
125
 
                        }
126
 
                }
127
 
                
128
 
                public virtual void AddAnnotation (object annotation)
129
 
                {
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
136
 
                        }
137
 
                        AnnotationList list = oldAnnotation as AnnotationList;
138
 
                        if (list == null) {
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)
145
 
                                        goto retry;
146
 
                                }
147
 
                        } else {
148
 
                                // once there's a list, use simple locking
149
 
                                lock (list) {
150
 
                                        list.Add (annotation);
151
 
                                }
152
 
                        }
153
 
                }
154
 
                
155
 
                public virtual void RemoveAnnotations<T> () where T : class
156
 
                {
157
 
                retry: // Retry until successful
158
 
                        object oldAnnotations = this.annotations;
159
 
                        AnnotationList list = oldAnnotations as AnnotationList;
160
 
                        if (list != null) {
161
 
                                lock (list)
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)
166
 
                                        goto retry;
167
 
                                }
168
 
                        }
169
 
                }
170
 
                
171
 
                public virtual void RemoveAnnotations (Type type)
172
 
                {
173
 
                        if (type == null)
174
 
                                throw new ArgumentNullException ("type");
175
 
                retry: // Retry until successful
176
 
                        object oldAnnotations = this.annotations;
177
 
                        AnnotationList list = oldAnnotations as AnnotationList;
178
 
                        if (list != null) {
179
 
                                lock (list)
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)
184
 
                                        goto retry;
185
 
                                }
186
 
                        }
187
 
                }
188
 
                
189
 
                public T Annotation<T> () where T: class
190
 
                {
191
 
                        object annotations = this.annotations;
192
 
                        AnnotationList list = annotations as AnnotationList;
193
 
                        if (list != null) {
194
 
                                lock (list) {
195
 
                                        foreach (object obj in list) {
196
 
                                                T t = obj as T;
197
 
                                                if (t != null)
198
 
                                                        return t;
199
 
                                        }
200
 
                                        return null;
201
 
                                }
202
 
                        } else {
203
 
                                return annotations as T;
204
 
                        }
205
 
                }
206
 
                
207
 
                public object Annotation (Type type)
208
 
                {
209
 
                        if (type == null)
210
 
                                throw new ArgumentNullException ("type");
211
 
                        object annotations = this.annotations;
212
 
                        AnnotationList list = annotations as AnnotationList;
213
 
                        if (list != null) {
214
 
                                lock (list) {
215
 
                                        foreach (object obj in list) {
216
 
                                                if (type.IsInstanceOfType (obj))
217
 
                                                        return obj;
218
 
                                        }
219
 
                                }
220
 
                        } else {
221
 
                                if (type.IsInstanceOfType (annotations))
222
 
                                        return annotations;
223
 
                        }
224
 
                        return null;
225
 
                }
226
 
                
227
 
                /// <summary>
228
 
                /// Gets all annotations stored on this AstNode.
229
 
                /// </summary>
230
 
                public IEnumerable<object> Annotations {
231
 
                        get {
232
 
                                object annotations = this.annotations;
233
 
                                AnnotationList list = annotations as AnnotationList;
234
 
                                if (list != null) {
235
 
                                        lock (list) {
236
 
                                                return list.ToArray ();
237
 
                                        }
238
 
                                } else {
239
 
                                        if (annotations != null)
240
 
                                                return new object[] { annotations };
241
 
                                        else
242
 
                                                return Enumerable.Empty<object> ();
243
 
                                }
244
 
                        }
245
 
                }
246
 
        }
247
 
}
248