2 * Queue Manager (BITS) Job Enumerator
4 * Copyright 2007 Google (Roy Shea)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
26 static void EnumBackgroundCopyJobsDestructor(EnumBackgroundCopyJobsImpl *This)
30 for(i = 0; i < This->numJobs; i++)
31 IBackgroundCopyJob_Release(This->jobs[i]);
33 HeapFree(GetProcessHeap(), 0, This->jobs);
34 HeapFree(GetProcessHeap(), 0, This);
37 static ULONG WINAPI BITS_IEnumBackgroundCopyJobs_AddRef(
38 IEnumBackgroundCopyJobs* iface)
40 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
41 return InterlockedIncrement(&This->ref);
44 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_QueryInterface(
45 IEnumBackgroundCopyJobs* iface,
49 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
50 TRACE("IID: %s\n", debugstr_guid(riid));
52 if (IsEqualGUID(riid, &IID_IUnknown)
53 || IsEqualGUID(riid, &IID_IEnumBackgroundCopyJobs))
55 *ppvObject = &This->lpVtbl;
56 BITS_IEnumBackgroundCopyJobs_AddRef(iface);
64 static ULONG WINAPI BITS_IEnumBackgroundCopyJobs_Release(
65 IEnumBackgroundCopyJobs* iface)
67 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
68 ULONG ref = InterlockedDecrement(&This->ref);
71 EnumBackgroundCopyJobsDestructor(This);
76 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Next(
77 IEnumBackgroundCopyJobs* iface,
79 IBackgroundCopyJob **rgelt,
82 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
85 IBackgroundCopyJob *job;
87 fetched = min(celt, This->numJobs - This->indexJobs);
89 *pceltFetched = fetched;
92 /* We need to initialize this array if the caller doesn't request
93 the length because length_is will default to celt. */
94 for (i = 0; i < celt; ++i)
97 /* pceltFetched can only be NULL if celt is 1 */
102 /* Fill in the array of objects */
103 for (i = 0; i < fetched; ++i)
105 job = This->jobs[This->indexJobs++];
106 IBackgroundCopyJob_AddRef(job);
110 return fetched == celt ? S_OK : S_FALSE;
113 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Skip(
114 IEnumBackgroundCopyJobs* iface,
117 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
119 if (This->numJobs - This->indexJobs < celt)
121 This->indexJobs = This->numJobs;
125 This->indexJobs += celt;
129 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Reset(
130 IEnumBackgroundCopyJobs* iface)
132 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
137 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Clone(
138 IEnumBackgroundCopyJobs* iface,
139 IEnumBackgroundCopyJobs **ppenum)
141 FIXME("Not implemented\n");
145 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_GetCount(
146 IEnumBackgroundCopyJobs* iface,
149 EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
150 *puCount = This->numJobs;
154 static const IEnumBackgroundCopyJobsVtbl BITS_IEnumBackgroundCopyJobs_Vtbl =
156 BITS_IEnumBackgroundCopyJobs_QueryInterface,
157 BITS_IEnumBackgroundCopyJobs_AddRef,
158 BITS_IEnumBackgroundCopyJobs_Release,
159 BITS_IEnumBackgroundCopyJobs_Next,
160 BITS_IEnumBackgroundCopyJobs_Skip,
161 BITS_IEnumBackgroundCopyJobs_Reset,
162 BITS_IEnumBackgroundCopyJobs_Clone,
163 BITS_IEnumBackgroundCopyJobs_GetCount
166 HRESULT EnumBackgroundCopyJobsConstructor(LPVOID *ppObj,
167 IBackgroundCopyManager* copyManager)
169 BackgroundCopyManagerImpl *qmgr = (BackgroundCopyManagerImpl *) copyManager;
170 EnumBackgroundCopyJobsImpl *This;
171 BackgroundCopyJobImpl *job;
174 TRACE("%p, %p)\n", ppObj, copyManager);
176 This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
178 return E_OUTOFMEMORY;
179 This->lpVtbl = &BITS_IEnumBackgroundCopyJobs_Vtbl;
182 /* Create array of jobs */
185 EnterCriticalSection(&qmgr->cs);
186 This->numJobs = list_count(&qmgr->jobs);
188 if (0 < This->numJobs)
190 This->jobs = HeapAlloc(GetProcessHeap(), 0,
191 This->numJobs * sizeof *This->jobs);
194 LeaveCriticalSection(&qmgr->cs);
195 HeapFree(GetProcessHeap(), 0, This);
196 return E_OUTOFMEMORY;
203 LIST_FOR_EACH_ENTRY(job, &qmgr->jobs, BackgroundCopyJobImpl, entryFromQmgr)
205 IBackgroundCopyJob *iJob = (IBackgroundCopyJob *) job;
206 IBackgroundCopyJob_AddRef(iJob);
207 This->jobs[i++] = iJob;
209 LeaveCriticalSection(&qmgr->cs);
211 *ppObj = &This->lpVtbl;