Bug 867104 - Add a crashtest. r=ehsan
[gecko.git] / widget / windows / IEnumFE.cpp
blob64bfade680579bfcf41e8fd3d41485c6837b75ac
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() :
11 mRefCnt(0),
12 mCurrentIdx(0)
16 // Constructor used by Clone()
17 CEnumFormatEtc::CEnumFormatEtc(nsTArray<FormatEtc>& aArray) :
18 mRefCnt(0),
19 mCurrentIdx(0)
21 // a deep copy, calls FormatEtc's copy constructor on each
22 mFormatList.AppendElements(aArray);
25 CEnumFormatEtc::~CEnumFormatEtc()
29 /* IUnknown impl. */
31 STDMETHODIMP
32 CEnumFormatEtc::QueryInterface(REFIID riid, LPVOID *ppv)
34 *ppv = NULL;
36 if (IsEqualIID(riid, IID_IUnknown) ||
37 IsEqualIID(riid, IID_IEnumFORMATETC))
38 *ppv = (LPVOID)this;
40 if (*ppv == NULL)
41 return E_NOINTERFACE;
43 // AddRef any interface we'll return.
44 ((LPUNKNOWN)*ppv)->AddRef();
45 return S_OK;
48 STDMETHODIMP_(ULONG)
49 CEnumFormatEtc::AddRef()
51 ++mRefCnt;
52 NS_LOG_ADDREF(this, mRefCnt, "CEnumFormatEtc",sizeof(*this));
53 return mRefCnt;
56 STDMETHODIMP_(ULONG)
57 CEnumFormatEtc::Release()
59 uint32_t refReturn;
61 refReturn = --mRefCnt;
62 NS_LOG_RELEASE(this, mRefCnt, "CEnumFormatEtc");
64 if (mRefCnt == 0)
65 delete this;
67 return refReturn;
70 /* IEnumFORMATETC impl. */
72 STDMETHODIMP
73 CEnumFormatEtc::Next(ULONG aMaxToFetch, FORMATETC *aResult, ULONG *aNumFetched)
75 // If the method retrieves the number of items requested, the return
76 // value is S_OK. Otherwise, it is S_FALSE.
78 if (aNumFetched)
79 *aNumFetched = 0;
81 // aNumFetched can be null if aMaxToFetch is 1
82 if (!aNumFetched && aMaxToFetch > 1)
83 return S_FALSE;
85 if (!aResult)
86 return S_FALSE;
88 // We're done walking the list
89 if (mCurrentIdx >= mFormatList.Length())
90 return S_FALSE;
92 uint32_t left = mFormatList.Length() - mCurrentIdx;
94 if (!aMaxToFetch)
95 return S_FALSE;
97 uint32_t count = std::min(static_cast<uint32_t>(aMaxToFetch), left);
99 uint32_t idx = 0;
100 while (count > 0) {
101 // Copy out to aResult
102 mFormatList[mCurrentIdx++].CopyOut(&aResult[idx++]);
103 count--;
106 if (aNumFetched)
107 *aNumFetched = idx;
109 return S_OK;
112 STDMETHODIMP
113 CEnumFormatEtc::Skip(ULONG aSkipNum)
115 // If the method skips the number of items requested, the return value is S_OK.
116 // Otherwise, it is S_FALSE.
118 if ((mCurrentIdx + aSkipNum) >= mFormatList.Length())
119 return S_FALSE;
121 mCurrentIdx += aSkipNum;
123 return S_OK;
126 STDMETHODIMP
127 CEnumFormatEtc::Reset(void)
129 mCurrentIdx = 0;
130 return S_OK;
133 STDMETHODIMP
134 CEnumFormatEtc::Clone(LPENUMFORMATETC *aResult)
136 // Must return a new IEnumFORMATETC interface with the same iterative state.
138 if (!aResult)
139 return E_INVALIDARG;
141 CEnumFormatEtc * pEnumObj = new CEnumFormatEtc(mFormatList);
143 if (!pEnumObj)
144 return E_OUTOFMEMORY;
146 pEnumObj->AddRef();
147 pEnumObj->SetIndex(mCurrentIdx);
149 *aResult = pEnumObj;
151 return S_OK;
154 /* utils */
156 void
157 CEnumFormatEtc::AddFormatEtc(LPFORMATETC aFormat)
159 if (!aFormat)
160 return;
161 FormatEtc * etc = mFormatList.AppendElement();
162 // Make a copy of aFormat
163 if (etc)
164 etc->CopyIn(aFormat);
167 /* private */
169 void
170 CEnumFormatEtc::SetIndex(uint32_t aIdx)
172 mCurrentIdx = aIdx;