2007-01-10 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / GroupingCollection.cs
1 //
2 // GroupingCollection.cs: Represents group of BuildItemGroup,
3 // BuildPropertyGroup and BuildChoose.
4 //
5 // Author:
6 //   Marek Sieradzki (marek.sieradzki@gmail.com)
7 // 
8 // (C) 2005 Marek Sieradzki
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 #if NET_2_0
30
31 using System.Collections;
32 using System.Collections.Generic;
33
34 namespace Microsoft.Build.BuildEngine {
35         internal class GroupingCollection : IEnumerable {
36                 
37                 int     imports;
38                 int     itemGroups;
39                 Project project;
40                 int     propertyGroups;
41                 int     chooses;
42
43                 LinkedList <object>     list;
44                 LinkedListNode <object> add_iterator;
45         
46                 public GroupingCollection (Project project)
47                 {
48                         list = new LinkedList <object> ();
49                         add_iterator = null;
50                         this.project = project;
51                 }
52
53                 public IEnumerator GetChooseEnumerator ()
54                 {
55                         foreach (object o in list)
56                                 if (o is BuildChoose)
57                                         yield return o;
58                 }
59
60                 public IEnumerator GetEnumerator ()
61                 {
62                         return list.GetEnumerator ();
63                 }
64
65                 public IEnumerator GetImportEnumerator ()
66                 {
67                         foreach (object o in list)
68                                 if (o is Import)
69                                         yield return o;
70                 }
71
72                 public IEnumerator GetItemGroupAndChooseEnumerator ()
73                 {
74                         foreach (object o in list)
75                                 if (o is BuildItemGroup || o is BuildPropertyGroup)
76                                         yield return o;
77                 }
78
79                 public IEnumerator GetItemGroupEnumerator ()
80                 {
81                         foreach (object o in list)
82                                 if (o is BuildItemGroup)
83                                         yield return o;
84                 }
85
86                 public IEnumerator GetPropertyGroupAndChooseEnumerator ()
87                 {
88                         foreach (object o in list)
89                                 if (o is BuildPropertyGroup || o is BuildChoose)
90                                         yield return o;
91                 }
92
93                 public IEnumerator GetPropertyGroupEnumerator ()
94                 {
95                         foreach (object o in list)
96                                 if (o is BuildPropertyGroup)
97                                         yield return o;
98                 }
99                 
100                 internal void Add (BuildPropertyGroup bpg)
101                 {
102                         bpg.GroupingCollection = this;
103                         propertyGroups++;
104                         if (add_iterator == null)
105                                 list.AddLast (bpg);
106                         else {
107                                 list.AddAfter (add_iterator, bpg);
108                                 add_iterator = add_iterator.Next;
109                         }
110                 }
111                 
112                 internal void Add (BuildItemGroup big)
113                 {
114                         itemGroups++;
115                         if (add_iterator == null)
116                                 list.AddLast (big);
117                         else {
118                                 list.AddAfter (add_iterator, big);
119                                 add_iterator = add_iterator.Next;
120                         }
121                 }
122                 
123                 internal void Add (BuildChoose bc)
124                 {
125                         chooses++;
126                         if (add_iterator == null)
127                                 list.AddLast (bc);
128                         else {
129                                 list.AddAfter (add_iterator, bc);
130                                 add_iterator = add_iterator.Next;
131                         }
132                 }
133
134                 internal void Add (Import import)
135                 {
136                         imports++;
137                         if (add_iterator == null)
138                                 list.AddLast (import);
139                         else {
140                                 list.AddAfter (add_iterator, import);
141                                 add_iterator = add_iterator.Next;
142                         }
143                 }
144
145                 internal void Evaluate ()
146                 {
147                         Evaluate (EvaluationType.Property);
148
149                         Evaluate (EvaluationType.Item);
150                 }
151
152                 void Evaluate (EvaluationType type)
153                 {
154                         BuildItemGroup big;
155                         BuildPropertyGroup bpg;
156                         Import import;
157                         LinkedListNode <object> evaluate_iterator;
158
159                         if (type == EvaluationType.Property) {
160                                 evaluate_iterator = list.First;
161                                 add_iterator = list.First;
162
163                                 while (evaluate_iterator != null) {
164                                         if (evaluate_iterator.Value is BuildPropertyGroup) {
165                                                 bpg = (BuildPropertyGroup) evaluate_iterator.Value;
166                                                 if (ConditionParser.ParseAndEvaluate (bpg.Condition, project))
167                                                         bpg.Evaluate ();
168                                         } else if (evaluate_iterator.Value is Import) {
169                                                 import = (Import) evaluate_iterator.Value;
170                                                 if (ConditionParser.ParseAndEvaluate (import.Condition, project))
171                                                         import.Evaluate ();
172                                         }
173
174                                         // if it wasn't moved by adding anything because of evaluating a Import shift it
175                                         if (add_iterator == evaluate_iterator)
176                                                 add_iterator = add_iterator.Next;
177
178                                         evaluate_iterator = evaluate_iterator.Next;
179                                 }
180                         } else {
181                                 evaluate_iterator = list.First;
182                                 add_iterator = list.First;
183
184                                 while (evaluate_iterator != null) {
185                                         if (evaluate_iterator.Value is BuildItemGroup) {
186                                                 big = (BuildItemGroup) evaluate_iterator.Value;
187                                                 if (ConditionParser.ParseAndEvaluate (big.Condition, project))
188                                                         big.Evaluate ();
189                                         }
190
191                                         evaluate_iterator = evaluate_iterator.Next;
192                                 }
193                         }
194                 }
195
196                 internal int Imports {
197                         get { return this.imports; }
198                 }
199                 
200                 internal int ItemGroups {
201                         get { return this.itemGroups; }
202                 }
203                 
204                 internal int PropertyGroups {
205                         get { return this.propertyGroups; }
206                 }
207                 
208                 internal int Chooses {
209                         get { return this.chooses; }
210                 } 
211         }
212
213         enum EvaluationType {
214                 Property,
215                 Item
216         }
217 }
218
219 #endif