LibreOffice: salhelper/dynload.hxx Source File

LibreOffice LibreOffice 6.3 SDK C/C++ API Reference

dynload.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #ifndef INCLUDED_SALHELPER_DYNLOAD_HXX
21 #define INCLUDED_SALHELPER_DYNLOAD_HXX
22
23 #include "sal/types.h"
24 #include "rtl/ustring.hxx"
25 #include "osl/module.h"
26 #include "salhelper/salhelperdllapi.h"
27
28 namespace salhelper
29 {
30
33 class SALHELPER_DLLPUBLIC ORealDynamicLoader
34 {
35 public:
43 static ORealDynamicLoader* SAL_CALL newInstance(
44 ORealDynamicLoader ** ppSetToZeroInDestructor,
45 const ::rtl::OUString& strModuleName,
46 const ::rtl::OUString& strInitFunction );
47
49 sal_uInt32 SAL_CALL acquire();
51 sal_uInt32 SAL_CALL release();
52
54 void* SAL_CALL getApi() const;
55
56 protected:
66 ORealDynamicLoader( ORealDynamicLoader ** ppSetToZeroInDestructor,
67 const ::rtl::OUString& strModuleName,
68 const ::rtl::OUString& strInitFunction,
69 void* pApi,
70 oslModule pModule );
71
73 virtual ~ORealDynamicLoader();
74
76 void* m_pApi;
78 sal_uInt32 m_refCount;
80 oslModule m_pModule;
82 ::rtl::OUString m_strModuleName;
84 ::rtl::OUString m_strInitFunction;
88 ORealDynamicLoader ** ppSetToZeroInDestructor;
89 };
90
91
104 template<class API>
105 class ODynamicLoader
106 {
107 public:
109 ODynamicLoader()
110 {
111 m_pLoader = NULL;
112 }
113
120 ODynamicLoader( const ::rtl::OUString& strModuleName,
121 const ::rtl::OUString& strInitFunction )
122 {
123 if (!m_pStaticLoader)
124 {
125 m_pStaticLoader = ORealDynamicLoader::newInstance(
126 &m_pStaticLoader,
127 strModuleName,
128 strInitFunction);
129 }
130 else
131 {
132 m_pStaticLoader->acquire();
133 }
134
135 m_pLoader = m_pStaticLoader;
136 }
137
139 ODynamicLoader(const ODynamicLoader<API>& toCopy)
140 {
141 m_pLoader = toCopy.m_pLoader;
142 if( m_pLoader )
143 m_pLoader->acquire();
144 }
145
147 ~ODynamicLoader()
148 {
149 if( m_pLoader )
150 if (m_pLoader->release()==0)
151 m_pStaticLoader = NULL;
152 }
153
155 ODynamicLoader<API>& SAL_CALL operator = (const ODynamicLoader<API>& toAssign)
156 {
157 if( m_pLoader != toAssign.m_pLoader )
158 {
159 if( toAssign.m_pLoader )
160 {
161 toAssign.m_pLoader->acquire();
162 }
163 if( m_pLoader )
164 {
165 m_pLoader->release();
166 }
167 m_pLoader = toAssign.m_pLoader;
168 }
169
170 return (*this);
171 }
172
174 API* SAL_CALL getApi() const
175 {
176 return static_cast<API*>(m_pLoader->getApi());
177 }
178
180 API* SAL_CALL operator->() const
181 {
182 return static_cast<API*>(m_pLoader->getApi());
183 }
184
186 bool SAL_CALL isLoaded() const
187 {
188 return (m_pLoader != NULL);
189 }
190
191 protected:
193 static ORealDynamicLoader* m_pStaticLoader;
194 ORealDynamicLoader* m_pLoader;
195 };
196
197
198 template<class API>
199 ORealDynamicLoader* ODynamicLoader<API>::m_pStaticLoader = NULL;
200
201 }
202
203 #endif
204
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
salhelper::ODynamicLoader::ODynamicLoader
ODynamicLoader(const ODynamicLoader< API > &toCopy)
Copy constructor.
Definition: dynload.hxx:139
SALHELPER_DLLPUBLIC
#define SALHELPER_DLLPUBLIC
Definition: salhelperdllapi.h:28
salhelper::ORealDynamicLoader::newInstance
static ORealDynamicLoader * newInstance(ORealDynamicLoader **ppSetToZeroInDestructor, const ::rtl::OUString &strModuleName, const ::rtl::OUString &strInitFunction)
initializes the loader, loads the library and call the initialization function.
salhelper::ORealDynamicLoader::m_strModuleName
::rtl::OUString m_strModuleName
stores the library name.
Definition: dynload.hxx:82
salhelper::ODynamicLoader::ODynamicLoader
ODynamicLoader()
Default constructor.
Definition: dynload.hxx:109
salhelper::ODynamicLoader::isLoaded
bool isLoaded() const
checks if the loader works on a loaded and initialized library.
Definition: dynload.hxx:186
salhelper::ORealDynamicLoader::acquire
sal_uInt32 acquire()
increase the reference count.
salhelper::ORealDynamicLoader::ppSetToZeroInDestructor
ORealDynamicLoader ** ppSetToZeroInDestructor
stores a pointer to itself, which must be reset in the destructor to signal that the loader is invali...
Definition: dynload.hxx:88
salhelper::ORealDynamicLoader::release
sal_uInt32 release()
decrease the reference count and delete the last instance.
salhelper::ORealDynamicLoader::m_pModule
oslModule m_pModule
stores the library handle.
Definition: dynload.hxx:80
salhelper
Definition: condition.hxx:30
types.h
ustring.hxx
salhelper::ODynamicLoader::m_pLoader
ORealDynamicLoader * m_pLoader
Definition: dynload.hxx:194
oslModule
void * oslModule
Definition: module.h:55
salhelper::ODynamicLoader::ODynamicLoader
ODynamicLoader(const ::rtl::OUString &strModuleName, const ::rtl::OUString &strInitFunction)
Constructor, loads the library if necessary otherwise the reference count will be increased.
Definition: dynload.hxx:120
salhelper::ODynamicLoader::~ODynamicLoader
~ODynamicLoader()
Destructor, decrease the reference count and unload the library if it is the last instance.
Definition: dynload.hxx:147
salhelper::ODynamicLoader::operator=
ODynamicLoader< API > & operator=(const ODynamicLoader< API > &toAssign)
Assign operator.
Definition: dynload.hxx:155
rtl::OUString
This String class provides base functionality for C++ like Unicode character array handling.
Definition: ustring.hxx:126
salhelper::ODynamicLoader
The ODynamicLoader provides a special load on call mechanism for dynamic libraries which support a C-...
Definition: dynload.hxx:105
salhelperdllapi.h
salhelper::ODynamicLoader::operator->
API * operator->() const
cast operator, which cast to a pointer with the initialized API function structure.
Definition: dynload.hxx:180
salhelper::ODynamicLoader::m_pStaticLoader
static ORealDynamicLoader * m_pStaticLoader
stores the real loader helper instance
Definition: dynload.hxx:193
salhelper::ORealDynamicLoader::m_strInitFunction
::rtl::OUString m_strInitFunction
stores the name of the initialization function.
Definition: dynload.hxx:84
salhelper::ORealDynamicLoader
The ORealDynamicLoader is an implementation helper class for the template loader ODynamicLoader.
Definition: dynload.hxx:33
salhelper::ORealDynamicLoader::m_pApi
void * m_pApi
points to the structure with the initialized API function pointers.
Definition: dynload.hxx:76
salhelper::ORealDynamicLoader::m_refCount
sal_uInt32 m_refCount
stores the reference count.
Definition: dynload.hxx:78
salhelper::ORealDynamicLoader::getApi
void * getApi() const
returns a pointer to the initialized API function structure.
module.h
salhelper::ODynamicLoader::getApi
API * getApi() const
returns a pointer to the initialized API function structure.
Definition: dynload.hxx:174