]> git.sesse.net Git - vlc/blob - activex/utils.h
5e8bea5f3e27a3bf08cf09a8e852b3a817e430a3
[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 *CStrFromWSTR(UINT codePage, LPCWSTR wstr, UINT len);
32 extern char *CStrFromBSTR(UINT codePage, BSTR bstr);
33 extern BSTR BSTRFromCStr(UINT codePage, LPCSTR s);
34
35 // properties
36 extern HRESULT GetObjectProperty(LPUNKNOWN object, DISPID dispID, VARIANT& v);
37
38 // properties
39 extern HDC CreateDevDC(DVTARGETDEVICE *ptd);
40 extern void DPFromHimetric(HDC hdc, LPPOINT pt, int count);
41 extern void HimetricFromDP(HDC hdc, LPPOINT pt, int count);
42
43 // URL
44 extern LPWSTR CombineURL(LPCWSTR baseUrl, LPCWSTR url);
45
46 /**************************************************************************************************/
47
48 /* this function object is used to dereference the iterator into a value */
49 template <typename T, class Iterator>
50 struct VLCDereference
51 {
52     T operator()(const Iterator& i) const
53     {
54         return *i;
55     };
56 };
57
58 template<REFIID EnumeratorIID, class Enumerator, typename T, class Iterator, typename Dereference = VLCDereference<T, Iterator> >
59 class VLCEnumIterator : public Enumerator
60 {
61
62 public:
63
64     VLCEnumIterator(const Iterator& from, const Iterator& to) :
65         _refcount(1),
66         _begin(from),
67         _curr(from),
68         _end(to)
69     {};
70
71     VLCEnumIterator(const VLCEnumIterator& e) :
72         Enumerator(),
73         _refcount(e._refcount),
74         _begin(e._begin),
75         _curr(e._curr),
76         _end(e._end)
77     {};
78
79     virtual ~VLCEnumIterator()
80     {};
81
82     // IUnknown methods
83     STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
84     {
85         if( NULL == ppv )
86             return E_POINTER;
87         if( (IID_IUnknown == riid) 
88          || (EnumeratorIID == riid) )
89         {
90             AddRef();
91             *ppv = reinterpret_cast<LPVOID>(this);
92             return NOERROR;
93         }
94         // standalone object
95         return E_NOINTERFACE;
96     };
97
98     STDMETHODIMP_(ULONG) AddRef(void)
99     {
100         return InterlockedIncrement(&_refcount);
101     };
102
103     STDMETHODIMP_(ULONG) Release(void)
104     {
105         ULONG refcount = InterlockedDecrement(&_refcount);
106         if( 0 == refcount )
107         {
108             delete this;
109             return 0;
110         }
111         return refcount;
112     };
113
114
115     // IEnumXXXX methods
116     STDMETHODIMP Next(ULONG celt, T *rgelt, ULONG *pceltFetched)
117     {
118         if( NULL == rgelt )
119             return E_POINTER;
120
121         if( (celt > 1) && (NULL == pceltFetched) )
122             return E_INVALIDARG;
123
124         ULONG c = 0;
125
126         while( (c < celt) && (_curr != _end) )
127         {
128             rgelt[c] = dereference(_curr);
129             ++_curr;
130             ++c;
131         }
132
133         if( NULL != pceltFetched )
134             *pceltFetched = c;
135
136         return (c == celt) ? S_OK : S_FALSE;
137     };
138
139     STDMETHODIMP Skip(ULONG celt)
140     {
141         ULONG c = 0;
142
143         while( (c < celt) && (_curr != _end) )
144         {
145             ++_curr;
146             ++c;
147         }
148         return (c == celt) ? S_OK : S_FALSE;
149     };
150
151     STDMETHODIMP Reset(void)
152     {
153         _curr = _begin;
154         return S_OK;
155     };
156
157     STDMETHODIMP Clone(Enumerator **ppEnum)
158     {
159         if( NULL == ppEnum )
160             return E_POINTER;
161         *ppEnum = dynamic_cast<Enumerator *>(new VLCEnumIterator(*this));
162         return (NULL != *ppEnum ) ? S_OK : E_OUTOFMEMORY;
163     };
164
165 private:
166
167     LONG     _refcount;
168     Iterator _begin, _curr, _end;
169
170     Dereference dereference;
171
172 };
173
174 #endif
175