Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Flash.cs
blob3f3e294e956c1d7e344821f50788286d1d1f413c
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework
17 using System;
18 using System.Runtime.Serialization;
19 using System.Collections;
21 /// <summary>
22 /// Keeps data across a single request using the session
23 /// </summary>
24 [Serializable]
25 public class Flash : Hashtable
27 /// <summary>
28 /// Flash key
29 /// </summary>
30 public static readonly String FlashKey = "flash";
32 [NonSerialized]
33 private ArrayList keep = new ArrayList();
34 [NonSerialized]
35 private bool hasItemsToKeep;
36 [NonSerialized]
37 private bool hasSwept;
39 /// <summary>
40 /// Initializes a new instance of the <see cref="Flash"/> class.
41 /// </summary>
42 public Flash() : base(StringComparer.InvariantCultureIgnoreCase)
46 /// <summary>
47 /// Initializes a new instance of the <see cref="Flash"/> class.
48 /// </summary>
49 /// <param name="copy">The copy.</param>
50 public Flash(Flash copy) : base(StringComparer.InvariantCultureIgnoreCase)
52 if (copy == null) return;
54 foreach(DictionaryEntry entry in copy)
56 base.Add(entry.Key, entry.Value);
60 /// <summary>
61 /// Initializes a new instance of the <see cref="Flash"/> class.
62 /// </summary>
63 /// <param name="info">A <see cref="T:System.Runtime.Serialization.SerializationInfo"/> object containing the information required to serialize the <see cref="T:System.Collections.Hashtable"/> object.</param>
64 /// <param name="context">A <see cref="T:System.Runtime.Serialization.StreamingContext"/> object containing the source and destination of the serialized stream associated with the <see cref="T:System.Collections.Hashtable"/>.</param>
65 /// <exception cref="T:System.ArgumentNullException">
66 /// <paramref name="info"/> is null. </exception>
67 public Flash(SerializationInfo info, StreamingContext context) : base(info, context)
71 /// <summary>
72 /// Remove any element thats not marked to be kept.
73 /// This method is automatically called by the framework after the controller is processed.
74 /// </summary>
75 public void Sweep()
77 if (hasSwept)
79 object[] keys = new object[Keys.Count];
81 Keys.CopyTo(keys, 0);
83 if (keys.Length != 0)
85 keep.AddRange(keys);
89 if (keep.Count == 0)
91 Clear();
93 else
95 object[] keys = new object[Keys.Count];
97 Keys.CopyTo(keys, 0);
99 for(int i = 0; i < keys.Length; i++)
101 if (!keep.Contains(keys[i]))
103 Remove(keys[i]);
105 else if (!hasItemsToKeep)
107 hasItemsToKeep = true;
111 keep.Clear();
114 hasSwept = true;
117 /// <summary>
118 /// Keeps the entire flash contents available for the next action
119 /// </summary>
120 public void Keep()
122 keep.Clear();
124 foreach(object key in base.Keys)
126 keep.Add(key);
130 /// <summary>
131 /// Keeps the Flash['key'] contents available for the next action
132 /// </summary>
133 public void Keep(object key)
135 if (!keep.Contains(key)) keep.Add(key);
138 /// <summary>
139 /// Marks the entire flash to be discarded by the end of the current action
140 /// </summary>
141 public void Discard()
143 keep.Clear();
146 /// <summary>
147 /// Marks Flash[key] to be discarded by the end of the current action
148 /// </summary>
149 public void Discard(object key)
151 keep.Remove(key);
154 /// <summary>
155 /// Sets a flash that will not be available to the next action, only to the current.
156 /// <code>
157 /// Flash.Now( key, "Hello current action" )
158 /// </code>
159 /// <para>
160 /// This method enables you to use the flash as a central messaging system in your app.
161 /// When you need to pass an object to the next action, you use the standard flash assign (<c>[]=</c>).
162 /// When you need to pass an object to the current action, you use <c>Now</c>, and your object will
163 /// vanish when the current action is done.
164 /// </para>
165 /// <para>
166 /// Entries set via <c>Now</c> are accessed the same way as standard entries: <c>Flash['my-key']</c>.
167 /// </para>
168 /// </summary>
169 public void Now(object key, object value)
171 base[key] = value;
174 /// <summary>
175 /// Adds an element with the specified key and value into the <see cref="T:System.Collections.Hashtable"></see>.
176 /// </summary>
177 /// <param name="key">The key of the element to add.</param>
178 /// <param name="value">The value of the element to add. The value can be null.</param>
179 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Hashtable"></see> is read-only.-or- The <see cref="T:System.Collections.Hashtable"></see> has a fixed size. </exception>
180 /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Hashtable"></see>. </exception>
181 /// <exception cref="T:System.ArgumentNullException">key is null. </exception>
182 public override void Add(object key, object value)
184 InternalAdd(key, value);
187 /// <summary>
188 /// Gets or sets the <see cref="System.Object"/> with the specified key.
189 /// </summary>
190 /// <value></value>
191 public override object this[object key]
193 get { return base[key]; }
194 set { InternalAdd(key, value); }
197 /// <summary>
198 /// Gets a value indicating whether this instance has items to keep.
199 /// </summary>
200 /// <value>
201 /// <c>true</c> if this instance has items to keep; otherwise, <c>false</c>.
202 /// </value>
203 public bool HasItemsToKeep
205 get { return hasItemsToKeep; }
208 /// <summary>
209 /// Making sure we keep any item added
210 /// to the flash directly for at least one more action.
211 /// </summary>
212 private void InternalAdd(object key, object value)
214 keep.Add(key);
215 base[key] = value;