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 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_stoc.hxx"
30 #include <cppuhelper/queryinterface.hxx>
31 #include <uno/any2.h>
32 
33 #include "base.hxx"
34 
35 namespace stoc_corefl
36 {
37 
38 #ifdef TEST_LIST_CLASSES
39 ClassNameList g_aClassNames;
40 #endif
41 
42 //--------------------------------------------------------------------------------------------------
43 Mutex & getMutexAccess()
44 {
45 	static Mutex * s_pMutex = 0;
46 	if (! s_pMutex)
47 	{
48 		MutexGuard aGuard( Mutex::getGlobalMutex() );
49 		if (! s_pMutex)
50 		{
51 			static Mutex s_aMutex;
52 			s_pMutex = &s_aMutex;
53 		}
54 	}
55 	return *s_pMutex;
56 }
57 
58 //__________________________________________________________________________________________________
59 IdlClassImpl::IdlClassImpl( IdlReflectionServiceImpl * pReflection,
60 							const OUString & rName, typelib_TypeClass eTypeClass,
61 							typelib_TypeDescription * pTypeDescr )
62 	: _pReflection( pReflection )
63 	, _aName( rName )
64 	, _eTypeClass( (TypeClass)eTypeClass )
65 	, _pTypeDescr( pTypeDescr )
66 {
67 	if (_pReflection)
68 		_pReflection->acquire();
69 	if (_pTypeDescr)
70 	{
71 		typelib_typedescription_acquire( _pTypeDescr );
72 		if (! _pTypeDescr->bComplete)
73 			typelib_typedescription_complete( &_pTypeDescr );
74 	}
75 
76 #ifdef TEST_LIST_CLASSES
77 	ClassNameList::const_iterator iFind( find( g_aClassNames.begin(), g_aClassNames.end(), _aName ) );
78 	OSL_ENSURE( iFind == g_aClassNames.end(), "### idl class already exists!" );
79 	g_aClassNames.push_front( _aName );
80 #endif
81 }
82 //__________________________________________________________________________________________________
83 IdlClassImpl::~IdlClassImpl()
84 {
85 	if (_pTypeDescr)
86 		typelib_typedescription_release( _pTypeDescr );
87 	if (_pReflection)
88 		_pReflection->release();
89 
90 #ifdef TEST_LIST_CLASSES
91 	ClassNameList::iterator iFind( find( g_aClassNames.begin(), g_aClassNames.end(), _aName ) );
92 	OSL_ENSURE( iFind != g_aClassNames.end(), "### idl class does not exist!" );
93 	g_aClassNames.erase( iFind );
94 #endif
95 }
96 
97 // XIdlClassImpl default implementation
98 //__________________________________________________________________________________________________
99 TypeClass IdlClassImpl::getTypeClass()
100 	throw(::com::sun::star::uno::RuntimeException)
101 {
102 	return _eTypeClass;
103 }
104 //__________________________________________________________________________________________________
105 OUString IdlClassImpl::getName()
106 	throw(::com::sun::star::uno::RuntimeException)
107 {
108 	return _aName;
109 }
110 //__________________________________________________________________________________________________
111 sal_Bool IdlClassImpl::equals( const Reference< XIdlClass >& xType )
112 	throw(::com::sun::star::uno::RuntimeException)
113 {
114 	return (xType.is() &&
115 			(xType->getTypeClass() == _eTypeClass) && (xType->getName() == _aName));
116 }
117 
118 static sal_Bool s_aAssignableFromTab[11][11] =
119 {
120 						 /* from CH,BO,BY,SH,US,LO,UL,HY,UH,FL,DO */
121 /* TypeClass_CHAR */			{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
122 /* TypeClass_BOOLEAN */			{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
123 /* TypeClass_BYTE */			{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
124 /* TypeClass_SHORT */			{ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 },
125 /* TypeClass_UNSIGNED_SHORT */	{ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 },
126 /* TypeClass_LONG */			{ 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
127 /* TypeClass_UNSIGNED_LONG */	{ 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
128 /* TypeClass_HYPER */			{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
129 /* TypeClass_UNSIGNED_HYPER */	{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
130 /* TypeClass_FLOAT */			{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
131 /* TypeClass_DOUBLE */			{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
132 };
133 //__________________________________________________________________________________________________
134 sal_Bool IdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
135 	throw(::com::sun::star::uno::RuntimeException)
136 {
137 	TypeClass eAssign = getTypeClass();
138 	if (equals( xType ) || eAssign == TypeClass_ANY) // default shot
139 	{
140 		return sal_True;
141 	}
142 	else
143 	{
144 		TypeClass eFrom	  = xType->getTypeClass();
145 		if (eAssign > TypeClass_VOID && eAssign < TypeClass_STRING &&
146 			eFrom > TypeClass_VOID && eFrom < TypeClass_STRING)
147 		{
148 			return s_aAssignableFromTab[eAssign-1][eFrom-1];
149 		}
150 	}
151 	return sal_False;
152 }
153 //__________________________________________________________________________________________________
154 void IdlClassImpl::createObject( Any & rObj )
155 	throw(::com::sun::star::uno::RuntimeException)
156 {
157 	rObj.clear();
158 	uno_any_destruct( &rObj, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
159 	uno_any_construct( &rObj, 0, getTypeDescr(), 0 );
160 }
161 
162 // what TODO ????
163 //__________________________________________________________________________________________________
164 Sequence< Reference< XIdlClass > > IdlClassImpl::getClasses()
165 	throw(::com::sun::star::uno::RuntimeException)
166 {
167 	OSL_ENSURE( sal_False, "### unexpected use!" );
168 	return Sequence< Reference< XIdlClass > >();
169 }
170 //__________________________________________________________________________________________________
171 Reference< XIdlClass > IdlClassImpl::getClass( const OUString & )
172 	throw(::com::sun::star::uno::RuntimeException)
173 {
174 	OSL_ENSURE( sal_False, "### unexpected use!" );
175 	return Reference< XIdlClass >();
176 }
177 //__________________________________________________________________________________________________
178 Sequence< Reference< XIdlClass > > IdlClassImpl::getInterfaces()
179 	throw(::com::sun::star::uno::RuntimeException)
180 {
181 //  	OSL_ENSURE( sal_False, "### unexpected use!" );
182 	return Sequence< Reference< XIdlClass > >();
183 }
184 
185 // structs, interfaces
186 //__________________________________________________________________________________________________
187 Sequence< Reference< XIdlClass > > IdlClassImpl::getSuperclasses() throw(::com::sun::star::uno::RuntimeException)
188 {
189 	return Sequence< Reference< XIdlClass > >();
190 }
191 // structs
192 //__________________________________________________________________________________________________
193 Reference< XIdlField > IdlClassImpl::getField( const OUString & )
194 	throw(::com::sun::star::uno::RuntimeException)
195 {
196 	return Reference< XIdlField >();
197 }
198 //__________________________________________________________________________________________________
199 Sequence< Reference< XIdlField > > IdlClassImpl::getFields()
200 	throw(::com::sun::star::uno::RuntimeException)
201 {
202 	return Sequence< Reference< XIdlField > >();
203 }
204 // interfaces
205 //__________________________________________________________________________________________________
206 Uik IdlClassImpl::getUik()
207 	throw(::com::sun::star::uno::RuntimeException)
208 {
209 	return Uik();
210 }
211 //__________________________________________________________________________________________________
212 Reference< XIdlMethod > IdlClassImpl::getMethod( const OUString & )
213 	throw(::com::sun::star::uno::RuntimeException)
214 {
215 	return Reference< XIdlMethod >();
216 }
217 //__________________________________________________________________________________________________
218 Sequence< Reference< XIdlMethod > > IdlClassImpl::getMethods()
219 	throw(::com::sun::star::uno::RuntimeException)
220 {
221 	return Sequence< Reference< XIdlMethod > >();
222 }
223 // array
224 //__________________________________________________________________________________________________
225 Reference< XIdlClass > IdlClassImpl::getComponentType()
226 	throw(::com::sun::star::uno::RuntimeException)
227 {
228 	return Reference< XIdlClass >();
229 }
230 //__________________________________________________________________________________________________
231 Reference< XIdlArray > IdlClassImpl::getArray()
232 	throw(::com::sun::star::uno::RuntimeException)
233 {
234 	return Reference< XIdlArray >();
235 }
236 
237 
238 //##################################################################################################
239 //##################################################################################################
240 //##################################################################################################
241 
242 
243 //__________________________________________________________________________________________________
244 IdlMemberImpl::IdlMemberImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
245 							  typelib_TypeDescription * pTypeDescr,
246 							  typelib_TypeDescription * pDeclTypeDescr )
247 	: _pReflection( pReflection )
248 	, _aName( rName )
249 	, _pTypeDescr( pTypeDescr )
250 	, _pDeclTypeDescr( pDeclTypeDescr )
251 {
252 	_pReflection->acquire();
253 	typelib_typedescription_acquire( _pTypeDescr );
254     if (! _pTypeDescr->bComplete)
255         typelib_typedescription_complete( &_pTypeDescr );
256 	typelib_typedescription_acquire( _pDeclTypeDescr );
257     if (! _pDeclTypeDescr->bComplete)
258         typelib_typedescription_complete( &_pDeclTypeDescr );
259 }
260 //__________________________________________________________________________________________________
261 IdlMemberImpl::~IdlMemberImpl()
262 {
263 	typelib_typedescription_release( _pDeclTypeDescr );
264 	typelib_typedescription_release( _pTypeDescr );
265 	_pReflection->release();
266 }
267 
268 // XIdlMember
269 //__________________________________________________________________________________________________
270 Reference< XIdlClass > IdlMemberImpl::getDeclaringClass()
271 	throw(::com::sun::star::uno::RuntimeException)
272 {
273 	if (! _xDeclClass.is())
274     {
275 		Reference< XIdlClass > xDeclClass( getReflection()->forType( getDeclTypeDescr() ) );
276 		MutexGuard aGuard( getMutexAccess() );
277         if (! _xDeclClass.is())
278             _xDeclClass = xDeclClass;
279     }
280 	return _xDeclClass;
281 }
282 //__________________________________________________________________________________________________
283 OUString IdlMemberImpl::getName()
284 	throw(::com::sun::star::uno::RuntimeException)
285 {
286 	return _aName;
287 }
288 
289 }
290 
291 
292