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