Library of Assembled Shared Sources
dynamic_library.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2023 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_DYNAMIC_LIBRARY_H
44#define LASS_GUARDIAN_OF_INCLUSION_UTIL_DYNAMIC_LIBRARY_H
45
46#include "util_common.h"
47
48#if (LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32) || LASS_HAVE_DLOPEN
49# define LASS_HAVE_DYNAMIC_LIBRARY 1
50#endif
51
52namespace lass
53{
54namespace util
55{
56
57#ifdef LASS_HAVE_DYNAMIC_LIBRARY
58
59/** Platform independant way to load a dynamic library or plugin
60 *
61 * @remark It is advised to provide absolute paths to the dynamic library as
62 * relative paths will not work reliably on Linux. It used to be that
63 * specifying -Wl,-rpath did set DT_RPATH which is a transitive dynamic
64 * array tag, meaning that DT_RPATH of the calling binary could in used
65 * by liblass-x-y.so to find the dynamic library. But since
66 * -Wl,--enable-new-dtags has become the default, -Wl,-rpath will set
67 * DT_RUNPATH instead, which is NOT transitive. Lass can thus not rely on
68 * it to find the dynamic library. Instead, the calling code should do one
69 * of these things:
70 * 1. rely on LD_LIBRARY_PATH environment variable
71 * 2. use absolute paths to the dynamic libraries to be loaded.
72 */
73class LASS_DLL DynamicLibrary: NonCopyable
74{
75public:
76
77 DynamicLibrary(const std::string& path);
78 ~DynamicLibrary();
79
80 template <typename FunPtr>
81 FunPtr resolveFunction(const std::string& functionName) const
82 {
83 // reinterpet_cast as some function signatures are not converted
84 // example: typedef BOOL (* WINAPI TQueryThreadCycleTimeFunPtr) (::HANDLE, PULONG64);
85 return reinterpret_cast<FunPtr>(resolveFunctionImpl(functionName));
86 }
87
88private:
89
90#if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
91 typedef int (*TFunctionPtr)(void);
92#elif LASS_HAVE_DLOPEN
93 // Not using a function pointer, because GCC 8 warns about cast between
94 // incompatible function types. In general, we shouldn't be using void*
95 // to cast function pointers, because they're not guaranteed to be the same
96 // size. However, dlsym already returns void*, so we're fine.
97 typedef void* TFunctionPtr;
98#else
99# error missing implementation
100#endif
101
102 TFunctionPtr resolveFunctionImpl (const std::string& functionName) const;
103
104 void* pimpl_;
105};
106
107#endif
108
109
110class DynamicLibraryError: public ExceptionMixin<DynamicLibraryError>
111{
112public:
113 DynamicLibraryError(std::string msg, std::string loc):
114 ExceptionMixin<DynamicLibraryError>(std::move(msg), std::move(loc))
115 {
116 }
117 ~DynamicLibraryError() noexcept {}
118};
119
120}
121}
122
123#endif
124
125// EOF
use as base class if derived should not be copyable
#define LASS_DLL
DLL interface: import or export symbols?
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53