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