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
20 /// Creates a new task enumerator.
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>
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.
29 public TaskEnumerator(IEnumerable
<Task
> items
, bool showIgnored
)
31 _showIgnored
= showIgnored
;
32 _items
= new List
<Task
>(items
);
35 #region IVsEnumTaskItems Members
38 /// Creates a new enumerator with identical content to this one.
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
;
49 /// Enumerates a requested number of items.
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>
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.
59 public int Next(uint celt
, IVsTaskItem
[] rgelt
, uint[] pceltFetched
)
63 while (pceltFetched
[0] < celt
&& _next
< _items
.Count
)
65 if (_showIgnored
|| !_items
[_next
].Ignored
)
67 rgelt
[pceltFetched
[0]] = _items
[_next
];
73 if (pceltFetched
[0] == celt
)
75 return VSConstants
.S_OK
;
79 return VSConstants
.S_FALSE
;
84 /// Resets the enumerator so that the next call to <c>Next</c> will begin at the first element.
86 /// <returns>S_OK</returns>
90 return VSConstants
.S_OK
;
94 /// Skips a specified number of items
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>
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.
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
;
116 readonly bool _showIgnored
;
118 #endregion Private Members