Line | |
---|
1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Diagnostics;
|
---|
6 |
|
---|
7 | namespace Microsoft.Research.DynamicDataDisplay.Common.Auxiliary
|
---|
8 | {
|
---|
9 | public static class ResourcePoolExtensions
|
---|
10 | {
|
---|
11 | /// <summary>
|
---|
12 | /// Gets item from the pool, or creates new item if pool doesn't have more items.
|
---|
13 | /// </summary>
|
---|
14 | /// <typeparam name="T"></typeparam>
|
---|
15 | /// <param name="pool">The pool.</param>
|
---|
16 | /// <returns></returns>
|
---|
17 | public static T GetOrCreate<T>(this ResourcePool<T> pool) where T : new()
|
---|
18 | {
|
---|
19 | T instance = pool.Get();
|
---|
20 | if (instance == null)
|
---|
21 | {
|
---|
22 | instance = new T();
|
---|
23 | }
|
---|
24 |
|
---|
25 | return instance;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /// <summary>
|
---|
29 | /// Releases all items of given sequence into the pool.
|
---|
30 | /// </summary>
|
---|
31 | /// <typeparam name="T"></typeparam>
|
---|
32 | /// <param name="pool">Pool, which will contain all released elements from the sequence.</param>
|
---|
33 | /// <param name="sequence"></param>
|
---|
34 | public static void ReleaseAll<T>(this ResourcePool<T> pool, IEnumerable<T> sequence)
|
---|
35 | {
|
---|
36 | foreach (var item in sequence)
|
---|
37 | {
|
---|
38 | pool.Put(item);
|
---|
39 | }
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.