1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: smartarray.h,v $
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 ************************************************************************/
34 template< class sourceType
>
40 SmartArray( sourceType
* parParams
, int count
, VARTYPE destVartype
): m_array(NULL
)
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
);
50 if( m_array
&& (SUCCEEDED( SafeArrayAccessData( m_array
, (void**)&pData
)) ) )
53 for( int i
=0; i
< count
; i
++)
55 CComVariant
varSource( parParams
[i
]);
60 char* p
= (char*) pData
;
61 if( SUCCEEDED( hr
= varSource
.ChangeType( destVartype
)))
62 p
[i
]= V_I1( &varSource
);
67 short* p
= (short*) pData
;
68 if( SUCCEEDED( hr
=varSource
.ChangeType( destVartype
)))
69 p
[i
]= V_I2( &varSource
);
74 unsigned short* p
= (unsigned short*) pData
;
75 if( SUCCEEDED( hr
=varSource
.ChangeType( destVartype
)))
76 p
[i
]= V_UI2( &varSource
);
81 long* p
= (long*)pData
;
82 if( SUCCEEDED( hr
=varSource
.ChangeType( destVartype
)))
83 p
[i
]= V_I4( &varSource
);
88 unsigned long* p
= (unsigned long*)pData
;
89 if( SUCCEEDED( hr
=varSource
.ChangeType( destVartype
)))
90 p
[i
]= V_UI4( &varSource
);
95 float* p
= (float*)pData
;
96 if( SUCCEEDED( hr
=varSource
.ChangeType( destVartype
)))
97 p
[i
]= V_R4( &varSource
);
102 double* p
= (double*)pData
;
103 if( SUCCEEDED( hr
=varSource
.ChangeType( destVartype
)))
104 p
[i
]= V_R8( &varSource
);
109 VARIANT_BOOL
* p
= (VARIANT_BOOL
*)pData
;
110 if( SUCCEEDED( hr
=varSource
.ChangeType( destVartype
)))
111 p
[i
]= V_BOOL( &varSource
);
116 BSTR
* pBstr
= ( BSTR
*)pData
;
117 if( SUCCEEDED( hr
=varSource
.ChangeType( destVartype
)))
118 pBstr
[i
]= SysAllocString(V_BSTR( &varSource
));
123 VARIANT
*pVariant
= (VARIANT
*)pData
;
124 hr
= VariantCopy( &pVariant
[i
], &varSource
); break;
128 // long* pUnk= (long*)pData;
129 // pUnk[i]= reinterpret_cast<long>(parParams[i]);
130 // ((IUnknown*)pUnk[i])->AddRef(); break;
134 // long* pDisp= (long*)pData;
135 // pDisp[i]= (long)parParams[i];
136 // ((IDispatch*)pDisp[i])->AddRef(); break;
144 SafeArrayDestroy( m_array
);
148 SafeArrayUnaccessData( m_array
);
151 SafeArrayUnlock( m_array
);
152 SafeArrayDestroy( m_array
);
155 operator bool (){ return m_array
== NULL
? false : true; }
157 operator SAFEARRAY
* (){ return m_array
;}
162 class SmartArray
<IUnknown
*>
167 SmartArray( sourceType
* parParams
, int count
, VARTYPE destVartype
);
169 // ATLTRACE("SmartArray<IUnknown>");
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);
178 // if( m_array && (SUCCEEDED( SafeArrayAccessData( m_array, (void**)&pData)) ) )
181 // for( int i=0; i< count; i++)
183 // CComVariant varSource( parParams[i]);
184 // switch (destVartype)
189 // pData[i]= parParams[i];
190 // pData[i]->AddRef();
198 // SafeArrayDestroy( m_array);
202 // SafeArrayUnaccessData( m_array);
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>");
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
);
226 if( m_array
&& (SUCCEEDED( SafeArrayAccessData( m_array
, (void**)&pData
)) ) )
228 for( int i
=0; i
< count
; i
++)
230 pData
[i
]= parParams
[i
];
234 SafeArrayUnaccessData( m_array
);