1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_COMPHELPER_SERVICEDECL_HXX
20 #define INCLUDED_COMPHELPER_SERVICEDECL_HXX
22 #include <comphelper/comphelperdllapi.h>
23 #include <cppuhelper/implbase.hxx>
24 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <initializer_list>
30 namespace comphelper
{
31 namespace service_decl
{
36 typedef ::std::function
<
37 css::uno::Reference
<css::uno::XInterface
> /* return */
39 css::uno::Sequence
<css::uno::Any
> const&,
40 css::uno::Reference
<css::uno::XComponentContext
> const&)> CreateFuncF
;
43 /** Class to declare a service implementation. There is no need to implement
44 lang::XServiceInfo nor lang::XInitialization anymore.
45 The declaration can be done in various ways, the (simplest) form is
48 class MyClass : public cppu::WeakImplHelper<XInterface1, XInterface2> {
50 MyClass( uno::Reference<uno::XComponentContext> const& xContext )
54 namespace sdecl = comphelper::service_decl;
55 sdecl::ServiceDecl const myDecl(
56 sdecl::class_<MyClass>(),
57 "my.unique.implementation.name",
58 "MyServiceSpec1;MyServiceSpec2" );
61 If the service demands initialization by arguments, the implementation
62 class has to define a constructor taking both arguments and component
66 class MyClass : public cppu::WeakImplHelper<XInterface1, XInterface2> {
68 MyClass( uno::Sequence<uno::Any> const& args,
69 uno::Reference<uno:XComponentContext> const& xContext )
73 namespace sdecl = comphelper::service_decl;
74 sdecl::ServiceDecl const myDecl(
75 sdecl::class_<MyClass, sdecl::with_args<true> >(),
76 "my.unique.implementation.name",
77 "MyServiceSpec1;MyServiceSpec2" );
80 Additionally, there is the possibility to process some code after creation,
81 e.g. to add the newly created object as a listener or perform aggregation
85 uno::Reference<uno::XInterface> somePostProcCode( MyClass * p );
87 namespace sdecl = comphelper::service_decl;
88 sdecl::ServiceDecl const myDecl(
89 sdecl::class_<MyClass, ... >(&somePostProcCode),
90 "my.unique.implementation.name",
91 "MyServiceSpec1;MyServiceSpec2" );
94 In the latter case, somePostProcCode gets the yet unacquired "raw" pointer.
96 class COMPHELPER_DLLPUBLIC ServiceDecl
99 /** Ctor for multiple supported service names.
101 @param implClass implementation class description
102 @param pImplName implementation name
103 @param pSupportedServiceNames supported service names
104 @param cDelim delimiter for supported service names
106 ServiceDecl( const ServiceDecl
& ) = delete;
107 ServiceDecl
& operator=( const ServiceDecl
& ) = delete;
108 template <typename ImplClassT
>
109 ServiceDecl( ImplClassT
const& implClass
,
110 char const* pImplName
,
111 char const* pSupportedServiceNames
)
112 : m_createFunc(implClass
.m_createFunc
),
113 m_pImplName(pImplName
),
114 m_pServiceNames(pSupportedServiceNames
) {}
116 /// @internal gets called by component_getFactoryHelper()
117 void * getFactory( sal_Char
const* pImplName
) const;
119 /// @return supported service names
120 css::uno::Sequence
< OUString
> getSupportedServiceNames() const;
122 /// @return whether name is in set of supported service names
123 bool supportsService( OUString
const& name
) const;
125 /// @return implementation name
126 OUString
getImplementationName() const;
130 friend class Factory
;
132 detail::CreateFuncF
const m_createFunc
;
133 char const* const m_pImplName
;
134 char const* const m_pServiceNames
;
137 /** To specify whether the implementation class expects arguments
138 (uno::Sequence<uno::Any>).
140 template <bool> struct with_args
;
144 template <typename ImplT
>
151 OwnServiceImpl( const OwnServiceImpl
& ) = delete;
152 OwnServiceImpl
& operator=( const OwnServiceImpl
& ) = delete;
154 ServiceDecl
const& rServiceDecl
,
155 css::uno::Sequence
<css::uno::Any
> const& args
,
156 css::uno::Reference
<css::uno::XComponentContext
> const& xContext
)
157 :BaseT(args
, xContext
), m_rServiceDecl(rServiceDecl
) {}
159 ServiceDecl
const& rServiceDecl
,
160 css::uno::Reference
<css::uno::XComponentContext
> const& xContext
)
161 : BaseT(xContext
), m_rServiceDecl(rServiceDecl
) {}
164 virtual OUString SAL_CALL
getImplementationName() override
{
165 return m_rServiceDecl
.getImplementationName();
167 virtual sal_Bool SAL_CALL
supportsService( OUString
const& name
) override
{
168 return m_rServiceDecl
.supportsService(name
);
170 virtual css::uno::Sequence
< OUString
>
171 SAL_CALL
getSupportedServiceNames() override
{
172 return m_rServiceDecl
.getSupportedServiceNames();
176 ServiceDecl
const& m_rServiceDecl
;
179 template <typename ImplT
>
180 class ServiceImpl final
: public OwnServiceImpl
< ::cppu::ImplInheritanceHelper
<ImplT
,css::lang::XServiceInfo
> >
182 typedef OwnServiceImpl
< ::cppu::ImplInheritanceHelper
<ImplT
,css::lang::XServiceInfo
> > ServiceImpl_BASE
;
185 ServiceDecl
const& rServiceDecl
,
186 css::uno::Sequence
<css::uno::Any
> const& args
,
187 css::uno::Reference
<css::uno::XComponentContext
> const& xContext
)
188 : ServiceImpl_BASE(rServiceDecl
, args
, xContext
) {}
190 ServiceDecl
const& rServiceDecl
,
191 css::uno::Reference
<css::uno::XComponentContext
> const& xContext
)
192 : ServiceImpl_BASE(rServiceDecl
, xContext
) {}
195 template <typename ImplT
>
196 class InheritingServiceImpl final
: public OwnServiceImpl
< ImplT
>
198 typedef OwnServiceImpl
< ImplT
> ServiceImpl_BASE
;
200 InheritingServiceImpl(
201 ServiceDecl
const& rServiceDecl
,
202 css::uno::Sequence
<css::uno::Any
> const& args
,
203 css::uno::Reference
<css::uno::XComponentContext
> const& xContext
)
204 : ServiceImpl_BASE(rServiceDecl
, args
, xContext
) {}
207 template <typename ServiceImplT
>
208 struct PostProcessDefault
{
209 css::uno::Reference
<css::uno::XInterface
>
210 operator()( ServiceImplT
* p
) const {
211 return static_cast<css::lang::XServiceInfo
*>(p
);
215 template <typename ImplT
, typename PostProcessFuncT
, typename WithArgsT
>
218 template <typename ImplT
, typename PostProcessFuncT
>
219 struct CreateFunc
<ImplT
, PostProcessFuncT
, with_args
<false> > {
220 PostProcessFuncT
const m_postProcessFunc
;
221 explicit CreateFunc( PostProcessFuncT
const& postProcessFunc
)
222 : m_postProcessFunc(postProcessFunc
) {}
224 css::uno::Reference
<css::uno::XInterface
>
225 operator()( ServiceDecl
const& rServiceDecl
,
226 css::uno::Sequence
<css::uno::Any
> const&,
227 css::uno::Reference
<css::uno::XComponentContext
>
228 const& xContext
) const
230 return m_postProcessFunc(
231 new ImplT( rServiceDecl
, xContext
) );
235 template <typename ImplT
, typename PostProcessFuncT
>
236 struct CreateFunc
<ImplT
, PostProcessFuncT
, with_args
<true> > {
237 PostProcessFuncT
const m_postProcessFunc
;
238 explicit CreateFunc( PostProcessFuncT
const& postProcessFunc
)
239 : m_postProcessFunc(postProcessFunc
) {}
241 css::uno::Reference
<css::uno::XInterface
>
242 operator()( ServiceDecl
const& rServiceDecl
,
243 css::uno::Sequence
<css::uno::Any
> const& args
,
244 css::uno::Reference
<css::uno::XComponentContext
>
245 const& xContext
) const
247 return m_postProcessFunc(
248 new ImplT( rServiceDecl
, args
, xContext
) );
252 } // namespace detail
254 /** Defines a service implementation class.
256 @tpl ImplT_ service implementation class
257 @WithArgsT whether the implementation class ctor expects arguments
258 (uno::Sequence<uno::Any>, uno::Reference<uno::XComponentContext>)
259 or just (uno::Reference<uno::XComponentContext>)
261 template <typename ImplT_
, typename WithArgsT
= with_args
<false> >
262 struct serviceimpl_base
{
263 typedef ImplT_ ImplT
;
265 detail::CreateFuncF
const m_createFunc
;
267 typedef detail::PostProcessDefault
<ImplT
> PostProcessDefaultT
;
269 /** Default ctor. Implementation class without args, expecting
270 component context as single argument.
272 serviceimpl_base() : m_createFunc(
273 detail::CreateFunc
<ImplT
, PostProcessDefaultT
, WithArgsT
>(
274 PostProcessDefaultT() ) ) {}
276 /** Ctor to pass a post processing function/functor.
278 @tpl PostProcessDefaultT let your compiler deduce this
279 @param postProcessFunc function/functor that gets the yet unacquired
280 ImplT_ pointer returning a
281 uno::Reference<uno::XInterface>
283 template <typename PostProcessFuncT
>
284 explicit serviceimpl_base( PostProcessFuncT
const& postProcessFunc
)
285 : m_createFunc( detail::CreateFunc
<ImplT
, PostProcessFuncT
, WithArgsT
>(
286 postProcessFunc
) ) {}
289 template <typename ImplT_
, typename WithArgsT
= with_args
<false> >
290 struct class_
: public serviceimpl_base
< detail::ServiceImpl
<ImplT_
>, WithArgsT
>
292 typedef serviceimpl_base
< detail::ServiceImpl
<ImplT_
>, WithArgsT
> baseT
;
293 /** Default ctor. Implementation class without args, expecting
294 component context as single argument.
296 class_() : baseT() {}
297 template <typename PostProcessFuncT
>
298 /** Ctor to pass a post processing function/functor.
300 @tpl PostProcessDefaultT let your compiler deduce this
301 @param postProcessFunc function/functor that gets the yet unacquired
302 ImplT_ pointer returning a
303 uno::Reference<uno::XInterface>
305 explicit class_( PostProcessFuncT
const& postProcessFunc
) : baseT( postProcessFunc
) {}
308 template <typename ImplT_
, typename WithArgsT
= with_args
<false> >
309 struct inheritingClass_
: public serviceimpl_base
< detail::InheritingServiceImpl
<ImplT_
>, WithArgsT
>
311 typedef serviceimpl_base
< detail::InheritingServiceImpl
<ImplT_
>, WithArgsT
> baseT
;
312 /** Default ctor. Implementation class without args, expecting
313 component context as single argument.
315 inheritingClass_() : baseT() {}
316 template <typename PostProcessFuncT
>
317 /** Ctor to pass a post processing function/functor.
319 @tpl PostProcessDefaultT let your compiler deduce this
320 @param postProcessFunc function/functor that gets the yet unacquired
321 ImplT_ pointer returning a
322 uno::Reference<uno::XInterface>
324 explicit inheritingClass_( PostProcessFuncT
const& postProcessFunc
) : baseT( postProcessFunc
) {}
328 void* component_getFactoryHelper( const sal_Char
* pImplName
,
329 std::initializer_list
<ServiceDecl
const *> args
);
331 } // namespace service_decl
332 } // namespace comphelper
335 #endif // ! defined( INCLUDED_COMPHELPER_SERVICEDECL_HXX)
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */