]> git.sesse.net Git - vlc/blob - activex/utils.h
A bit of cleanup and test
[vlc] / activex / utils.h
1 /*****************************************************************************
2  * utils.h: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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(UINT codePage, BSTR bstr);
32 extern BSTR BSTRFromCStr(UINT codePage, LPCSTR s);
33
34 extern char *CStrFromGUID(REFGUID clsid);
35
36 // properties
37 extern HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v);
38
39 // properties
40 extern HDC CreateDevDC(DVTARGETDEVICE *ptd);
41 extern void DPFromHimetric(HDC hdc, LPPOINT pt, int count);
42 extern void HimetricFromDP(HDC hdc, LPPOINT pt, int count);
43
44
45 // enumeration
46 template<class T> class VLCEnum : IUnknown
47 {
48
49 public:
50
51     VLCEnum(REFIID riid, std::vector<T> &);
52     VLCEnum(const VLCEnum<T> &);
53     virtual ~VLCEnum() {};
54
55     VLCEnum<T>& operator=(const VLCEnum<T> &t);
56
57     // IUnknown methods
58     STDMETHODIMP QueryInterface(REFIID riid, void **);
59     STDMETHODIMP_(ULONG) AddRef(void);
60     STDMETHODIMP_(ULONG) Release(void);
61
62     // IEnumXXXX methods
63     STDMETHODIMP Next(ULONG, T *, ULONG *);
64     STDMETHODIMP Skip(ULONG);
65     STDMETHODIMP Reset(void);
66     // cloning is implemented by subclasses and must use copy constructor
67     //STDMETHODIMP Clone(VLCEnum<T> **);
68
69     typedef void (*retainer)(T);
70
71     void setRetainOperation(retainer retain) { _retain = retain; };
72
73 private:
74
75     LONG                                _refcount;
76     std::vector<T>                      _v;
77     typename std::vector<T>::iterator   _i;
78     REFIID                              _riid;
79     retainer                            _retain;
80 };
81
82 template<class T>
83 VLCEnum<T>::VLCEnum(REFIID riid, std::vector<T> &v) :
84     _refcount(1),
85     _v(v),
86     _riid(riid),
87     _retain(NULL)
88 {
89     _i= v.begin();
90 };
91
92 template<class T>
93 VLCEnum<T>::VLCEnum(const VLCEnum<T> &e) :
94     _refcount(1),
95     _v(e._v),
96     _riid(e._riid)
97 {
98 };
99
100 template<class T>
101 VLCEnum<T>& VLCEnum<T>::operator=(const VLCEnum<T> &e)
102 {
103     this->_refcount = 1;
104     this->_riid = e._riid;
105     this->_v    = e._v;
106     this->_i    = e._i;
107     return this;
108 };
109
110 template<class T>
111 STDMETHODIMP VLCEnum<T>::QueryInterface(REFIID riid, void **ppv)
112 {
113     if( NULL == ppv )
114         return E_POINTER;
115     if( (IID_IUnknown == riid) 
116      || (_riid == riid) )
117     {
118         AddRef();
119         *ppv = reinterpret_cast<LPVOID>(this);
120         return NOERROR;
121     }
122     // standalone object
123     return E_NOINTERFACE;
124 };
125
126 template<class T>
127 STDMETHODIMP_(ULONG) VLCEnum<T>::AddRef(void)
128 {
129     return InterlockedIncrement(&_refcount);
130 };
131
132 template<class T>
133 STDMETHODIMP_(ULONG) VLCEnum<T>::Release(void)
134 {
135     ULONG refcount = InterlockedDecrement(&_refcount);
136     if( 0 == refcount )
137     {
138         delete this;
139         return 0;
140     }
141     return refcount;
142 };
143
144 template<class T>
145 STDMETHODIMP VLCEnum<T>::Next(ULONG celt, T *rgelt, ULONG *pceltFetched)
146 {
147     if( NULL == rgelt )
148         return E_POINTER;
149
150     if( (celt > 1) && (NULL == pceltFetched) )
151         return E_INVALIDARG;
152
153     ULONG c = 0;
154     typename std::vector<T>::iterator end = _v.end();
155
156     while( (c < celt) && (_i != end) )
157     {
158         rgelt[c] = *_i;
159         if( NULL != _retain ) _retain(rgelt[c]);
160         ++_i;
161         ++c;
162     }
163
164     if( NULL != pceltFetched )
165         *pceltFetched = c;
166
167     return (c == celt) ? S_OK : S_FALSE;
168 };
169
170 template<class T>
171 STDMETHODIMP VLCEnum<T>::Skip(ULONG celt)
172 {
173     ULONG c = 0;
174     typename std::vector<T>::iterator end = _v.end();
175
176     while( (c < celt) && (_i != end) )
177     {
178         ++_i;
179         ++c;
180     }
181     return (c == celt) ? S_OK : S_FALSE;
182 };
183
184 template<class T>
185 STDMETHODIMP VLCEnum<T>::Reset(void)
186 {
187     _i= _v.begin();
188     return S_OK;
189 };
190
191 #endif
192