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