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