]> Creatis software - cpPlugins.git/blob - lib/third_party/Pluma/DLibrary.cpp
Widget integration (step 5/6): Interactive plugins now supported, widgets updates...
[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 \r
33 \r
34 namespace pluma{\r
35 \r
36 ////////////////////////////////////////////////////////////\r
37 DLibrary* DLibrary::load(const std::string& path){\r
38     if ( path.empty() ){\r
39         fprintf(stderr, "Failed to load library: Empty path\n");\r
40         return NULL;\r
41     }\r
42     void* handle = NULL;\r
43 \r
44     // load library - OS dependent operation\r
45     #ifdef PLUMA_SYS_WINDOWS\r
46         handle = ::LoadLibraryA(path.c_str());\r
47         if (!handle){\r
48             fprintf(stderr, "Failed to load library \"%s\".\n", path.c_str());\r
49             return NULL;\r
50         }\r
51     #else\r
52         handle = ::dlopen(path.c_str(), RTLD_NOW);\r
53         if (!handle){\r
54             const char* errorString = ::dlerror();\r
55             fprintf(stderr, "Failed to load library \"%s\".", path.c_str());\r
56             if(errorString) fprintf(stderr, " OS returned error: \"%s\".", errorString);\r
57             fprintf(stderr, "\n");\r
58             return NULL;\r
59         }\r
60     #endif\r
61     // return a DLibrary with the DLL handle\r
62     return new DLibrary(handle);\r
63 }\r
64 \r
65 \r
66 ////////////////////////////////////////////////////////////\r
67 DLibrary::~DLibrary(){\r
68     if (handle){\r
69         #ifdef PLUMA_SYS_WINDOWS\r
70             ::FreeLibrary( (HMODULE)handle );\r
71         #else\r
72             ::dlclose(handle);\r
73         #endif\r
74     }\r
75 }\r
76 \r
77 \r
78 ////////////////////////////////////////////////////////////\r
79 void* DLibrary::getSymbol(const std::string& symbol){\r
80     if (!handle){\r
81         fprintf(stderr, "Cannot inspect library symbols, library isn't loaded.\n");\r
82         return NULL;\r
83     }\r
84     void* res;\r
85     #ifdef PLUMA_SYS_WINDOWS\r
86         res = (void*)(::GetProcAddress((HMODULE)handle, symbol.c_str()));\r
87     #else\r
88         res = (void*)(::dlsym(handle, symbol.c_str()));\r
89     #endif\r
90     if (!res){\r
91         fprintf(stderr, "Library symbol \"%s\" not found.\n", symbol.c_str());\r
92         return NULL;\r
93     }\r
94     return res;\r
95 }\r
96 \r
97 \r
98 ////////////////////////////////////////////////////////////\r
99 DLibrary::DLibrary(void* handle):\r
100     handle(handle)\r
101 {\r
102     // Nothing to do\r
103 }\r
104 \r
105 }   // namespace pluma\r
106 \r