1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "componentmodule.hxx"
22 #include <tools/resmgr.hxx>
23 #include <svl/solar.hrc>
24 #include <comphelper/sequence.hxx>
25 #include <tools/debug.hxx>
26 #include <rtl/strbuf.hxx>
28 #define ENTER_MOD_METHOD() \
29 ::osl::MutexGuard aGuard(s_aMutex); \
33 namespace COMPMOD_NAMESPACE
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::lang
;
39 using namespace ::com::sun::star::registry
;
40 using namespace ::comphelper
;
41 using namespace ::cppu
;
43 // implementation for <type>OModule</type>. not threadsafe, has to be guarded by its owner
46 std::unique_ptr
<ResMgr
> m_pResources
;
48 OString m_sFilePrefix
;
55 /// get the manager for the resources of the module
56 ResMgr
* getResManager();
57 void setResourceFilePrefix(const OString
& _rPrefix
) { m_sFilePrefix
= _rPrefix
; }
61 OModuleImpl::OModuleImpl()
62 :m_pResources(nullptr)
63 ,m_bInitialized(false)
68 OModuleImpl::~OModuleImpl()
73 ResMgr
* OModuleImpl::getResManager()
75 // note that this method is not threadsafe, which counts for the whole class !
76 if (!m_pResources
&& !m_bInitialized
)
78 DBG_ASSERT(!m_sFilePrefix
.isEmpty(), "OModuleImpl::getResManager: no resource file prefix!");
79 // create a manager with a fixed prefix
80 m_pResources
.reset( ResMgr::CreateResMgr(m_sFilePrefix
.getStr()) );
81 DBG_ASSERT(m_pResources
,
82 OStringBuffer("OModuleImpl::getResManager: could not create the resource manager (file name: ")
83 .append(m_sFilePrefix
)
84 .append(")!").getStr());
86 m_bInitialized
= true;
88 return m_pResources
.get();
92 ::osl::Mutex
OModule::s_aMutex
;
93 sal_Int32
OModule::s_nClients
= 0;
94 OModuleImpl
* OModule::s_pImpl
= nullptr;
95 OString
OModule::s_sResPrefix
;
97 ResMgr
* OModule::getResManager()
100 return s_pImpl
->getResManager();
104 void OModule::setResourceFilePrefix(const OString
& _rPrefix
)
106 ::osl::MutexGuard
aGuard(s_aMutex
);
107 s_sResPrefix
= _rPrefix
;
109 s_pImpl
->setResourceFilePrefix(_rPrefix
);
113 void OModule::registerClient()
115 ::osl::MutexGuard
aGuard(s_aMutex
);
120 void OModule::revokeClient()
122 ::osl::MutexGuard
aGuard(s_aMutex
);
123 if (!--s_nClients
&& s_pImpl
)
131 void OModule::ensureImpl()
135 s_pImpl
= new OModuleImpl();
136 s_pImpl
->setResourceFilePrefix(s_sResPrefix
);
140 //- registration helper
143 std::vector
< OUString
>* OModule::s_pImplementationNames
= nullptr;
144 std::vector
< Sequence
< OUString
> >* OModule::s_pSupportedServices
= nullptr;
145 std::vector
< ComponentInstantiation
>* OModule::s_pCreationFunctionPointers
= nullptr;
146 std::vector
< FactoryInstantiation
>* OModule::s_pFactoryFunctionPointers
= nullptr;
149 void OModule::registerComponent(
150 const OUString
& _rImplementationName
,
151 const Sequence
< OUString
>& _rServiceNames
,
152 ComponentInstantiation _pCreateFunction
,
153 FactoryInstantiation _pFactoryFunction
)
155 if (!s_pImplementationNames
)
157 OSL_ENSURE(!s_pSupportedServices
&& !s_pCreationFunctionPointers
&& !s_pFactoryFunctionPointers
,
158 "OModule::registerComponent : inconsistent state (the pointers (1)) !");
159 s_pImplementationNames
= new std::vector
< OUString
>;
160 s_pSupportedServices
= new std::vector
< Sequence
< OUString
> >;
161 s_pCreationFunctionPointers
= new std::vector
< ComponentInstantiation
>;
162 s_pFactoryFunctionPointers
= new std::vector
< FactoryInstantiation
>;
164 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
165 "OModule::registerComponent : inconsistent state (the pointers (2)) !");
167 OSL_ENSURE( (s_pImplementationNames
->size() == s_pSupportedServices
->size())
168 && (s_pImplementationNames
->size() == s_pCreationFunctionPointers
->size())
169 && (s_pImplementationNames
->size() == s_pFactoryFunctionPointers
->size()),
170 "OModule::registerComponent : inconsistent state !");
172 s_pImplementationNames
->push_back(_rImplementationName
);
173 s_pSupportedServices
->push_back(_rServiceNames
);
174 s_pCreationFunctionPointers
->push_back(_pCreateFunction
);
175 s_pFactoryFunctionPointers
->push_back(_pFactoryFunction
);
179 void OModule::revokeComponent(const OUString
& _rImplementationName
)
181 if (!s_pImplementationNames
)
183 OSL_FAIL("OModule::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
186 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
187 "OModule::revokeComponent : inconsistent state (the pointers) !");
188 OSL_ENSURE( (s_pImplementationNames
->size() == s_pSupportedServices
->size())
189 && (s_pImplementationNames
->size() == s_pCreationFunctionPointers
->size())
190 && (s_pImplementationNames
->size() == s_pFactoryFunctionPointers
->size()),
191 "OModule::revokeComponent : inconsistent state !");
193 sal_Int32 nLen
= s_pImplementationNames
->size();
194 for (sal_Int32 i
=0; i
<nLen
; ++i
)
196 if ((*s_pImplementationNames
)[i
] == _rImplementationName
)
198 s_pImplementationNames
->erase(s_pImplementationNames
->begin() + i
);
199 s_pSupportedServices
->erase(s_pSupportedServices
->begin() + i
);
200 s_pCreationFunctionPointers
->erase(s_pCreationFunctionPointers
->begin() + i
);
201 s_pFactoryFunctionPointers
->erase(s_pFactoryFunctionPointers
->begin() + i
);
206 if (s_pImplementationNames
->empty())
208 delete s_pImplementationNames
; s_pImplementationNames
= nullptr;
209 delete s_pSupportedServices
; s_pSupportedServices
= nullptr;
210 delete s_pCreationFunctionPointers
; s_pCreationFunctionPointers
= nullptr;
211 delete s_pFactoryFunctionPointers
; s_pFactoryFunctionPointers
= nullptr;
216 Reference
< XInterface
> OModule::getComponentFactory(
217 const OUString
& _rImplementationName
,
218 const Reference
< XMultiServiceFactory
>& _rxServiceManager
)
220 OSL_ENSURE(_rxServiceManager
.is(), "OModule::getComponentFactory : invalid argument (service manager) !");
221 OSL_ENSURE(!_rImplementationName
.isEmpty(), "OModule::getComponentFactory : invalid argument (implementation name) !");
223 if (!s_pImplementationNames
)
225 OSL_FAIL("OModule::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
228 OSL_ENSURE(s_pImplementationNames
&& s_pSupportedServices
&& s_pCreationFunctionPointers
&& s_pFactoryFunctionPointers
,
229 "OModule::getComponentFactory : inconsistent state (the pointers) !");
230 OSL_ENSURE( (s_pImplementationNames
->size() == s_pSupportedServices
->size())
231 && (s_pImplementationNames
->size() == s_pCreationFunctionPointers
->size())
232 && (s_pImplementationNames
->size() == s_pFactoryFunctionPointers
->size()),
233 "OModule::getComponentFactory : inconsistent state !");
236 Reference
< XInterface
> xReturn
;
239 sal_Int32 nLen
= s_pImplementationNames
->size();
241 for (sal_Int32 i
=0; i
<nLen
; ++i
)
243 if ((*s_pImplementationNames
)[i
] == _rImplementationName
)
245 const FactoryInstantiation FactoryInstantiationFunction
= (*s_pFactoryFunctionPointers
)[i
];
247 xReturn
= FactoryInstantiationFunction( _rxServiceManager
, _rImplementationName
,
248 (*s_pCreationFunctionPointers
)[i
],
249 (*s_pSupportedServices
)[i
], nullptr);
253 return xReturn
.get();
262 } // namespace COMPMOD_NAMESPACE
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */