Rev | Line | |
---|
[12503] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Windows.Controls;
|
---|
| 6 | using System.Diagnostics;
|
---|
| 7 | using System.Diagnostics.Contracts;
|
---|
| 8 |
|
---|
| 9 | namespace Microsoft.Research.DynamicDataDisplay.Common
|
---|
| 10 | {
|
---|
| 11 | [DebuggerDisplay("Count = {Count}")]
|
---|
| 12 | public sealed class ResourcePool<T>
|
---|
| 13 | {
|
---|
| 14 | private readonly List<T> pool = new List<T>();
|
---|
| 15 |
|
---|
| 16 | public T Get()
|
---|
| 17 | {
|
---|
| 18 | T item;
|
---|
| 19 |
|
---|
| 20 | if (pool.Count < 1)
|
---|
| 21 | {
|
---|
| 22 | item = default(T);
|
---|
| 23 | }
|
---|
| 24 | else
|
---|
| 25 | {
|
---|
| 26 | int index = pool.Count - 1;
|
---|
| 27 | item = pool[index];
|
---|
| 28 | pool.RemoveAt(index);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | return item;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public void Put(T item)
|
---|
| 35 | {
|
---|
| 36 | if (item == null)
|
---|
| 37 | throw new ArgumentNullException("item");
|
---|
| 38 |
|
---|
| 39 | int index = pool.IndexOf(item);
|
---|
| 40 | if (index != -1)
|
---|
| 41 | throw new InvalidOperationException("Cannot release item that is already contained in pool.");
|
---|
| 42 |
|
---|
| 43 | pool.Add(item);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public int Count
|
---|
| 47 | {
|
---|
| 48 | get { return pool.Count; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public void Clear()
|
---|
| 52 | {
|
---|
| 53 | pool.Clear();
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.