Сущность технологии СОМ. Библиотека программиста   ::   Бокс Дональд

Страница: 522 из 528

begin();

InitializeCriticalSection(&m_csLock);

}

SessionNamesEnumerator::SessionNamesEnumerator(

SessionNamesEnumerator *pCloneSource)

: m_cRef(0),

m_pStrings(0),

m_pCloneSource(pCloneSource)

{

m_pCloneSource->AddRef();

m_cursor = Strings().begin();

InitializeCriticalSection(&m_csLock);

}

SessionNamesEnumerator::~SessionNamesEnumerator(void)

{

if (m_pCloneSource)

m_pCloneSource->Release();

else if (m_pStrings)

delete m_pStrings;

DeleteCriticalSection(&m_csLock);

}

// IUnknown methods

STDMETHODIMP

SessionNamesEnumerator::QueryInterface(REFIID riid, void **ppv)

{

if (riid == IID_IUnknown)

*ppv = static_cast(this);

else if (riid == IID_IEnumString)

*ppv = static_cast(this);

else

return (*ppv = 0), E_NOINTERFACE;

reinterpret_cast(*ppv)->AddRef();

return S_OK;

}

STDMETHODIMP_(ULONG)

SessionNamesEnumerator::AddRef(void)

{

ModuleLock();

return InterlockedIncrement(&m_cRef);

}

STDMETHODIMP_(ULONG)

SessionNamesEnumerator::Release(void)

{

LONG res = InterlockedDecrement(&m_cRef);

if (res == 0)

delete this;

ModuleUnlock();

return res;

}

// IEnumString methods

STDMETHODIMP

SessionNamesEnumerator::Next(ULONG cElems, OLECHAR **rgElems,

ULONG *pcFetched)

{

if (cElems > 1 && pcFetched == 0)

return E_INVALIDARG;

ULONG cActual = 0;

vector&rstrings = Strings();

Lock();

while (cActual < cElems

&& m_cursor != rstrings.end())

{

if (rgElems[cActual] = OLESTRDUP((*m_cursor).c_str()))

{

m_cursor++;

cActual++;

}

else // allocation error, unwind

{

while (cActual > 0)

{

cActual–;

CoTaskMemFree(rgElems[cActual]);

rgElems[cActual] = 0;

}

break;

}

}

Unlock();

if (cActual)

*pcFetched = cActual;

return cActual == cElems ? S_OK : S_FALSE;

}

STDMETHODIMP

SessionNamesEnumerator::Skip(ULONG cElems)

{

ULONG cActual = 0;

|< Пред. 520 521 522 523 524 След. >|

Контакты: [email protected]