cid#1607171 Data race condition
[LibreOffice.git] / extensions / test / ole / OleConverterVar1 / smartarray.h
blob1c8fbd6e6e926146b01babe5168b05d69eb31f5d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #pragma once
22 template< class sourceType>
23 class SmartArray
25 SAFEARRAY *m_array;
26 public:
28 SmartArray( sourceType * parParams, int count, VARTYPE destVartype): m_array(NULL)
30 HRESULT hr= S_OK;
31 SAFEARRAYBOUND rgsabound[1];
32 rgsabound[0].cElements= count;
33 rgsabound[0].lLbound= 0;
34 m_array= SafeArrayCreate( destVartype, 1, rgsabound);
35 SafeArrayLock( m_array);
37 void* pData;
38 if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) )
41 for( int i=0; i< count; i++)
43 CComVariant varSource( parParams[i]);
44 switch (destVartype)
46 case VT_I1:
48 char* p= (char*) pData;
49 if( SUCCEEDED( hr= varSource.ChangeType( destVartype)))
50 p[i]= V_I1( &varSource);
51 break;
53 case VT_I2:
55 short* p= (short*) pData;
56 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
57 p[i]= V_I2( &varSource);
58 break;
60 case VT_UI2:
62 unsigned short* p= (unsigned short*) pData;
63 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
64 p[i]= V_UI2( &varSource);
65 break;
67 case VT_I4:
69 long* p= (long*)pData;
70 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
71 p[i]= V_I4( &varSource);
72 break;
74 case VT_UI4:
76 unsigned long* p= (unsigned long*)pData;
77 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
78 p[i]= V_UI4( &varSource);
79 break;
81 case VT_R4:
83 float* p= (float*)pData;
84 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
85 p[i]= V_R4( &varSource);
86 break;
88 case VT_R8:
90 double* p= (double*)pData;
91 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
92 p[i]= V_R8( &varSource);
93 break;
95 case VT_BOOL:
97 VARIANT_BOOL* p= (VARIANT_BOOL*)pData;
98 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
99 p[i]= V_BOOL( &varSource);
100 break;
102 case VT_BSTR:
104 BSTR* pBstr= ( BSTR*)pData;
105 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
106 pBstr[i]= SysAllocString(V_BSTR( &varSource));
107 break;
109 case VT_VARIANT:
111 VARIANT *pVariant= (VARIANT*)pData;
112 hr= VariantCopy( &pVariant[i], &varSource); break;
114 // case VT_UNKNOWN:
115 // {
116 // long* pUnk= (long*)pData;
117 // pUnk[i]= reinterpret_cast<long>(parParams[i]);
118 // ((IUnknown*)pUnk[i])->AddRef(); break;
119 // }
120 // case VT_DISPATCH:
121 // {
122 // long* pDisp= (long*)pData;
123 // pDisp[i]= (long)parParams[i];
124 // ((IDispatch*)pDisp[i])->AddRef(); break;
125 // }
126 default:
127 hr= E_FAIL;
130 if( FAILED( hr))
132 SafeArrayDestroy( m_array);
133 m_array= NULL;
136 SafeArrayUnaccessData( m_array);
138 ~SmartArray(){
139 SafeArrayUnlock( m_array);
140 SafeArrayDestroy( m_array );
143 operator bool (){ return m_array == NULL ? false : true; }
145 operator SAFEARRAY* (){ return m_array;}
149 template<>
150 class SmartArray<IUnknown*>
152 SAFEARRAY *m_array;
153 public:
155 SmartArray( sourceType * parParams, int count, VARTYPE destVartype);
156 // {
157 // ATLTRACE("SmartArray<IUnknown>");
158 // HRESULT hr= S_OK;
159 // SAFEARRAYBOUND rgsabound[1];
160 // rgsabound[0].cElements= count;
161 // rgsabound[0].lLbound= 0;
162 // m_array= SafeArrayCreateVector( VT_UNKNOWN, 0, count);
163 // SafeArrayLock( m_array);
165 // IUnknown* *pData;
166 // if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) )
167 // {
169 // for( int i=0; i< count; i++)
170 // {
171 // CComVariant varSource( parParams[i]);
172 // switch (destVartype)
173 // {
175 // case VT_UNKNOWN:
176 // {
177 // pData[i]= parParams[i];
178 // pData[i]->AddRef();
179 // }
180 // default:
181 // hr= E_FAIL;
182 // }
183 // }
184 // if( FAILED( hr))
185 // {
186 // SafeArrayDestroy( m_array);
187 // m_array= NULL;
188 // }
189 // }
190 // SafeArrayUnaccessData( m_array);
191 // }
192 ~SmartArray(){
193 SafeArrayUnlock( m_array);
194 SafeArrayDestroy( m_array );
197 operator bool (){ return m_array == NULL ? false : true; }
199 operator SAFEARRAY* (){ return m_array;}
203 template <> SmartArray <IUnknown*>::SmartArray(sourceType * parParams, int count, VARTYPE destVartype):m_array(NULL)
205 ATLTRACE("SmartArray<IUnknown>");
206 HRESULT hr= S_OK;
207 m_array= SafeArrayCreateVector( VT_UNKNOWN, 0, count);
208 SafeArrayLock( m_array);
210 IUnknown* *pData;
211 if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) )
213 for( int i=0; i< count; i++)
215 pData[i]= parParams[i];
216 pData[i]->AddRef();
219 SafeArrayUnaccessData( m_array);
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */