Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / salhelper / source / dynload.cxx
blobf69ace831168b9c68c09f6c57e343de04863afbb
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 <salhelper/dynload.hxx>
21 #include <rtl/ustrbuf.hxx>
23 namespace salhelper
26 typedef void* (SAL_CALL *ApiInitFunction) (void);
28 ORealDynamicLoader::ORealDynamicLoader(ORealDynamicLoader ** ppSetToZeroInDestructor_,
29 const rtl::OUString& moduleName,
30 const rtl::OUString& initFunction,
31 void* pApi,
32 oslModule pModule)
33 : m_pApi(pApi)
34 , m_refCount(1)
35 , m_pModule(pModule)
36 , m_strModuleName(moduleName)
37 , m_strInitFunction(initFunction)
38 , ppSetToZeroInDestructor( ppSetToZeroInDestructor_ )
42 ORealDynamicLoader* ORealDynamicLoader::newInstance(ORealDynamicLoader ** ppSetToZeroInDestructor,
43 const rtl::OUString& moduleName,
44 const rtl::OUString& initFunction)
46 #ifdef DISABLE_DYNLOADING
47 (void) ppSetToZeroInDestructor;
48 (void) moduleName;
49 (void) initFunction;
51 return NULL;
52 #else
53 ApiInitFunction initFunc;
54 oslModule pModule = osl_loadModule(moduleName.pData, SAL_LOADMODULE_DEFAULT);
56 if ( !pModule )
58 return NULL;
61 initFunc = (ApiInitFunction)osl_getFunctionSymbol(
62 pModule, initFunction.pData);
64 if ( !initFunc )
66 osl_unloadModule(pModule);
67 return NULL;
70 return(new ORealDynamicLoader(ppSetToZeroInDestructor, moduleName,
71 initFunction,
72 initFunc(),
73 pModule));
74 #endif
77 ORealDynamicLoader::~ORealDynamicLoader()
79 // set the address to zero
80 if( ppSetToZeroInDestructor )
81 *ppSetToZeroInDestructor = 0;
83 if (m_pModule)
85 #ifndef DISABLE_DYNLOADING
86 osl_unloadModule(m_pModule);
87 #endif
88 m_pModule = NULL;
92 sal_uInt32 ORealDynamicLoader::acquire()
94 return ++m_refCount;
97 sal_uInt32 ORealDynamicLoader::release()
99 sal_uInt32 nRet = --m_refCount;
100 if( nRet == 0 )
101 delete this;
102 return nRet;
106 void* ORealDynamicLoader::getApi() const
108 return m_pApi;
111 } // namespace salhelper
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */