xref: /aoo41x/main/cppu/source/uno/constr.hxx (revision c6ed87c9)
1*c6ed87c9SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c6ed87c9SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c6ed87c9SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c6ed87c9SAndrew Rist  * distributed with this work for additional information
6*c6ed87c9SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c6ed87c9SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c6ed87c9SAndrew Rist  * "License"); you may not use this file except in compliance
9*c6ed87c9SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c6ed87c9SAndrew Rist  *
11*c6ed87c9SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c6ed87c9SAndrew Rist  *
13*c6ed87c9SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c6ed87c9SAndrew Rist  * software distributed under the License is distributed on an
15*c6ed87c9SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c6ed87c9SAndrew Rist  * KIND, either express or implied.  See the License for the
17*c6ed87c9SAndrew Rist  * specific language governing permissions and limitations
18*c6ed87c9SAndrew Rist  * under the License.
19*c6ed87c9SAndrew Rist  *
20*c6ed87c9SAndrew Rist  *************************************************************/
21*c6ed87c9SAndrew Rist 
22*c6ed87c9SAndrew Rist 
23cdf0e10cSrcweir #ifndef CONSTR_HXX
24cdf0e10cSrcweir #define CONSTR_HXX
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "prim.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace cppu
30cdf0e10cSrcweir {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir //##################################################################################################
33cdf0e10cSrcweir //#### construction ################################################################################
34cdf0e10cSrcweir //##################################################################################################
35cdf0e10cSrcweir 
36cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
_defaultConstructUnion(void * pMem,typelib_TypeDescription * pTypeDescr)37cdf0e10cSrcweir inline void _defaultConstructUnion(
38cdf0e10cSrcweir 	void * pMem,
39cdf0e10cSrcweir 	typelib_TypeDescription * pTypeDescr )
40cdf0e10cSrcweir 	SAL_THROW( () )
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 	::uno_type_constructData(
43cdf0e10cSrcweir 		(char *)pMem + ((typelib_UnionTypeDescription *)pTypeDescr)->nValueOffset,
44cdf0e10cSrcweir 		((typelib_UnionTypeDescription *)pTypeDescr)->pDefaultTypeRef );
45cdf0e10cSrcweir 	*(sal_Int64 *)pMem = ((typelib_UnionTypeDescription *)pTypeDescr)->nDefaultDiscriminant;
46cdf0e10cSrcweir }
47cdf0e10cSrcweir //==================================================================================================
48cdf0e10cSrcweir void defaultConstructStruct(
49cdf0e10cSrcweir 	void * pMem,
50cdf0e10cSrcweir 	typelib_CompoundTypeDescription * pCompType )
51cdf0e10cSrcweir 	SAL_THROW( () );
52cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
_defaultConstructStruct(void * pMem,typelib_CompoundTypeDescription * pTypeDescr)53cdf0e10cSrcweir inline void _defaultConstructStruct(
54cdf0e10cSrcweir 	void * pMem,
55cdf0e10cSrcweir 	typelib_CompoundTypeDescription * pTypeDescr )
56cdf0e10cSrcweir 	SAL_THROW( () )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	if (pTypeDescr->pBaseTypeDescription)
59cdf0e10cSrcweir 	{
60cdf0e10cSrcweir 		defaultConstructStruct( pMem, pTypeDescr->pBaseTypeDescription );
61cdf0e10cSrcweir 	}
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	typelib_TypeDescriptionReference ** ppTypeRefs = (pTypeDescr)->ppTypeRefs;
64cdf0e10cSrcweir 	sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets;
65cdf0e10cSrcweir 	sal_Int32 nDescr = pTypeDescr->nMembers;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	while (nDescr--)
68cdf0e10cSrcweir 	{
69cdf0e10cSrcweir 		::uno_type_constructData( (char *)pMem + pMemberOffsets[nDescr], ppTypeRefs[nDescr] );
70cdf0e10cSrcweir 	}
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
_defaultConstructArray(void * pMem,typelib_ArrayTypeDescription * pTypeDescr)74cdf0e10cSrcweir inline void _defaultConstructArray(
75cdf0e10cSrcweir 	void * pMem,
76cdf0e10cSrcweir 	typelib_ArrayTypeDescription * pTypeDescr )
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 	typelib_TypeDescription * pElementType = NULL;
79cdf0e10cSrcweir 	TYPELIB_DANGER_GET( &pElementType, ((typelib_IndirectTypeDescription *)pTypeDescr)->pType );
80cdf0e10cSrcweir 	sal_Int32 nTotalElements = pTypeDescr->nTotalElements;
81cdf0e10cSrcweir 	sal_Int32 nElementSize = pElementType->nSize;
82cdf0e10cSrcweir 	sal_Int32 i;
83cdf0e10cSrcweir 	switch ( pElementType->eTypeClass )
84cdf0e10cSrcweir 	{
85cdf0e10cSrcweir 	case typelib_TypeClass_CHAR:
86cdf0e10cSrcweir 	case typelib_TypeClass_BOOLEAN:
87cdf0e10cSrcweir 	case typelib_TypeClass_BYTE:
88cdf0e10cSrcweir 	case typelib_TypeClass_SHORT:
89cdf0e10cSrcweir 	case typelib_TypeClass_UNSIGNED_SHORT:
90cdf0e10cSrcweir 	case typelib_TypeClass_LONG:
91cdf0e10cSrcweir 	case typelib_TypeClass_UNSIGNED_LONG:
92cdf0e10cSrcweir 	case typelib_TypeClass_HYPER:
93cdf0e10cSrcweir 	case typelib_TypeClass_UNSIGNED_HYPER:
94cdf0e10cSrcweir 	case typelib_TypeClass_FLOAT:
95cdf0e10cSrcweir 	case typelib_TypeClass_DOUBLE:
96cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE:
97cdf0e10cSrcweir 		::rtl_zeroMemory(pMem, nElementSize * nTotalElements);
98cdf0e10cSrcweir 		break;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	case typelib_TypeClass_STRING:
101cdf0e10cSrcweir 		for (i=0; i < nTotalElements; i++)
102cdf0e10cSrcweir 		{
103cdf0e10cSrcweir 			rtl_uString** ppElement = (rtl_uString **)pMem + i;
104cdf0e10cSrcweir 			*ppElement = 0;
105cdf0e10cSrcweir 			rtl_uString_new( ppElement);
106cdf0e10cSrcweir 		}
107cdf0e10cSrcweir 		break;
108cdf0e10cSrcweir 	case typelib_TypeClass_TYPE:
109cdf0e10cSrcweir 		for (i=0; i < nTotalElements; i++)
110cdf0e10cSrcweir 		{
111cdf0e10cSrcweir 			typelib_TypeDescriptionReference** ppElement = (typelib_TypeDescriptionReference **)pMem + i;
112cdf0e10cSrcweir 			*ppElement = _getVoidType();
113cdf0e10cSrcweir 		}
114cdf0e10cSrcweir 		break;
115cdf0e10cSrcweir 	case typelib_TypeClass_ANY:
116cdf0e10cSrcweir 		for (i=0; i < nTotalElements; i++)
117cdf0e10cSrcweir 		{
118cdf0e10cSrcweir 			CONSTRUCT_EMPTY_ANY( (uno_Any *)pMem + i );
119cdf0e10cSrcweir 		}
120cdf0e10cSrcweir 		break;
121cdf0e10cSrcweir 	case typelib_TypeClass_ENUM:
122cdf0e10cSrcweir 		for (i=0; i < nTotalElements; i++)
123cdf0e10cSrcweir 		{
124cdf0e10cSrcweir 			*((sal_Int32 *)pMem + i) = ((typelib_EnumTypeDescription *)pElementType)->nDefaultEnumValue;
125cdf0e10cSrcweir 		}
126cdf0e10cSrcweir 		break;
127cdf0e10cSrcweir 	case typelib_TypeClass_STRUCT:
128cdf0e10cSrcweir 	case typelib_TypeClass_EXCEPTION:
129cdf0e10cSrcweir 		for (i=0; i < nTotalElements; i++)
130cdf0e10cSrcweir 		{
131cdf0e10cSrcweir 			_defaultConstructStruct( (sal_Char*)pMem + i * nElementSize, (typelib_CompoundTypeDescription *)pElementType );
132cdf0e10cSrcweir 		}
133cdf0e10cSrcweir 		break;
134cdf0e10cSrcweir 	case typelib_TypeClass_UNION:
135cdf0e10cSrcweir 		for (i=0; i < nTotalElements; i++)
136cdf0e10cSrcweir 		{
137cdf0e10cSrcweir 			_defaultConstructUnion( (sal_Char*)pMem + i * nElementSize, pElementType );
138cdf0e10cSrcweir 		}
139cdf0e10cSrcweir 		break;
140cdf0e10cSrcweir 	case typelib_TypeClass_SEQUENCE:
141cdf0e10cSrcweir 		for (i=0; i < nTotalElements; i++)
142cdf0e10cSrcweir 		{
143cdf0e10cSrcweir 			uno_Sequence** ppElement = (uno_Sequence **)pMem + i;
144cdf0e10cSrcweir 			*ppElement = createEmptySequence();
145cdf0e10cSrcweir 		}
146cdf0e10cSrcweir 		break;
147cdf0e10cSrcweir     default:
148cdf0e10cSrcweir         OSL_ASSERT(false);
149cdf0e10cSrcweir         break;
150cdf0e10cSrcweir 	}
151cdf0e10cSrcweir 	TYPELIB_DANGER_RELEASE( pElementType );
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
_defaultConstructData(void * pMem,typelib_TypeDescriptionReference * pType,typelib_TypeDescription * pTypeDescr)155cdf0e10cSrcweir inline void _defaultConstructData(
156cdf0e10cSrcweir 	void * pMem,
157cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pType,
158cdf0e10cSrcweir 	typelib_TypeDescription * pTypeDescr )
159cdf0e10cSrcweir 	SAL_THROW( () )
160cdf0e10cSrcweir {
161cdf0e10cSrcweir 	switch (pType->eTypeClass)
162cdf0e10cSrcweir 	{
163cdf0e10cSrcweir 	case typelib_TypeClass_CHAR:
164cdf0e10cSrcweir 		*(sal_Unicode *)pMem = '\0';
165cdf0e10cSrcweir 		break;
166cdf0e10cSrcweir 	case typelib_TypeClass_BOOLEAN:
167cdf0e10cSrcweir 		*(sal_Bool *)pMem = sal_False;
168cdf0e10cSrcweir 		break;
169cdf0e10cSrcweir 	case typelib_TypeClass_BYTE:
170cdf0e10cSrcweir 		*(sal_Int8 *)pMem = 0;
171cdf0e10cSrcweir 		break;
172cdf0e10cSrcweir 	case typelib_TypeClass_SHORT:
173cdf0e10cSrcweir 	case typelib_TypeClass_UNSIGNED_SHORT:
174cdf0e10cSrcweir 		*(sal_Int16 *)pMem = 0;
175cdf0e10cSrcweir 		break;
176cdf0e10cSrcweir 	case typelib_TypeClass_LONG:
177cdf0e10cSrcweir 	case typelib_TypeClass_UNSIGNED_LONG:
178cdf0e10cSrcweir 		*(sal_Int32 *)pMem = 0;
179cdf0e10cSrcweir 		break;
180cdf0e10cSrcweir 	case typelib_TypeClass_HYPER:
181cdf0e10cSrcweir 	case typelib_TypeClass_UNSIGNED_HYPER:
182cdf0e10cSrcweir 		*(sal_Int64 *)pMem = 0;
183cdf0e10cSrcweir 		break;
184cdf0e10cSrcweir 	case typelib_TypeClass_FLOAT:
185cdf0e10cSrcweir 		*(float *)pMem = 0.0;
186cdf0e10cSrcweir 		break;
187cdf0e10cSrcweir 	case typelib_TypeClass_DOUBLE:
188cdf0e10cSrcweir 		*(double *)pMem = 0.0;
189cdf0e10cSrcweir 		break;
190cdf0e10cSrcweir 	case typelib_TypeClass_STRING:
191cdf0e10cSrcweir 		*(rtl_uString **)pMem = 0;
192cdf0e10cSrcweir 		::rtl_uString_new( (rtl_uString **)pMem );
193cdf0e10cSrcweir 		break;
194cdf0e10cSrcweir 	case typelib_TypeClass_TYPE:
195cdf0e10cSrcweir 		*(typelib_TypeDescriptionReference **)pMem = _getVoidType();
196cdf0e10cSrcweir 		break;
197cdf0e10cSrcweir 	case typelib_TypeClass_ANY:
198cdf0e10cSrcweir 		CONSTRUCT_EMPTY_ANY( (uno_Any *)pMem );
199cdf0e10cSrcweir 		break;
200cdf0e10cSrcweir 	case typelib_TypeClass_ENUM:
201cdf0e10cSrcweir 		if (pTypeDescr)
202cdf0e10cSrcweir 		{
203cdf0e10cSrcweir 			*(sal_Int32 *)pMem = ((typelib_EnumTypeDescription *)pTypeDescr)->nDefaultEnumValue;
204cdf0e10cSrcweir 		}
205cdf0e10cSrcweir 		else
206cdf0e10cSrcweir 		{
207cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTypeDescr, pType );
208cdf0e10cSrcweir 			*(sal_Int32 *)pMem = ((typelib_EnumTypeDescription *)pTypeDescr)->nDefaultEnumValue;
209cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pTypeDescr );
210cdf0e10cSrcweir 		}
211cdf0e10cSrcweir 		break;
212cdf0e10cSrcweir 	case typelib_TypeClass_STRUCT:
213cdf0e10cSrcweir 	case typelib_TypeClass_EXCEPTION:
214cdf0e10cSrcweir 		if (pTypeDescr)
215cdf0e10cSrcweir 		{
216cdf0e10cSrcweir 			_defaultConstructStruct( pMem, (typelib_CompoundTypeDescription *)pTypeDescr );
217cdf0e10cSrcweir 		}
218cdf0e10cSrcweir 		else
219cdf0e10cSrcweir 		{
220cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTypeDescr, pType );
221cdf0e10cSrcweir 			_defaultConstructStruct( pMem, (typelib_CompoundTypeDescription *)pTypeDescr );
222cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pTypeDescr );
223cdf0e10cSrcweir 		}
224cdf0e10cSrcweir 		break;
225cdf0e10cSrcweir 	case typelib_TypeClass_ARRAY:
226cdf0e10cSrcweir 		if (pTypeDescr)
227cdf0e10cSrcweir 		{
228cdf0e10cSrcweir 			_defaultConstructArray( pMem, (typelib_ArrayTypeDescription *)pTypeDescr );
229cdf0e10cSrcweir 		}
230cdf0e10cSrcweir 		else
231cdf0e10cSrcweir 		{
232cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTypeDescr, pType );
233cdf0e10cSrcweir 			_defaultConstructArray( pMem, (typelib_ArrayTypeDescription *)pTypeDescr );
234cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pTypeDescr );
235cdf0e10cSrcweir 		}
236cdf0e10cSrcweir 		break;
237cdf0e10cSrcweir 	case typelib_TypeClass_UNION:
238cdf0e10cSrcweir 		if (pTypeDescr)
239cdf0e10cSrcweir 		{
240cdf0e10cSrcweir 			_defaultConstructUnion( pMem, pTypeDescr );
241cdf0e10cSrcweir 		}
242cdf0e10cSrcweir 		else
243cdf0e10cSrcweir 		{
244cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTypeDescr, pType );
245cdf0e10cSrcweir 			_defaultConstructUnion( pMem, pTypeDescr );
246cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pTypeDescr );
247cdf0e10cSrcweir 		}
248cdf0e10cSrcweir 		break;
249cdf0e10cSrcweir 	case typelib_TypeClass_SEQUENCE:
250cdf0e10cSrcweir 		*(uno_Sequence **)pMem = createEmptySequence();
251cdf0e10cSrcweir 		break;
252cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE:
253cdf0e10cSrcweir 		*(void **)pMem = 0; // either cpp or c-uno interface
254cdf0e10cSrcweir 		break;
255cdf0e10cSrcweir     default:
256cdf0e10cSrcweir         OSL_ASSERT(false);
257cdf0e10cSrcweir         break;
258cdf0e10cSrcweir 	}
259cdf0e10cSrcweir }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir #endif
264