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