Skip to content

Commit

Permalink
Handle error in ITfCandidateList ::SetResult
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancorvussolis committed Nov 8, 2016
1 parent f459d26 commit d819a47
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 55 deletions.
7 changes: 7 additions & 0 deletions imcrvtip/DisplayAttributeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CDisplayAttributeInfo : public ITfDisplayAttributeInfo
_pguid = &guid;
_pDisplayAttribute = pDisplayAttribute;
}

~CDisplayAttributeInfo()
{
DllRelease();
Expand Down Expand Up @@ -42,10 +43,12 @@ class CDisplayAttributeInfo : public ITfDisplayAttributeInfo

return E_NOINTERFACE;
}

STDMETHODIMP_(ULONG) AddRef(void)
{
return ++_cRef;
}

STDMETHODIMP_(ULONG) Release(void)
{
if(--_cRef == 0)
Expand Down Expand Up @@ -74,6 +77,7 @@ class CDisplayAttributeInfo : public ITfDisplayAttributeInfo

return S_OK;
}

STDMETHODIMP GetDescription(BSTR *pbstrDesc)
{
BSTR bstrDesc;
Expand All @@ -96,6 +100,7 @@ class CDisplayAttributeInfo : public ITfDisplayAttributeInfo

return S_OK;
}

STDMETHODIMP GetAttributeInfo(TF_DISPLAYATTRIBUTE *pda)
{
if(pda == nullptr)
Expand All @@ -107,10 +112,12 @@ class CDisplayAttributeInfo : public ITfDisplayAttributeInfo

return S_OK;
}

STDMETHODIMP SetAttributeInfo(const TF_DISPLAYATTRIBUTE *pda)
{
return S_OK;
}

STDMETHODIMP Reset()
{
return SetAttributeInfo(_pDisplayAttribute);
Expand Down
3 changes: 3 additions & 0 deletions imcrvtip/EditSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CEditSessionBase : public ITfEditSession
_pTextService = pTextService;
_pTextService->AddRef();
}

virtual ~CEditSessionBase()
{
SafeRelease(&_pContext);
Expand Down Expand Up @@ -47,10 +48,12 @@ class CEditSessionBase : public ITfEditSession

return E_NOINTERFACE;
}

STDMETHODIMP_(ULONG) AddRef(void)
{
return ++_cRef;
}

STDMETHODIMP_(ULONG) Release(void)
{
if(--_cRef == 0)
Expand Down
32 changes: 18 additions & 14 deletions imcrvtip/EnumDisplayAttributeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class CEnumDisplayAttributeInfo : public IEnumTfDisplayAttributeInfo
DllAddRef();

_cRef = 1;
_iIndex = 0;
_nIndex = 0;
}

~CEnumDisplayAttributeInfo()
{
DllRelease();
Expand Down Expand Up @@ -40,10 +41,12 @@ class CEnumDisplayAttributeInfo : public IEnumTfDisplayAttributeInfo

return E_NOINTERFACE;
}

STDMETHODIMP_(ULONG) AddRef(void)
{
return ++_cRef;
}

STDMETHODIMP_(ULONG) Release(void)
{
if(--_cRef == 0)
Expand Down Expand Up @@ -76,12 +79,13 @@ class CEnumDisplayAttributeInfo : public IEnumTfDisplayAttributeInfo
return E_OUTOFMEMORY;
}

pClone->_iIndex = _iIndex;
pClone->_nIndex = _nIndex;

*ppEnum = pClone;

return S_OK;
}

STDMETHODIMP Next(ULONG ulCount, ITfDisplayAttributeInfo **rgInfo, ULONG *pcFetched)
{
ULONG cFetched = 0;
Expand All @@ -99,29 +103,28 @@ class CEnumDisplayAttributeInfo : public IEnumTfDisplayAttributeInfo

while(cFetched < ulCount)
{
if(_iIndex >= DISPLAYATTRIBUTE_INFO_NUM)
if(_nIndex >= DISPLAYATTRIBUTE_INFO_NUM)
{
break;
}

try
{
pDisplayAttributeInfo = new CDisplayAttributeInfo(
c_gdDisplayAttributeInfo[_iIndex].guid, &CTextService::display_attribute_info[_iIndex]);
c_gdDisplayAttributeInfo[_nIndex].guid, &CTextService::display_attribute_info[_nIndex]);
}
catch(...)
{
for(ULONG i = 0; i < cFetched; i++)
{
delete *(rgInfo + i);
}
cFetched = 0;
return E_OUTOFMEMORY;
}

*(rgInfo + cFetched) = pDisplayAttributeInfo;
cFetched++;
_iIndex++;
_nIndex++;
}

if(pcFetched != nullptr)
Expand All @@ -131,27 +134,28 @@ class CEnumDisplayAttributeInfo : public IEnumTfDisplayAttributeInfo

return (cFetched == ulCount) ? S_OK : S_FALSE;
}

STDMETHODIMP Reset()
{
_iIndex = 0;
_nIndex = 0;
return S_OK;
}

STDMETHODIMP Skip(ULONG ulCount)
{
if((ulCount < DISPLAYATTRIBUTE_INFO_NUM) &&
((_iIndex + ulCount) < DISPLAYATTRIBUTE_INFO_NUM))
if((_nIndex + ulCount) >= DISPLAYATTRIBUTE_INFO_NUM)
{
_iIndex += ulCount;
return S_OK;
_nIndex = DISPLAYATTRIBUTE_INFO_NUM;
return S_FALSE;
}

_iIndex = DISPLAYATTRIBUTE_INFO_NUM;
return S_FALSE;
_nIndex += ulCount;
return S_OK;
}

private:
LONG _iIndex;
LONG _cRef;
ULONG _nIndex;
};

#endif //ENUMDISPLAYATTRIBUTEINFO_H
30 changes: 10 additions & 20 deletions imcrvtip/FnCandidateList.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,9 @@
#define FNCANDIDATELIST_H

#include "TextService.h"
#include "EditSession.h"
#include "FnCandidateString.h"
#include "FnEnumCandidates.h"

class CSetResultEditSession : public CEditSessionBase
{
public:
CSetResultEditSession(CTextService *pTextService, ITfContext *pContext) : CEditSessionBase(pTextService, pContext)
{
}

~CSetResultEditSession()
{
}

// ITfEditSession
STDMETHODIMP DoEditSession(TfEditCookie ec)
{
return _pTextService->_HandleCharReturn(ec, _pContext);
}
};

class CFnCandidateList : public ITfCandidateList
{
public:
Expand All @@ -48,6 +29,7 @@ class CFnCandidateList : public ITfCandidateList
DllRelease();
}

// IUnknown
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObj)
{
if(ppvObj == nullptr)
Expand Down Expand Up @@ -87,6 +69,7 @@ class CFnCandidateList : public ITfCandidateList
return _cRef;
}

// ITfCandidateList
STDMETHODIMP EnumCandidates(IEnumTfCandidates **ppEnum)
{
CFnEnumCandidates *pCandidateEnum;
Expand Down Expand Up @@ -155,7 +138,14 @@ class CFnCandidateList : public ITfCandidateList
HRESULT hr = S_OK;
if(imcr == CAND_FINALIZED)
{
hr = _pTextService->_SetReconvertResult(_searchkey, _candidates, nIndex);
try
{
hr = _pTextService->_SetResult(_searchkey, _candidates, nIndex);
}
catch(...)
{
hr = E_FAIL;
}
}

return hr;
Expand Down
8 changes: 5 additions & 3 deletions imcrvtip/FnCandidateString.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CFnCandidateString : public ITfCandidateString
DllAddRef();

_cRef = 1;
_iIndex = index;
_nIndex = index;
_candidate = candidate;
}

Expand All @@ -19,6 +19,7 @@ class CFnCandidateString : public ITfCandidateString
DllRelease();
}

// IUnknown
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObj)
{
if(ppvObj == nullptr)
Expand Down Expand Up @@ -58,6 +59,7 @@ class CFnCandidateString : public ITfCandidateString
return _cRef;
}

// ITfCandidateString
STDMETHODIMP GetString(BSTR *pbstr)
{
BSTR bstr;
Expand Down Expand Up @@ -88,14 +90,14 @@ class CFnCandidateString : public ITfCandidateString
return E_INVALIDARG;
}

*pnIndex = _iIndex;
*pnIndex = _nIndex;

return S_OK;
}

private:
LONG _cRef;
LONG _iIndex;
ULONG _nIndex;
std::wstring _candidate;
};

Expand Down
28 changes: 14 additions & 14 deletions imcrvtip/FnEnumCandidates.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CFnEnumCandidates : public IEnumTfCandidates
DllAddRef();

_cRef = 1;
_iIndex = 0;
_nIndex = 0;
_candidates = candidates;
}

Expand All @@ -21,6 +21,7 @@ class CFnEnumCandidates : public IEnumTfCandidates
DllRelease();
}

// IUnknown
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObj)
{
if(ppvObj == nullptr)
Expand Down Expand Up @@ -60,6 +61,7 @@ class CFnEnumCandidates : public IEnumTfCandidates
return _cRef;
}

// IEnumTfCandidates
STDMETHODIMP Clone(IEnumTfCandidates **ppEnum)
{
CFnEnumCandidates *pClone;
Expand All @@ -80,7 +82,7 @@ class CFnEnumCandidates : public IEnumTfCandidates
return E_OUTOFMEMORY;
}

pClone->_iIndex = _iIndex;
pClone->_nIndex = _nIndex;

*ppEnum = pClone;

Expand All @@ -104,28 +106,27 @@ class CFnEnumCandidates : public IEnumTfCandidates

while(cFetched < ulCount)
{
if(_iIndex >= (LONG)_candidates.size())
if(_nIndex >= (ULONG)_candidates.size())
{
break;
}

try
{
pCandidateString = new CFnCandidateString(_iIndex, _candidates[_iIndex].first.first);
pCandidateString = new CFnCandidateString(_nIndex, _candidates[_nIndex].first.first);
}
catch(...)
{
for(ULONG i = 0; i < cFetched; i++)
{
delete *(ppCand + i);
}
cFetched = 0;
return E_OUTOFMEMORY;
}

*(ppCand + cFetched) = pCandidateString;
cFetched++;
_iIndex++;
_nIndex++;
}

if(pcFetched != nullptr)
Expand All @@ -138,26 +139,25 @@ class CFnEnumCandidates : public IEnumTfCandidates

STDMETHODIMP Reset(void)
{
_iIndex = 0;
_nIndex = 0;
return S_OK;
}

STDMETHODIMP Skip(ULONG ulCount)
{
if((ulCount < (ULONG)_candidates.size()) &&
((_iIndex + ulCount) < (ULONG)_candidates.size()))
if((_nIndex + ulCount) >= (ULONG)_candidates.size())
{
_iIndex += ulCount;
return S_OK;
_nIndex = (ULONG)_candidates.size();
return S_FALSE;
}

_iIndex = (LONG)_candidates.size();
return S_FALSE;
_nIndex += ulCount;
return S_OK;
}

private:
LONG _cRef;
LONG _iIndex;
ULONG _nIndex;
CANDIDATES _candidates;
};

Expand Down
Loading

0 comments on commit d819a47

Please sign in to comment.