LibreOffice Module comphelper (master) 1
servicedecl.cxx
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
20
22#include <o3tl/string_view.hxx>
23#include <rtl/string.hxx>
26#include <com/sun/star/lang/XSingleComponentFactory.hpp>
27#include <cassert>
28#include <vector>
29
30using namespace com::sun::star;
31
33
34const char cDelim = ';';
35
37 public cppu::WeakImplHelper<lang::XSingleComponentFactory,
38 lang::XServiceInfo>
39{
40public:
41 explicit Factory( ServiceDecl const& rServiceDecl )
42 : m_rServiceDecl(rServiceDecl) {}
43 // noncopyable
44 Factory(const Factory&) = delete;
45 const Factory& operator=(const Factory&) = delete;
46
47 // XServiceInfo:
48 virtual OUString SAL_CALL getImplementationName() override;
49 virtual sal_Bool SAL_CALL supportsService( OUString const& name ) override;
50 virtual uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
51 // XSingleComponentFactory:
52 virtual uno::Reference<uno::XInterface> SAL_CALL createInstanceWithContext(
53 uno::Reference<uno::XComponentContext> const& xContext ) override;
54 virtual uno::Reference<uno::XInterface> SAL_CALL
56 uno::Sequence<uno::Any> const& args,
57 uno::Reference<uno::XComponentContext> const& xContext ) override;
58
59private:
60 virtual ~Factory() override;
61
63};
64
66{
67}
68
69// XServiceInfo:
71{
72 return m_rServiceDecl.getImplementationName();
73}
74
76{
77 return m_rServiceDecl.supportsService(name);
78}
79
81{
82 return m_rServiceDecl.getSupportedServiceNames();
83}
84
85// XSingleComponentFactory:
87 uno::Reference<uno::XComponentContext> const& xContext )
88{
89 return m_rServiceDecl.m_createFunc(
90 m_rServiceDecl, uno::Sequence<uno::Any>(), xContext );
91}
92
93uno::Reference<uno::XInterface>
95 uno::Sequence<uno::Any > const& args,
96 uno::Reference<uno::XComponentContext> const& xContext )
97{
98 return m_rServiceDecl.m_createFunc(
99 m_rServiceDecl, args, xContext );
100}
101
102void * ServiceDecl::getFactory( char const* pImplName ) const
103{
104 if (rtl_str_compare(m_pImplName, pImplName) == 0) {
105 lang::XSingleComponentFactory * const pFac( new Factory(*this) );
106 pFac->acquire();
107 return pFac;
108 }
109 return nullptr;
110}
111
112uno::Sequence<OUString> ServiceDecl::getSupportedServiceNames() const
113{
114 std::vector<OUString> vec;
115
116 OString const str(m_pServiceNames);
117 sal_Int32 nIndex = 0;
118 do {
119 std::string_view const token( o3tl::getToken(str, 0, cDelim, nIndex ) );
120 vec.emplace_back( token.data(), token.size(),
121 RTL_TEXTENCODING_ASCII_US );
122 }
123 while (nIndex >= 0);
124
126}
127
128bool ServiceDecl::supportsService( std::u16string_view name ) const
129{
130 OString const str(m_pServiceNames);
131 sal_Int32 nIndex = 0;
132 do {
133 std::string_view const token( o3tl::getToken(str, 0, cDelim, nIndex ) );
134 if (o3tl::equalsAscii(name, token))
135 return true;
136 }
137 while (nIndex >= 0);
138 return false;
139}
140
142{
143 return OUString::createFromAscii(m_pImplName);
144}
145
146void* component_getFactoryHelper( const char* pImplName,
147 std::initializer_list<ServiceDecl const *> args )
148{
149 for (auto const i: args) {
150 assert(i != nullptr);
151 void * fac = i->getFactory(pImplName);
152 if (fac != nullptr) {
153 return fac;
154 }
155 }
156 return nullptr;
157}
158
159} // namespace
160
161/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
int fac(int n)
Factory(ServiceDecl const &rServiceDecl)
Definition: servicedecl.cxx:41
virtual sal_Bool SAL_CALL supportsService(OUString const &name) override
Definition: servicedecl.cxx:75
virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: servicedecl.cxx:80
virtual OUString SAL_CALL getImplementationName() override
Definition: servicedecl.cxx:70
const Factory & operator=(const Factory &)=delete
virtual uno::Reference< uno::XInterface > SAL_CALL createInstanceWithContext(uno::Reference< uno::XComponentContext > const &xContext) override
Definition: servicedecl.cxx:86
virtual uno::Reference< uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext(uno::Sequence< uno::Any > const &args, uno::Reference< uno::XComponentContext > const &xContext) override
Definition: servicedecl.cxx:94
Class to declare a service implementation.
Definition: servicedecl.hxx:95
css::uno::Sequence< OUString > getSupportedServiceNames() const
void * getFactory(char const *pImplName) const
bool supportsService(std::u16string_view name) const
const char * name
sal_Int32 nIndex
void * component_getFactoryHelper(const char *pImplName, std::initializer_list< ServiceDecl const * > args)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Copy from a container into a Sequence.
Definition: sequence.hxx:190
int i
bool equalsAscii(std::u16string_view s1, const char *s2)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
args
unsigned char sal_Bool