Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / widget / windows / IEnumFE.cpp
blob8fe713860964a02d7264687838cd3f576edfc532
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "IEnumFE.h"
7 #include "nsAlgorithm.h"
8 #include <algorithm>
10 CEnumFormatEtc::CEnumFormatEtc() : mRefCnt(0), mCurrentIdx(0) {}
12 // Constructor used by Clone()
13 CEnumFormatEtc::CEnumFormatEtc(nsTArray<FormatEtc>& aArray)
14 : mRefCnt(0), mCurrentIdx(0) {
15 // a deep copy, calls FormatEtc's copy constructor on each
16 mFormatList.AppendElements(aArray);
19 CEnumFormatEtc::~CEnumFormatEtc() {}
21 /* IUnknown impl. */
23 STDMETHODIMP
24 CEnumFormatEtc::QueryInterface(REFIID riid, LPVOID* ppv) {
25 *ppv = nullptr;
27 if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IEnumFORMATETC))
28 *ppv = (LPVOID)this;
30 if (*ppv == nullptr) return E_NOINTERFACE;
32 // AddRef any interface we'll return.
33 ((LPUNKNOWN)*ppv)->AddRef();
34 return S_OK;
37 STDMETHODIMP_(ULONG)
38 CEnumFormatEtc::AddRef() {
39 ++mRefCnt;
40 NS_LOG_ADDREF(this, mRefCnt, "CEnumFormatEtc", sizeof(*this));
41 return mRefCnt;
44 STDMETHODIMP_(ULONG)
45 CEnumFormatEtc::Release() {
46 uint32_t refReturn;
48 refReturn = --mRefCnt;
49 NS_LOG_RELEASE(this, mRefCnt, "CEnumFormatEtc");
51 if (mRefCnt == 0) delete this;
53 return refReturn;
56 /* IEnumFORMATETC impl. */
58 STDMETHODIMP
59 CEnumFormatEtc::Next(ULONG aMaxToFetch, FORMATETC* aResult,
60 ULONG* aNumFetched) {
61 // If the method retrieves the number of items requested, the return
62 // value is S_OK. Otherwise, it is S_FALSE.
64 if (aNumFetched) *aNumFetched = 0;
66 // aNumFetched can be null if aMaxToFetch is 1
67 if (!aNumFetched && aMaxToFetch > 1) return S_FALSE;
69 if (!aResult) return S_FALSE;
71 // We're done walking the list
72 if (mCurrentIdx >= mFormatList.Length()) return S_FALSE;
74 uint32_t left = mFormatList.Length() - mCurrentIdx;
76 if (!aMaxToFetch) return S_FALSE;
78 uint32_t count = std::min(static_cast<uint32_t>(aMaxToFetch), left);
80 uint32_t idx = 0;
81 while (count > 0) {
82 // Copy out to aResult
83 mFormatList[mCurrentIdx++].CopyOut(&aResult[idx++]);
84 count--;
87 if (aNumFetched) *aNumFetched = idx;
89 return S_OK;
92 STDMETHODIMP
93 CEnumFormatEtc::Skip(ULONG aSkipNum) {
94 // If the method skips the number of items requested, the return value is
95 // S_OK. Otherwise, it is S_FALSE.
97 if ((mCurrentIdx + aSkipNum) >= mFormatList.Length()) return S_FALSE;
99 mCurrentIdx += aSkipNum;
101 return S_OK;
104 STDMETHODIMP
105 CEnumFormatEtc::Reset(void) {
106 mCurrentIdx = 0;
107 return S_OK;
110 STDMETHODIMP
111 CEnumFormatEtc::Clone(LPENUMFORMATETC* aResult) {
112 // Must return a new IEnumFORMATETC interface with the same iterative state.
114 if (!aResult) return E_INVALIDARG;
116 CEnumFormatEtc* pEnumObj = new CEnumFormatEtc(mFormatList);
118 if (!pEnumObj) return E_OUTOFMEMORY;
120 pEnumObj->AddRef();
121 pEnumObj->SetIndex(mCurrentIdx);
123 *aResult = pEnumObj;
125 return S_OK;
128 /* utils */
130 void CEnumFormatEtc::AddFormatEtc(LPFORMATETC aFormat) {
131 if (!aFormat) return;
132 FormatEtc* etc = mFormatList.AppendElement();
133 // Make a copy of aFormat
134 if (etc) etc->CopyIn(aFormat);
137 /* private */
139 void CEnumFormatEtc::SetIndex(uint32_t aIdx) { mCurrentIdx = aIdx; }