xref: /trunk/main/forms/source/inc/forms_module.hxx (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 #ifndef FORMS_MODULE_INCLUDE_CONTEXT
29*cdf0e10cSrcweir     #error "not to be included directly! use 'foo_module.hxx instead'!"
30*cdf0e10cSrcweir #endif
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #ifndef FORMS_MODULE_NAMESPACE
33*cdf0e10cSrcweir     #error "set FORMS_MODULE_NAMESPACE to your namespace identifier!"
34*cdf0e10cSrcweir #endif
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <osl/mutex.hxx>
37*cdf0e10cSrcweir #include <tools/resid.hxx>
38*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
41*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
42*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
43*cdf0e10cSrcweir #include <rtl/string.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir //.........................................................................
46*cdf0e10cSrcweir namespace FORMS_MODULE_NAMESPACE
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir //.........................................................................
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir     typedef ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory > (SAL_CALL *FactoryInstantiation)
51*cdf0e10cSrcweir         (
52*cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rServiceManager,
53*cdf0e10cSrcweir             const ::rtl::OUString & _rComponentName,
54*cdf0e10cSrcweir             ::cppu::ComponentInstantiation _pCreateFunction,
55*cdf0e10cSrcweir             const ::com::sun::star::uno::Sequence< ::rtl::OUString > & _rServiceNames,
56*cdf0e10cSrcweir             rtl_ModuleCount* _pModuleCounter
57*cdf0e10cSrcweir         );
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     //=========================================================================
60*cdf0e10cSrcweir     //= OFormsModule
61*cdf0e10cSrcweir     //=========================================================================
62*cdf0e10cSrcweir     class OFormsModule
63*cdf0e10cSrcweir     {
64*cdf0e10cSrcweir     private:
65*cdf0e10cSrcweir         OFormsModule();
66*cdf0e10cSrcweir             // not implemented. OFormsModule is a static class
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     protected:
69*cdf0e10cSrcweir         // auto registration administration
70*cdf0e10cSrcweir         static  ::com::sun::star::uno::Sequence< ::rtl::OUString >*
71*cdf0e10cSrcweir             s_pImplementationNames;
72*cdf0e10cSrcweir         static  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::rtl::OUString > >*
73*cdf0e10cSrcweir             s_pSupportedServices;
74*cdf0e10cSrcweir         static  ::com::sun::star::uno::Sequence< sal_Int64 >*
75*cdf0e10cSrcweir             s_pCreationFunctionPointers;
76*cdf0e10cSrcweir         static  ::com::sun::star::uno::Sequence< sal_Int64 >*
77*cdf0e10cSrcweir             s_pFactoryFunctionPointers;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     public:
80*cdf0e10cSrcweir         /** register a component implementing a service with the given data.
81*cdf0e10cSrcweir             @param  _rImplementationName
82*cdf0e10cSrcweir                         the implementation name of the component
83*cdf0e10cSrcweir             @param  _rServiceNames
84*cdf0e10cSrcweir                         the services the component supports
85*cdf0e10cSrcweir             @param  _pCreateFunction
86*cdf0e10cSrcweir                         a function for creating an instance of the component
87*cdf0e10cSrcweir             @param  _pFactoryFunction
88*cdf0e10cSrcweir                         a function for creating a factory for that component
89*cdf0e10cSrcweir             @see revokeComponent
90*cdf0e10cSrcweir         */
91*cdf0e10cSrcweir         static void registerComponent(
92*cdf0e10cSrcweir             const ::rtl::OUString& _rImplementationName,
93*cdf0e10cSrcweir             const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rServiceNames,
94*cdf0e10cSrcweir             ::cppu::ComponentInstantiation _pCreateFunction,
95*cdf0e10cSrcweir             FactoryInstantiation _pFactoryFunction);
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         /** revoke the registration for the specified component
98*cdf0e10cSrcweir             @param  _rImplementationName
99*cdf0e10cSrcweir                 the implementation name of the component
100*cdf0e10cSrcweir         */
101*cdf0e10cSrcweir         static void revokeComponent(
102*cdf0e10cSrcweir             const ::rtl::OUString& _rImplementationName);
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         /** creates a Factory for the component with the given implementation name.
105*cdf0e10cSrcweir             <p>Usually used from within component_getFactory.<p/>
106*cdf0e10cSrcweir             @param  _rxServiceManager
107*cdf0e10cSrcweir                         a pointer to an XMultiServiceFactory interface as got in component_getFactory
108*cdf0e10cSrcweir             @param  _pImplementationName
109*cdf0e10cSrcweir                         the implementation name of the component
110*cdf0e10cSrcweir             @return
111*cdf0e10cSrcweir                         the XInterface access to a factory for the component
112*cdf0e10cSrcweir         */
113*cdf0e10cSrcweir         static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > getComponentFactory(
114*cdf0e10cSrcweir             const ::rtl::OUString& _rImplementationName,
115*cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxServiceManager
116*cdf0e10cSrcweir             );
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     private:
119*cdf0e10cSrcweir         /** ensure that the impl class exists
120*cdf0e10cSrcweir             @precond m_aMutex is guarded when this method gets called
121*cdf0e10cSrcweir         */
122*cdf0e10cSrcweir         static void ensureImpl();
123*cdf0e10cSrcweir     };
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     //==========================================================================
126*cdf0e10cSrcweir     //= OMultiInstanceAutoRegistration
127*cdf0e10cSrcweir     //==========================================================================
128*cdf0e10cSrcweir     template <class TYPE>
129*cdf0e10cSrcweir     class OMultiInstanceAutoRegistration
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir     public:
132*cdf0e10cSrcweir         /** automatically registeres a multi instance component
133*cdf0e10cSrcweir             <p>Assumed that the template argument has the three methods
134*cdf0e10cSrcweir                 <ul>
135*cdf0e10cSrcweir                     <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
136*cdf0e10cSrcweir                     <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
137*cdf0e10cSrcweir                     <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
138*cdf0e10cSrcweir                         Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
139*cdf0e10cSrcweir                         </li>
140*cdf0e10cSrcweir                 <ul/>
141*cdf0e10cSrcweir             the instantiation of this object will automatically register the class via <method>OFormsModule::registerComponent</method>.
142*cdf0e10cSrcweir             <p/>
143*cdf0e10cSrcweir             <p>The factory creation function used is <code>::cppu::createSingleFactory</code>.</p>
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir             @see OOneInstanceAutoRegistration
146*cdf0e10cSrcweir         */
147*cdf0e10cSrcweir         OMultiInstanceAutoRegistration();
148*cdf0e10cSrcweir         ~OMultiInstanceAutoRegistration();
149*cdf0e10cSrcweir     };
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     template <class TYPE>
152*cdf0e10cSrcweir     OMultiInstanceAutoRegistration<TYPE>::OMultiInstanceAutoRegistration()
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         OFormsModule::registerComponent(
155*cdf0e10cSrcweir             TYPE::getImplementationName_Static(),
156*cdf0e10cSrcweir             TYPE::getSupportedServiceNames_Static(),
157*cdf0e10cSrcweir             TYPE::Create,
158*cdf0e10cSrcweir             ::cppu::createSingleFactory
159*cdf0e10cSrcweir             );
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     template <class TYPE>
163*cdf0e10cSrcweir     OMultiInstanceAutoRegistration<TYPE>::~OMultiInstanceAutoRegistration()
164*cdf0e10cSrcweir     {
165*cdf0e10cSrcweir         OFormsModule::revokeComponent(TYPE::getImplementationName_Static());
166*cdf0e10cSrcweir     }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     //==========================================================================
169*cdf0e10cSrcweir     //= OOneInstanceAutoRegistration
170*cdf0e10cSrcweir     //==========================================================================
171*cdf0e10cSrcweir     template <class TYPE>
172*cdf0e10cSrcweir     class OOneInstanceAutoRegistration
173*cdf0e10cSrcweir     {
174*cdf0e10cSrcweir     public:
175*cdf0e10cSrcweir         /** automatically registeres a single instance component
176*cdf0e10cSrcweir             <p>Assumed that the template argument has the three methods
177*cdf0e10cSrcweir                 <ul>
178*cdf0e10cSrcweir                     <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
179*cdf0e10cSrcweir                     <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
180*cdf0e10cSrcweir                     <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
181*cdf0e10cSrcweir                         Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
182*cdf0e10cSrcweir                         </li>
183*cdf0e10cSrcweir                 <ul/>
184*cdf0e10cSrcweir             the instantiation of this object will automatically register the class via <method>OFormsModule::registerComponent</method>.
185*cdf0e10cSrcweir             <p/>
186*cdf0e10cSrcweir             The factory creation function used is <code>::cppu::createOneInstanceFactory</code>.
187*cdf0e10cSrcweir             @see OOneInstanceAutoRegistration
188*cdf0e10cSrcweir         */
189*cdf0e10cSrcweir         OOneInstanceAutoRegistration();
190*cdf0e10cSrcweir         ~OOneInstanceAutoRegistration();
191*cdf0e10cSrcweir     };
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir     template <class TYPE>
194*cdf0e10cSrcweir     OOneInstanceAutoRegistration<TYPE>::OOneInstanceAutoRegistration()
195*cdf0e10cSrcweir     {
196*cdf0e10cSrcweir         OFormsModule::registerComponent(
197*cdf0e10cSrcweir             TYPE::getImplementationName_Static(),
198*cdf0e10cSrcweir             TYPE::getSupportedServiceNames_Static(),
199*cdf0e10cSrcweir             TYPE::Create,
200*cdf0e10cSrcweir             ::cppu::createOneInstanceFactory
201*cdf0e10cSrcweir             );
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     template <class TYPE>
205*cdf0e10cSrcweir     OOneInstanceAutoRegistration<TYPE>::~OOneInstanceAutoRegistration()
206*cdf0e10cSrcweir     {
207*cdf0e10cSrcweir         OFormsModule::revokeComponent(TYPE::getImplementationName_Static());
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir     //==========================================================================
211*cdf0e10cSrcweir     //= helper for classes implementing the service handling via
212*cdf0e10cSrcweir     //= OMultiInstanceAutoRegistration or OOneInstanceAutoRegistration
213*cdf0e10cSrcweir     //==========================================================================
214*cdf0e10cSrcweir     #define DECLARE_SERVICE_REGISTRATION( classname ) \
215*cdf0e10cSrcweir         virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException); \
216*cdf0e10cSrcweir         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException); \
217*cdf0e10cSrcweir         \
218*cdf0e10cSrcweir         static  ::rtl::OUString SAL_CALL getImplementationName_Static(); \
219*cdf0e10cSrcweir         static  ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_Static(); \
220*cdf0e10cSrcweir         static  ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL Create( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory ); \
221*cdf0e10cSrcweir         \
222*cdf0e10cSrcweir         friend class OOneInstanceAutoRegistration< classname >; \
223*cdf0e10cSrcweir         friend class OMultiInstanceAutoRegistration< classname >; \
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir     #define IMPLEMENT_SERVICE_REGISTRATION_BASE( classname, baseclass ) \
226*cdf0e10cSrcweir         \
227*cdf0e10cSrcweir         ::rtl::OUString SAL_CALL classname::getImplementationName(  ) throw ( RuntimeException ) \
228*cdf0e10cSrcweir         { return getImplementationName_Static(); } \
229*cdf0e10cSrcweir         \
230*cdf0e10cSrcweir         Sequence< ::rtl::OUString > SAL_CALL classname::getSupportedServiceNames(  ) throw (RuntimeException) \
231*cdf0e10cSrcweir         { \
232*cdf0e10cSrcweir             return ::comphelper::concatSequences( \
233*cdf0e10cSrcweir                 getAggregateServiceNames(), \
234*cdf0e10cSrcweir                 getSupportedServiceNames_Static() \
235*cdf0e10cSrcweir             ); \
236*cdf0e10cSrcweir         } \
237*cdf0e10cSrcweir         \
238*cdf0e10cSrcweir         ::rtl::OUString SAL_CALL classname::getImplementationName_Static() \
239*cdf0e10cSrcweir         { return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.forms."#classname ) ); } \
240*cdf0e10cSrcweir         \
241*cdf0e10cSrcweir         Reference< XInterface > SAL_CALL classname::Create( const Reference< XMultiServiceFactory >& _rxFactory ) \
242*cdf0e10cSrcweir         { return static_cast< XServiceInfo* >( new classname( _rxFactory ) ); } \
243*cdf0e10cSrcweir         \
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir     #define IMPLEMENT_SERVICE_REGISTRATION_1( classname, baseclass, service1 ) \
246*cdf0e10cSrcweir         IMPLEMENT_SERVICE_REGISTRATION_BASE( classname, baseclass ) \
247*cdf0e10cSrcweir         \
248*cdf0e10cSrcweir         Sequence< ::rtl::OUString > SAL_CALL classname::getSupportedServiceNames_Static() \
249*cdf0e10cSrcweir         { \
250*cdf0e10cSrcweir             Sequence< ::rtl::OUString > aOwnNames( 1 ); \
251*cdf0e10cSrcweir             aOwnNames[ 0 ] = service1; \
252*cdf0e10cSrcweir             \
253*cdf0e10cSrcweir             return ::comphelper::concatSequences( \
254*cdf0e10cSrcweir                 baseclass::getSupportedServiceNames_Static(), \
255*cdf0e10cSrcweir                 aOwnNames \
256*cdf0e10cSrcweir             ); \
257*cdf0e10cSrcweir         } \
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir     #define IMPLEMENT_SERVICE_REGISTRATION_2( classname, baseclass, service1, service2 ) \
260*cdf0e10cSrcweir         IMPLEMENT_SERVICE_REGISTRATION_BASE( classname, baseclass ) \
261*cdf0e10cSrcweir         \
262*cdf0e10cSrcweir         Sequence< ::rtl::OUString > SAL_CALL classname::getSupportedServiceNames_Static() \
263*cdf0e10cSrcweir         { \
264*cdf0e10cSrcweir             Sequence< ::rtl::OUString > aOwnNames( 2 ); \
265*cdf0e10cSrcweir             aOwnNames[ 0 ] = service1; \
266*cdf0e10cSrcweir             aOwnNames[ 1 ] = service2; \
267*cdf0e10cSrcweir             \
268*cdf0e10cSrcweir             return ::comphelper::concatSequences( \
269*cdf0e10cSrcweir                 baseclass::getSupportedServiceNames_Static(), \
270*cdf0e10cSrcweir                 aOwnNames \
271*cdf0e10cSrcweir             ); \
272*cdf0e10cSrcweir         } \
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir     #define IMPLEMENT_SERVICE_REGISTRATION_7( classname, baseclass, service1, service2, service3, service4 , service5, service6, service7 ) \
275*cdf0e10cSrcweir         IMPLEMENT_SERVICE_REGISTRATION_BASE( classname, baseclass ) \
276*cdf0e10cSrcweir         \
277*cdf0e10cSrcweir            Sequence< ::rtl::OUString > SAL_CALL classname::getSupportedServiceNames_Static() \
278*cdf0e10cSrcweir            { \
279*cdf0e10cSrcweir                    Sequence< ::rtl::OUString > aOwnNames( 7 ); \
280*cdf0e10cSrcweir                    aOwnNames[ 0 ] = service1; \
281*cdf0e10cSrcweir                    aOwnNames[ 1 ] = service2; \
282*cdf0e10cSrcweir                    aOwnNames[ 2 ] = service3; \
283*cdf0e10cSrcweir                    aOwnNames[ 3 ] = service4; \
284*cdf0e10cSrcweir                    aOwnNames[ 4 ] = service5; \
285*cdf0e10cSrcweir                    aOwnNames[ 5 ] = service6; \
286*cdf0e10cSrcweir                    aOwnNames[ 6 ] = service7; \
287*cdf0e10cSrcweir             \
288*cdf0e10cSrcweir             return ::comphelper::concatSequences( \
289*cdf0e10cSrcweir                 baseclass::getSupportedServiceNames_Static(), \
290*cdf0e10cSrcweir                 aOwnNames \
291*cdf0e10cSrcweir             ); \
292*cdf0e10cSrcweir            } \
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir     #define IMPLEMENT_SERVICE_REGISTRATION_8( classname, baseclass, service1, service2, service3, service4 , service5, service6, service7, service8 ) \
295*cdf0e10cSrcweir         IMPLEMENT_SERVICE_REGISTRATION_BASE( classname, baseclass ) \
296*cdf0e10cSrcweir         \
297*cdf0e10cSrcweir            Sequence< ::rtl::OUString > SAL_CALL classname::getSupportedServiceNames_Static() \
298*cdf0e10cSrcweir            { \
299*cdf0e10cSrcweir                    Sequence< ::rtl::OUString > aOwnNames( 8 ); \
300*cdf0e10cSrcweir                    aOwnNames[ 0 ] = service1; \
301*cdf0e10cSrcweir                    aOwnNames[ 1 ] = service2; \
302*cdf0e10cSrcweir                    aOwnNames[ 2 ] = service3; \
303*cdf0e10cSrcweir                    aOwnNames[ 3 ] = service4; \
304*cdf0e10cSrcweir                    aOwnNames[ 4 ] = service5; \
305*cdf0e10cSrcweir                    aOwnNames[ 5 ] = service6; \
306*cdf0e10cSrcweir                    aOwnNames[ 6 ] = service7; \
307*cdf0e10cSrcweir                    aOwnNames[ 6 ] = service8; \
308*cdf0e10cSrcweir             \
309*cdf0e10cSrcweir             return ::comphelper::concatSequences( \
310*cdf0e10cSrcweir                 baseclass::getSupportedServiceNames_Static(), \
311*cdf0e10cSrcweir                 aOwnNames \
312*cdf0e10cSrcweir             ); \
313*cdf0e10cSrcweir            } \
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir //.........................................................................
316*cdf0e10cSrcweir }   // namespace FORMS_MODULE_NAMESPACE
317*cdf0e10cSrcweir //.........................................................................
318