xref: /trunk/main/salhelper/inc/salhelper/dynload.hxx (revision 914d351e5f5b84e4342a86d6ab8d4aca7308b9bd)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SALHELPER_DYNLOAD_HXX_
25 #define _SALHELPER_DYNLOAD_HXX_
26 
27 #include <sal/types.h>
28 #include <rtl/ustring.hxx>
29 #include <osl/module.h>
30 #include <salhelper/salhelperdllapi.h>
31 
32 namespace salhelper
33 {
34 
35 /** The ORealDynamicLoader is an implementation helper class for the template loader ODynamicLoader.
36  */
37 class SALHELPER_DLLPUBLIC ORealDynamicLoader
38 {
39 public:
40     /** initializes the loader, loads the library and call the initialization function.
41 
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.
46      */
47     static ORealDynamicLoader* SAL_CALL newInstance(
48             ORealDynamicLoader ** ppSetToZeroInDestructor,
49             const ::rtl::OUString& strModuleName,
50             const ::rtl::OUString& strInitFunction );
51 
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();
56 
57     /// returns a poiner to the initialized API function structure.
58     void* SAL_CALL getApi() const;
59 
60 protected:
61     /** Constructor.
62 
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.
69      */
70     ORealDynamicLoader( ORealDynamicLoader ** ppSetToZeroInDestructor,
71                         const ::rtl::OUString& strModuleName,
72                         const ::rtl::OUString& strInitFunction,
73                         void* pApi,
74                         oslModule pModule );
75 
76     /// Destructor, try to unload the library.
77     virtual ~ORealDynamicLoader();
78 
79     /// points to  the structure with the initialzed API function pointers.
80     void*                   m_pApi;
81     /// stores the reference count.
82     sal_uInt32              m_refCount;
83     /// stores the library handle.
84     oslModule               m_pModule;
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.
91     */
92     ORealDynamicLoader **   ppSetToZeroInDestructor;
93 };
94 
95 
96 /** The ODynmaicLoader provides a special load on call mechanism for dynamic libraries
97     which support a C-API.
98 
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 laoder will
103     be destroyed the loader will unload the library.
104 
105     @deprecated
106     Do not use.
107  */
108 template<class API>
109 class ODynamicLoader
110 {
111 public:
112     /// Default constructor
113     ODynamicLoader() SAL_THROW(())
114     {
115         m_pLoader = 0;
116     }
117 
118     /** Constructor, loads the library if necessary otherwise the refernece count will
119         be increased.
120 
121         @param strModuleName specifies the library name.
122         @param strInitFunction specifies the name of the initialization function.
123      */
ODynamicLoader(const::rtl::OUString & strModuleName,const::rtl::OUString & strInitFunction)124     ODynamicLoader( const ::rtl::OUString& strModuleName,
125                     const ::rtl::OUString& strInitFunction ) SAL_THROW(())
126     {
127         if (!m_pStaticLoader)
128         {
129             m_pStaticLoader = ORealDynamicLoader::newInstance(
130                &m_pStaticLoader,
131                strModuleName,
132                strInitFunction);
133         }
134         else
135         {
136             m_pStaticLoader->acquire();
137         }
138 
139         m_pLoader = m_pStaticLoader;
140     }
141 
142     /// Copy constructor
143     ODynamicLoader(const ODynamicLoader<API>& toCopy) SAL_THROW(())
144     {
145         m_pLoader = toCopy.m_pLoader;
146         if( m_pLoader )
147             m_pLoader->acquire();
148     }
149 
150     /// Destructor, decrease the reference count and unload the library if it is tha last instance.
151     ~ODynamicLoader() SAL_THROW(())
152     {
153         if( m_pLoader )
154             m_pLoader->release();
155     }
156 
157     /// Assign operator
operator =(const ODynamicLoader<API> & toAssign)158     ODynamicLoader<API>& SAL_CALL operator = (const ODynamicLoader<API>& toAssign) SAL_THROW(())
159     {
160         if( m_pLoader != toAssign.m_pLoader )
161         {
162             if( toAssign.m_pLoader )
163             toAssign.m_pLoader->acquire();
164             if( m_pLoader )
165             m_pLoader->release();
166             m_pLoader = toAssign.m_pLoader;
167         }
168 
169         return (*this);
170     }
171 
172     /// returns a poiner to the initialized API function structure.
getApi() const173     API* SAL_CALL getApi() const SAL_THROW(())
174     {
175         return (API*)m_pLoader->getApi();
176     }
177 
178     /// cast operator, which cast to a poiner with the initialized API function structure.
operator ->() const179     API* SAL_CALL operator->() const SAL_THROW(())
180     {
181         return (API*)m_pLoader->getApi();
182     }
183 
184     /// checks if the loader works on a loaded and initialized library.
isLoaded() const185     sal_Bool SAL_CALL isLoaded() const SAL_THROW(())
186     {
187         return (m_pLoader != NULL);
188     }
189 
190 protected:
191     /// stores the real loader helper instance
192     static ORealDynamicLoader*  m_pStaticLoader;
193     ORealDynamicLoader*         m_pLoader;
194 };
195 
196 
197 template<class API>
198 ORealDynamicLoader* ODynamicLoader<API>::m_pStaticLoader = NULL;
199 
200 }
201 
202 #endif
203