1*9d7e27acSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9d7e27acSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9d7e27acSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9d7e27acSAndrew Rist  * distributed with this work for additional information
6*9d7e27acSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9d7e27acSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9d7e27acSAndrew Rist  * "License"); you may not use this file except in compliance
9*9d7e27acSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9d7e27acSAndrew Rist  *
11*9d7e27acSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9d7e27acSAndrew Rist  *
13*9d7e27acSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9d7e27acSAndrew Rist  * software distributed under the License is distributed on an
15*9d7e27acSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9d7e27acSAndrew Rist  * KIND, either express or implied.  See the License for the
17*9d7e27acSAndrew Rist  * specific language governing permissions and limitations
18*9d7e27acSAndrew Rist  * under the License.
19*9d7e27acSAndrew Rist  *
20*9d7e27acSAndrew Rist  *************************************************************/
21*9d7e27acSAndrew Rist 
22*9d7e27acSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <osl/mutex.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
30cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
31cdf0e10cSrcweir #include <cppuhelper/stdidlclass.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlClassProvider.hpp>
34cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlReflection.hpp>
35cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
36cdf0e10cSrcweir #include <com/sun/star/uno/DeploymentException.hpp>
37cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace com::sun::star;
40cdf0e10cSrcweir using namespace com::sun::star::uno;
41cdf0e10cSrcweir using namespace com::sun::star::lang;
42cdf0e10cSrcweir using namespace com::sun::star::reflection;
43cdf0e10cSrcweir using namespace rtl;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir namespace cppu {
46cdf0e10cSrcweir 
47cdf0e10cSrcweir /*---------------------------------------------------------
48cdf0e10cSrcweir *	This helper class implements XIdlClass. Is used by
49cdf0e10cSrcweir *	createStdIdlClass()
50cdf0e10cSrcweir *---------------------------------------------------------*/
51cdf0e10cSrcweir class OStdIdlClass :
52cdf0e10cSrcweir 			public OWeakObject,
53cdf0e10cSrcweir 			public XIdlClass,
54cdf0e10cSrcweir 			public XIdlClassProvider
55cdf0e10cSrcweir {
56cdf0e10cSrcweir public:
57cdf0e10cSrcweir 	OStdIdlClass(
58cdf0e10cSrcweir 					const Reference < XMultiServiceFactory > &rSMgr ,
59cdf0e10cSrcweir 					const OUString & sImplementationName ,
60cdf0e10cSrcweir 					const Reference < XIdlClass > & rSuperClass,
61cdf0e10cSrcweir 					const Sequence < OUString > &seq
62cdf0e10cSrcweir 				) SAL_THROW( () );
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	// XInterface
65cdf0e10cSrcweir 	Any					SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType )
66cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
67cdf0e10cSrcweir 
acquire()68cdf0e10cSrcweir 	void 				SAL_CALL acquire() throw() 	 { OWeakObject::acquire(); }
release()69cdf0e10cSrcweir 	void 				SAL_CALL release() throw()	 { OWeakObject::release(); }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	// XIdlClassProvider
72cdf0e10cSrcweir     Sequence< Reference < XIdlClass > > SAL_CALL getIdlClasses(void)
73cdf0e10cSrcweir 		throw (RuntimeException);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     // XIdlClass
getClasses()76cdf0e10cSrcweir     virtual Sequence< Reference< XIdlClass > > SAL_CALL getClasses(  ) throw(RuntimeException)
77cdf0e10cSrcweir     									{ return Sequence < Reference < XIdlClass > > (); }
getClass(const::rtl::OUString &)78cdf0e10cSrcweir     virtual Reference< XIdlClass > SAL_CALL getClass( const ::rtl::OUString& ) throw(RuntimeException)
79cdf0e10cSrcweir     									{ return Reference < XIdlClass > (); }
equals(const Reference<XIdlClass> & Type)80cdf0e10cSrcweir     virtual sal_Bool SAL_CALL equals( const Reference< XIdlClass >& Type ) throw(RuntimeException)
81cdf0e10cSrcweir     									{ return getName() == Type->getName(); }
isAssignableFrom(const Reference<XIdlClass> & xType)82cdf0e10cSrcweir     virtual sal_Bool SAL_CALL isAssignableFrom( const Reference< XIdlClass >& xType ) throw(RuntimeException)
83cdf0e10cSrcweir     									{ return equals( xType ); }
getTypeClass()84cdf0e10cSrcweir     virtual TypeClass SAL_CALL getTypeClass(  ) throw(RuntimeException)
85cdf0e10cSrcweir     									{ return TypeClass_UNKNOWN; }
getName()86cdf0e10cSrcweir     virtual OUString SAL_CALL getName(  ) throw(RuntimeException)
87cdf0e10cSrcweir     									{ return m_sImplementationName; }
getUik()88cdf0e10cSrcweir     virtual Uik SAL_CALL getUik(  ) throw(RuntimeException)
89cdf0e10cSrcweir     									{ return Uik(); }
getSuperclasses()90cdf0e10cSrcweir     virtual Sequence< Reference< XIdlClass > > SAL_CALL getSuperclasses(  ) throw(RuntimeException)
91cdf0e10cSrcweir     									{ return m_seqSuperClasses; }
92cdf0e10cSrcweir     virtual Sequence< Reference< XIdlClass > > SAL_CALL getInterfaces(  ) throw(RuntimeException);
93cdf0e10cSrcweir 
getComponentType()94cdf0e10cSrcweir     virtual Reference< XIdlClass > SAL_CALL getComponentType(  ) throw(RuntimeException)
95cdf0e10cSrcweir     									{ return Reference < XIdlClass > (); }
getField(const::rtl::OUString &)96cdf0e10cSrcweir     virtual Reference< XIdlField > SAL_CALL getField( const ::rtl::OUString& ) throw(RuntimeException)
97cdf0e10cSrcweir     									{ return Reference < XIdlField > (); }
getFields()98cdf0e10cSrcweir     virtual Sequence< Reference< XIdlField > > SAL_CALL getFields(  ) throw(RuntimeException)
99cdf0e10cSrcweir     									{ return Sequence< Reference < XIdlField > > (); }
getMethod(const::rtl::OUString &)100cdf0e10cSrcweir     virtual Reference< XIdlMethod > SAL_CALL getMethod( const ::rtl::OUString& ) throw(RuntimeException)
101cdf0e10cSrcweir     									{ return Reference < XIdlMethod > (); }
getMethods()102cdf0e10cSrcweir     virtual Sequence< Reference< XIdlMethod > > SAL_CALL getMethods(  ) throw(RuntimeException)
103cdf0e10cSrcweir     									{ return Sequence < Reference < XIdlMethod > > (); }
getArray()104cdf0e10cSrcweir     virtual Reference< XIdlArray > SAL_CALL getArray(  ) throw(RuntimeException)
105cdf0e10cSrcweir     									{ return Reference < XIdlArray > (); }
createObject(Any &)106cdf0e10cSrcweir     virtual void SAL_CALL createObject( Any& ) throw(RuntimeException) {}
107cdf0e10cSrcweir 
108cdf0e10cSrcweir private:
109cdf0e10cSrcweir 	OUString 								m_sImplementationName;
110cdf0e10cSrcweir 	Sequence < OUString >					m_seqSupportedInterface;
111cdf0e10cSrcweir 	Sequence < Reference < XIdlClass > > 	m_seqSuperClasses;
112cdf0e10cSrcweir 	Reference < XMultiServiceFactory >		m_rSMgr;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     Reference< XIdlReflection > m_xCorefl;
115cdf0e10cSrcweir     Reference< XIdlReflection > const & get_corefl() SAL_THROW( (RuntimeException) );
116cdf0e10cSrcweir };
117cdf0e10cSrcweir 
get_corefl()118cdf0e10cSrcweir Reference< XIdlReflection > const & OStdIdlClass::get_corefl()
119cdf0e10cSrcweir     SAL_THROW( (RuntimeException) )
120cdf0e10cSrcweir {
121cdf0e10cSrcweir     if (! m_xCorefl.is())
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         if( m_rSMgr.is() )
124cdf0e10cSrcweir         {
125cdf0e10cSrcweir             Reference< beans::XPropertySet > xProps( m_rSMgr, UNO_QUERY );
126cdf0e10cSrcweir             OSL_ASSERT( xProps.is() );
127cdf0e10cSrcweir             if (xProps.is())
128cdf0e10cSrcweir             {
129cdf0e10cSrcweir                 Reference< XComponentContext > xContext;
130cdf0e10cSrcweir                 xProps->getPropertyValue(
131cdf0e10cSrcweir                     OUString( RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) ) >>= xContext;
132cdf0e10cSrcweir                 OSL_ASSERT( xContext.is() );
133cdf0e10cSrcweir                 if (xContext.is())
134cdf0e10cSrcweir                 {
135cdf0e10cSrcweir                     Reference < XIdlReflection > x;
136cdf0e10cSrcweir                     xContext->getValueByName(
137cdf0e10cSrcweir                         OUString( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.reflection.theCoreReflection") ) ) >>= x;
138cdf0e10cSrcweir                     OSL_ENSURE( x.is(), "### CoreReflection singleton not accessible!?" );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir                     if (x.is())
141cdf0e10cSrcweir                     {
142cdf0e10cSrcweir                         ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
143cdf0e10cSrcweir                         if (! m_xCorefl.is())
144cdf0e10cSrcweir                         {
145cdf0e10cSrcweir                             m_xCorefl = x;
146cdf0e10cSrcweir                         }
147cdf0e10cSrcweir                     }
148cdf0e10cSrcweir                 }
149cdf0e10cSrcweir             }
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir         if (! m_xCorefl.is())
152cdf0e10cSrcweir         {
153cdf0e10cSrcweir             throw DeploymentException(
154cdf0e10cSrcweir                 OUString( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.reflection.theCoreReflection singleton not accessible") ),
155cdf0e10cSrcweir                 Reference< XInterface >() );
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir     return m_xCorefl;
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
OStdIdlClass(const Reference<XMultiServiceFactory> & rSMgr,const OUString & sImplementationName,const Reference<XIdlClass> & rSuperClass,const Sequence<OUString> & seq)161cdf0e10cSrcweir OStdIdlClass::OStdIdlClass(
162cdf0e10cSrcweir 					const Reference < XMultiServiceFactory > &rSMgr ,
163cdf0e10cSrcweir 					const OUString & sImplementationName ,
164cdf0e10cSrcweir 					const Reference < XIdlClass > & rSuperClass,
165cdf0e10cSrcweir 					const Sequence < OUString > &seq
166cdf0e10cSrcweir 						  ) SAL_THROW( () ) :
167cdf0e10cSrcweir 				m_sImplementationName( sImplementationName ) ,
168cdf0e10cSrcweir 				m_seqSupportedInterface( seq ),
169cdf0e10cSrcweir 				m_rSMgr( rSMgr )
170cdf0e10cSrcweir {
171cdf0e10cSrcweir 	if( rSuperClass.is() )
172cdf0e10cSrcweir 		m_seqSuperClasses = Sequence< Reference < XIdlClass > >( &rSuperClass, 1 );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
queryInterface(const Type & rType)176cdf0e10cSrcweir Any SAL_CALL OStdIdlClass::queryInterface( const Type & rType )
177cdf0e10cSrcweir 	 throw(::com::sun::star::uno::RuntimeException)
178cdf0e10cSrcweir {
179cdf0e10cSrcweir 	Any aRet( ::cppu::queryInterface(
180cdf0e10cSrcweir 		rType, static_cast< XIdlClass * >( this ), static_cast< XIdlClassProvider * >( this ) ) );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 	return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 
getInterfaces()186cdf0e10cSrcweir Sequence< Reference< XIdlClass > > SAL_CALL OStdIdlClass::getInterfaces(  ) throw(RuntimeException)
187cdf0e10cSrcweir {
188cdf0e10cSrcweir 	int nMax = m_seqSupportedInterface.getLength();
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     Reference< XIdlReflection > const & rCoreRefl = get_corefl();
191cdf0e10cSrcweir     if( rCoreRefl.is() )
192cdf0e10cSrcweir     {
193cdf0e10cSrcweir         Sequence< Reference< XIdlClass > > seqClasses( nMax );
194cdf0e10cSrcweir 
195cdf0e10cSrcweir         for( int n = 0 ; n < nMax ; n++ )
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir             seqClasses.getArray()[n] = rCoreRefl->forName( m_seqSupportedInterface.getArray()[n] );
198cdf0e10cSrcweir         }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         return seqClasses;
201cdf0e10cSrcweir 	}
202cdf0e10cSrcweir 	return Sequence< Reference< XIdlClass > > () ;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 
206cdf0e10cSrcweir // XIdlClassProvider
getIdlClasses(void)207cdf0e10cSrcweir Sequence< Reference < XIdlClass > > SAL_CALL OStdIdlClass::getIdlClasses(void)
208cdf0e10cSrcweir 	throw (RuntimeException)
209cdf0e10cSrcweir {
210cdf0e10cSrcweir 	// weak reference to cache the standard class
211cdf0e10cSrcweir     static WeakReference< XIdlClass >	weakRef;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 	// try to make weakref hard
214cdf0e10cSrcweir 	Reference < XIdlClass > r = weakRef;
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 	if( ! r.is() ) {
217cdf0e10cSrcweir 		// xidlclass has not been initialized before or has been destroyed already.
218cdf0e10cSrcweir 		r = ::cppu::createStandardClass(
219cdf0e10cSrcweir 										m_rSMgr ,
220cdf0e10cSrcweir 										OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.cppuhelper.OStdIdlClass") ) ,
221cdf0e10cSrcweir 										Reference < XIdlClass > () ,
222cdf0e10cSrcweir   										SAL_STATIC_CAST( XIdlClassProvider * , this ) ,
223cdf0e10cSrcweir   										SAL_STATIC_CAST( XIdlClass * , this )
224cdf0e10cSrcweir 					 					);
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 		// store reference for later use
227cdf0e10cSrcweir 		weakRef = r;
228cdf0e10cSrcweir 	}
229cdf0e10cSrcweir 
230cdf0e10cSrcweir 	return Sequence < Reference < XIdlClass > > ( &r , 1 );
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 
236cdf0e10cSrcweir // external constructor
createStandardClassWithSequence(const Reference<XMultiServiceFactory> & rSMgr,const OUString & sImplementationName,const Reference<XIdlClass> & rSuperClass,const Sequence<OUString> & seqInterfaceNames)237cdf0e10cSrcweir XIdlClass *  SAL_CALL createStandardClassWithSequence(
238cdf0e10cSrcweir 					const Reference < XMultiServiceFactory > &rSMgr ,
239cdf0e10cSrcweir 					const OUString & sImplementationName ,
240cdf0e10cSrcweir 					const Reference < XIdlClass > & rSuperClass,
241cdf0e10cSrcweir 					const Sequence < OUString > &seqInterfaceNames )
242cdf0e10cSrcweir 	SAL_THROW( () )
243cdf0e10cSrcweir {
244cdf0e10cSrcweir 	return SAL_STATIC_CAST(
245cdf0e10cSrcweir 						XIdlClass * ,
246cdf0e10cSrcweir 						new OStdIdlClass(
247cdf0e10cSrcweir 											rSMgr ,
248cdf0e10cSrcweir 											sImplementationName,
249cdf0e10cSrcweir 											rSuperClass,
250cdf0e10cSrcweir 											seqInterfaceNames
251cdf0e10cSrcweir 										  )
252cdf0e10cSrcweir 					   );
253cdf0e10cSrcweir }
254cdf0e10cSrcweir 
255cdf0e10cSrcweir } //end namespace cppu
256