xref: /AOO41X/main/cppuhelper/source/stdidlclass.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <osl/mutex.hxx>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
34*cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
35*cdf0e10cSrcweir #include <cppuhelper/stdidlclass.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlClassProvider.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlReflection.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/uno/DeploymentException.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir using namespace com::sun::star;
44*cdf0e10cSrcweir using namespace com::sun::star::uno;
45*cdf0e10cSrcweir using namespace com::sun::star::lang;
46*cdf0e10cSrcweir using namespace com::sun::star::reflection;
47*cdf0e10cSrcweir using namespace rtl;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace cppu {
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir /*---------------------------------------------------------
52*cdf0e10cSrcweir *	This helper class implements XIdlClass. Is used by
53*cdf0e10cSrcweir *	createStdIdlClass()
54*cdf0e10cSrcweir *---------------------------------------------------------*/
55*cdf0e10cSrcweir class OStdIdlClass :
56*cdf0e10cSrcweir 			public OWeakObject,
57*cdf0e10cSrcweir 			public XIdlClass,
58*cdf0e10cSrcweir 			public XIdlClassProvider
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir public:
61*cdf0e10cSrcweir 	OStdIdlClass(
62*cdf0e10cSrcweir 					const Reference < XMultiServiceFactory > &rSMgr ,
63*cdf0e10cSrcweir 					const OUString & sImplementationName ,
64*cdf0e10cSrcweir 					const Reference < XIdlClass > & rSuperClass,
65*cdf0e10cSrcweir 					const Sequence < OUString > &seq
66*cdf0e10cSrcweir 				) SAL_THROW( () );
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	// XInterface
69*cdf0e10cSrcweir 	Any					SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType )
70*cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	void 				SAL_CALL acquire() throw() 	 { OWeakObject::acquire(); }
73*cdf0e10cSrcweir 	void 				SAL_CALL release() throw()	 { OWeakObject::release(); }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	// XIdlClassProvider
76*cdf0e10cSrcweir     Sequence< Reference < XIdlClass > > SAL_CALL getIdlClasses(void)
77*cdf0e10cSrcweir 		throw (RuntimeException);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     // XIdlClass
80*cdf0e10cSrcweir     virtual Sequence< Reference< XIdlClass > > SAL_CALL getClasses(  ) throw(RuntimeException)
81*cdf0e10cSrcweir     									{ return Sequence < Reference < XIdlClass > > (); }
82*cdf0e10cSrcweir     virtual Reference< XIdlClass > SAL_CALL getClass( const ::rtl::OUString& ) throw(RuntimeException)
83*cdf0e10cSrcweir     									{ return Reference < XIdlClass > (); }
84*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL equals( const Reference< XIdlClass >& Type ) throw(RuntimeException)
85*cdf0e10cSrcweir     									{ return getName() == Type->getName(); }
86*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL isAssignableFrom( const Reference< XIdlClass >& xType ) throw(RuntimeException)
87*cdf0e10cSrcweir     									{ return equals( xType ); }
88*cdf0e10cSrcweir     virtual TypeClass SAL_CALL getTypeClass(  ) throw(RuntimeException)
89*cdf0e10cSrcweir     									{ return TypeClass_UNKNOWN; }
90*cdf0e10cSrcweir     virtual OUString SAL_CALL getName(  ) throw(RuntimeException)
91*cdf0e10cSrcweir     									{ return m_sImplementationName; }
92*cdf0e10cSrcweir     virtual Uik SAL_CALL getUik(  ) throw(RuntimeException)
93*cdf0e10cSrcweir     									{ return Uik(); }
94*cdf0e10cSrcweir     virtual Sequence< Reference< XIdlClass > > SAL_CALL getSuperclasses(  ) throw(RuntimeException)
95*cdf0e10cSrcweir     									{ return m_seqSuperClasses; }
96*cdf0e10cSrcweir     virtual Sequence< Reference< XIdlClass > > SAL_CALL getInterfaces(  ) throw(RuntimeException);
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     virtual Reference< XIdlClass > SAL_CALL getComponentType(  ) throw(RuntimeException)
99*cdf0e10cSrcweir     									{ return Reference < XIdlClass > (); }
100*cdf0e10cSrcweir     virtual Reference< XIdlField > SAL_CALL getField( const ::rtl::OUString& ) throw(RuntimeException)
101*cdf0e10cSrcweir     									{ return Reference < XIdlField > (); }
102*cdf0e10cSrcweir     virtual Sequence< Reference< XIdlField > > SAL_CALL getFields(  ) throw(RuntimeException)
103*cdf0e10cSrcweir     									{ return Sequence< Reference < XIdlField > > (); }
104*cdf0e10cSrcweir     virtual Reference< XIdlMethod > SAL_CALL getMethod( const ::rtl::OUString& ) throw(RuntimeException)
105*cdf0e10cSrcweir     									{ return Reference < XIdlMethod > (); }
106*cdf0e10cSrcweir     virtual Sequence< Reference< XIdlMethod > > SAL_CALL getMethods(  ) throw(RuntimeException)
107*cdf0e10cSrcweir     									{ return Sequence < Reference < XIdlMethod > > (); }
108*cdf0e10cSrcweir     virtual Reference< XIdlArray > SAL_CALL getArray(  ) throw(RuntimeException)
109*cdf0e10cSrcweir     									{ return Reference < XIdlArray > (); }
110*cdf0e10cSrcweir     virtual void SAL_CALL createObject( Any& ) throw(RuntimeException) {}
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir private:
113*cdf0e10cSrcweir 	OUString 								m_sImplementationName;
114*cdf0e10cSrcweir 	Sequence < OUString >					m_seqSupportedInterface;
115*cdf0e10cSrcweir 	Sequence < Reference < XIdlClass > > 	m_seqSuperClasses;
116*cdf0e10cSrcweir 	Reference < XMultiServiceFactory >		m_rSMgr;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     Reference< XIdlReflection > m_xCorefl;
119*cdf0e10cSrcweir     Reference< XIdlReflection > const & get_corefl() SAL_THROW( (RuntimeException) );
120*cdf0e10cSrcweir };
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir Reference< XIdlReflection > const & OStdIdlClass::get_corefl()
123*cdf0e10cSrcweir     SAL_THROW( (RuntimeException) )
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir     if (! m_xCorefl.is())
126*cdf0e10cSrcweir     {
127*cdf0e10cSrcweir         if( m_rSMgr.is() )
128*cdf0e10cSrcweir         {
129*cdf0e10cSrcweir             Reference< beans::XPropertySet > xProps( m_rSMgr, UNO_QUERY );
130*cdf0e10cSrcweir             OSL_ASSERT( xProps.is() );
131*cdf0e10cSrcweir             if (xProps.is())
132*cdf0e10cSrcweir             {
133*cdf0e10cSrcweir                 Reference< XComponentContext > xContext;
134*cdf0e10cSrcweir                 xProps->getPropertyValue(
135*cdf0e10cSrcweir                     OUString( RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) ) >>= xContext;
136*cdf0e10cSrcweir                 OSL_ASSERT( xContext.is() );
137*cdf0e10cSrcweir                 if (xContext.is())
138*cdf0e10cSrcweir                 {
139*cdf0e10cSrcweir                     Reference < XIdlReflection > x;
140*cdf0e10cSrcweir                     xContext->getValueByName(
141*cdf0e10cSrcweir                         OUString( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.reflection.theCoreReflection") ) ) >>= x;
142*cdf0e10cSrcweir                     OSL_ENSURE( x.is(), "### CoreReflection singleton not accessible!?" );
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir                     if (x.is())
145*cdf0e10cSrcweir                     {
146*cdf0e10cSrcweir                         ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
147*cdf0e10cSrcweir                         if (! m_xCorefl.is())
148*cdf0e10cSrcweir                         {
149*cdf0e10cSrcweir                             m_xCorefl = x;
150*cdf0e10cSrcweir                         }
151*cdf0e10cSrcweir                     }
152*cdf0e10cSrcweir                 }
153*cdf0e10cSrcweir             }
154*cdf0e10cSrcweir         }
155*cdf0e10cSrcweir         if (! m_xCorefl.is())
156*cdf0e10cSrcweir         {
157*cdf0e10cSrcweir             throw DeploymentException(
158*cdf0e10cSrcweir                 OUString( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.reflection.theCoreReflection singleton not accessible") ),
159*cdf0e10cSrcweir                 Reference< XInterface >() );
160*cdf0e10cSrcweir         }
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir     return m_xCorefl;
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir OStdIdlClass::OStdIdlClass(
166*cdf0e10cSrcweir 					const Reference < XMultiServiceFactory > &rSMgr ,
167*cdf0e10cSrcweir 					const OUString & sImplementationName ,
168*cdf0e10cSrcweir 					const Reference < XIdlClass > & rSuperClass,
169*cdf0e10cSrcweir 					const Sequence < OUString > &seq
170*cdf0e10cSrcweir 						  ) SAL_THROW( () ) :
171*cdf0e10cSrcweir 				m_sImplementationName( sImplementationName ) ,
172*cdf0e10cSrcweir 				m_seqSupportedInterface( seq ),
173*cdf0e10cSrcweir 				m_rSMgr( rSMgr )
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir 	if( rSuperClass.is() )
176*cdf0e10cSrcweir 		m_seqSuperClasses = Sequence< Reference < XIdlClass > >( &rSuperClass, 1 );
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir Any SAL_CALL OStdIdlClass::queryInterface( const Type & rType )
181*cdf0e10cSrcweir 	 throw(::com::sun::star::uno::RuntimeException)
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir 	Any aRet( ::cppu::queryInterface(
184*cdf0e10cSrcweir 		rType, static_cast< XIdlClass * >( this ), static_cast< XIdlClassProvider * >( this ) ) );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 	return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
187*cdf0e10cSrcweir }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir Sequence< Reference< XIdlClass > > SAL_CALL OStdIdlClass::getInterfaces(  ) throw(RuntimeException)
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir 	int nMax = m_seqSupportedInterface.getLength();
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     Reference< XIdlReflection > const & rCoreRefl = get_corefl();
195*cdf0e10cSrcweir     if( rCoreRefl.is() )
196*cdf0e10cSrcweir     {
197*cdf0e10cSrcweir         Sequence< Reference< XIdlClass > > seqClasses( nMax );
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         for( int n = 0 ; n < nMax ; n++ )
200*cdf0e10cSrcweir         {
201*cdf0e10cSrcweir             seqClasses.getArray()[n] = rCoreRefl->forName( m_seqSupportedInterface.getArray()[n] );
202*cdf0e10cSrcweir         }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir         return seqClasses;
205*cdf0e10cSrcweir 	}
206*cdf0e10cSrcweir 	return Sequence< Reference< XIdlClass > > () ;
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir // XIdlClassProvider
211*cdf0e10cSrcweir Sequence< Reference < XIdlClass > > SAL_CALL OStdIdlClass::getIdlClasses(void)
212*cdf0e10cSrcweir 	throw (RuntimeException)
213*cdf0e10cSrcweir {
214*cdf0e10cSrcweir 	// weak reference to cache the standard class
215*cdf0e10cSrcweir     static WeakReference< XIdlClass >	weakRef;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 	// try to make weakref hard
218*cdf0e10cSrcweir 	Reference < XIdlClass > r = weakRef;
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 	if( ! r.is() ) {
221*cdf0e10cSrcweir 		// xidlclass has not been initialized before or has been destroyed already.
222*cdf0e10cSrcweir 		r = ::cppu::createStandardClass(
223*cdf0e10cSrcweir 										m_rSMgr ,
224*cdf0e10cSrcweir 										OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.cppuhelper.OStdIdlClass") ) ,
225*cdf0e10cSrcweir 										Reference < XIdlClass > () ,
226*cdf0e10cSrcweir   										SAL_STATIC_CAST( XIdlClassProvider * , this ) ,
227*cdf0e10cSrcweir   										SAL_STATIC_CAST( XIdlClass * , this )
228*cdf0e10cSrcweir 					 					);
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 		// store reference for later use
231*cdf0e10cSrcweir 		weakRef = r;
232*cdf0e10cSrcweir 	}
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 	return Sequence < Reference < XIdlClass > > ( &r , 1 );
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir // external constructor
241*cdf0e10cSrcweir XIdlClass *  SAL_CALL createStandardClassWithSequence(
242*cdf0e10cSrcweir 					const Reference < XMultiServiceFactory > &rSMgr ,
243*cdf0e10cSrcweir 					const OUString & sImplementationName ,
244*cdf0e10cSrcweir 					const Reference < XIdlClass > & rSuperClass,
245*cdf0e10cSrcweir 					const Sequence < OUString > &seqInterfaceNames )
246*cdf0e10cSrcweir 	SAL_THROW( () )
247*cdf0e10cSrcweir {
248*cdf0e10cSrcweir 	return SAL_STATIC_CAST(
249*cdf0e10cSrcweir 						XIdlClass * ,
250*cdf0e10cSrcweir 						new OStdIdlClass(
251*cdf0e10cSrcweir 											rSMgr ,
252*cdf0e10cSrcweir 											sImplementationName,
253*cdf0e10cSrcweir 											rSuperClass,
254*cdf0e10cSrcweir 											seqInterfaceNames
255*cdf0e10cSrcweir 										  )
256*cdf0e10cSrcweir 					   );
257*cdf0e10cSrcweir }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir } //end namespace cppu
260