summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/kodi-addon-dev-kit/include/kodi/tools/DllHelper.h
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/addons/kodi-addon-dev-kit/include/kodi/tools/DllHelper.h')
-rw-r--r--xbmc/addons/kodi-addon-dev-kit/include/kodi/tools/DllHelper.h129
1 files changed, 129 insertions, 0 deletions
diff --git a/xbmc/addons/kodi-addon-dev-kit/include/kodi/tools/DllHelper.h b/xbmc/addons/kodi-addon-dev-kit/include/kodi/tools/DllHelper.h
new file mode 100644
index 0000000..232f67d
--- /dev/null
+++ b/xbmc/addons/kodi-addon-dev-kit/include/kodi/tools/DllHelper.h
@@ -0,0 +1,129 @@
1#pragma once
2/*
3 * Copyright (C) 2005-2017 Team Kodi
4 * http://kodi.tv
5 *
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Kodi; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <string>
23#include <kodi/AddonBase.h>
24
25#ifdef _WIN32 // windows
26#include <p8-platform/windows/dlfcn-win32.h>
27#else
28#include <dlfcn.h> // linux+osx
29#endif
30
31#define REGISTER_DLL_SYMBOL(functionPtr) \
32 CDllHelper::RegisterSymbol(functionPtr, #functionPtr)
33
34/// @brief Class to help with load of shared library functions
35///
36/// You can add them as parent to your class and to help with load of shared
37/// library functions.
38///
39/// @note To use on Windows must you also include p8-platform on your addon!
40///
41///
42/// ----------------------------------------------------------------------------
43///
44/// **Example:**
45/// ~~~~~~~~~~~~~{.cpp}
46///
47/// #include <kodi/tools/DllHelper.h>
48///
49/// ...
50/// class CMyInstance : public kodi::addon::CInstanceAudioDecoder,
51/// private CDllHelper
52/// {
53/// public:
54/// CMyInstance(KODI_HANDLE instance);
55/// bool Start();
56///
57/// ...
58///
59/// /* The pointers for on shared library exported functions */
60/// int (*Init)();
61/// void (*Cleanup)();
62/// int (*GetLength)();
63/// };
64///
65/// CMyInstance::CMyInstance(KODI_HANDLE instance)
66/// : CInstanceAudioDecoder(instance)
67/// {
68/// }
69///
70/// bool CMyInstance::Start()
71/// {
72/// std::string lib = kodi::GetAddonPath("myLib.so");
73/// if (!LoadDll(lib)) return false;
74/// if (!REGISTER_DLL_SYMBOL(Init)) return false;
75/// if (!REGISTER_DLL_SYMBOL(Cleanup)) return false;
76/// if (!REGISTER_DLL_SYMBOL(GetLength)) return false;
77///
78/// Init();
79/// return true;
80/// }
81/// ...
82/// ~~~~~~~~~~~~~
83///
84class CDllHelper
85{
86public:
87 CDllHelper() : m_dll(nullptr) { }
88 virtual ~CDllHelper()
89 {
90 if (m_dll)
91 dlclose(m_dll);
92 }
93
94 /// @brief Function to load requested library
95 ///
96 /// @param[in] path The path with filename of shared library to load
97 /// @return true if load was successful done
98 ///
99 bool LoadDll(const std::string& path)
100 {
101 m_dll = dlopen(path.c_str(), RTLD_LAZY);
102 if (m_dll == nullptr)
103 {
104 kodi::Log(ADDON_LOG_ERROR, "Unable to load %s", dlerror());
105 return false;
106 }
107 return true;
108 }
109
110 /// @brief Function to register requested library symbol
111 ///
112 /// @note This function should not be used, use instead the macro
113 /// REGISTER_DLL_SYMBOL to register the symbol pointer.
114 ///
115 template <typename T>
116 bool RegisterSymbol(T& functionPtr, const char* strFunctionPtr)
117 {
118 functionPtr = reinterpret_cast<T>(dlsym(m_dll, strFunctionPtr));
119 if (functionPtr == nullptr)
120 {
121 kodi::Log(ADDON_LOG_ERROR, "Unable to assign function %s", dlerror());
122 return false;
123 }
124 return true;
125 }
126
127private:
128 void* m_dll;
129};