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 .
20 #ifndef INCLUDED_SALHELPER_DYNLOAD_HXX
21 #define INCLUDED_SALHELPER_DYNLOAD_HXX
23 #include <sal/types.h>
24 #include <rtl/ustring.hxx>
25 #include <osl/module.h>
26 #include <salhelper/salhelperdllapi.h>
31 /** The ORealDynamicLoader is an implementation helper class for the template loader ODynamicLoader.
33 class SALHELPER_DLLPUBLIC ORealDynamicLoader
36 /** initializes the loader, loads the library and call the initialization function.
38 @param ppSetToZeroInDestructor points to the loader instance which must be set to NULL
39 if the loader will be destroyed.
40 @param strModuleName specifies the library name.
41 @param strInitFunction specifies the name of the initialization function.
43 static ORealDynamicLoader
* SAL_CALL
newInstance(
44 ORealDynamicLoader
** ppSetToZeroInDestructor
,
45 const ::rtl::OUString
& strModuleName
,
46 const ::rtl::OUString
& strInitFunction
);
48 /// increase the reference count.
49 sal_uInt32 SAL_CALL
acquire();
50 /// decrease the reference count and delete the last instance.
51 sal_uInt32 SAL_CALL
release();
53 /// returns a poiner to the initialized API function structure.
54 void* SAL_CALL
getApi() const;
59 @param ppSetToZeroInDestructor points to the loader instance which must be set to NULL
60 if the loader will be destroyed.
61 @param strModuleName specifies the library name.
62 @param strInitFunction specifies the name of the initialization function.
63 @param pApi points to a structure with the initialized API function pointers.
64 @param pModule points to the loaded library handle.
66 ORealDynamicLoader( ORealDynamicLoader
** ppSetToZeroInDestructor
,
67 const ::rtl::OUString
& strModuleName
,
68 const ::rtl::OUString
& strInitFunction
,
72 /// Destructor, try to unload the library.
73 virtual ~ORealDynamicLoader();
75 /// points to the structure with the initialzed API function pointers.
77 /// stores the reference count.
78 sal_uInt32 m_refCount
;
79 /// stores the library handle.
81 /// stores the library name.
82 ::rtl::OUString m_strModuleName
;
83 /// stores the name of the initialization function.
84 ::rtl::OUString m_strInitFunction
;
85 /** stores a pointer to itself, which must be reset in the destructor to signal
86 that the loader is invalid.
88 ORealDynamicLoader
** ppSetToZeroInDestructor
;
92 /** The ODynmaicLoader provides a special load on call mechanism for dynamic libraries
93 which support a C-API.
95 The libraries must provide a struct with function pointers for all supported C functions.
96 The loader loads the specified library and call the specified initialization function
97 to initialize the function pointers with the real functions. Furthermore provides the
98 loader a reference counter for the library. When the last instance of the laoder will
99 be destroyed the loader will unload the library.
108 /// Default constructor
114 /** Constructor, loads the library if necessary otherwise the refernece count will
117 @param strModuleName specifies the library name.
118 @param strInitFunction specifies the name of the initialization function.
120 ODynamicLoader( const ::rtl::OUString
& strModuleName
,
121 const ::rtl::OUString
& strInitFunction
)
123 if (!m_pStaticLoader
)
125 m_pStaticLoader
= ORealDynamicLoader::newInstance(
132 m_pStaticLoader
->acquire();
135 m_pLoader
= m_pStaticLoader
;
139 ODynamicLoader(const ODynamicLoader
<API
>& toCopy
)
141 m_pLoader
= toCopy
.m_pLoader
;
143 m_pLoader
->acquire();
146 /// Destructor, decrease the reference count and unload the library if it is tha last instance.
150 m_pLoader
->release();
154 ODynamicLoader
<API
>& SAL_CALL
operator = (const ODynamicLoader
<API
>& toAssign
)
156 if( m_pLoader
!= toAssign
.m_pLoader
)
158 if( toAssign
.m_pLoader
)
160 toAssign
.m_pLoader
->acquire();
164 m_pLoader
->release();
166 m_pLoader
= toAssign
.m_pLoader
;
172 /// returns a poiner to the initialized API function structure.
173 API
* SAL_CALL
getApi() const
175 return static_cast<API
*>(m_pLoader
->getApi());
178 /// cast operator, which cast to a poiner with the initialized API function structure.
179 API
* SAL_CALL
operator->() const
181 return static_cast<API
*>(m_pLoader
->getApi());
184 /// checks if the loader works on a loaded and initialized library.
185 bool SAL_CALL
isLoaded() const
187 return (m_pLoader
!= NULL
);
191 /// stores the real loader helper instance
192 static ORealDynamicLoader
* m_pStaticLoader
;
193 ORealDynamicLoader
* m_pLoader
;
198 ORealDynamicLoader
* ODynamicLoader
<API
>::m_pStaticLoader
= NULL
;
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */