utils.h 4.41 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 55 56 57
struct VLCDereference
{
    T operator()(const Iterator& i) const
    {
        return *i;
    };
};

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

public:

64 65 66 67 68 69 70 71 72 73 74 75 76 77
    VLCEnumIterator(const Iterator& from, const Iterator& to) :
        _refcount(1),
        _begin(from),
        _curr(from),
        _end(to)
    {};

    VLCEnumIterator(const VLCEnumIterator& e) :
        Enumerator(),
        _refcount(e._refcount),
        _begin(e._begin),
        _curr(e._curr),
        _end(e._end)
    {};
78

79 80
    virtual ~VLCEnumIterator()
    {};
81 82

    // IUnknown methods
83 84 85 86
    STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
    {
        if( NULL == ppv )
            return E_POINTER;
87
        if( (IID_IUnknown == riid)
88 89 90 91 92 93 94 95 96 97 98 99 100 101
         || (EnumeratorIID == riid) )
        {
            AddRef();
            *ppv = reinterpret_cast<LPVOID>(this);
            return NOERROR;
        }
        // standalone object
        return E_NOINTERFACE;
    };

    STDMETHODIMP_(ULONG) AddRef(void)
    {
        return InterlockedIncrement(&_refcount);
    };
102

103 104 105 106 107 108 109 110 111 112
    STDMETHODIMP_(ULONG) Release(void)
    {
        ULONG refcount = InterlockedDecrement(&_refcount);
        if( 0 == refcount )
        {
            delete this;
            return 0;
        }
        return refcount;
    };
113 114


115 116 117 118 119
    // IEnumXXXX methods
    STDMETHODIMP Next(ULONG celt, T *rgelt, ULONG *pceltFetched)
    {
        if( NULL == rgelt )
            return E_POINTER;
120

121 122
        if( (celt > 1) && (NULL == pceltFetched) )
            return E_INVALIDARG;
123

124
        ULONG c = 0;
125

126 127 128 129 130 131
        while( (c < celt) && (_curr != _end) )
        {
            rgelt[c] = dereference(_curr);
            ++_curr;
            ++c;
        }
132

133 134
        if( NULL != pceltFetched )
            *pceltFetched = c;
135

136 137
        return (c == celt) ? S_OK : S_FALSE;
    };
138

139
    STDMETHODIMP Skip(ULONG celt)
140
    {
141
        ULONG c = 0;
142

143 144 145 146 147 148 149
        while( (c < celt) && (_curr != _end) )
        {
            ++_curr;
            ++c;
        }
        return (c == celt) ? S_OK : S_FALSE;
    };
150

151
    STDMETHODIMP Reset(void)
152
    {
153 154 155
        _curr = _begin;
        return S_OK;
    };
156

157
    STDMETHODIMP Clone(Enumerator **ppEnum)
158
    {
159 160 161 162 163
        if( NULL == ppEnum )
            return E_POINTER;
        *ppEnum = dynamic_cast<Enumerator *>(new VLCEnumIterator(*this));
        return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY;
    };
164

165
private:
166

167 168
    LONG     _refcount;
    Iterator _begin, _curr, _end;
169

170
    Dereference dereference;
171 172 173 174 175

};

#endif