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