]> git.sesse.net Git - vlc/blob - activex/utils.h
compilation fixes for MSVC
[vlc] / activex / utils.h
1 /*****************************************************************************
2  * utils.h: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
5  *
6  * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 #ifndef __UTILS_H__
24 #define __UTILS_H__
25
26 #include <ole2.h>
27
28 #include <vector>
29
30 // utilities
31 extern char *CStrFromBSTR(int codePage, BSTR bstr);
32 extern BSTR BSTRFromCStr(int codePage, const char *s);
33
34 // properties
35 extern HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v);
36
37 // properties
38 extern HDC CreateDevDC(DVTARGETDEVICE *ptd);
39
40 // enumeration
41 template<class T> class VLCEnum : IUnknown
42 {
43
44 public:
45
46     VLCEnum(REFIID riid, std::vector<T> &);
47     VLCEnum(const VLCEnum<T> &);
48     virtual ~VLCEnum() {};
49
50     VLCEnum<T>& operator=(const VLCEnum<T> &t);
51
52     // IUnknown methods
53     STDMETHODIMP QueryInterface(REFIID riid, void **);
54     STDMETHODIMP_(ULONG) AddRef(void);
55     STDMETHODIMP_(ULONG) Release(void);
56
57     // IEnumXXXX methods
58     STDMETHODIMP Next(ULONG, T *, ULONG *);
59     STDMETHODIMP Skip(ULONG);
60     STDMETHODIMP Reset(void);
61     // cloning is implemented by subclasses and must use copy constructor
62     //STDMETHODIMP Clone(VLCEnum<T> **);
63     // cloning is implemented by subclasses and must use copy constructor
64
65     typedef void (*retainer)(T);
66
67     void setRetainOperation(retainer retain) { _retain = retain; };
68
69 private:
70
71     LONG                                _refcount;
72     std::vector<T>                      _v;
73     typename std::vector<T>::iterator   _i;
74     REFIID                              _riid;
75     retainer                            _retain;
76 };
77
78 template<class T>
79 VLCEnum<T>::VLCEnum(REFIID riid, std::vector<T> &v) :
80     _refcount(1),
81     _v(v),
82     _riid(riid),
83     _retain(NULL)
84 {
85     _i= v.begin();
86 };
87
88 template<class T>
89 VLCEnum<T>::VLCEnum(const VLCEnum<T> &e) :
90     _refcount(1),
91     _v(e._v),
92     _riid(e._riid)
93 {
94 };
95
96 template<class T>
97 VLCEnum<T>& VLCEnum<T>::operator=(const VLCEnum<T> &e)
98 {
99     this->_refcount = 1;
100     this->_riid = e._riid;
101     this->_v    = e._v;
102     this->_i    = e._i;
103 };
104
105 template<class T>
106 STDMETHODIMP VLCEnum<T>::QueryInterface(REFIID riid, void **ppv)
107 {
108     if( NULL == ppv ) return E_POINTER;
109     if( (IID_IUnknown == riid) 
110      && ( _riid == riid) ) {
111         AddRef();
112         *ppv = reinterpret_cast<LPVOID>(this);
113         return NOERROR;
114     }
115     return E_NOINTERFACE;
116 };
117
118 template<class T>
119 STDMETHODIMP_(ULONG) VLCEnum<T>::AddRef(void)
120 {
121     return InterlockedIncrement(&_refcount);
122 };
123
124 template<class T>
125 STDMETHODIMP_(ULONG) VLCEnum<T>::Release(void)
126 {
127     ULONG refcount = InterlockedDecrement(&_refcount);
128     if( 0 == refcount )
129     {
130         delete this;
131         return 0;
132     }
133     return refcount;
134 };
135
136 template<class T>
137 STDMETHODIMP VLCEnum<T>::Next(ULONG celt, T *rgelt, ULONG *pceltFetched)
138 {
139     if( NULL == rgelt )
140         return E_POINTER;
141
142     if( (celt > 1) && (NULL == pceltFetched) )
143         return E_INVALIDARG;
144
145     ULONG c = 0;
146     typename std::vector<T>::iterator end = _v.end();
147
148     while( (c < celt) && (_i != end) )
149     {
150         rgelt[c] = *_i;
151         if( NULL != _retain ) _retain(rgelt[c]);
152         ++_i;
153         ++c;
154     }
155
156     if( NULL != pceltFetched )
157         *pceltFetched = c;
158
159     return (c == celt) ? S_OK : S_FALSE;
160 };
161
162 template<class T>
163 STDMETHODIMP VLCEnum<T>::Skip(ULONG celt)
164 {
165     ULONG c = 0;
166     typename std::vector<T>::iterator end = _v.end();
167
168     while( (c < celt) && (_i != end) )
169     {
170         ++_i;
171         ++c;
172     }
173     return (c == celt) ? S_OK : S_FALSE;
174 };
175
176 template<class T>
177 STDMETHODIMP VLCEnum<T>::Reset(void)
178 {
179     _i= _v.begin();
180     return S_OK;
181 };
182
183 #endif
184