Merge pull request #1659 from alexanderkyte/stringbuilder-referencesource
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / MethodDictionary.cs
1 //
2 // System.Runtime.Remoting.Messaging.MethodDictionary.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
5 //
6 // 2003 (C) Lluis Sanchez Gual
7 //
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34
35 namespace System.Runtime.Remoting.Messaging
36 {
37         [Serializable]
38         internal class MessageDictionary : IDictionary
39         {
40                 IDictionary _internalProperties = null;
41                 protected IMethodMessage _message;
42                 string[] _methodKeys;
43                 bool _ownProperties = false;
44
45                 public MessageDictionary (IMethodMessage message)
46                 {
47                         _message = message;
48                 }
49
50                 internal bool HasUserData () 
51                 {
52                                 if (null != _internalProperties)
53                                 {
54                                         // MethodCallMessageWrapper uses a nested MessageDictionary
55                                         if (_internalProperties is MessageDictionary)
56                                                 return ((MessageDictionary)_internalProperties).HasUserData ();
57                                         else 
58                                                 return _internalProperties.Count > 0;
59                                 }
60                                 return false;
61                 }
62
63                 internal IDictionary InternalDictionary
64                 {
65                         get 
66                         {
67                                 if (null != _internalProperties)
68                                 {
69                                         if (_internalProperties is MessageDictionary)
70                                                 return ((MessageDictionary)_internalProperties).InternalDictionary;
71                                 }
72                                 return _internalProperties;
73                         }
74                 }
75
76                 public string[] MethodKeys
77                 {
78                         get { return _methodKeys; }
79                         set { _methodKeys = value; }
80                 }
81
82                 protected virtual IDictionary AllocInternalProperties()
83                 {
84                         _ownProperties = true;
85                         return new Hashtable();
86                 }
87
88                 public IDictionary GetInternalProperties()
89                 {
90                         if (_internalProperties == null) _internalProperties = AllocInternalProperties();
91                         return _internalProperties;
92                 }
93
94                 private bool IsOverridenKey (string key)
95                 {
96                         // Small optimization. If the internal properties have been
97                         // created by this dictionary, then it can be assured that it does
98                         // not contain values for overriden keys.
99                         if (_ownProperties) return false;
100
101                         foreach (string mkey in _methodKeys)
102                                 if (key == mkey) return true;
103                         return false;
104                 }
105
106                 public MessageDictionary(string[] keys)
107                 {
108                         _methodKeys = keys;
109                 }
110
111                 public bool IsFixedSize 
112                 { 
113                         get { return false; } 
114                 }
115                 
116                 public bool IsReadOnly 
117                 { 
118                         get { return false; } 
119                 }
120
121                 public object this[object key] 
122                 { 
123                         get
124                         {
125                                 string keyStr = (string)key;
126                                 for (int n=0; n<_methodKeys.Length; n++)
127                                         if (_methodKeys[n] == keyStr) return GetMethodProperty (keyStr);
128
129                                 if (_internalProperties != null) 
130                                         return _internalProperties[key];
131                                 else 
132                                         return null;
133                         }
134
135                         set
136                         {
137                                 Add (key, value);
138                         }
139                 }
140
141                 protected virtual object GetMethodProperty (string key)
142                 {
143                         switch (key)
144                         {
145                                 case "__Uri" : return _message.Uri;
146                                 case "__MethodName" : return _message.MethodName;
147                                 case "__TypeName" : return _message.TypeName;
148                                 case "__MethodSignature" : return _message.MethodSignature;
149                                 case "__CallContext" : return _message.LogicalCallContext;
150                                 case "__Args" : return _message.Args;
151                                 case "__OutArgs": return ((IMethodReturnMessage)_message).OutArgs;
152                                 case "__Return": return ((IMethodReturnMessage)_message).ReturnValue;
153                                 default : return null;
154                         }
155                 }
156
157                 protected virtual void SetMethodProperty (string key, object value)
158                 {
159                         switch (key)
160                         {
161                                 case "__CallContext": // Ignore?
162                                 case "__OutArgs":
163                                 case "__Return": return;
164
165                                 case "__MethodName" :
166                                 case "__TypeName" :
167                                 case "__MethodSignature" :
168                                 case "__Args" : return; //throw new ArgumentException ("key was invalid " + key);
169                                 case "__Uri": ((IInternalMessage)_message).Uri = (string) value; return;
170                         }
171                 }
172
173                 public ICollection Keys 
174                 { 
175                         get 
176                         { 
177                                 ArrayList keys = new ArrayList();
178                                 for (int n=0; n<_methodKeys.Length; n++)
179                                         keys.Add (_methodKeys[n]);
180
181                                 if (_internalProperties != null)
182                                 {
183                                         foreach (string key in _internalProperties.Keys)
184                                                 if (!IsOverridenKey (key)) keys.Add (key);
185                                 }
186
187                                 return keys; 
188                         }
189                 }
190
191                 public ICollection Values 
192                 { 
193                         get 
194                         { 
195                                 ArrayList values = new ArrayList();
196                                 for (int n=0; n<_methodKeys.Length; n++)
197                                         values.Add (GetMethodProperty(_methodKeys[n]));
198
199                                 if (_internalProperties != null)
200                                 {
201                                         foreach (DictionaryEntry entry in _internalProperties)
202                                                 if (!IsOverridenKey((string)entry.Key)) values.Add (entry.Value);
203                                 }
204
205                                 return values; 
206                         }
207                 }
208
209                 public void Add (object key, object value)
210                 {
211                         string keyStr = (string)key;
212                         for (int n=0; n<_methodKeys.Length; n++)
213                                 if (_methodKeys[n] == keyStr) {
214                                         SetMethodProperty (keyStr, value);
215                                         return;
216                                 }
217
218                         if (_internalProperties == null) _internalProperties = AllocInternalProperties();
219                         _internalProperties[key] = value;
220                 }
221
222                 public void Clear ()
223                 {
224                         if (_internalProperties != null) _internalProperties.Clear();
225                 }
226
227                 public bool Contains (object key)
228                 {
229                         string keyStr = (string)key;
230                         for (int n=0; n<_methodKeys.Length; n++)
231                                 if (_methodKeys[n] == keyStr) return true;
232
233                         if (_internalProperties != null) return _internalProperties.Contains (key);
234                         else return false;
235                 }
236
237                 public void Remove (object key)
238                 {
239                         string keyStr = (string)key;
240                         for (int n=0; n<_methodKeys.Length; n++)
241                                 if (_methodKeys[n] == keyStr) throw new ArgumentException ("key was invalid");
242
243                         if (_internalProperties != null) _internalProperties.Remove (key);
244                 }
245
246                 public int Count 
247                 { 
248                         get 
249                         {
250                                 if (_internalProperties != null) return _internalProperties.Count + _methodKeys.Length;
251                                 else return _methodKeys.Length;
252                         }
253                 }
254
255                 public bool IsSynchronized 
256                 { 
257                         get { return false; }
258                 }
259
260                 public object SyncRoot 
261                 { 
262                         get { return this; }
263                 }
264
265                 public void CopyTo (Array array, int index)
266                 {
267                         Values.CopyTo (array, index);
268                 }
269
270                 IEnumerator IEnumerable.GetEnumerator()
271                 {
272                         return new DictionaryEnumerator (this);
273                 }
274
275                 public IDictionaryEnumerator GetEnumerator ()
276                 {
277                         return new DictionaryEnumerator (this);
278                 }
279
280                 // Dictionary enumerator
281
282                 class DictionaryEnumerator : IDictionaryEnumerator
283                 {
284                         MessageDictionary _methodDictionary;
285                         IDictionaryEnumerator _hashtableEnum;
286                         int _posMethod;
287
288                         public DictionaryEnumerator (MessageDictionary methodDictionary)
289                         {
290                                 _methodDictionary = methodDictionary;
291                                 _hashtableEnum = (_methodDictionary._internalProperties != null) ? _methodDictionary._internalProperties.GetEnumerator() : null;
292                                 _posMethod = -1;
293                         }
294
295                         public object Current 
296                         {
297                                 get {return Entry; }
298                         }
299
300                         public bool MoveNext()
301                         {
302                                 if (_posMethod != -2)
303                                 {
304                                         _posMethod++;
305                                         if (_posMethod < _methodDictionary._methodKeys.Length) return true;
306                                         _posMethod = -2;
307                                 }
308
309                                 if (_hashtableEnum == null) return false;
310                                 
311                                 while (_hashtableEnum.MoveNext())
312                                 {
313                                         if (!_methodDictionary.IsOverridenKey((string)_hashtableEnum.Key)) 
314                                                 return true;
315                                 }
316                                 return false;
317                         }
318
319                         public void Reset()
320                         {
321                                 _posMethod = -1;
322                                 _hashtableEnum.Reset();
323                         }
324
325                         public DictionaryEntry Entry 
326                         {
327                                 get
328                                 {
329                                         if (_posMethod >= 0) 
330                                                 return new DictionaryEntry (_methodDictionary._methodKeys[_posMethod], _methodDictionary.GetMethodProperty(_methodDictionary._methodKeys[_posMethod]));
331                                         else if (_posMethod == -1 || _hashtableEnum == null) 
332                                                 throw new InvalidOperationException ("The enumerator is positioned before the first element of the collection or after the last element");
333                                         else
334                                                 return _hashtableEnum.Entry;
335                                 }
336                         }
337
338                         public object Key
339                         {
340                                 get { return Entry.Key; }
341                         }
342
343                         public object Value
344                         {
345                                 get { return Entry.Value; }
346                         }
347                 }
348
349         }
350 }