Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

utils.h

00001 /*****************************************************************************
00002  * utils.h: ActiveX control for VLC
00003  *****************************************************************************
00004  * Copyright (C) 2005 the VideoLAN team
00005  *
00006  * Authors: Damien Fouilleul <[email protected]>
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00021  *****************************************************************************/
00022 
00023 #ifndef __UTILS_H__
00024 #define __UTILS_H__
00025 
00026 #include <ole2.h>
00027 
00028 #include <vector>
00029 
00030 // utilities
00031 extern char *CStrFromBSTR(UINT codePage, BSTR bstr);
00032 extern BSTR BSTRFromCStr(UINT codePage, LPCSTR s);
00033 
00034 // properties
00035 extern HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v);
00036 
00037 // properties
00038 extern HDC CreateDevDC(DVTARGETDEVICE *ptd);
00039 extern void DPFromHimetric(HDC hdc, LPPOINT pt, int count);
00040 extern void HimetricFromDP(HDC hdc, LPPOINT pt, int count);
00041 
00042 
00043 // enumeration
00044 template<class T> class VLCEnum : IUnknown
00045 {
00046 
00047 public:
00048 
00049     VLCEnum(REFIID riid, std::vector<T> &);
00050     VLCEnum(const VLCEnum<T> &);
00051     virtual ~VLCEnum() {};
00052 
00053     VLCEnum<T>& operator=(const VLCEnum<T> &t);
00054 
00055     // IUnknown methods
00056     STDMETHODIMP QueryInterface(REFIID riid, void **);
00057     STDMETHODIMP_(ULONG) AddRef(void);
00058     STDMETHODIMP_(ULONG) Release(void);
00059 
00060     // IEnumXXXX methods
00061     STDMETHODIMP Next(ULONG, T *, ULONG *);
00062     STDMETHODIMP Skip(ULONG);
00063     STDMETHODIMP Reset(void);
00064     // cloning is implemented by subclasses and must use copy constructor
00065     //STDMETHODIMP Clone(VLCEnum<T> **);
00066 
00067     typedef void (*retainer)(T);
00068 
00069     void setRetainOperation(retainer retain) { _retain = retain; };
00070 
00071 private:
00072 
00073     LONG                                _refcount;
00074     std::vector<T>                      _v;
00075     typename std::vector<T>::iterator   _i;
00076     REFIID                              _riid;
00077     retainer                            _retain;
00078 };
00079 
00080 template<class T>
00081 VLCEnum<T>::VLCEnum(REFIID riid, std::vector<T> &v) :
00082     _refcount(1),
00083     _v(v),
00084     _riid(riid),
00085     _retain(NULL)
00086 {
00087     _i= v.begin();
00088 };
00089 
00090 template<class T>
00091 VLCEnum<T>::VLCEnum(const VLCEnum<T> &e) :
00092     _refcount(1),
00093     _v(e._v),
00094     _riid(e._riid)
00095 {
00096 };
00097 
00098 template<class T>
00099 VLCEnum<T>& VLCEnum<T>::operator=(const VLCEnum<T> &e)
00100 {
00101     this->_refcount = 1;
00102     this->_riid = e._riid;
00103     this->_v    = e._v;
00104     this->_i    = e._i;
00105     return this;
00106 };
00107 
00108 template<class T>
00109 STDMETHODIMP VLCEnum<T>::QueryInterface(REFIID riid, void **ppv)
00110 {
00111     if( NULL == ppv ) return E_POINTER;
00112     if( (IID_IUnknown == riid) 
00113      && ( _riid == riid) ) {
00114         AddRef();
00115         *ppv = reinterpret_cast<LPVOID>(this);
00116         return NOERROR;
00117     }
00118     return E_NOINTERFACE;
00119 };
00120 
00121 template<class T>
00122 STDMETHODIMP_(ULONG) VLCEnum<T>::AddRef(void)
00123 {
00124     return InterlockedIncrement(&_refcount);
00125 };
00126 
00127 template<class T>
00128 STDMETHODIMP_(ULONG) VLCEnum<T>::Release(void)
00129 {
00130     ULONG refcount = InterlockedDecrement(&_refcount);
00131     if( 0 == refcount )
00132     {
00133         delete this;
00134         return 0;
00135     }
00136     return refcount;
00137 };
00138 
00139 template<class T>
00140 STDMETHODIMP VLCEnum<T>::Next(ULONG celt, T *rgelt, ULONG *pceltFetched)
00141 {
00142     if( NULL == rgelt )
00143         return E_POINTER;
00144 
00145     if( (celt > 1) && (NULL == pceltFetched) )
00146         return E_INVALIDARG;
00147 
00148     ULONG c = 0;
00149     typename std::vector<T>::iterator end = _v.end();
00150 
00151     while( (c < celt) && (_i != end) )
00152     {
00153         rgelt[c] = *_i;
00154         if( NULL != _retain ) _retain(rgelt[c]);
00155         ++_i;
00156         ++c;
00157     }
00158 
00159     if( NULL != pceltFetched )
00160         *pceltFetched = c;
00161 
00162     return (c == celt) ? S_OK : S_FALSE;
00163 };
00164 
00165 template<class T>
00166 STDMETHODIMP VLCEnum<T>::Skip(ULONG celt)
00167 {
00168     ULONG c = 0;
00169     typename std::vector<T>::iterator end = _v.end();
00170 
00171     while( (c < celt) && (_i != end) )
00172     {
00173         ++_i;
00174         ++c;
00175     }
00176     return (c == celt) ? S_OK : S_FALSE;
00177 };
00178 
00179 template<class T>
00180 STDMETHODIMP VLCEnum<T>::Reset(void)
00181 {
00182     _i= _v.begin();
00183     return S_OK;
00184 };
00185 
00186 #endif
00187 

Generated on Tue Dec 20 10:14:18 2005 for vlc-0.8.4a by  doxygen 1.4.2