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 {
32 namespace service_decl {
33 
34 const char cDelim = ';';
35 
37  public cppu::WeakImplHelper<lang::XSingleComponentFactory,
38  lang::XServiceInfo>
39 {
40 public:
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
55  createInstanceWithArgumentsAndContext(
56  uno::Sequence<uno::Any> const& args,
57  uno::Reference<uno::XComponentContext> const& xContext ) override;
58 
59 private:
60  virtual ~Factory() override;
61 
63 };
64 
65 ServiceDecl::Factory::~Factory()
66 {
67 }
68 
69 // XServiceInfo:
70 OUString ServiceDecl::Factory::getImplementationName()
71 {
72  return m_rServiceDecl.getImplementationName();
73 }
74 
75 sal_Bool ServiceDecl::Factory::supportsService( OUString const& name )
76 {
77  return m_rServiceDecl.supportsService(name);
78 }
79 
80 uno::Sequence<OUString> ServiceDecl::Factory::getSupportedServiceNames()
81 {
82  return m_rServiceDecl.getSupportedServiceNames();
83 }
84 
85 // XSingleComponentFactory:
86 uno::Reference<uno::XInterface> ServiceDecl::Factory::createInstanceWithContext(
87  uno::Reference<uno::XComponentContext> const& xContext )
88 {
89  return m_rServiceDecl.m_createFunc(
90  m_rServiceDecl, uno::Sequence<uno::Any>(), xContext );
91 }
92 
93 uno::Reference<uno::XInterface>
94 ServiceDecl::Factory::createInstanceWithArgumentsAndContext(
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 
102 void * ServiceDecl::getFactory( sal_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 
112 uno::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  OString const token( str.getToken( 0, cDelim, nIndex ) );
120  vec.emplace_back( token.getStr(), token.getLength(),
121  RTL_TEXTENCODING_ASCII_US );
122  }
123  while (nIndex >= 0);
124 
126 }
127 
128 bool ServiceDecl::supportsService( OUString const& name ) const
129 {
130  OString const str(m_pServiceNames);
131  sal_Int32 nIndex = 0;
132  do {
133  OString const token( str.getToken( 0, cDelim, nIndex ) );
134  if (name.equalsAsciiL( token.getStr(), token.getLength() ))
135  return true;
136  }
137  while (nIndex >= 0);
138  return false;
139 }
140 
141 OUString ServiceDecl::getImplementationName() const
142 {
143  return OUString::createFromAscii(m_pImplName);
144 }
145 
146 void* component_getFactoryHelper( const sal_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 service_decl
160 } // namespace comphelper
161 
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString getImplementationName()
Class to declare a service implementation.
Definition: servicedecl.hxx:96
void * component_getFactoryHelper(const sal_Char *pImplName, std::initializer_list< ServiceDecl const * > args)
char sal_Char
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
const sal_Unicode cDelim
css::uno::Sequence< OUString > getSupportedServiceNames()
int fac(int n)
int i
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:41