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 : : #ifndef COMPHELPER_SERVICEDECL_HXX_INCLUDED
20 : : #define COMPHELPER_SERVICEDECL_HXX_INCLUDED
21 : :
22 : : #include <comphelper/comphelperdllapi.h>
23 : : #include <cppuhelper/implbase1.hxx>
24 : : #include <com/sun/star/uno/XComponentContext.hpp>
25 : : #include <com/sun/star/lang/XServiceInfo.hpp>
26 : : #include <com/sun/star/registry/XRegistryKey.hpp>
27 : : #include <uno/environment.h>
28 : : #include <boost/utility.hpp>
29 : : #include <boost/function.hpp>
30 : : #include <boost/preprocessor/cat.hpp>
31 : : #include <boost/preprocessor/repetition.hpp>
32 : : #include <boost/preprocessor/seq/enum.hpp>
33 : :
34 : : namespace comphelper {
35 : : namespace service_decl {
36 : :
37 : : class ServiceDecl;
38 : :
39 : : namespace detail {
40 : : namespace css = ::com::sun::star;
41 : : typedef ::boost::function3<
42 : : css::uno::Reference<css::uno::XInterface> /* return */,
43 : : ServiceDecl const&,
44 : : css::uno::Sequence<css::uno::Any> const&,
45 : : css::uno::Reference<css::uno::XComponentContext> const&> CreateFuncF;
46 : : }
47 : :
48 : : /** Class to declare a service implementation. There is no need to implement
49 : : lang::XServiceInfo nor lang::XInitialization anymore.
50 : : The declaration can be done in various ways, the (simplest) form is
51 : :
52 : : <pre>
53 : : class MyClass : public cppu::WeakImplHelper2<XInterface1, XInterface2> {
54 : : public:
55 : : MyClass( uno::Reference<uno::XComponentContext> const& xContext )
56 : : [...]
57 : : };
58 : : [...]
59 : : namespace sdecl = comphelper::service_decl;
60 : : sdecl::ServiceDecl const myDecl(
61 : : sdecl::class_<MyClass>(),
62 : : "my.unique.implementation.name",
63 : : "MyServiceSpec1;MyServiceSpec2" );
64 : : </pre>
65 : :
66 : : If the service demands initialization by arguments, the implementation
67 : : class has to define a constructor taking both arguments and component
68 : : context:
69 : :
70 : : <pre>
71 : : class MyClass : public cppu::WeakImplHelper2<XInterface1, XInterface2> {
72 : : public:
73 : : MyClass( uno::Sequence<uno::Any> const& args,
74 : : uno::Reference<uno:XComponentContext> const& xContext )
75 : : [...]
76 : : };
77 : : [...]
78 : : namespace sdecl = comphelper::service_decl;
79 : : sdecl::ServiceDecl const myDecl(
80 : : sdecl::class_<MyClass, sdecl::with_args<true> >(),
81 : : "my.unique.implementation.name",
82 : : "MyServiceSpec1;MyServiceSpec2" );
83 : : </pre>
84 : :
85 : : Additionally, there is the possibility to process some code after creation,
86 : : e.g. to add the newly created object as a listener or perform aggregation
87 : : (C++-UNO only):
88 : :
89 : : <pre>
90 : : uno::Reference<uno::XInterface> somePostProcCode( MyClass * p );
91 : : [...]
92 : : namespace sdecl = comphelper::service_decl;
93 : : sdecl::ServiceDecl const myDecl(
94 : : sdecl::class_<MyClass, ... >(&somePostProcCode),
95 : : "my.unique.implementation.name",
96 : : "MyServiceSpec1;MyServiceSpec2" );
97 : : </pre>
98 : :
99 : : In the latter case, somePostProcCode gets the yet unacquired "raw" pointer.
100 : : */
101 [ + - ]: 1450 : class COMPHELPER_DLLPUBLIC ServiceDecl : private ::boost::noncopyable
102 : : {
103 : : public:
104 : : /** Ctor for multiple supported service names.
105 : :
106 : : @param implClass implementation class description
107 : : @param pImplName implementation name
108 : : @param pSupportedServiceNames supported service names
109 : : @param cDelim delimiter for supported service names
110 : : */
111 : : template <typename ImplClassT>
112 : 1574 : ServiceDecl( ImplClassT const& implClass,
113 : : char const* pImplName,
114 : : char const* pSupportedServiceNames, char cDelim = ';' )
115 : : : m_createFunc(implClass.m_createFunc),
116 : : m_pImplName(pImplName),
117 : : m_pServiceNames(pSupportedServiceNames),
118 [ + - ][ + - ]: 1574 : m_cDelim(cDelim) {}
119 : :
120 : : /// @internal gets called by component_getFactoryHelper()
121 : : void * getFactory( sal_Char const* pImplName ) const;
122 : :
123 : : /// @return supported service names
124 : : ::com::sun::star::uno::Sequence< ::rtl::OUString>
125 : : getSupportedServiceNames() const;
126 : :
127 : : /// @return whether name is in set of supported service names
128 : : bool supportsService( ::rtl::OUString const& name ) const;
129 : :
130 : : /// @return implementation name
131 : : ::rtl::OUString getImplementationName() const;
132 : :
133 : : private:
134 : : class Factory;
135 : : friend class Factory;
136 : :
137 : : detail::CreateFuncF const m_createFunc;
138 : : char const* const m_pImplName;
139 : : char const* const m_pServiceNames;
140 : : char const m_cDelim;
141 : : };
142 : :
143 : : /** To specify whether the implementation class expects arguments
144 : : (uno::Sequence<uno::Any>).
145 : : */
146 : : template <bool> struct with_args;
147 : :
148 : : /// @internal
149 : : namespace detail {
150 : : template <typename ImplT>
151 [ - + ][ # # ]: 2848 : class OwnServiceImpl
152 : : : public ImplT,
153 : : private ::boost::noncopyable
154 : : {
155 : : typedef ImplT BaseT;
156 : :
157 : : public:
158 : 2599 : OwnServiceImpl(
159 : : ServiceDecl const& rServiceDecl,
160 : : css::uno::Sequence<css::uno::Any> const& args,
161 : : css::uno::Reference<css::uno::XComponentContext> const& xContext )
162 : 2599 : :BaseT(args, xContext), m_rServiceDecl(rServiceDecl) {}
163 : 262 : OwnServiceImpl(
164 : : ServiceDecl const& rServiceDecl,
165 : : css::uno::Reference<css::uno::XComponentContext> const& xContext )
166 : 262 : : BaseT(xContext), m_rServiceDecl(rServiceDecl) {}
167 : :
168 : : // XServiceInfo
169 : 0 : virtual ::rtl::OUString SAL_CALL getImplementationName()
170 : : throw (css::uno::RuntimeException) {
171 : 0 : return m_rServiceDecl.getImplementationName();
172 : : }
173 : 20 : virtual sal_Bool SAL_CALL supportsService( ::rtl::OUString const& name )
174 : : throw (css::uno::RuntimeException) {
175 : 20 : return m_rServiceDecl.supportsService(name);
176 : : }
177 : : virtual css::uno::Sequence< ::rtl::OUString>
178 : 0 : SAL_CALL getSupportedServiceNames() throw (css::uno::RuntimeException) {
179 : 0 : return m_rServiceDecl.getSupportedServiceNames();
180 : : }
181 : :
182 : : private:
183 : : ServiceDecl const& m_rServiceDecl;
184 : : };
185 : :
186 : : template <typename ImplT>
187 [ - + ][ - + ]: 5664 : class ServiceImpl : public OwnServiceImpl< ::cppu::ImplInheritanceHelper1<ImplT,css::lang::XServiceInfo> >
188 : : {
189 : : typedef OwnServiceImpl< ::cppu::ImplInheritanceHelper1<ImplT,css::lang::XServiceInfo> > ServiceImpl_BASE;
190 : : public:
191 : 2575 : ServiceImpl(
192 : : ServiceDecl const& rServiceDecl,
193 : : css::uno::Sequence<css::uno::Any> const& args,
194 : : css::uno::Reference<css::uno::XComponentContext> const& xContext )
195 : 2575 : : ServiceImpl_BASE(rServiceDecl, args, xContext) {}
196 : 262 : ServiceImpl(
197 : : ServiceDecl const& rServiceDecl,
198 : : css::uno::Reference<css::uno::XComponentContext> const& xContext )
199 : 262 : : ServiceImpl_BASE(rServiceDecl, xContext) {}
200 : : };
201 : :
202 : : template <typename ServiceImplT>
203 : : struct PostProcessDefault {
204 : : css::uno::Reference<css::uno::XInterface>
205 : 2861 : operator()( ServiceImplT * p ) const {
206 [ + - ][ # # ]: 2861 : return static_cast<css::lang::XServiceInfo *>(p);
207 : : }
208 : : };
209 : :
210 : : template <typename ImplT, typename PostProcessFuncT, typename WithArgsT>
211 : : struct CreateFunc;
212 : :
213 : : template <typename ImplT, typename PostProcessFuncT>
214 : : struct CreateFunc<ImplT, PostProcessFuncT, with_args<false> > {
215 : : PostProcessFuncT const m_postProcessFunc;
216 : 385 : explicit CreateFunc( PostProcessFuncT const& postProcessFunc )
217 : 385 : : m_postProcessFunc(postProcessFunc) {}
218 : :
219 : : css::uno::Reference<css::uno::XInterface>
220 : 262 : operator()( ServiceDecl const& rServiceDecl,
221 : : css::uno::Sequence<css::uno::Any> const&,
222 : : css::uno::Reference<css::uno::XComponentContext>
223 : : const& xContext ) const
224 : : {
225 : : return m_postProcessFunc(
226 [ + - ]: 262 : new ImplT( rServiceDecl, xContext ) );
227 : : }
228 : : };
229 : :
230 : : template <typename ImplT, typename PostProcessFuncT>
231 : : struct CreateFunc<ImplT, PostProcessFuncT, with_args<true> > {
232 : : PostProcessFuncT const m_postProcessFunc;
233 : 1189 : explicit CreateFunc( PostProcessFuncT const& postProcessFunc )
234 : 1189 : : m_postProcessFunc(postProcessFunc) {}
235 : :
236 : : css::uno::Reference<css::uno::XInterface>
237 : 2599 : operator()( ServiceDecl const& rServiceDecl,
238 : : css::uno::Sequence<css::uno::Any> const& args,
239 : : css::uno::Reference<css::uno::XComponentContext>
240 : : const& xContext ) const
241 : : {
242 : : return m_postProcessFunc(
243 [ + - ]: 2599 : new ImplT( rServiceDecl, args, xContext ) );
244 : : }
245 : : };
246 : :
247 : : } // namespace detail
248 : :
249 : : /** Defines a service implementation class.
250 : :
251 : : @tpl ImplT_ service implementation class
252 : : @WithArgsT whether the implementation class ctor expects arguments
253 : : (uno::Sequence<uno::Any>, uno::Reference<uno::XComponentContext>)
254 : : or just (uno::Reference<uno::XComponentContext>)
255 : : */
256 : : template <typename ImplT_, typename WithArgsT = with_args<false> >
257 : 1450 : struct serviceimpl_base {
258 : : typedef ImplT_ ImplT;
259 : :
260 : : detail::CreateFuncF const m_createFunc;
261 : :
262 : : typedef detail::PostProcessDefault<ImplT> PostProcessDefaultT;
263 : :
264 : : /** Default ctor. Implementation class without args, expecting
265 : : component context as single argument.
266 : : */
267 : 1574 : serviceimpl_base() : m_createFunc(
268 : : detail::CreateFunc<ImplT, PostProcessDefaultT, WithArgsT>(
269 [ + - ][ + - ]: 1574 : PostProcessDefaultT() ) ) {}
270 : :
271 : : /** Ctor to pass a post processing function/functor.
272 : :
273 : : @tpl PostProcessDefaultT let your compiler deduce this
274 : : @param postProcessFunc function/functor that gets the yet unacquired
275 : : ImplT_ pointer returning a
276 : : uno::Reference<uno::XInterface>
277 : : */
278 : : template <typename PostProcessFuncT>
279 : 0 : explicit serviceimpl_base( PostProcessFuncT const& postProcessFunc )
280 : : : m_createFunc( detail::CreateFunc<ImplT, PostProcessFuncT, WithArgsT>(
281 : 0 : postProcessFunc ) ) {}
282 : : };
283 : :
284 : : template <typename ImplT_, typename WithArgsT = with_args<false> >
285 : 1417 : struct class_ : public serviceimpl_base< detail::ServiceImpl<ImplT_>, WithArgsT >
286 : : {
287 : : typedef serviceimpl_base< detail::ServiceImpl<ImplT_>, WithArgsT > baseT;
288 : : /** Default ctor. Implementation class without args, expecting
289 : : component context as single argument.
290 : : */
291 : 1541 : class_() : baseT() {}
292 : : template <typename PostProcessFuncT>
293 : : /** Ctor to pass a post processing function/functor.
294 : :
295 : : @tpl PostProcessDefaultT let your compiler deduce this
296 : : @param postProcessFunc function/functor that gets the yet unacquired
297 : : ImplT_ pointer returning a
298 : : uno::Reference<uno::XInterface>
299 : : */
300 : 0 : explicit class_( PostProcessFuncT const& postProcessFunc ) : baseT( postProcessFunc ) {}
301 : : };
302 : :
303 : : //
304 : : // component_... helpers with arbitrary service declarations:
305 : : //
306 : :
307 : : #define COMPHELPER_SERVICEDECL_getFactory(z_, n_, unused_) \
308 : : if (pRet == 0) \
309 : : pRet = BOOST_PP_CAT(s, n_).getFactory(pImplName);
310 : :
311 : : /** The following preprocessor repetitions generate functions like
312 : :
313 : : <pre>
314 : : inline void * component_getFactoryHelper(
315 : : sal_Char const* pImplName,
316 : : ::com::sun::star::lang::XMultiServiceFactory *,
317 : : ::com::sun::star::registry::XRegistryKey * xRegistryKey,
318 : : ServiceDecl const& s0, ServiceDecl const& s1, ... );
319 : : </pre>
320 : :
321 : : which call on the passed service declarations.
322 : :
323 : : The maximum number of service declarations can be set by defining
324 : : COMPHELPER_SERVICEDECL_COMPONENT_HELPER_MAX_ARGS; its default is 8.
325 : : */
326 : : #define COMPHELPER_SERVICEDECL_make(z_, n_, unused_) \
327 : : inline void * component_getFactoryHelper( \
328 : : sal_Char const* pImplName, \
329 : : ::com::sun::star::lang::XMultiServiceFactory *, \
330 : : ::com::sun::star::registry::XRegistryKey *, \
331 : : BOOST_PP_ENUM_PARAMS(n_, ServiceDecl const& s) ) \
332 : : { \
333 : : void * pRet = 0; \
334 : : BOOST_PP_REPEAT(n_, COMPHELPER_SERVICEDECL_getFactory, ~) \
335 : : return pRet; \
336 : : }
337 : :
338 : : #ifndef COMPHELPER_SERVICEDECL_COMPONENT_HELPER_MAX_ARGS
339 : : #define COMPHELPER_SERVICEDECL_COMPONENT_HELPER_MAX_ARGS 8
340 : : #endif
341 : :
342 : : BOOST_PP_REPEAT_FROM_TO(1, COMPHELPER_SERVICEDECL_COMPONENT_HELPER_MAX_ARGS,
343 [ + - ][ + + ]: 1153 : COMPHELPER_SERVICEDECL_make, ~)
[ + + ][ + + ]
[ + + ][ + + ]
[ + + ][ + + ]
[ + - + + ]
[ - + + + ]
344 : :
345 : : #undef COMPHELPER_SERVICEDECL_COMPONENT_HELPER_MAX_ARGS
346 : : #undef COMPHELPER_SERVICEDECL_make
347 : : #undef COMPHELPER_SERVICEDECL_getFactory
348 : :
349 : : } // namespace service_decl
350 : : } // namespace comphelper
351 : :
352 : : /** The following preprocessor macro generates the C access functions,
353 : : that are used to initialize and register the components of a
354 : : shared library object.
355 : :
356 : : If you have, say, written a lib that contains three distinct
357 : : components, each with its own ServiceDecl object, you might want
358 : : to employ the following code:
359 : :
360 : : <pre>
361 : : // must reside outside _any_ namespace
362 : : COMPHELPER_SERVICEDECL_EXPORTS3(yourServiceDecl1,
363 : : yourServiceDecl2,
364 : : yourServiceDecl3);
365 : : </pre>
366 : :
367 : : For your convenience, the COMPHELPER_SERVICEDECL_EXPORTS<N> macro
368 : : comes pre-defined up to N=8, if you should need more arguments,
369 : : call COMPHELPER_SERVICEDECL_make_exports directly, like this:
370 : :
371 : : <pre>
372 : : // must reside outside _any_ namespace
373 : : COMPHELPER_SERVICEDECL_make_exports((yourServiceDecl1)(yourServiceDecl2)...(yourServiceDeclN));
374 : : </pre>
375 : :
376 : : Note the missing colons between the bracketed arguments.
377 : : */
378 : : #define COMPHELPER_SERVICEDECL_make_exports(compName, varargs_ ) \
379 : : extern "C" \
380 : : { \
381 : : SAL_DLLPUBLIC_EXPORT void* SAL_CALL compName##_component_getFactory( sal_Char const* pImplName, \
382 : : ::com::sun::star::lang::XMultiServiceFactory* pServiceManager, \
383 : : ::com::sun::star::registry::XRegistryKey* pRegistryKey ) \
384 : : { \
385 : : return component_getFactoryHelper( pImplName, pServiceManager, \
386 : : pRegistryKey, \
387 : : BOOST_PP_SEQ_ENUM(varargs_) ); \
388 : : } \
389 : : }
390 : :
391 : : #define COMPHELPER_SERVICEDECL_EXPORTS1(compName,comp0_) \
392 : : COMPHELPER_SERVICEDECL_make_exports(compName,(comp0_))
393 : : #define COMPHELPER_SERVICEDECL_EXPORTS2(compName,comp0_,comp1_) \
394 : : COMPHELPER_SERVICEDECL_make_exports(compName,(comp0_)(comp1_))
395 : : #define COMPHELPER_SERVICEDECL_EXPORTS3(compName,comp0_,comp1_,comp2_) \
396 : : COMPHELPER_SERVICEDECL_make_exports(compName,(comp0_)(comp1_)(comp2_))
397 : : #define COMPHELPER_SERVICEDECL_EXPORTS4(compName,comp0_,comp1_,comp2_,comp3_) \
398 : : COMPHELPER_SERVICEDECL_make_exports(compName,(comp0_)(comp1_)(comp2_)(comp3_))
399 : : #define COMPHELPER_SERVICEDECL_EXPORTS5(compName,comp0_,comp1_,comp2_,comp3_,comp4_) \
400 : : COMPHELPER_SERVICEDECL_make_exports(compName,(comp0_)(comp1_)(comp2_)(comp3_)(comp4_))
401 : : #define COMPHELPER_SERVICEDECL_EXPORTS6(compName,comp0_,comp1_,comp2_,comp3_,comp4_,comp5_) \
402 : : COMPHELPER_SERVICEDECL_make_exports(compName,(comp0_)(comp1_)(comp2_)(comp3_)(comp4_)(comp5_))
403 : : #define COMPHELPER_SERVICEDECL_EXPORTS7(compName,comp0_,comp1_,comp2_,comp3_,comp4_,comp5_,comp6_) \
404 : : COMPHELPER_SERVICEDECL_make_exports(compName,(comp0_)(comp1_)(comp2_)(comp3_)(comp4_)(comp5_)(comp6_))
405 : : #define COMPHELPER_SERVICEDECL_EXPORTS8(compName,comp0_,comp1_,comp2_,comp3_,comp4_,comp5_,comp6_,comp7_) \
406 : : COMPHELPER_SERVICEDECL_make_exports(compName,(comp0_)(comp1_)(comp2_)(comp3_)(comp4_)(comp5_)(comp6_)(comp7_))
407 : :
408 : : #endif // ! defined(COMPHELPER_SERVICEDECL_HXX_INCLUDED)
409 : :
410 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|