utils.h 4.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * utils.h: ActiveX control for VLC
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 22 23 24 25 26 27 28 29 30
 *****************************************************************************/

#ifndef __UTILS_H__
#define __UTILS_H__

#include <ole2.h>

#include <vector>

// utilities
31
extern char *CStrFromWSTR(UINT codePage, LPCWSTR wstr, UINT len);
32 33
extern char *CStrFromBSTR(UINT codePage, BSTR bstr);
extern BSTR BSTRFromCStr(UINT codePage, LPCSTR s);
34 35 36 37

// properties
extern HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v);

Damien Fouilleul's avatar
Damien Fouilleul committed
38 39
// properties
extern HDC CreateDevDC(DVTARGETDEVICE *ptd);
40 41 42
extern void DPFromHimetric(HDC hdc, LPPOINT pt, int count);
extern void HimetricFromDP(HDC hdc, LPPOINT pt, int count);

43 44 45
// URL
extern LPWSTR CombineURL(LPCWSTR baseUrl, LPCWSTR url);

46
/**************************************************************************************************/
Damien Fouilleul's avatar
Damien Fouilleul committed
47

48
/* this function object is used to dereference the iterator into a value */
49
template <typename T, class Iterator>
50 51 52 53 54
struct VLCDereference
{
    T operator()(const Iterator& i) const
    {
        return *i;
55
    }
56 57
};

58
template<REFIID EnumeratorIID, class Enumerator, typename T, class Container, typename Dereference = VLCDereference<T, typename Container::iterator> >
59
class VLCEnumIterator : public Enumerator
60 61 62 63
{

public:

64
    VLCEnumIterator(Container& container) :
65
        _refcount(1),
66 67 68 69 70 71 72 73 74
        _begin(begin(container)),
        _curr(_begin),
        _end(end(container))
    {
    }

    VLCEnumIterator(const VLCEnumIterator& e) = default;

    virtual ~VLCEnumIterator() = default;
75 76

    // IUnknown methods
77 78 79 80
    STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
    {
        if( NULL == ppv )
            return E_POINTER;
81
        if( (IID_IUnknown == riid)
82 83 84 85 86 87 88 89
         || (EnumeratorIID == riid) )
        {
            AddRef();
            *ppv = reinterpret_cast<LPVOID>(this);
            return NOERROR;
        }
        // standalone object
        return E_NOINTERFACE;
90
    }
91

92
    STDMETHODIMP_(ULONG) AddRef()
93 94
    {
        return InterlockedIncrement(&_refcount);
95
    }
96

97 98 99 100 101 102 103 104 105
    STDMETHODIMP_(ULONG) Release(void)
    {
        ULONG refcount = InterlockedDecrement(&_refcount);
        if( 0 == refcount )
        {
            delete this;
            return 0;
        }
        return refcount;
106
    }
107 108


109 110 111 112 113
    // IEnumXXXX methods
    STDMETHODIMP Next(ULONG celt, T *rgelt, ULONG *pceltFetched)
    {
        if( NULL == rgelt )
            return E_POINTER;
114

115 116
        if( (celt > 1) && (NULL == pceltFetched) )
            return E_INVALIDARG;
117

118
        ULONG c = 0;
119

120 121 122 123 124 125
        while( (c < celt) && (_curr != _end) )
        {
            rgelt[c] = dereference(_curr);
            ++_curr;
            ++c;
        }
126

127 128
        if( NULL != pceltFetched )
            *pceltFetched = c;
129

130
        return (c == celt) ? S_OK : S_FALSE;
131
    }
132

133
    STDMETHODIMP Skip(ULONG celt)
134
    {
135
        ULONG c = 0;
136

137 138 139 140 141 142
        while( (c < celt) && (_curr != _end) )
        {
            ++_curr;
            ++c;
        }
        return (c == celt) ? S_OK : S_FALSE;
143
    }
144

145
    STDMETHODIMP Reset()
146
    {
147 148
        _curr = _begin;
        return S_OK;
149
    }
150

151
    STDMETHODIMP Clone(Enumerator **ppEnum)
152
    {
153 154
        if( NULL == ppEnum )
            return E_POINTER;
155
        *ppEnum = new VLCEnumIterator(*this);
156
        return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY;
157
    }
158

159
private:
160

161
    LONG     _refcount;
162
    typename Container::iterator _begin, _curr, _end;
163

164
    Dereference dereference;
165 166 167 168
};

#endif