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