X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=activex%2Fconnectioncontainer.cpp;h=36e8eba8053319b9211c222888dcb1d864dfe874;hb=1afa10099843ddfe068ee747047442849c8d6ffa;hp=308f2645579b46305358aa6f46a3fb184e877a2e;hpb=35db6dd6ecab2daf4c03ccc2c0be4b25a42c092c;p=vlc diff --git a/activex/connectioncontainer.cpp b/activex/connectioncontainer.cpp index 308f264557..36e8eba805 100644 --- a/activex/connectioncontainer.cpp +++ b/activex/connectioncontainer.cpp @@ -17,7 +17,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ #include "plugin.h" @@ -29,46 +29,62 @@ using namespace std; //////////////////////////////////////////////////////////////////////////////////////////////// -class VLCEnumConnections : public IEnumConnections +/* this function object is used to return the value from a map pair */ +struct VLCEnumConnectionsDereference { -public: - VLCEnumConnections(vector &v) : - e(VLCEnum(IID_IEnumConnections, v)) - { e.setRetainOperation((VLCEnum::retainer)&retain); }; - - VLCEnumConnections(const VLCEnumConnections &vlcEnum) : e(vlcEnum.e) {}; - - virtual ~VLCEnumConnections() {}; - - // IUnknown methods - STDMETHODIMP QueryInterface(REFIID riid, void **ppv) - { return e.QueryInterface(riid, ppv); }; - STDMETHODIMP_(ULONG) AddRef(void) - { return e.AddRef(); }; - STDMETHODIMP_(ULONG) Release(void) - {return e.Release(); }; - - //IEnumConnectionPoints - STDMETHODIMP Next(ULONG celt, LPCONNECTDATA rgelt, ULONG *pceltFetched) - { return e.Next(celt, rgelt, pceltFetched); }; - STDMETHODIMP Skip(ULONG celt) - { return e.Skip(celt);}; - STDMETHODIMP Reset(void) - { return e.Reset();}; - STDMETHODIMP Clone(LPENUMCONNECTIONS *ppenum) - { if( NULL == ppenum ) return E_POINTER; - *ppenum = dynamic_cast(new VLCEnumConnections(*this)); - return (NULL != *ppenum) ? S_OK : E_OUTOFMEMORY; - }; - -private: - - static void retain(CONNECTDATA cd) + CONNECTDATA operator()(const map::iterator& i) { - cd.pUnk->AddRef(); + CONNECTDATA cd; + + cd.dwCookie = i->first; + cd.pUnk = i->second; + return cd; }; +}; - VLCEnum e; +class VLCEnumConnections : public VLCEnumIterator::iterator, + VLCEnumConnectionsDereference> +{ +public: + VLCEnumConnections(map &m) : + VLCEnumIterator::iterator, + VLCEnumConnectionsDereference> (m.begin(), m.end()) + {}; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////// + +/* this function object is used to retain the dereferenced iterator value */ +struct VLCEnumConnectionPointsDereference +{ + LPCONNECTIONPOINT operator()(const vector::iterator& i) + { + LPCONNECTIONPOINT cp = *i; + cp->AddRef(); + return cp; + } +}; + +class VLCEnumConnectionPoints: public VLCEnumIterator::iterator, + VLCEnumConnectionPointsDereference> +{ +public: + VLCEnumConnectionPoints(vector& v) : + VLCEnumIterator::iterator, + VLCEnumConnectionPointsDereference> (v.begin(), v.end()) + {}; }; //////////////////////////////////////////////////////////////////////////////////////////////// @@ -94,31 +110,28 @@ STDMETHODIMP VLCConnectionPoint::GetConnectionPointContainer(LPCONNECTIONPOINTCO STDMETHODIMP VLCConnectionPoint::Advise(IUnknown *pUnk, DWORD *pdwCookie) { + static DWORD dwCookieCounter = 0; + if( (NULL == pUnk) || (NULL == pdwCookie) ) return E_POINTER; - CONNECTDATA cd; - pUnk->AddRef(); - cd.pUnk = pUnk; - *pdwCookie = cd.dwCookie = _connections.size(); - _connections.push_back(cd); + *pdwCookie = ++dwCookieCounter; + _connections[*pdwCookie] = pUnk; return S_OK; }; STDMETHODIMP VLCConnectionPoint::Unadvise(DWORD pdwCookie) { - if( pdwCookie < _connections.size() ) + map::iterator pcd = _connections.find((DWORD)pdwCookie); + if( pcd != _connections.end() ) { - CONNECTDATA cd = _connections[pdwCookie]; - if( NULL != cd.pUnk ) - { - cd.pUnk->Release(); - cd.pUnk = NULL; - return S_OK; - } + pcd->second->Release(); + + _connections.erase(pdwCookie); + return S_OK; } return CONNECT_E_NOCONNECTION; }; @@ -133,18 +146,18 @@ STDMETHODIMP VLCConnectionPoint::EnumConnections(IEnumConnections **ppEnum) return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY; }; -void VLCConnectionPoint::fireEvent(DISPID dispId, DISPPARAMS* pDispParams) +void VLCConnectionPoint::fireEvent(DISPID dispId, DISPPARAMS *pDispParams) { - vector::iterator end = _connections.end(); - vector::iterator iter = _connections.begin(); + map::iterator end = _connections.end(); + map::iterator iter = _connections.begin(); while( iter != end ) { - CONNECTDATA cd = *iter; - if( NULL != cd.pUnk ) + LPUNKNOWN pUnk = iter->second; + if( NULL != pUnk ) { IDispatch *pDisp; - if( SUCCEEDED(cd.pUnk->QueryInterface(IID_IDispatch, (LPVOID *)&pDisp)) ) + if( SUCCEEDED(pUnk->QueryInterface(IID_IDispatch, (LPVOID *)&pDisp)) ) { pDisp->Invoke(dispId, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, pDispParams, NULL, NULL, NULL); pDisp->Release(); @@ -156,16 +169,16 @@ void VLCConnectionPoint::fireEvent(DISPID dispId, DISPPARAMS* pDispParams) void VLCConnectionPoint::firePropChangedEvent(DISPID dispId) { - vector::iterator end = _connections.end(); - vector::iterator iter = _connections.begin(); + map::iterator end = _connections.end(); + map::iterator iter = _connections.begin(); while( iter != end ) { - CONNECTDATA cd = *iter; - if( NULL != cd.pUnk ) + LPUNKNOWN pUnk = iter->second; + if( NULL != pUnk ) { IPropertyNotifySink *pPropSink; - if( SUCCEEDED(cd.pUnk->QueryInterface(IID_IPropertyNotifySink, (LPVOID *)&pPropSink)) ) + if( SUCCEEDED(pUnk->QueryInterface(IID_IPropertyNotifySink, (LPVOID *)&pPropSink)) ) { pPropSink->OnChanged(dispId); pPropSink->Release(); @@ -177,50 +190,6 @@ void VLCConnectionPoint::firePropChangedEvent(DISPID dispId) //////////////////////////////////////////////////////////////////////////////////////////////// -class VLCEnumConnectionPoints : public IEnumConnectionPoints -{ -public: - VLCEnumConnectionPoints(vector &v) : - e(VLCEnum(IID_IEnumConnectionPoints, v)) - { e.setRetainOperation((VLCEnum::retainer)&retain); }; - - VLCEnumConnectionPoints(const VLCEnumConnectionPoints &vlcEnum) : e(vlcEnum.e) {}; - - virtual ~VLCEnumConnectionPoints() {}; - - // IUnknown methods - STDMETHODIMP QueryInterface(REFIID riid, void **ppv) - { return e.QueryInterface(riid, ppv); }; - STDMETHODIMP_(ULONG) AddRef(void) - { return e.AddRef(); }; - STDMETHODIMP_(ULONG) Release(void) - {return e.Release(); }; - - //IEnumConnectionPoints - STDMETHODIMP Next(ULONG celt, LPCONNECTIONPOINT *rgelt, ULONG *pceltFetched) - { return e.Next(celt, rgelt, pceltFetched); }; - STDMETHODIMP Skip(ULONG celt) - { return e.Skip(celt);}; - STDMETHODIMP Reset(void) - { return e.Reset();}; - STDMETHODIMP Clone(LPENUMCONNECTIONPOINTS *ppenum) - { if( NULL == ppenum ) return E_POINTER; - *ppenum = dynamic_cast(new VLCEnumConnectionPoints(*this)); - return (NULL != *ppenum) ? S_OK : E_OUTOFMEMORY; - }; - -private: - - static void retain(LPCONNECTIONPOINT cp) - { - cp->AddRef(); - }; - - VLCEnum e; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////// - VLCDispatchEvent::~VLCDispatchEvent() { //clear event arguments @@ -297,9 +266,9 @@ void VLCConnectionPointContainer::freezeEvents(BOOL freeze) while( ! _q_events.empty() ) { VLCDispatchEvent *ev = _q_events.front(); + _q_events.pop(); _p_events->fireEvent(ev->_dispId, &ev->_dispParams); delete ev; - _q_events.pop(); } } _b_freeze = freeze; @@ -307,11 +276,10 @@ void VLCConnectionPointContainer::freezeEvents(BOOL freeze) void VLCConnectionPointContainer::fireEvent(DISPID dispId, DISPPARAMS* pDispParams) { - VLCDispatchEvent *evt = new VLCDispatchEvent(dispId, *pDispParams); if( _b_freeze ) { // queue event for later use when container is ready - _q_events.push(evt); + _q_events.push(new VLCDispatchEvent(dispId, *pDispParams)); if( _q_events.size() > 10 ) { // too many events in queue, get rid of older one @@ -322,7 +290,6 @@ void VLCConnectionPointContainer::fireEvent(DISPID dispId, DISPPARAMS* pDispPara else { _p_events->fireEvent(dispId, pDispParams); - delete evt; } };