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/. */
9 CEnumFormatEtc::CEnumFormatEtc() : mRefCnt(0), mCurrentIdx(0) {}
11 // Constructor used by Clone()
12 CEnumFormatEtc::CEnumFormatEtc(nsTArray
<FormatEtc
>& aArray
)
13 : mRefCnt(0), mCurrentIdx(0) {
14 // a deep copy, calls FormatEtc's copy constructor on each
15 mFormatList
.AppendElements(aArray
);
18 CEnumFormatEtc::~CEnumFormatEtc() {}
23 CEnumFormatEtc::QueryInterface(REFIID riid
, LPVOID
* ppv
) {
26 if (IsEqualIID(riid
, IID_IUnknown
) || IsEqualIID(riid
, IID_IEnumFORMATETC
))
29 if (*ppv
== nullptr) return E_NOINTERFACE
;
31 // AddRef any interface we'll return.
32 ((LPUNKNOWN
)*ppv
)->AddRef();
37 CEnumFormatEtc::AddRef() {
39 NS_LOG_ADDREF(this, mRefCnt
, "CEnumFormatEtc", sizeof(*this));
44 CEnumFormatEtc::Release() {
47 refReturn
= --mRefCnt
;
48 NS_LOG_RELEASE(this, mRefCnt
, "CEnumFormatEtc");
50 if (mRefCnt
== 0) delete this;
55 /* IEnumFORMATETC impl. */
58 CEnumFormatEtc::Next(ULONG aMaxToFetch
, FORMATETC
* aResult
,
60 // If the method retrieves the number of items requested, the return
61 // value is S_OK. Otherwise, it is S_FALSE.
63 if (aNumFetched
) *aNumFetched
= 0;
65 // aNumFetched can be null if aMaxToFetch is 1
66 if (!aNumFetched
&& aMaxToFetch
> 1) return S_FALSE
;
68 if (!aResult
) return S_FALSE
;
70 // We're done walking the list
71 if (mCurrentIdx
>= mFormatList
.Length()) return S_FALSE
;
73 uint32_t left
= mFormatList
.Length() - mCurrentIdx
;
75 if (!aMaxToFetch
) return S_FALSE
;
77 uint32_t count
= std::min(static_cast<uint32_t>(aMaxToFetch
), left
);
81 // Copy out to aResult
82 mFormatList
[mCurrentIdx
++].CopyOut(&aResult
[idx
++]);
86 if (aNumFetched
) *aNumFetched
= idx
;
92 CEnumFormatEtc::Skip(ULONG aSkipNum
) {
93 // If the method skips the number of items requested, the return value is
94 // S_OK. Otherwise, it is S_FALSE.
96 if ((mCurrentIdx
+ aSkipNum
) >= mFormatList
.Length()) return S_FALSE
;
98 mCurrentIdx
+= aSkipNum
;
104 CEnumFormatEtc::Reset(void) {
110 CEnumFormatEtc::Clone(LPENUMFORMATETC
* aResult
) {
111 // Must return a new IEnumFORMATETC interface with the same iterative state.
113 if (!aResult
) return E_INVALIDARG
;
115 CEnumFormatEtc
* pEnumObj
= new CEnumFormatEtc(mFormatList
);
117 if (!pEnumObj
) return E_OUTOFMEMORY
;
120 pEnumObj
->SetIndex(mCurrentIdx
);
129 void CEnumFormatEtc::AddFormatEtc(LPFORMATETC aFormat
) {
130 if (!aFormat
) return;
131 FormatEtc
* etc
= mFormatList
.AppendElement();
132 // Make a copy of aFormat
133 if (etc
) etc
->CopyIn(aFormat
);
138 void CEnumFormatEtc::SetIndex(uint32_t aIdx
) { mCurrentIdx
= aIdx
; }