]> Creatis software - cpPlugins.git/blob - lib/third_party/Pluma/DLibrary.cpp
14516ba74e42ef82bfabbdfec02b43a0370ff3fe
[cpPlugins.git] / lib / third_party / Pluma / DLibrary.cpp
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // Pluma - Plug-in Management Framework\r
4 // Copyright (C) 2010-2012 Gil Costa (gsaurus@gmail.com)\r
5 //\r
6 // This software is provided 'as-is', without any express or implied warranty.\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.\r
8 //\r
9 // Permission is granted to anyone to use this software for any purpose,\r
10 // including commercial applications, and to alter it and redistribute it freely,\r
11 // subject to the following restrictions:\r
12 //\r
13 // 1. The origin of this software must not be misrepresented;\r
14 //    you must not claim that you wrote the original software.\r
15 //    If you use this software in a product, an acknowledgment\r
16 //    in the product documentation would be appreciated but is not required.\r
17 //\r
18 // 2. Altered source versions must be plainly marked as such,\r
19 //    and must not be misrepresented as being the original software.\r
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 \r
26 ////////////////////////////////////////////////////////////\r
27 // Headers\r
28 ////////////////////////////////////////////////////////////\r
29 #include <Pluma/DLibrary.hpp>\r
30 #include <cstdio>\r
31 #include <string>\r
32 #include <stdexcept>\r
33 \r
34 \r
35 namespace pluma{\r
36 \r
37 ////////////////////////////////////////////////////////////\r
38 DLibrary* DLibrary::load(const std::string& path){\r
39     if ( path.empty() ){\r
40       // fprintf(stderr, "Failed to load library: Empty path\n");\r
41       throw std::runtime_error( "Failed to load library: Empty path." );\r
42         return NULL;\r
43     }\r
44     void* handle = NULL;\r
45 \r
46     // load library - OS dependent operation\r
47     #ifdef PLUMA_SYS_WINDOWS\r
48         handle = ::LoadLibraryA(path.c_str());\r
49         if (!handle){\r
50           //fprintf(stderr, "Failed to load library \"%s\".\n", path.c_str());\r
51           throw std::runtime_error(\r
52             std::string( "Failed to load library \"" ) +\r
53             path +\r
54             std::string( "\"." )\r
55             );\r
56           return NULL;\r
57         }\r
58     #else\r
59         handle = ::dlopen(path.c_str(), RTLD_NOW);\r
60         if (!handle){\r
61             const char* errorString = ::dlerror();\r
62             std::string error_text = "Failed to load library \"";\r
63             // fprintf(stderr, "Failed to load library \"%s\".", path.c_str());\r
64             if(errorString) error_text += std::string( errorString ); // fprintf(stderr, " OS returned error: \"%s\".", errorString);\r
65             // fprintf(stderr, "\n");\r
66             throw std::runtime_error( error_text + std::string( "\"." ) );\r
67             return NULL;\r
68         }\r
69     #endif\r
70     // return a DLibrary with the DLL handle\r
71     return new DLibrary(handle);\r
72 }\r
73 \r
74 \r
75 ////////////////////////////////////////////////////////////\r
76 DLibrary::~DLibrary(){\r
77     if (handle){\r
78         #ifdef PLUMA_SYS_WINDOWS\r
79             ::FreeLibrary( (HMODULE)handle );\r
80         #else\r
81             ::dlclose(handle);\r
82         #endif\r
83     }\r
84 }\r
85 \r
86 \r
87 ////////////////////////////////////////////////////////////\r
88 void* DLibrary::getSymbol(const std::string& symbol){\r
89     if (!handle){\r
90       //fprintf(stderr, "Cannot inspect library symbols, library isn't loaded.\n");\r
91       throw std::runtime_error( "Cannot inspect library symbols, library isn't loaded." );\r
92         return NULL;\r
93     }\r
94     void* res;\r
95     #ifdef PLUMA_SYS_WINDOWS\r
96         res = (void*)(::GetProcAddress((HMODULE)handle, symbol.c_str()));\r
97     #else\r
98         res = (void*)(::dlsym(handle, symbol.c_str()));\r
99     #endif\r
100     if (!res){\r
101       // fprintf(stderr, "Library symbol \"%s\" not found.\n", symbol.c_str());\r
102       throw std::runtime_error(\r
103         std::string( "Library symbol \"" ) +\r
104         symbol +\r
105         std::string( "\" not found." )\r
106         );\r
107       return NULL;\r
108     }\r
109     return res;\r
110 }\r
111 \r
112 \r
113 ////////////////////////////////////////////////////////////\r
114 DLibrary::DLibrary(void* handle):\r
115     handle(handle)\r
116 {\r
117     // Nothing to do\r
118 }\r
119 \r
120 }   // namespace pluma\r
121 \r