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