Update ooo320-m1
[ooovba.git] / extensions / test / ole / OleConverterVar1 / smartarray.h
blob733c5d6426da7bfef5a75efcc8d61fba3976b224
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: smartarray.h,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef _SMARTARRAY_H
31 #define _SMARTARRAY_H
34 template< class sourceType>
35 class SmartArray
37 SAFEARRAY *m_array;
38 public:
40 SmartArray( sourceType * parParams, int count, VARTYPE destVartype): m_array(NULL)
42 HRESULT hr= S_OK;
43 SAFEARRAYBOUND rgsabound[1];
44 rgsabound[0].cElements= count;
45 rgsabound[0].lLbound= 0;
46 m_array= SafeArrayCreate( destVartype, 1, rgsabound);
47 SafeArrayLock( m_array);
49 void* pData;
50 if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) )
53 for( int i=0; i< count; i++)
55 CComVariant varSource( parParams[i]);
56 switch (destVartype)
58 case VT_I1:
60 char* p= (char*) pData;
61 if( SUCCEEDED( hr= varSource.ChangeType( destVartype)))
62 p[i]= V_I1( &varSource);
63 break;
65 case VT_I2:
67 short* p= (short*) pData;
68 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
69 p[i]= V_I2( &varSource);
70 break;
72 case VT_UI2:
74 unsigned short* p= (unsigned short*) pData;
75 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
76 p[i]= V_UI2( &varSource);
77 break;
79 case VT_I4:
81 long* p= (long*)pData;
82 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
83 p[i]= V_I4( &varSource);
84 break;
86 case VT_UI4:
88 unsigned long* p= (unsigned long*)pData;
89 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
90 p[i]= V_UI4( &varSource);
91 break;
93 case VT_R4:
95 float* p= (float*)pData;
96 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
97 p[i]= V_R4( &varSource);
98 break;
100 case VT_R8:
102 double* p= (double*)pData;
103 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
104 p[i]= V_R8( &varSource);
105 break;
107 case VT_BOOL:
109 VARIANT_BOOL* p= (VARIANT_BOOL*)pData;
110 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
111 p[i]= V_BOOL( &varSource);
112 break;
114 case VT_BSTR:
116 BSTR* pBstr= ( BSTR*)pData;
117 if( SUCCEEDED( hr=varSource.ChangeType( destVartype)))
118 pBstr[i]= SysAllocString(V_BSTR( &varSource));
119 break;
121 case VT_VARIANT:
123 VARIANT *pVariant= (VARIANT*)pData;
124 hr= VariantCopy( &pVariant[i], &varSource); break;
126 // case VT_UNKNOWN:
127 // {
128 // long* pUnk= (long*)pData;
129 // pUnk[i]= reinterpret_cast<long>(parParams[i]);
130 // ((IUnknown*)pUnk[i])->AddRef(); break;
131 // }
132 // case VT_DISPATCH:
133 // {
134 // long* pDisp= (long*)pData;
135 // pDisp[i]= (long)parParams[i];
136 // ((IDispatch*)pDisp[i])->AddRef(); break;
137 // }
138 default:
139 hr= E_FAIL;
142 if( FAILED( hr))
144 SafeArrayDestroy( m_array);
145 m_array= NULL;
148 SafeArrayUnaccessData( m_array);
150 ~SmartArray(){
151 SafeArrayUnlock( m_array);
152 SafeArrayDestroy( m_array );
155 operator bool (){ return m_array == NULL ? false : true; }
157 operator SAFEARRAY* (){ return m_array;}
161 template<>
162 class SmartArray<IUnknown*>
164 SAFEARRAY *m_array;
165 public:
167 SmartArray( sourceType * parParams, int count, VARTYPE destVartype);
168 // {
169 // ATLTRACE("SmartArray<IUnknown>");
170 // HRESULT hr= S_OK;
171 // SAFEARRAYBOUND rgsabound[1];
172 // rgsabound[0].cElements= count;
173 // rgsabound[0].lLbound= 0;
174 // m_array= SafeArrayCreateVector( VT_UNKNOWN, 0, count);
175 // SafeArrayLock( m_array);
177 // IUnknown* *pData;
178 // if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) )
179 // {
181 // for( int i=0; i< count; i++)
182 // {
183 // CComVariant varSource( parParams[i]);
184 // switch (destVartype)
185 // {
187 // case VT_UNKNOWN:
188 // {
189 // pData[i]= parParams[i];
190 // pData[i]->AddRef();
191 // }
192 // default:
193 // hr= E_FAIL;
194 // }
195 // }
196 // if( FAILED( hr))
197 // {
198 // SafeArrayDestroy( m_array);
199 // m_array= NULL;
200 // }
201 // }
202 // SafeArrayUnaccessData( m_array);
203 // }
204 ~SmartArray(){
205 SafeArrayUnlock( m_array);
206 SafeArrayDestroy( m_array );
209 operator bool (){ return m_array == NULL ? false : true; }
211 operator SAFEARRAY* (){ return m_array;}
215 template <> SmartArray <IUnknown*>::SmartArray(sourceType * parParams, int count, VARTYPE destVartype):m_array(NULL)
217 ATLTRACE("SmartArray<IUnknown>");
218 HRESULT hr= S_OK;
219 SAFEARRAYBOUND rgsabound[1];
220 rgsabound[0].cElements= count;
221 rgsabound[0].lLbound= 0;
222 m_array= SafeArrayCreateVector( VT_UNKNOWN, 0, count);
223 SafeArrayLock( m_array);
225 IUnknown* *pData;
226 if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) )
228 for( int i=0; i< count; i++)
230 pData[i]= parParams[i];
231 pData[i]->AddRef();
234 SafeArrayUnaccessData( m_array);
236 #endif