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