xref: /aoo41x/main/cppu/source/uno/constr.hxx (revision cdf0e10c)
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 CONSTR_HXX
28 #define CONSTR_HXX
29 
30 #include "prim.hxx"
31 
32 
33 namespace cppu
34 {
35 
36 //##################################################################################################
37 //#### construction ################################################################################
38 //##################################################################################################
39 
40 //--------------------------------------------------------------------------------------------------
41 inline void _defaultConstructUnion(
42 	void * pMem,
43 	typelib_TypeDescription * pTypeDescr )
44 	SAL_THROW( () )
45 {
46 	::uno_type_constructData(
47 		(char *)pMem + ((typelib_UnionTypeDescription *)pTypeDescr)->nValueOffset,
48 		((typelib_UnionTypeDescription *)pTypeDescr)->pDefaultTypeRef );
49 	*(sal_Int64 *)pMem = ((typelib_UnionTypeDescription *)pTypeDescr)->nDefaultDiscriminant;
50 }
51 //==================================================================================================
52 void defaultConstructStruct(
53 	void * pMem,
54 	typelib_CompoundTypeDescription * pCompType )
55 	SAL_THROW( () );
56 //--------------------------------------------------------------------------------------------------
57 inline void _defaultConstructStruct(
58 	void * pMem,
59 	typelib_CompoundTypeDescription * pTypeDescr )
60 	SAL_THROW( () )
61 {
62 	if (pTypeDescr->pBaseTypeDescription)
63 	{
64 		defaultConstructStruct( pMem, pTypeDescr->pBaseTypeDescription );
65 	}
66 
67 	typelib_TypeDescriptionReference ** ppTypeRefs = (pTypeDescr)->ppTypeRefs;
68 	sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets;
69 	sal_Int32 nDescr = pTypeDescr->nMembers;
70 
71 	while (nDescr--)
72 	{
73 		::uno_type_constructData( (char *)pMem + pMemberOffsets[nDescr], ppTypeRefs[nDescr] );
74 	}
75 }
76 
77 //--------------------------------------------------------------------------------------------------
78 inline void _defaultConstructArray(
79 	void * pMem,
80 	typelib_ArrayTypeDescription * pTypeDescr )
81 {
82 	typelib_TypeDescription * pElementType = NULL;
83 	TYPELIB_DANGER_GET( &pElementType, ((typelib_IndirectTypeDescription *)pTypeDescr)->pType );
84 	sal_Int32 nTotalElements = pTypeDescr->nTotalElements;
85 	sal_Int32 nElementSize = pElementType->nSize;
86 	sal_Int32 i;
87 	switch ( pElementType->eTypeClass )
88 	{
89 	case typelib_TypeClass_CHAR:
90 	case typelib_TypeClass_BOOLEAN:
91 	case typelib_TypeClass_BYTE:
92 	case typelib_TypeClass_SHORT:
93 	case typelib_TypeClass_UNSIGNED_SHORT:
94 	case typelib_TypeClass_LONG:
95 	case typelib_TypeClass_UNSIGNED_LONG:
96 	case typelib_TypeClass_HYPER:
97 	case typelib_TypeClass_UNSIGNED_HYPER:
98 	case typelib_TypeClass_FLOAT:
99 	case typelib_TypeClass_DOUBLE:
100 	case typelib_TypeClass_INTERFACE:
101 		::rtl_zeroMemory(pMem, nElementSize * nTotalElements);
102 		break;
103 
104 	case typelib_TypeClass_STRING:
105 		for (i=0; i < nTotalElements; i++)
106 		{
107 			rtl_uString** ppElement = (rtl_uString **)pMem + i;
108 			*ppElement = 0;
109 			rtl_uString_new( ppElement);
110 		}
111 		break;
112 	case typelib_TypeClass_TYPE:
113 		for (i=0; i < nTotalElements; i++)
114 		{
115 			typelib_TypeDescriptionReference** ppElement = (typelib_TypeDescriptionReference **)pMem + i;
116 			*ppElement = _getVoidType();
117 		}
118 		break;
119 	case typelib_TypeClass_ANY:
120 		for (i=0; i < nTotalElements; i++)
121 		{
122 			CONSTRUCT_EMPTY_ANY( (uno_Any *)pMem + i );
123 		}
124 		break;
125 	case typelib_TypeClass_ENUM:
126 		for (i=0; i < nTotalElements; i++)
127 		{
128 			*((sal_Int32 *)pMem + i) = ((typelib_EnumTypeDescription *)pElementType)->nDefaultEnumValue;
129 		}
130 		break;
131 	case typelib_TypeClass_STRUCT:
132 	case typelib_TypeClass_EXCEPTION:
133 		for (i=0; i < nTotalElements; i++)
134 		{
135 			_defaultConstructStruct( (sal_Char*)pMem + i * nElementSize, (typelib_CompoundTypeDescription *)pElementType );
136 		}
137 		break;
138 	case typelib_TypeClass_UNION:
139 		for (i=0; i < nTotalElements; i++)
140 		{
141 			_defaultConstructUnion( (sal_Char*)pMem + i * nElementSize, pElementType );
142 		}
143 		break;
144 	case typelib_TypeClass_SEQUENCE:
145 		for (i=0; i < nTotalElements; i++)
146 		{
147 			uno_Sequence** ppElement = (uno_Sequence **)pMem + i;
148 			*ppElement = createEmptySequence();
149 		}
150 		break;
151     default:
152         OSL_ASSERT(false);
153         break;
154 	}
155 	TYPELIB_DANGER_RELEASE( pElementType );
156 }
157 
158 //--------------------------------------------------------------------------------------------------
159 inline void _defaultConstructData(
160 	void * pMem,
161 	typelib_TypeDescriptionReference * pType,
162 	typelib_TypeDescription * pTypeDescr )
163 	SAL_THROW( () )
164 {
165 	switch (pType->eTypeClass)
166 	{
167 	case typelib_TypeClass_CHAR:
168 		*(sal_Unicode *)pMem = '\0';
169 		break;
170 	case typelib_TypeClass_BOOLEAN:
171 		*(sal_Bool *)pMem = sal_False;
172 		break;
173 	case typelib_TypeClass_BYTE:
174 		*(sal_Int8 *)pMem = 0;
175 		break;
176 	case typelib_TypeClass_SHORT:
177 	case typelib_TypeClass_UNSIGNED_SHORT:
178 		*(sal_Int16 *)pMem = 0;
179 		break;
180 	case typelib_TypeClass_LONG:
181 	case typelib_TypeClass_UNSIGNED_LONG:
182 		*(sal_Int32 *)pMem = 0;
183 		break;
184 	case typelib_TypeClass_HYPER:
185 	case typelib_TypeClass_UNSIGNED_HYPER:
186 		*(sal_Int64 *)pMem = 0;
187 		break;
188 	case typelib_TypeClass_FLOAT:
189 		*(float *)pMem = 0.0;
190 		break;
191 	case typelib_TypeClass_DOUBLE:
192 		*(double *)pMem = 0.0;
193 		break;
194 	case typelib_TypeClass_STRING:
195 		*(rtl_uString **)pMem = 0;
196 		::rtl_uString_new( (rtl_uString **)pMem );
197 		break;
198 	case typelib_TypeClass_TYPE:
199 		*(typelib_TypeDescriptionReference **)pMem = _getVoidType();
200 		break;
201 	case typelib_TypeClass_ANY:
202 		CONSTRUCT_EMPTY_ANY( (uno_Any *)pMem );
203 		break;
204 	case typelib_TypeClass_ENUM:
205 		if (pTypeDescr)
206 		{
207 			*(sal_Int32 *)pMem = ((typelib_EnumTypeDescription *)pTypeDescr)->nDefaultEnumValue;
208 		}
209 		else
210 		{
211 			TYPELIB_DANGER_GET( &pTypeDescr, pType );
212 			*(sal_Int32 *)pMem = ((typelib_EnumTypeDescription *)pTypeDescr)->nDefaultEnumValue;
213 			TYPELIB_DANGER_RELEASE( pTypeDescr );
214 		}
215 		break;
216 	case typelib_TypeClass_STRUCT:
217 	case typelib_TypeClass_EXCEPTION:
218 		if (pTypeDescr)
219 		{
220 			_defaultConstructStruct( pMem, (typelib_CompoundTypeDescription *)pTypeDescr );
221 		}
222 		else
223 		{
224 			TYPELIB_DANGER_GET( &pTypeDescr, pType );
225 			_defaultConstructStruct( pMem, (typelib_CompoundTypeDescription *)pTypeDescr );
226 			TYPELIB_DANGER_RELEASE( pTypeDescr );
227 		}
228 		break;
229 	case typelib_TypeClass_ARRAY:
230 		if (pTypeDescr)
231 		{
232 			_defaultConstructArray( pMem, (typelib_ArrayTypeDescription *)pTypeDescr );
233 		}
234 		else
235 		{
236 			TYPELIB_DANGER_GET( &pTypeDescr, pType );
237 			_defaultConstructArray( pMem, (typelib_ArrayTypeDescription *)pTypeDescr );
238 			TYPELIB_DANGER_RELEASE( pTypeDescr );
239 		}
240 		break;
241 	case typelib_TypeClass_UNION:
242 		if (pTypeDescr)
243 		{
244 			_defaultConstructUnion( pMem, pTypeDescr );
245 		}
246 		else
247 		{
248 			TYPELIB_DANGER_GET( &pTypeDescr, pType );
249 			_defaultConstructUnion( pMem, pTypeDescr );
250 			TYPELIB_DANGER_RELEASE( pTypeDescr );
251 		}
252 		break;
253 	case typelib_TypeClass_SEQUENCE:
254 		*(uno_Sequence **)pMem = createEmptySequence();
255 		break;
256 	case typelib_TypeClass_INTERFACE:
257 		*(void **)pMem = 0; // either cpp or c-uno interface
258 		break;
259     default:
260         OSL_ASSERT(false);
261         break;
262 	}
263 }
264 
265 }
266 
267 #endif
268