added some development tools
[windows-sources.git] / developer / VSSDK / Samples / Code_Sweep / C# / VsPackage / TaskEnumerator.cs
blob7ab6e880df503cb75a5b740a91e15bc010d1e4f5
1 /***************************************************************************
3 Copyright (c) Microsoft Corporation. All rights reserved.
4 THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
5 ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
6 IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
7 PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 ***************************************************************************/
11 using Microsoft.VisualStudio;
12 using Microsoft.VisualStudio.Shell.Interop;
13 using System.Collections.Generic;
15 namespace Microsoft.Samples.VisualStudio.CodeSweep.VSPackage
17 class TaskEnumerator : IVsEnumTaskItems
19 /// <summary>
20 /// Creates a new task enumerator.
21 /// </summary>
22 /// <param name="items">The items this enumerator will enumerate.</param>
23 /// <param name="showIgnored">Determines whether tasks for which Ignored==true will be skipped.</param>
24 /// <exception cref="System.ArgumentNullException">Thrown if <c>items</c> is null.</exception>
25 /// <remarks>
26 /// This enumerator operates on a copy of the contents of <c>items</c>, so if <c>items</c>
27 /// is changed after this call, it will not affect the results.
28 /// </remarks>
29 public TaskEnumerator(IEnumerable<Task> items, bool showIgnored)
31 _showIgnored = showIgnored;
32 _items = new List<Task>(items);
35 #region IVsEnumTaskItems Members
37 /// <summary>
38 /// Creates a new enumerator with identical content to this one.
39 /// </summary>
40 /// <param name="ppenum">The newly created enumerator.</param>
41 /// <returns>An HRESULT indicating the success of the operation.</returns>
42 public int Clone(out IVsEnumTaskItems ppenum)
44 ppenum = new TaskEnumerator(_items, _showIgnored);
45 return VSConstants.S_OK;
48 /// <summary>
49 /// Enumerates a requested number of items.
50 /// </summary>
51 /// <param name="celt">The number of items to return.</param>
52 /// <param name="rgelt">The array of items that will be returned.</param>
53 /// <param name="pceltFetched">The array whose first element will be set to the actual number of items returned.</param>
54 /// <returns>S_OK if all requested items were returned; S_FALSE if fewer were available; E_INVALIDARG if <c>celt</c> is less than zero, <c>rgelt</c> is null, or <c>pceltFetched</c> is null.</returns>
55 /// <remarks>
56 /// This method returns failure codes instead of throwing exceptions because it is intended to be called from native code.
57 /// If the task provider's IsShowingIgnoredInstances property is false, ignored instances will be skipped.
58 /// </remarks>
59 public int Next(uint celt, IVsTaskItem[] rgelt, uint[] pceltFetched)
61 pceltFetched[0] = 0;
63 while (pceltFetched[0] < celt && _next < _items.Count)
65 if (_showIgnored || !_items[_next].Ignored)
67 rgelt[pceltFetched[0]] = _items[_next];
68 pceltFetched[0]++;
70 ++_next;
73 if (pceltFetched[0] == celt)
75 return VSConstants.S_OK;
77 else
79 return VSConstants.S_FALSE;
83 /// <summary>
84 /// Resets the enumerator so that the next call to <c>Next</c> will begin at the first element.
85 /// </summary>
86 /// <returns>S_OK</returns>
87 public int Reset()
89 _next = 0;
90 return VSConstants.S_OK;
93 /// <summary>
94 /// Skips a specified number of items
95 /// </summary>
96 /// <param name="celt">The number of items to skip.</param>
97 /// <returns>S_OK if the requested number of items was skipped; S_FALSE if <c>celt</c> is larger than the number of available items; E_INVALIDARG if <c>celt</c> is less than zero.</returns>
98 /// <remarks>
99 /// This method returns failure codes instead of throwing exceptions because it is intended to be called from native code.
100 /// If the task provider's IsShowingIgnoredInstances property is false, ignored instances will not be counted.
101 /// </remarks>
102 public int Skip(uint celt)
104 IVsTaskItem[] items = new IVsTaskItem[celt];
105 uint[] fetched = new uint[] { 0 };
107 return Next(celt, items, fetched);
110 #endregion IVsEnumTaskItems Members
112 #region Private Members
114 readonly List<Task> _items;
115 int _next = 0;
116 readonly bool _showIgnored;
118 #endregion Private Members