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