LibreOffice Module comphelper (master)  1
servicedecl.hxx
Go to the documentation of this file.
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 INCLUDED_COMPHELPER_SERVICEDECL_HXX
20 #define INCLUDED_COMPHELPER_SERVICEDECL_HXX
21 
23 #include <cppuhelper/implbase.hxx>
24 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
26 
27 #include <functional>
28 #include <initializer_list>
29 
30 namespace comphelper {
31 namespace service_decl {
32 
33 class ServiceDecl;
34 
35 namespace detail {
36 typedef ::std::function<
37  css::uno::Reference<css::uno::XInterface> /* return */
38  (ServiceDecl const&,
39  css::uno::Sequence<css::uno::Any> const&,
40  css::uno::Reference<css::uno::XComponentContext> const&)> CreateFuncF;
41 }
42 
97 {
98 public:
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) {}
115 
117  void * getFactory( sal_Char const* pImplName ) const;
118 
120  css::uno::Sequence< OUString> getSupportedServiceNames() const;
121 
123  bool supportsService( OUString const& name ) const;
124 
126  OUString getImplementationName() const;
127 
128 private:
129  class Factory;
130  friend class Factory;
131 
133  char const* const m_pImplName;
134  char const* const m_pServiceNames;
135 };
136 
140 template <bool> struct with_args;
141 
143 namespace detail {
144 template <typename ImplT>
146  : public ImplT
147 {
148  typedef ImplT BaseT;
149 
150 public:
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) {}
162 
163  // XServiceInfo
164  virtual OUString SAL_CALL getImplementationName() override {
166  }
167  virtual sal_Bool SAL_CALL supportsService( OUString const& name ) override {
168  return m_rServiceDecl.supportsService(name);
169  }
170  virtual css::uno::Sequence< OUString>
171  SAL_CALL getSupportedServiceNames() override {
173  }
174 
175 private:
177 };
178 
179 template <typename ImplT>
180 class ServiceImpl final : public OwnServiceImpl< ::cppu::ImplInheritanceHelper<ImplT,css::lang::XServiceInfo> >
181 {
183 public:
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) {}
193 };
194 
195 template <typename ImplT>
196 class InheritingServiceImpl final : public OwnServiceImpl< ImplT >
197 {
199 public:
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) {}
205 };
206 
207 template <typename ServiceImplT>
209  css::uno::Reference<css::uno::XInterface>
210  operator()( ServiceImplT * p ) const {
211  return static_cast<css::lang::XServiceInfo *>(p);
212  }
213 };
214 
215 template <typename ImplT, typename PostProcessFuncT, typename WithArgsT>
216 struct CreateFunc;
217 
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) {}
223 
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
229  {
230  return m_postProcessFunc(
231  new ImplT( rServiceDecl, xContext ) );
232  }
233 };
234 
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) {}
240 
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
246  {
247  return m_postProcessFunc(
248  new ImplT( rServiceDecl, args, xContext ) );
249  }
250 };
251 
252 } // namespace detail
253 
261 template <typename ImplT_, typename WithArgsT = with_args<false> >
263  typedef ImplT_ ImplT;
264 
266 
268 
272  serviceimpl_base() : m_createFunc(
273  detail::CreateFunc<ImplT, PostProcessDefaultT, WithArgsT>(
274  PostProcessDefaultT() ) ) {}
275 
283  template <typename PostProcessFuncT>
284  explicit serviceimpl_base( PostProcessFuncT const& postProcessFunc )
285  : m_createFunc( detail::CreateFunc<ImplT, PostProcessFuncT, WithArgsT>(
286  postProcessFunc ) ) {}
287 };
288 
289 template <typename ImplT_, typename WithArgsT = with_args<false> >
290 struct class_ : public serviceimpl_base< detail::ServiceImpl<ImplT_>, WithArgsT >
291 {
296  class_() : baseT() {}
297  template <typename PostProcessFuncT>
305  explicit class_( PostProcessFuncT const& postProcessFunc ) : baseT( postProcessFunc ) {}
306 };
307 
308 template <typename ImplT_, typename WithArgsT = with_args<false> >
309 struct inheritingClass_ : public serviceimpl_base< detail::InheritingServiceImpl<ImplT_>, WithArgsT >
310 {
315  inheritingClass_() : baseT() {}
316  template <typename PostProcessFuncT>
324  explicit inheritingClass_( PostProcessFuncT const& postProcessFunc ) : baseT( postProcessFunc ) {}
325 };
326 
328 void* component_getFactoryHelper( const sal_Char* pImplName,
329  std::initializer_list<ServiceDecl const *> args );
330 
331 } // namespace service_decl
332 } // namespace comphelper
333 
334 
335 #endif // ! defined( INCLUDED_COMPHELPER_SERVICEDECL_HXX)
336 
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OwnServiceImpl(const OwnServiceImpl &)=delete
detail::CreateFuncF const m_createFunc
css::uno::Reference< css::uno::XInterface > operator()(ServiceDecl const &rServiceDecl, css::uno::Sequence< css::uno::Any > const &, css::uno::Reference< css::uno::XComponentContext > const &xContext) const
detail::CreateFuncF const m_createFunc
inheritingClass_(PostProcessFuncT const &postProcessFunc)
Ctor to pass a post processing function/functor.
OUString getImplementationName()
Class to declare a service implementation.
Definition: servicedecl.hxx:96
::std::function< css::uno::Reference< css::uno::XInterface >(ServiceDecl const &, css::uno::Sequence< css::uno::Any > const &, css::uno::Reference< css::uno::XComponentContext > const &)> CreateFuncF
Definition: servicedecl.hxx:40
virtual OUString SAL_CALL getImplementationName() override
void * component_getFactoryHelper(const sal_Char *pImplName, std::initializer_list< ServiceDecl const * > args)
class_(PostProcessFuncT const &postProcessFunc)
Ctor to pass a post processing function/functor.
To specify whether the implementation class expects arguments (uno::Sequence).
char sal_Char
virtual sal_Bool SAL_CALL supportsService(OUString const &name) override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
bool supportsService(OUString const &name) const
css::uno::Reference< css::uno::XInterface > operator()(ServiceImplT *p) const
OwnServiceImpl(ServiceDecl const &rServiceDecl, css::uno::Sequence< css::uno::Any > const &args, css::uno::Reference< css::uno::XComponentContext > const &xContext)
OwnServiceImpl & operator=(const OwnServiceImpl &)=delete
OwnServiceImpl< ::cppu::ImplInheritanceHelper< ImplT, css::lang::XServiceInfo > > ServiceImpl_BASE
ServiceDecl(ImplClassT const &implClass, char const *pImplName, char const *pSupportedServiceNames)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
css::uno::Sequence< OUString > getSupportedServiceNames()
serviceimpl_base(PostProcessFuncT const &postProcessFunc)
Ctor to pass a post processing function/functor.
unsigned char sal_Bool
detail::PostProcessDefault< ImplT > PostProcessDefaultT
OwnServiceImpl(ServiceDecl const &rServiceDecl, css::uno::Reference< css::uno::XComponentContext > const &xContext)
serviceimpl_base< detail::ServiceImpl< ImplT_ >, WithArgsT > baseT
InheritingServiceImpl(ServiceDecl const &rServiceDecl, css::uno::Sequence< css::uno::Any > const &args, css::uno::Reference< css::uno::XComponentContext > const &xContext)
serviceimpl_base< detail::InheritingServiceImpl< ImplT_ >, WithArgsT > baseT
#define COMPHELPER_DLLPUBLIC
css::uno::Sequence< OUString > getSupportedServiceNames() const
Defines a service implementation class.
css::uno::Reference< css::uno::XInterface > operator()(ServiceDecl const &rServiceDecl, css::uno::Sequence< css::uno::Any > const &args, css::uno::Reference< css::uno::XComponentContext > const &xContext) const
ServiceImpl(ServiceDecl const &rServiceDecl, css::uno::Sequence< css::uno::Any > const &args, css::uno::Reference< css::uno::XComponentContext > const &xContext)
ServiceImpl(ServiceDecl const &rServiceDecl, css::uno::Reference< css::uno::XComponentContext > const &xContext)