File: safearray.h

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (394 lines) | stat: -rw-r--r-- 11,528 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
///////////////////////////////////////////////////////////////////////////////
// Name:        msw/ole/safearray.h
// Purpose:     Helpers for working with OLE SAFEARRAYs.
// Author:      PB
// Created:     2012-09-23
// Copyright:   (c) 2012 wxWidgets development team
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#ifndef _MSW_OLE_SAFEARRAY_H_
#define _MSW_OLE_SAFEARRAY_H_

#include "wx/msw/ole/oleutils.h"

#if wxUSE_OLE && wxUSE_VARIANT

/*
    wxSafeArray is wxWidgets wrapper for working with MS Windows SAFEARRAYs.
    It also has convenience functions for converting between SAFEARRAY
    and wxVariant with list type or wxArrayString.
*/

// The base class with type-independent methods. It exists solely in order to
// reduce the template bloat.
class WXDLLIMPEXP_CORE wxSafeArrayBase
{
public:
    // If owns a SAFEARRAY, it's unlocked and destroyed.
    virtual ~wxSafeArrayBase() { Destroy(); }

    // Unlocks and destroys the owned SAFEARRAY.
    void Destroy();

    // Unlocks the owned SAFEARRAY, returns it and gives up its ownership.
    SAFEARRAY* Detach();

    // Returns true if has a valid SAFEARRAY.
    bool HasArray() const { return m_array != NULL; }

    // Returns the number of dimensions.
    size_t GetDim() const;

    // Returns lower bound for dimension dim in bound. Dimensions start at 1.
    bool GetLBound(size_t dim, long& bound) const;

    // Returns upper bound for dimension dim in bound. Dimensions start at 1.
    bool GetUBound(size_t dim, long& bound) const;

    // Returns element count for dimension dim. Dimensions start at 1.
    size_t GetCount(size_t dim) const;

protected:
    // Default constructor, protected so the class can't be used on its own,
    // it's only used as a base class of wxSafeArray<>.
    wxSafeArrayBase()
    {
        m_array = NULL;
    }

    bool Lock();
    bool Unlock();

    SAFEARRAY* m_array;
};

// wxSafeArrayConvertor<> must be specialized for the type in order to allow
// using it with wxSafeArray<>.
//
// We specialize it below for the standard types.
template <VARTYPE varType>
struct wxSafeArrayConvertor {};

/**
    Macro for specializing wxSafeArrayConvertor for simple types.

    The template parameters are:
        - externType: basic C data type, e.g. wxFloat64 or wxInt32
        - varType: corresponding VARIANT type constant, e.g. VT_R8 or VT_I4.
*/
#define wxSPECIALIZE_WXSAFEARRAY_CONVERTOR_SIMPLE(externType, varType) \
template <>                                                 \
struct wxSafeArrayConvertor<varType>                        \
{                                                           \
    typedef externType externT;                             \
    typedef externT    internT;                             \
    static bool ToArray(const externT& from, internT& to)   \
    {                                                       \
        to = from;                                          \
        return true;                                        \
    }                                                       \
    static bool FromArray(const internT& from, externT& to) \
    {                                                       \
        to = from;                                          \
        return true;                                        \
    }                                                       \
}

wxSPECIALIZE_WXSAFEARRAY_CONVERTOR_SIMPLE(wxInt16, VT_I2);
wxSPECIALIZE_WXSAFEARRAY_CONVERTOR_SIMPLE(wxInt32, VT_I4);
wxSPECIALIZE_WXSAFEARRAY_CONVERTOR_SIMPLE(wxFloat32, VT_R4);
wxSPECIALIZE_WXSAFEARRAY_CONVERTOR_SIMPLE(wxFloat64, VT_R8);

// Specialization for VT_BSTR using wxString.
template <>
struct wxSafeArrayConvertor<VT_BSTR>
{
    typedef wxString externT;
    typedef BSTR internT;

    static bool ToArray(const wxString& from, BSTR& to)
    {
        BSTR bstr = wxConvertStringToOle(from);

        if ( !bstr && !from.empty() )
        {
            // BSTR can be NULL for empty strings but if the string was
            // not empty, it means we failed to allocate memory for it.
            return false;
        }
        to = bstr;
        return true;
    }

    static bool FromArray(const BSTR from, wxString& to)
    {
        to = wxConvertStringFromOle(from);
        return true;
    }
};

// Specialization for VT_VARIANT using wxVariant.
template <>
struct wxSafeArrayConvertor<VT_VARIANT>
{
    typedef wxVariant externT;
    typedef VARIANT internT;

    static bool ToArray(const wxVariant& from, VARIANT& to)
    {
        return wxConvertVariantToOle(from, to);
    }

    static bool FromArray(const VARIANT& from, wxVariant& to)
    {
        return wxConvertOleToVariant(from, to);
    }
};


template <VARTYPE varType>
class wxSafeArray : public wxSafeArrayBase
{
public:
    typedef wxSafeArrayConvertor<varType> Convertor;
    typedef typename Convertor::internT internT;
    typedef typename Convertor::externT externT;

    // Default constructor.
    wxSafeArray()
    {
        m_array = NULL;
    }

    // Creates and locks a zero-based one-dimensional SAFEARRAY with the given
    // number of elements.
    bool Create(size_t count)
    {
        SAFEARRAYBOUND bound;

        bound.lLbound = 0;
        bound.cElements = count;
        return Create(&bound, 1);
    }

    // Creates and locks a SAFEARRAY. See SafeArrayCreate() in MSDN
    // documentation for more information.
    bool Create(SAFEARRAYBOUND* bound, size_t dimensions)
    {
        wxCHECK_MSG( !m_array, false, wxS("Can't be created twice") );

        m_array = SafeArrayCreate(varType, dimensions, bound);
        if ( !m_array )
            return false;

        return Lock();
    }

    /**
        Creates a 0-based one-dimensional SAFEARRAY from wxVariant with the
        list type.

        Can be called only for wxSafeArray<VT_VARIANT>.
    */
    bool CreateFromListVariant(const wxVariant& variant)
    {
        wxCHECK(varType == VT_VARIANT, false);
        wxCHECK(variant.GetType() == wxS("list"), false);

        if ( !Create(variant.GetCount()) )
            return false;

        VARIANT* data = static_cast<VARIANT*>(m_array->pvData);

        for ( size_t i = 0; i < variant.GetCount(); i++)
        {
            if ( !Convertor::ToArray(variant[i], data[i]) )
                return false;
        }
        return true;
    }

    /**
        Creates a 0-based one-dimensional SAFEARRAY from wxArrayString.

        Can be called only for wxSafeArray<VT_BSTR>.
    */
    bool CreateFromArrayString(const wxArrayString& strings)
    {
        wxCHECK(varType == VT_BSTR, false);

        if ( !Create(strings.size()) )
            return false;

        BSTR* data = static_cast<BSTR*>(m_array->pvData);

        for ( size_t i = 0; i < strings.size(); i++ )
        {
            if ( !Convertor::ToArray(strings[i], data[i]) )
                return false;
        }
        return true;
    }

    /**
        Attaches and locks an existing SAFEARRAY.
        The array must have the same VARTYPE as this wxSafeArray was
        instantiated with.
    */
    bool Attach(SAFEARRAY* array)
    {
        wxCHECK_MSG(!m_array && array, false,
                    wxS("Can only attach a valid array to an uninitialized one") );

        VARTYPE vt;
        HRESULT hr = SafeArrayGetVartype(array, &vt);
        if ( FAILED(hr) )
        {
            wxLogApiError(wxS("SafeArrayGetVarType()"), hr);
            return false;
        }

        wxCHECK_MSG(vt == varType, false,
                    wxS("Attaching array of invalid type"));

        m_array = array;
        return Lock();
    }

    /**
        Indices have the same row-column order as rgIndices in
        SafeArrayPutElement(), i.e. they follow BASIC rules, NOT C ones.
    */
    bool SetElement(long* indices, const externT& element)
    {
        wxCHECK_MSG( m_array, false, wxS("Uninitialized array") );
        wxCHECK_MSG( indices, false, wxS("Invalid index") );

        internT* data;

        if ( FAILED( SafeArrayPtrOfIndex(m_array, (LONG *)indices, (void**)&data) ) )
            return false;

        return Convertor::ToArray(element, *data);
    }

    /**
        Indices have the same row-column order as rgIndices in
        SafeArrayPutElement(), i.e. they follow BASIC rules, NOT C ones.
    */
    bool GetElement(long* indices, externT& element) const
    {
        wxCHECK_MSG( m_array, false, wxS("Uninitialized array") );
        wxCHECK_MSG( indices, false, wxS("Invalid index") );

        internT* data;

        if ( FAILED( SafeArrayPtrOfIndex(m_array, (LONG *)indices, (void**)&data) ) )
            return false;

        return Convertor::FromArray(*data, element);
    }

    /**
        Converts the array to a wxVariant with the list type, regardless of the
        underlying SAFEARRAY type.

        If the array is multidimensional, it is flattened using the alghoritm
        originally employed in wxConvertOleToVariant().
    */
    bool ConvertToVariant(wxVariant& variant) const
    {
        wxCHECK_MSG( m_array, false, wxS("Uninitialized array") );

        size_t dims = m_array->cDims;
        size_t count = 1;

        for ( size_t i = 0; i < dims; i++ )
            count *= m_array->rgsabound[i].cElements;

        const internT* data = static_cast<const internT*>(m_array->pvData);
        externT element;

        variant.ClearList();
        for ( size_t i1 = 0; i1 < count; i1++ )
        {
            if ( !Convertor::FromArray(data[i1], element) )
            {
                variant.ClearList();
                return false;
            }
            variant.Append(element);
        }
        return true;
    }

    /**
        Converts an array to an ArrayString.

        Can be called only for wxSafeArray<VT_BSTR>. If the array is
        multidimensional, it is flattened using the alghoritm originally
        employed in wxConvertOleToVariant().
    */
    bool ConvertToArrayString(wxArrayString& strings) const
    {
        wxCHECK_MSG( m_array, false, wxS("Uninitialized array") );
        wxCHECK(varType == VT_BSTR, false);

        size_t dims = m_array->cDims;
        size_t count = 1;

        for ( size_t i = 0; i < dims; i++ )
            count *= m_array->rgsabound[i].cElements;

        const BSTR* data = static_cast<const BSTR*>(m_array->pvData);
        wxString element;

        strings.clear();
        strings.reserve(count);
        for ( size_t i1 = 0; i1 < count; i1++ )
        {
            if ( !Convertor::FromArray(data[i1], element) )
            {
                strings.clear();
                return false;
            }
            strings.push_back(element);
        }
        return true;
    }

    static bool ConvertToVariant(SAFEARRAY* psa, wxVariant& variant)
    {
        wxSafeArray<varType> sa;
        bool result = false;

        if ( sa.Attach(psa) )
            result = sa.ConvertToVariant(variant);

        if ( sa.HasArray() )
            sa.Detach();

        return result;
    }

    static bool ConvertToArrayString(SAFEARRAY* psa, wxArrayString& strings)
    {
        wxSafeArray<varType> sa;
        bool result = false;

        if ( sa.Attach(psa) )
            result = sa.ConvertToArrayString(strings);

        if ( sa.HasArray() )
            sa.Detach();

        return result;
    }

    wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxSafeArray, varType);
};

#endif // wxUSE_OLE && wxUSE_VARIANT

#endif // _MSW_OLE_SAFEARRAY_H_