Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / comphelper / source / misc / componentmodule.cxx
blob8a4cc74f8ced402e6f2ca06f717545d73486829a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <comphelper/componentmodule.hxx>
22 #include <comphelper/sequence.hxx>
23 #include <osl/diagnose.h>
25 #include <vector>
27 //........................................................................
28 namespace comphelper
30 //........................................................................
32 using namespace ::cppu;
33 /** === being UNO using === **/
34 using ::com::sun::star::uno::Sequence;
35 using ::com::sun::star::uno::RuntimeException;
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::lang::XMultiServiceFactory;
38 using ::com::sun::star::registry::XRegistryKey;
39 using ::com::sun::star::uno::Exception;
40 using ::com::sun::star::uno::XInterface;
41 /** === end UNO using === **/
43 typedef ::std::vector< ComponentDescription > ComponentDescriptions;
45 //=========================================================================
46 //= OModuleImpl
47 //=========================================================================
48 /** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
50 class OModuleImpl
52 public:
53 ComponentDescriptions m_aRegisteredComponents;
55 OModuleImpl();
56 ~OModuleImpl();
59 //-------------------------------------------------------------------------
60 OModuleImpl::OModuleImpl()
64 //-------------------------------------------------------------------------
65 OModuleImpl::~OModuleImpl()
69 //=========================================================================
70 //= OModule
71 //=========================================================================
72 //-------------------------------------------------------------------------
73 OModule::OModule()
74 : m_nClients(0)
75 , m_pImpl(new OModuleImpl)
79 OModule::~OModule()
81 delete m_pImpl;
84 //-------------------------------------------------------------------------
85 void OModule::registerClient( OModule::ClientAccess )
87 ::osl::MutexGuard aGuard(m_aMutex);
88 if ( 1 == osl_atomic_increment( &m_nClients ) )
89 onFirstClient();
92 //-------------------------------------------------------------------------
93 void OModule::revokeClient( OModule::ClientAccess )
95 ::osl::MutexGuard aGuard(m_aMutex);
96 if ( 0 == osl_atomic_decrement( &m_nClients ) )
97 onLastClient();
100 //--------------------------------------------------------------------------
101 void OModule::onFirstClient()
105 //--------------------------------------------------------------------------
106 void OModule::onLastClient()
110 //--------------------------------------------------------------------------
111 void OModule::registerImplementation( const ComponentDescription& _rComp )
113 ::osl::MutexGuard aGuard( m_aMutex );
114 if ( !m_pImpl )
115 throw RuntimeException();
117 m_pImpl->m_aRegisteredComponents.push_back( _rComp );
120 //--------------------------------------------------------------------------
121 void OModule::registerImplementation( const ::rtl::OUString& _rImplementationName, const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rServiceNames,
122 ::cppu::ComponentFactoryFunc _pCreateFunction, FactoryInstantiation _pFactoryFunction )
124 ComponentDescription aComponent( _rImplementationName, _rServiceNames, ::rtl::OUString(), _pCreateFunction, _pFactoryFunction );
125 registerImplementation( aComponent );
128 //--------------------------------------------------------------------------
129 void* OModule::getComponentFactory( const sal_Char* _pImplementationName )
131 Reference< XInterface > xFactory( getComponentFactory(
132 ::rtl::OUString::createFromAscii( _pImplementationName ) ) );
133 return xFactory.get();
136 //--------------------------------------------------------------------------
137 Reference< XInterface > OModule::getComponentFactory( const ::rtl::OUString& _rImplementationName )
139 Reference< XInterface > xReturn;
141 for ( ComponentDescriptions::const_iterator component = m_pImpl->m_aRegisteredComponents.begin();
142 component != m_pImpl->m_aRegisteredComponents.end();
143 ++component
146 if ( component->sImplementationName == _rImplementationName )
148 xReturn = component->pFactoryCreationFunc(
149 component->pComponentCreationFunc,
150 component->sImplementationName,
151 component->aSupportedServices,
152 NULL
154 if ( xReturn.is() )
156 xReturn->acquire();
157 return xReturn.get();
162 return NULL;
165 //........................................................................
166 } // namespace comphelper
167 //........................................................................
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */