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 <com/sun/star/uno/XComponentContext.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <initializer_list>
29 namespace comphelper::service_decl
{
34 typedef ::std::function
<
35 css::uno::Reference
<css::uno::XInterface
> /* return */
37 css::uno::Sequence
<css::uno::Any
> const&,
38 css::uno::Reference
<css::uno::XComponentContext
> const&)> CreateFuncF
;
41 /** Class to declare a service implementation. There is no need to implement
42 lang::XServiceInfo nor lang::XInitialization anymore.
43 The declaration can be done in various ways, the (simplest) form is
46 class MyClass : public cppu::WeakImplHelper<XInterface1, XInterface2> {
48 MyClass( uno::Reference<uno::XComponentContext> const& xContext )
52 namespace sdecl = comphelper::service_decl;
53 sdecl::ServiceDecl const myDecl(
54 sdecl::class_<MyClass>(),
55 "my.unique.implementation.name",
56 "MyServiceSpec1;MyServiceSpec2" );
59 If the service demands initialization by arguments, the implementation
60 class has to define a constructor taking both arguments and component
64 class MyClass : public cppu::WeakImplHelper<XInterface1, XInterface2> {
66 MyClass( uno::Sequence<uno::Any> const& args,
67 uno::Reference<uno:XComponentContext> const& xContext )
71 namespace sdecl = comphelper::service_decl;
72 sdecl::ServiceDecl const myDecl(
73 sdecl::class_<MyClass, sdecl::with_args<true> >(),
74 "my.unique.implementation.name",
75 "MyServiceSpec1;MyServiceSpec2" );
78 Additionally, there is the possibility to process some code after creation,
79 e.g. to add the newly created object as a listener or perform aggregation
83 uno::Reference<uno::XInterface> somePostProcCode( MyClass * p );
85 namespace sdecl = comphelper::service_decl;
86 sdecl::ServiceDecl const myDecl(
87 sdecl::class_<MyClass, ... >(&somePostProcCode),
88 "my.unique.implementation.name",
89 "MyServiceSpec1;MyServiceSpec2" );
92 In the latter case, somePostProcCode gets the yet unacquired "raw" pointer.
94 class COMPHELPER_DLLPUBLIC ServiceDecl
97 /** Ctor for multiple supported service names.
99 @param implClass implementation class description
100 @param pImplName implementation name
101 @param pSupportedServiceNames supported service names
102 @param cDelim delimiter for supported service names
104 ServiceDecl( const ServiceDecl
& ) = delete;
105 ServiceDecl
& operator=( const ServiceDecl
& ) = delete;
106 template <typename ImplClassT
>
107 ServiceDecl( ImplClassT
const& implClass
,
108 char const* pImplName
,
109 char const* pSupportedServiceNames
)
110 : m_createFunc(implClass
.m_createFunc
),
111 m_pImplName(pImplName
),
112 m_pServiceNames(pSupportedServiceNames
) {}
114 /// @internal gets called by component_getFactoryHelper()
115 void * getFactory( char const* pImplName
) const;
117 /// @return supported service names
118 css::uno::Sequence
< OUString
> getSupportedServiceNames() const;
120 /// @return whether name is in set of supported service names
121 bool supportsService( OUString
const& name
) const;
123 /// @return implementation name
124 OUString
getImplementationName() const;
128 friend class Factory
;
130 detail::CreateFuncF
const m_createFunc
;
131 char const* const m_pImplName
;
132 char const* const m_pServiceNames
;
135 /** To specify whether the implementation class expects arguments
136 (uno::Sequence<uno::Any>).
138 template <bool> struct with_args
;
143 } // namespace detail
147 void* component_getFactoryHelper( const char* pImplName
,
148 std::initializer_list
<ServiceDecl
const *> args
);
150 } // namespace comphelper::service_decl
153 #endif // ! defined( INCLUDED_COMPHELPER_SERVICEDECL_HXX)
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */