utils.h 4.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
/*****************************************************************************
 * utils.h: ActiveX control for VLC
 *****************************************************************************
 * Copyright (C) 2005 VideoLAN
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#ifndef __UTILS_H__
#define __UTILS_H__

#include <ole2.h>

#include <vector>

// utilities
extern char *CStrFromBSTR(int codePage, BSTR bstr);
extern BSTR BSTRFromCStr(int codePage, const char *s);

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

// enumeration
template<class T> class VLCEnum : IUnknown
{

public:

    VLCEnum(REFIID riid, std::vector<T> &);
    VLCEnum(const VLCEnum<T> &);
    virtual ~VLCEnum() {};

    VLCEnum<T>& operator=(const VLCEnum<T> &t);

    // IUnknown methods
    STDMETHODIMP QueryInterface(REFIID riid, void **);
    STDMETHODIMP_(ULONG) AddRef(void);
    STDMETHODIMP_(ULONG) Release(void);

    // IEnumXXXX methods
    STDMETHODIMP Next(ULONG, T *, ULONG *);
    STDMETHODIMP Skip(ULONG);
    STDMETHODIMP Reset(void);
    // cloning is implemented by subclasses and must use copy constructor
    //STDMETHODIMP Clone(VLCEnum<T> **);
    // cloning is implemented by subclasses and must use copy constructor

    typedef void (*retainer)(T);

    void setRetainOperation(retainer retain) { _retain = retain; };

private:

    LONG                                _refcount;
    std::vector<T>                      _v;
    typename std::vector<T>::iterator   _i;
    REFIID                              _riid;
    retainer                            _retain;
};

template<class T>
VLCEnum<T>::VLCEnum(REFIID riid, std::vector<T> &v) :
    _refcount(1),
    _v(v),
    _riid(riid),
    _retain(NULL)
{
    _i= v.begin();
};

template<class T>
VLCEnum<T>::VLCEnum(const VLCEnum<T> &e) :
    _refcount(1),
    _v(e._v),
    _riid(e._riid)
{
};

template<class T>
VLCEnum<T>& VLCEnum<T>::operator=(const VLCEnum<T> &e)
{
    this->_refcount = 1;
    this->_riid = e._riid;
    this->_v    = e._v;
    this->_i    = e._i;
};

template<class T>
STDMETHODIMP VLCEnum<T>::QueryInterface(REFIID riid, void **ppv)
{
    if( NULL == ppv ) return E_POINTER;
    if( (IID_IUnknown == riid) 
     && ( _riid == riid) ) {
        AddRef();
        *ppv = reinterpret_cast<LPVOID>(this);
        return NOERROR;
    }
    return E_NOINTERFACE;
};

template<class T>
STDMETHODIMP_(ULONG) VLCEnum<T>::AddRef(void)
{
    return InterlockedIncrement(&_refcount);
};

template<class T>
STDMETHODIMP_(ULONG) VLCEnum<T>::Release(void)
{
    ULONG refcount = InterlockedDecrement(&_refcount);
    if( 0 == refcount )
    {
        delete this;
        return 0;
    }
    return refcount;
};

template<class T>
STDMETHODIMP VLCEnum<T>::Next(ULONG celt, T *rgelt, ULONG *pceltFetched)
{
    if( NULL == rgelt )
        return E_POINTER;

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

    ULONG c = 0;
    typename std::vector<T>::iterator end = _v.end();

    while( (c < celt) && (_i != end) )
    {
        rgelt[c] = *_i;
        if( NULL != _retain ) _retain(rgelt[c]);
        ++_i;
        ++c;
    }

    if( NULL != pceltFetched )
        *pceltFetched = c;

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

template<class T>
STDMETHODIMP VLCEnum<T>::Skip(ULONG celt)
{
    ULONG c = 0;
    typename std::vector<T>::iterator end = _v.end();

    while( (c < celt) && (_i != end) )
    {
        ++_i;
        ++c;
    }
    return (c == celt) ? S_OK : S_FALSE;
};

template<class T>
STDMETHODIMP VLCEnum<T>::Reset(void)
{
    _i= _v.begin();
    return S_OK;
};

#endif