LibreOffice Module comphelper (master)  1
componentmodule.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 #include <sal/config.h>
21 
22 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
24 
25 #include <vector>
26 
27 namespace comphelper
28 {
29 
30  using namespace ::cppu;
31  using ::com::sun::star::uno::Sequence;
32  using ::com::sun::star::uno::RuntimeException;
33  using ::com::sun::star::uno::Reference;
34  using ::com::sun::star::uno::XInterface;
35 
36  typedef std::vector< ComponentDescription > ComponentDescriptions;
37 
41  {
42  public:
43  ComponentDescriptions m_aRegisteredComponents;
44 
45  OModuleImpl();
46  };
47 
49  {
50  }
51 
53  : m_pImpl(new OModuleImpl)
54  {
55  }
56 
58  {
59  }
60 
62  {
63  ::osl::MutexGuard aGuard( m_aMutex );
64  if ( !m_pImpl )
65  throw RuntimeException();
66 
67  m_pImpl->m_aRegisteredComponents.push_back( _rComp );
68  }
69 
70  void OModule::registerImplementation( const OUString& _rImplementationName, const css::uno::Sequence< OUString >& _rServiceNames,
71  ::cppu::ComponentFactoryFunc _pCreateFunction )
72  {
73  ComponentDescription aComponent( _rImplementationName, _rServiceNames, _pCreateFunction, ::cppu::createSingleComponentFactory );
74  registerImplementation( aComponent );
75  }
76 
77  void* OModule::getComponentFactory( const char* _pImplementationName )
78  {
80  OUString::createFromAscii( _pImplementationName ) ) );
81  return xFactory.get();
82  }
83 
84  Reference< XInterface > OModule::getComponentFactory( const OUString& _rImplementationName )
85  {
87 
88  for (const auto& rComponent : m_pImpl->m_aRegisteredComponents)
89  {
90  if ( rComponent.sImplementationName == _rImplementationName )
91  {
92  xReturn = rComponent.pFactoryCreationFunc(
93  rComponent.pComponentCreationFunc,
94  rComponent.sImplementationName,
95  rComponent.aSupportedServices,
96  nullptr
97  );
98  if ( xReturn.is() )
99  {
100  xReturn->acquire();
101  return xReturn.get();
102  }
103  }
104  }
105 
106  return nullptr;
107  }
108 
109 } // namespace comphelper
110 
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ComponentDescriptions m_aRegisteredComponents
css::uno::Reference< css::uno::XInterface >(SAL_CALL *ComponentFactoryFunc)(css CPPUHELPER_DLLPUBLIC css::uno::Reference< css::lang::XSingleComponentFactory > SAL_CALL createSingleComponentFactory(ComponentFactoryFunc fptr,::rtl::OUString const &rImplementationName, css::uno::Sequence< ::rtl::OUString > const &rServiceNames, rtl_ModuleCount *pModCount=NULL)
OModule()
access safety
css::uno::Reference< css::uno::XInterface > getComponentFactory(const OUString &_rImplementationName)
creates a Factory for the component with the given implementation name.
mutable::osl::Mutex m_aMutex
impl class. lives as long as at least one client for the module is registered
void registerImplementation(const OUString &_rImplementationName, const css::uno::Sequence< OUString > &_rServiceNames,::cppu::ComponentFactoryFunc _pCreateFunction)
register a component implementing a service with the given data.
implementation for OModule.
std::vector< ComponentDescription > ComponentDescriptions
Reference< XSingleServiceFactory > xFactory
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
std::unique_ptr< OModuleImpl > m_pImpl