Debian Codegraph @

3rdparty/xspublic/xstypes/xslibraryloader.h

raw
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef XSLIBRARYLOADER_H
34 #define XSLIBRARYLOADER_H
35
36 #include "xstypesconfig.h"
37 #include "xsstring.h"
38
39 struct XsLibraryLoader;
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 #ifndef __cplusplus
45 #define XSLIBRARYLOADER_INITIALIZER { NULL }
46 typedef struct XsLibraryLoader XsLibraryLoader;
47 #endif
48
49 XSTYPES_DLL_API int XsLibraryLoader_load(XsLibraryLoader* thisp, const XsString* libraryName);
50 XSTYPES_DLL_API void* XsLibraryLoader_resolve(const XsLibraryLoader* thisp, const char *functionName);
51 XSTYPES_DLL_API int XsLibraryLoader_unload(XsLibraryLoader* thisp);
52 XSTYPES_DLL_API int XsLibraryLoader_isLoaded(const XsLibraryLoader* thisp);
53 XSTYPES_DLL_API void XsLibraryLoader_getErrorString(XsString* error);
54
55 #ifdef __cplusplus
56 }
57 #endif
58
59 /*! \brief The Xsens dynamic library loader base class
60 */
61 struct XsLibraryLoader {
62 #ifdef __cplusplus
63 public:
64 /*! \brief Create a library loader */
65 inline XsLibraryLoader() :
66 m_handle(NULL)
67 {
68 // avoid compiler warnings about
69 // an unused handle. It is used in the c implementations
70 (void)m_handle;
71 }
72
73 /*! \brief Destroy a library loader */
74 inline ~XsLibraryLoader()
75 {
76 unload();
77 }
78
79 /*! \brief Load the library
80 \param[in] libraryName the name of the library to load
81 \return true if the library could be loaded, false otherwise
82 */
83 inline bool load(const XsString& libraryName)
84 {
85 return XsLibraryLoader_load(this, &libraryName) != 0;
86 }
87
88 /*! \brief Return true if a library has been loaded
89
90 \return true if a library has been loaded, false otherwise
91 */
92 inline bool isLoaded() const
93 {
94 return XsLibraryLoader_isLoaded(this) != 0;
95 }
96
97 /*! \brief Resolve a function from the library
98
99 \param[in] functionName the name of the function to resolve
100 \return a pointer to the resolved function, NULL if nothing could be resolved
101 */
102 inline void* resolve(const char *functionName) const
103 {
104 return XsLibraryLoader_resolve(this, functionName);
105 }
106
107 /*! \brief Unload the loaded library
108 */
109 inline void unload() noexcept
110 {
111 XsLibraryLoader_unload(this);
112 }
113
114 /*! \brief Return a string describing the error that occurred
115
116 Use this function after a function returned with an error to
117 receive some extra information about what went wrong.
118
119 \returns a string describing the error that occurred
120 */
121 inline static XsString errorString()
122 {
123 XsString rv;
124 XsLibraryLoader_getErrorString(&rv);
125 return rv;
126 }
127 private:
128 #endif
129 void* m_handle;
130 };
131
132 #endif