]> git.sesse.net Git - vlc/blob - activex/utils.h
cdd295b9ce8949fdab961327af2e8f662db91833
[vlc] / activex / utils.h
1 /*****************************************************************************
2  * utils.h: ActiveX control for VLC
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN (Centrale Réseaux) and its contributors
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
64     typedef void (*retainer)(T);
65
66     void setRetainOperation(retainer retain) { _retain = retain; };
67
68 private:
69
70     LONG                                _refcount;
71     std::vector<T>                      _v;
72     typename std::vector<T>::iterator   _i;
73     REFIID                              _riid;
74     retainer                            _retain;
75 };
76
77 template<class T>
78 VLCEnum<T>::VLCEnum(REFIID riid, std::vector<T> &v) :
79     _refcount(1),
80     _v(v),
81     _riid(riid),
82     _retain(NULL)
83 {
84     _i= v.begin();
85 };
86
87 template<class T>
88 VLCEnum<T>::VLCEnum(const VLCEnum<T> &e) :
89     _refcount(1),
90     _v(e._v),
91     _riid(e._riid)
92 {
93 };
94
95 template<class T>
96 VLCEnum<T>& VLCEnum<T>::operator=(const VLCEnum<T> &e)
97 {
98     this->_refcount = 1;
99     this->_riid = e._riid;
100     this->_v    = e._v;
101     this->_i    = e._i;
102 };
103
104 template<class T>
105 STDMETHODIMP VLCEnum<T>::QueryInterface(REFIID riid, void **ppv)
106 {
107     if( NULL == ppv ) return E_POINTER;
108     if( (IID_IUnknown == riid) 
109      && ( _riid == riid) ) {
110         AddRef();
111         *ppv = reinterpret_cast<LPVOID>(this);
112         return NOERROR;
113     }
114     return E_NOINTERFACE;
115 };
116
117 template<class T>
118 STDMETHODIMP_(ULONG) VLCEnum<T>::AddRef(void)
119 {
120     return InterlockedIncrement(&_refcount);
121 };
122
123 template<class T>
124 STDMETHODIMP_(ULONG) VLCEnum<T>::Release(void)
125 {
126     ULONG refcount = InterlockedDecrement(&_refcount);
127     if( 0 == refcount )
128     {
129         delete this;
130         return 0;
131     }
132     return refcount;
133 };
134
135 template<class T>
136 STDMETHODIMP VLCEnum<T>::Next(ULONG celt, T *rgelt, ULONG *pceltFetched)
137 {
138     if( NULL == rgelt )
139         return E_POINTER;
140
141     if( (celt > 1) && (NULL == pceltFetched) )
142         return E_INVALIDARG;
143
144     ULONG c = 0;
145     typename std::vector<T>::iterator end = _v.end();
146
147     while( (c < celt) && (_i != end) )
148     {
149         rgelt[c] = *_i;
150         if( NULL != _retain ) _retain(rgelt[c]);
151         ++_i;
152         ++c;
153     }
154
155     if( NULL != pceltFetched )
156         *pceltFetched = c;
157
158     return (c == celt) ? S_OK : S_FALSE;
159 };
160
161 template<class T>
162 STDMETHODIMP VLCEnum<T>::Skip(ULONG celt)
163 {
164     ULONG c = 0;
165     typename std::vector<T>::iterator end = _v.end();
166
167     while( (c < celt) && (_i != end) )
168     {
169         ++_i;
170         ++c;
171     }
172     return (c == celt) ? S_OK : S_FALSE;
173 };
174
175 template<class T>
176 STDMETHODIMP VLCEnum<T>::Reset(void)
177 {
178     _i= _v.begin();
179     return S_OK;
180 };
181
182 #endif
183