CWS-TOOLING: integrate CWS os146
[LibreOffice.git] / comphelper / source / misc / legacysingletonfactory.cxx
blobcfc1793e7db2f6840b19a17b0deab514ed84ecdf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
31 #include "comphelper/legacysingletonfactory.hxx"
33 /** === begin UNO includes === **/
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/lang/XInitialization.hpp>
36 /** === end UNO includes === **/
38 #include <cppuhelper/implbase2.hxx>
40 #include <algorithm>
42 //........................................................................
43 namespace comphelper
45 //........................................................................
47 /** === begin UNO using === **/
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::uno::XInterface;
50 using ::com::sun::star::uno::UNO_QUERY;
51 using ::com::sun::star::uno::UNO_QUERY_THROW;
52 using ::com::sun::star::uno::UNO_SET_THROW;
53 using ::com::sun::star::uno::Exception;
54 using ::com::sun::star::uno::RuntimeException;
55 using ::com::sun::star::uno::Any;
56 using ::com::sun::star::uno::makeAny;
57 using ::com::sun::star::lang::XSingleComponentFactory;
58 using ::com::sun::star::uno::Sequence;
59 using ::com::sun::star::uno::XComponentContext;
60 using ::com::sun::star::lang::XServiceInfo;
61 using ::com::sun::star::lang::XInitialization;
62 /** === end UNO using === **/
64 //====================================================================
65 //= LegacySingletonFactory
66 //====================================================================
67 typedef ::cppu::WeakImplHelper2 < XServiceInfo
68 , XSingleComponentFactory
69 > LegacySingletonFactory_Base;
71 class COMPHELPER_DLLPRIVATE LegacySingletonFactory : public LegacySingletonFactory_Base
73 public:
74 LegacySingletonFactory(
75 ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName,
76 const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount
79 // XServiceInfo
80 virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (RuntimeException);
81 virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException);
82 virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (RuntimeException);
84 // XSingleComponentFactory
85 virtual Reference< XInterface > SAL_CALL createInstanceWithContext( const Reference< XComponentContext >& Context ) throw (Exception, RuntimeException);
86 virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext( const Sequence< Any >& Arguments, const Reference< XComponentContext >& Context ) throw (Exception, RuntimeException);
88 protected:
89 ~LegacySingletonFactory();
91 private:
92 /** creates m_xInstance, returns whether it actually was created (<TRUE/>) or existed before (<FALSE/>
94 bool impl_nts_ensureInstance( const Reference< XComponentContext >& _rxContext );
96 private:
97 ::osl::Mutex m_aMutex;
98 ::cppu::ComponentFactoryFunc m_componentFactoryFunc;
99 ::rtl::OUString m_sImplementationName;
100 Sequence< ::rtl::OUString > m_aServiceNames;
101 rtl_ModuleCount* m_pModuleCount;
102 Reference< XInterface > m_xTheInstance;
105 //--------------------------------------------------------------------
106 LegacySingletonFactory::LegacySingletonFactory( ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName,
107 const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount )
108 :m_componentFactoryFunc ( _componentFactoryFunc )
109 ,m_sImplementationName ( _rImplementationName )
110 ,m_aServiceNames ( _rServiceNames )
111 ,m_pModuleCount ( _pModCount )
112 ,m_xTheInstance ( )
114 if ( m_pModuleCount )
115 m_pModuleCount->acquire( m_pModuleCount );
118 //--------------------------------------------------------------------
119 LegacySingletonFactory::~LegacySingletonFactory()
121 if ( m_pModuleCount )
122 m_pModuleCount->release( m_pModuleCount );
125 //--------------------------------------------------------------------
126 ::rtl::OUString SAL_CALL LegacySingletonFactory::getImplementationName( ) throw (RuntimeException)
128 return m_sImplementationName;
131 //--------------------------------------------------------------------
132 ::sal_Bool SAL_CALL LegacySingletonFactory::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
134 Sequence< ::rtl::OUString > aServices( getSupportedServiceNames() );
135 const ::rtl::OUString* pStart = aServices.getConstArray();
136 const ::rtl::OUString* pEnd = aServices.getConstArray() + aServices.getLength();
137 return ::std::find( pStart, pEnd, _rServiceName ) != pEnd;
140 //--------------------------------------------------------------------
141 Sequence< ::rtl::OUString > SAL_CALL LegacySingletonFactory::getSupportedServiceNames( ) throw (RuntimeException)
143 return m_aServiceNames;
146 //--------------------------------------------------------------------
147 bool LegacySingletonFactory::impl_nts_ensureInstance( const Reference< XComponentContext >& _rxContext )
149 if ( m_xTheInstance.is() )
150 return false;
152 m_xTheInstance = (*m_componentFactoryFunc)( _rxContext );
153 if ( !m_xTheInstance.is() )
154 throw RuntimeException();
156 return true; // true -> "was newly created"
159 //--------------------------------------------------------------------
160 Reference< XInterface > SAL_CALL LegacySingletonFactory::createInstanceWithContext( const Reference< XComponentContext >& _rxContext ) throw (Exception, RuntimeException)
162 ::osl::MutexGuard aGuard( m_aMutex );
163 impl_nts_ensureInstance( _rxContext );
165 return m_xTheInstance;
168 //--------------------------------------------------------------------
169 Reference< XInterface > SAL_CALL LegacySingletonFactory::createInstanceWithArgumentsAndContext( const Sequence< Any >& _rArguments, const Reference< XComponentContext >& _rxContext ) throw (Exception, RuntimeException)
171 ::osl::MutexGuard aGuard( m_aMutex );
172 if ( !impl_nts_ensureInstance( _rxContext ) )
173 throw RuntimeException(
174 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Instance already created before, unable to initialize it." ) ),
175 *this
178 Reference< XInitialization > xInit( m_xTheInstance, UNO_QUERY_THROW );
179 xInit->initialize( _rArguments );
181 return m_xTheInstance;
184 //====================================================================
185 //= createLegacySingletonFactory
186 //====================================================================
187 Reference< XSingleComponentFactory > createLegacySingletonFactory(
188 ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName,
189 const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount )
191 return new LegacySingletonFactory( _componentFactoryFunc, _rImplementationName, _rServiceNames, _pModCount );
195 //........................................................................
196 } // namespace comphelper
197 //........................................................................