1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 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. 14 * 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). 20 * 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. 25 * 26 ************************************************************************/ 27 #ifndef _SMARTARRAY_H 28 #define _SMARTARRAY_H 29 30 31 template< class sourceType> 32 class SmartArray 33 { 34 SAFEARRAY *m_array; 35 public: 36 37 SmartArray( sourceType * parParams, int count, VARTYPE destVartype): m_array(NULL) 38 { 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); 45 46 void* pData; 47 if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) ) 48 { 49 50 for( int i=0; i< count; i++) 51 { 52 CComVariant varSource( parParams[i]); 53 switch (destVartype) 54 { 55 case VT_I1: 56 { 57 char* p= (char*) pData; 58 if( SUCCEEDED( hr= varSource.ChangeType( destVartype))) 59 p[i]= V_I1( &varSource); 60 break; 61 } 62 case VT_I2: 63 { 64 short* p= (short*) pData; 65 if( SUCCEEDED( hr=varSource.ChangeType( destVartype))) 66 p[i]= V_I2( &varSource); 67 break; 68 } 69 case VT_UI2: 70 { 71 unsigned short* p= (unsigned short*) pData; 72 if( SUCCEEDED( hr=varSource.ChangeType( destVartype))) 73 p[i]= V_UI2( &varSource); 74 break; 75 } 76 case VT_I4: 77 { 78 long* p= (long*)pData; 79 if( SUCCEEDED( hr=varSource.ChangeType( destVartype))) 80 p[i]= V_I4( &varSource); 81 break; 82 } 83 case VT_UI4: 84 { 85 unsigned long* p= (unsigned long*)pData; 86 if( SUCCEEDED( hr=varSource.ChangeType( destVartype))) 87 p[i]= V_UI4( &varSource); 88 break; 89 } 90 case VT_R4: 91 { 92 float* p= (float*)pData; 93 if( SUCCEEDED( hr=varSource.ChangeType( destVartype))) 94 p[i]= V_R4( &varSource); 95 break; 96 } 97 case VT_R8: 98 { 99 double* p= (double*)pData; 100 if( SUCCEEDED( hr=varSource.ChangeType( destVartype))) 101 p[i]= V_R8( &varSource); 102 break; 103 } 104 case VT_BOOL: 105 { 106 VARIANT_BOOL* p= (VARIANT_BOOL*)pData; 107 if( SUCCEEDED( hr=varSource.ChangeType( destVartype))) 108 p[i]= V_BOOL( &varSource); 109 break; 110 } 111 case VT_BSTR: 112 { 113 BSTR* pBstr= ( BSTR*)pData; 114 if( SUCCEEDED( hr=varSource.ChangeType( destVartype))) 115 pBstr[i]= SysAllocString(V_BSTR( &varSource)); 116 break; 117 } 118 case VT_VARIANT: 119 { 120 VARIANT *pVariant= (VARIANT*)pData; 121 hr= VariantCopy( &pVariant[i], &varSource); break; 122 } 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; 137 } 138 } 139 if( FAILED( hr)) 140 { 141 SafeArrayDestroy( m_array); 142 m_array= NULL; 143 } 144 } 145 SafeArrayUnaccessData( m_array); 146 } 147 ~SmartArray(){ 148 SafeArrayUnlock( m_array); 149 SafeArrayDestroy( m_array ); 150 } 151 152 operator bool (){ return m_array == NULL ? false : true; } 153 154 operator SAFEARRAY* (){ return m_array;} 155 156 }; 157 158 template<> 159 class SmartArray<IUnknown*> 160 { 161 SAFEARRAY *m_array; 162 public: 163 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); 173 // 174 // IUnknown* *pData; 175 // if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) ) 176 // { 177 // 178 // for( int i=0; i< count; i++) 179 // { 180 // CComVariant varSource( parParams[i]); 181 // switch (destVartype) 182 // { 183 // 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 ); 204 } 205 206 operator bool (){ return m_array == NULL ? false : true; } 207 208 operator SAFEARRAY* (){ return m_array;} 209 210 }; 211 212 template <> SmartArray <IUnknown*>::SmartArray(sourceType * parParams, int count, VARTYPE destVartype):m_array(NULL) 213 { 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); 221 222 IUnknown* *pData; 223 if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) ) 224 { 225 for( int i=0; i< count; i++) 226 { 227 pData[i]= parParams[i]; 228 pData[i]->AddRef(); 229 } 230 } 231 SafeArrayUnaccessData( m_array); 232 }; 233 #endif 234