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 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_SALHELPER_DYNLOAD_HXX
25 #define INCLUDED_SALHELPER_DYNLOAD_HXX
27 #include "sal/types.h"
28 #include "rtl/ustring.hxx"
29 #include "osl/module.h"
30 #include "salhelper/salhelperdllapi.h"
35 /** The ORealDynamicLoader is an implementation helper class for the template loader ODynamicLoader.
37 class SALHELPER_DLLPUBLIC ORealDynamicLoader
40 /** initializes the loader, loads the library and call the initialization function.
42 @param ppSetToZeroInDestructor points to the loader instance which must be set to NULL
43 if the loader will be destroyed.
44 @param strModuleName specifies the library name.
45 @param strInitFunction specifies the name of the initialization function.
47 static ORealDynamicLoader
* SAL_CALL
newInstance(
48 ORealDynamicLoader
** ppSetToZeroInDestructor
,
49 const ::rtl::OUString
& strModuleName
,
50 const ::rtl::OUString
& strInitFunction
);
52 /// increase the reference count.
53 sal_uInt32 SAL_CALL
acquire();
54 /// decrease the reference count and delete the last instance.
55 sal_uInt32 SAL_CALL
release();
57 /// returns a pointer to the initialized API function structure.
58 void* SAL_CALL
getApi() const;
63 @param ppSetToZeroInDestructor points to the loader instance which must be set to NULL
64 if the loader will be destroyed.
65 @param strModuleName specifies the library name.
66 @param strInitFunction specifies the name of the initialization function.
67 @param pApi points to a structure with the initialized API function pointers.
68 @param pModule points to the loaded library handle.
70 ORealDynamicLoader( ORealDynamicLoader
** ppSetToZeroInDestructor
,
71 const ::rtl::OUString
& strModuleName
,
72 const ::rtl::OUString
& strInitFunction
,
76 /// Destructor, try to unload the library.
77 virtual ~ORealDynamicLoader();
79 /// points to the structure with the initialized API function pointers.
81 /// stores the reference count.
82 sal_uInt32 m_refCount
;
83 /// stores the library handle.
85 /// stores the library name.
86 ::rtl::OUString m_strModuleName
;
87 /// stores the name of the initialization function.
88 ::rtl::OUString m_strInitFunction
;
89 /** stores a pointer to itself, which must be reset in the destructor to signal
90 that the loader is invalid.
92 ORealDynamicLoader
** ppSetToZeroInDestructor
;
96 /** The ODynamicLoader provides a special load on call mechanism for dynamic libraries
97 which support a C-API.
99 The libraries must provide a struct with function pointers for all supported C functions.
100 The loader loads the specified library and call the specified initialization function
101 to initialize the function pointers with the real functions. Furthermore provides the
102 loader a reference counter for the library. When the last instance of the loader will
103 be destroyed the loader will unload the library.
112 /// Default constructor
118 /** Constructor, loads the library if necessary otherwise the reference count will
121 @param strModuleName specifies the library name.
122 @param strInitFunction specifies the name of the initialization function.
124 ODynamicLoader( const ::rtl::OUString
& strModuleName
,
125 const ::rtl::OUString
& strInitFunction
)
127 if (!m_pStaticLoader
)
129 m_pStaticLoader
= ORealDynamicLoader::newInstance(
136 m_pStaticLoader
->acquire();
139 m_pLoader
= m_pStaticLoader
;
143 ODynamicLoader(const ODynamicLoader
<API
>& toCopy
)
145 m_pLoader
= toCopy
.m_pLoader
;
147 m_pLoader
->acquire();
150 /// Destructor, decrease the reference count and unload the library if it is the last instance.
154 if (m_pLoader
->release()==0)
155 m_pStaticLoader
= NULL
;
159 ODynamicLoader
<API
>& SAL_CALL
operator = (const ODynamicLoader
<API
>& toAssign
)
161 if( m_pLoader
!= toAssign
.m_pLoader
)
163 if( toAssign
.m_pLoader
)
165 toAssign
.m_pLoader
->acquire();
169 m_pLoader
->release();
171 m_pLoader
= toAssign
.m_pLoader
;
177 /// returns a pointer to the initialized API function structure.
178 API
* SAL_CALL
getApi() const
180 return static_cast<API
*>(m_pLoader
->getApi());
183 /// cast operator, which cast to a pointer with the initialized API function structure.
184 API
* SAL_CALL
operator->() const
186 return static_cast<API
*>(m_pLoader
->getApi());
189 /// checks if the loader works on a loaded and initialized library.
190 bool SAL_CALL
isLoaded() const
192 return (m_pLoader
!= NULL
);
196 /// stores the real loader helper instance
197 static ORealDynamicLoader
* m_pStaticLoader
;
198 ORealDynamicLoader
* m_pLoader
;
203 ORealDynamicLoader
* ODynamicLoader
<API
>::m_pStaticLoader
= NULL
;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */