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