xref: /aoo41x/main/sal/inc/osl/module.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 /** @HTML */
29 
30 #ifndef _OSL_MODULE_HXX_
31 #define _OSL_MODULE_HXX_
32 
33 #include <rtl/ustring.hxx>
34 #include <osl/module.h>
35 
36 namespace osl
37 {
38 
39 class Module
40 {
41     Module( const Module&);
42     Module& operator = ( const Module&);
43 
44 public:
45     static sal_Bool getUrlFromAddress(void * addr, ::rtl::OUString & libraryUrl) {
46         return osl_getModuleURLFromAddress(addr, &libraryUrl.pData);
47     }
48 
49     /** Get module URL from the specified function address in the module.
50 
51         Similar to getUrlFromAddress, but use a function address to get URL of the Module.
52         Use Function pointer as symbol address to conceal type conversion.
53 
54         @param addr
55         [in] function address in oslGenericFunction format.
56 
57         @param libraryUrl
58         [in|out] receives the URL of the module.
59 
60         @return
61         <dl>
62         <dt>sal_True</dt>
63         <dd>on success</dd>
64         <dt>sal_False</dt>
65         <dd>can not get the URL from the specified function address or the parameter is invalid.</dd>
66         </dl>
67 
68         @see getUrlFromAddress
69     */
70     static sal_Bool getUrlFromAddress( oslGenericFunction addr, ::rtl::OUString & libraryUrl){
71         return osl_getModuleURLFromFunctionAddress( addr, &libraryUrl.pData );
72     }
73 
74     Module(): m_Module(0){}
75 
76     Module( const ::rtl::OUString& strModuleName, sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT) : m_Module(0)
77     {
78         load( strModuleName, nRtldMode);
79     }
80 
81     ~Module()
82     {
83         osl_unloadModule(m_Module);
84     }
85 
86     sal_Bool SAL_CALL load( const ::rtl::OUString& strModuleName,
87         sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT)
88     {
89         unload();
90         m_Module= osl_loadModule( strModuleName.pData, nRtldMode );
91         return is();
92     }
93 
94     /// @since UDK 3.2.8
95     sal_Bool SAL_CALL loadRelative(
96         ::oslGenericFunction baseModule, ::rtl::OUString const & relativePath,
97         ::sal_Int32 mode = SAL_LOADMODULE_DEFAULT)
98     {
99         unload();
100         m_Module = osl_loadModuleRelative(baseModule, relativePath.pData, mode);
101         return is();
102     }
103 
104     void SAL_CALL unload()
105     {
106         if (m_Module)
107         {
108             osl_unloadModule(m_Module);
109             m_Module = 0;
110         }
111     }
112 
113 	sal_Bool SAL_CALL is() const
114     {
115        	return m_Module != NULL;
116     }
117 
118     void* SAL_CALL getSymbol( const ::rtl::OUString& strSymbolName)
119     {
120 	return ( osl_getSymbol( m_Module, strSymbolName.pData ) );
121     }
122 
123     /** Get function address by the function name in the module.
124 
125         getFunctionSymbol is an alternative function for getSymbol.
126         Use Function pointer as symbol address to conceal type conversion.
127 
128         @param ustrFunctionSymbolName
129         [in] Function name to be looked up.
130 
131         @return
132         <dl>
133         <dt>oslGenericFunction format function address</dt>
134         <dd>on success</dd>
135         <dt>NULL</dt>
136         <dd>lookup failed or parameter is somewhat invalid</dd>
137         </dl>
138 
139         @see getSymbol
140     */
141     oslGenericFunction SAL_CALL getFunctionSymbol( const ::rtl::OUString& ustrFunctionSymbolName )
142     {
143         return ( osl_getFunctionSymbol( m_Module, ustrFunctionSymbolName.pData ) );
144     }
145 
146     operator oslModule() const
147     {
148         return m_Module;
149     }
150 
151 private:
152     oslModule m_Module;
153 
154 };
155 
156 }
157 
158 #endif
159