merged tag ooo/OOO330_m14
[LibreOffice.git] / extensions / test / ole / OleConverterVar1 / smartarray.h
blob273b43a9e6c8647118df06c18c121f185256a404
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 #ifndef _SMARTARRAY_H
28 #define _SMARTARRAY_H
31 template< class sourceType>
32 class SmartArray
34 SAFEARRAY *m_array;
35 public:
37 SmartArray( sourceType * parParams, int count, VARTYPE destVartype): m_array(NULL)
39 HRESULT hr= S_OK;
40 SAFEARRAYBOUND rgsabound[1];
41 rgsabound[0].cElements= count;
42 rgsabound[0].lLbound= 0;
43 m_array= SafeArrayCreate( destVartype, 1, rgsabound);
44 SafeArrayLock( m_array);
46 void* pData;
47 if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) )
50 for( int i=0; i< count; i++)
52 CComVariant varSource( parParams[i]);
53 switch (destVartype)
55 case VT_I1:
57 char* p= (char*) pData;
58 if( SUCCEEDED( hr= varSource.ChangeType( destVartype)))
59 p[i]= V_I1( &varSource);
60 break;
62 case VT_I2:
64 short* p= (short*) pData;
65 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
66 p[i]= V_I2( &varSource);
67 break;
69 case VT_UI2:
71 unsigned short* p= (unsigned short*) pData;
72 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
73 p[i]= V_UI2( &varSource);
74 break;
76 case VT_I4:
78 long* p= (long*)pData;
79 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
80 p[i]= V_I4( &varSource);
81 break;
83 case VT_UI4:
85 unsigned long* p= (unsigned long*)pData;
86 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
87 p[i]= V_UI4( &varSource);
88 break;
90 case VT_R4:
92 float* p= (float*)pData;
93 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
94 p[i]= V_R4( &varSource);
95 break;
97 case VT_R8:
99 double* p= (double*)pData;
100 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
101 p[i]= V_R8( &varSource);
102 break;
104 case VT_BOOL:
106 VARIANT_BOOL* p= (VARIANT_BOOL*)pData;
107 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
108 p[i]= V_BOOL( &varSource);
109 break;
111 case VT_BSTR:
113 BSTR* pBstr= ( BSTR*)pData;
114 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
115 pBstr[i]= SysAllocString(V_BSTR( &varSource));
116 break;
118 case VT_VARIANT:
120 VARIANT *pVariant= (VARIANT*)pData;
121 hr= VariantCopy( &pVariant[i], &varSource); break;
123 // case VT_UNKNOWN:
124 // {
125 // long* pUnk= (long*)pData;
126 // pUnk[i]= reinterpret_cast<long>(parParams[i]);
127 // ((IUnknown*)pUnk[i])->AddRef(); break;
128 // }
129 // case VT_DISPATCH:
130 // {
131 // long* pDisp= (long*)pData;
132 // pDisp[i]= (long)parParams[i];
133 // ((IDispatch*)pDisp[i])->AddRef(); break;
134 // }
135 default:
136 hr= E_FAIL;
139 if( FAILED( hr))
141 SafeArrayDestroy( m_array);
142 m_array= NULL;
145 SafeArrayUnaccessData( m_array);
147 ~SmartArray(){
148 SafeArrayUnlock( m_array);
149 SafeArrayDestroy( m_array );
152 operator bool (){ return m_array == NULL ? false : true; }
154 operator SAFEARRAY* (){ return m_array;}
158 template<>
159 class SmartArray<IUnknown*>
161 SAFEARRAY *m_array;
162 public:
164 SmartArray( sourceType * parParams, int count, VARTYPE destVartype);
165 // {
166 // ATLTRACE("SmartArray<IUnknown>");
167 // HRESULT hr= S_OK;
168 // SAFEARRAYBOUND rgsabound[1];
169 // rgsabound[0].cElements= count;
170 // rgsabound[0].lLbound= 0;
171 // m_array= SafeArrayCreateVector( VT_UNKNOWN, 0, count);
172 // SafeArrayLock( m_array);
174 // IUnknown* *pData;
175 // if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) )
176 // {
178 // for( int i=0; i< count; i++)
179 // {
180 // CComVariant varSource( parParams[i]);
181 // switch (destVartype)
182 // {
184 // case VT_UNKNOWN:
185 // {
186 // pData[i]= parParams[i];
187 // pData[i]->AddRef();
188 // }
189 // default:
190 // hr= E_FAIL;
191 // }
192 // }
193 // if( FAILED( hr))
194 // {
195 // SafeArrayDestroy( m_array);
196 // m_array= NULL;
197 // }
198 // }
199 // SafeArrayUnaccessData( m_array);
200 // }
201 ~SmartArray(){
202 SafeArrayUnlock( m_array);
203 SafeArrayDestroy( m_array );
206 operator bool (){ return m_array == NULL ? false : true; }
208 operator SAFEARRAY* (){ return m_array;}
212 template <> SmartArray <IUnknown*>::SmartArray(sourceType * parParams, int count, VARTYPE destVartype):m_array(NULL)
214 ATLTRACE("SmartArray<IUnknown>");
215 HRESULT hr= S_OK;
216 SAFEARRAYBOUND rgsabound[1];
217 rgsabound[0].cElements= count;
218 rgsabound[0].lLbound= 0;
219 m_array= SafeArrayCreateVector( VT_UNKNOWN, 0, count);
220 SafeArrayLock( m_array);
222 IUnknown* *pData;
223 if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) )
225 for( int i=0; i< count; i++)
227 pData[i]= parParams[i];
228 pData[i]->AddRef();
231 SafeArrayUnaccessData( m_array);
233 #endif