widget/windows/IEnumFE.cpp

branch
TOR_BUG_9701
changeset 10
ac0c01689b40
equal deleted inserted replaced
-1:000000000000 0:19b8c4cf6357
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/. */
5
6 #include "IEnumFE.h"
7 #include "nsAlgorithm.h"
8 #include <algorithm>
9
10 CEnumFormatEtc::CEnumFormatEtc() :
11 mRefCnt(0),
12 mCurrentIdx(0)
13 {
14 }
15
16 // Constructor used by Clone()
17 CEnumFormatEtc::CEnumFormatEtc(nsTArray<FormatEtc>& aArray) :
18 mRefCnt(0),
19 mCurrentIdx(0)
20 {
21 // a deep copy, calls FormatEtc's copy constructor on each
22 mFormatList.AppendElements(aArray);
23 }
24
25 CEnumFormatEtc::~CEnumFormatEtc()
26 {
27 }
28
29 /* IUnknown impl. */
30
31 STDMETHODIMP
32 CEnumFormatEtc::QueryInterface(REFIID riid, LPVOID *ppv)
33 {
34 *ppv = nullptr;
35
36 if (IsEqualIID(riid, IID_IUnknown) ||
37 IsEqualIID(riid, IID_IEnumFORMATETC))
38 *ppv = (LPVOID)this;
39
40 if (*ppv == nullptr)
41 return E_NOINTERFACE;
42
43 // AddRef any interface we'll return.
44 ((LPUNKNOWN)*ppv)->AddRef();
45 return S_OK;
46 }
47
48 STDMETHODIMP_(ULONG)
49 CEnumFormatEtc::AddRef()
50 {
51 ++mRefCnt;
52 NS_LOG_ADDREF(this, mRefCnt, "CEnumFormatEtc",sizeof(*this));
53 return mRefCnt;
54 }
55
56 STDMETHODIMP_(ULONG)
57 CEnumFormatEtc::Release()
58 {
59 uint32_t refReturn;
60
61 refReturn = --mRefCnt;
62 NS_LOG_RELEASE(this, mRefCnt, "CEnumFormatEtc");
63
64 if (mRefCnt == 0)
65 delete this;
66
67 return refReturn;
68 }
69
70 /* IEnumFORMATETC impl. */
71
72 STDMETHODIMP
73 CEnumFormatEtc::Next(ULONG aMaxToFetch, FORMATETC *aResult, ULONG *aNumFetched)
74 {
75 // If the method retrieves the number of items requested, the return
76 // value is S_OK. Otherwise, it is S_FALSE.
77
78 if (aNumFetched)
79 *aNumFetched = 0;
80
81 // aNumFetched can be null if aMaxToFetch is 1
82 if (!aNumFetched && aMaxToFetch > 1)
83 return S_FALSE;
84
85 if (!aResult)
86 return S_FALSE;
87
88 // We're done walking the list
89 if (mCurrentIdx >= mFormatList.Length())
90 return S_FALSE;
91
92 uint32_t left = mFormatList.Length() - mCurrentIdx;
93
94 if (!aMaxToFetch)
95 return S_FALSE;
96
97 uint32_t count = std::min(static_cast<uint32_t>(aMaxToFetch), left);
98
99 uint32_t idx = 0;
100 while (count > 0) {
101 // Copy out to aResult
102 mFormatList[mCurrentIdx++].CopyOut(&aResult[idx++]);
103 count--;
104 }
105
106 if (aNumFetched)
107 *aNumFetched = idx;
108
109 return S_OK;
110 }
111
112 STDMETHODIMP
113 CEnumFormatEtc::Skip(ULONG aSkipNum)
114 {
115 // If the method skips the number of items requested, the return value is S_OK.
116 // Otherwise, it is S_FALSE.
117
118 if ((mCurrentIdx + aSkipNum) >= mFormatList.Length())
119 return S_FALSE;
120
121 mCurrentIdx += aSkipNum;
122
123 return S_OK;
124 }
125
126 STDMETHODIMP
127 CEnumFormatEtc::Reset(void)
128 {
129 mCurrentIdx = 0;
130 return S_OK;
131 }
132
133 STDMETHODIMP
134 CEnumFormatEtc::Clone(LPENUMFORMATETC *aResult)
135 {
136 // Must return a new IEnumFORMATETC interface with the same iterative state.
137
138 if (!aResult)
139 return E_INVALIDARG;
140
141 CEnumFormatEtc * pEnumObj = new CEnumFormatEtc(mFormatList);
142
143 if (!pEnumObj)
144 return E_OUTOFMEMORY;
145
146 pEnumObj->AddRef();
147 pEnumObj->SetIndex(mCurrentIdx);
148
149 *aResult = pEnumObj;
150
151 return S_OK;
152 }
153
154 /* utils */
155
156 void
157 CEnumFormatEtc::AddFormatEtc(LPFORMATETC aFormat)
158 {
159 if (!aFormat)
160 return;
161 FormatEtc * etc = mFormatList.AppendElement();
162 // Make a copy of aFormat
163 if (etc)
164 etc->CopyIn(aFormat);
165 }
166
167 /* private */
168
169 void
170 CEnumFormatEtc::SetIndex(uint32_t aIdx)
171 {
172 mCurrentIdx = aIdx;
173 }

mercurial