WSL/SLF GitLab Repository

DynamicLibrary.cc 2.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/***********************************************************************************/
/*  Copyright 2009 WSL Institute for Snow and Avalanche Research    SLF-DAVOS      */
/***********************************************************************************/
/* This file is part of MeteoIO.
    MeteoIO is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MeteoIO is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with MeteoIO.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <meteoio/DynamicLibrary.h>

namespace mio {

22
#ifdef _WIN32
23
DynamicLibrary::DynamicLibrary(HINSTANCE i_objFile) : objFile(i_objFile){}
24
#else
25
DynamicLibrary::DynamicLibrary(void* i_objFile) : objFile(i_objFile){}
26
27
28
29
#endif

DynamicLibrary::~DynamicLibrary(void)
{
30
#ifdef _WIN32
31
    FreeLibrary(objFile);
32
#else
33
    dlclose(objFile);
34
35
36
#endif
}

37
PluginObject* DynamicLibrary::newObject(const std::string& name, const Config& cfg)
38
39
{
  // If there is no valid library, return null
40
	if(objFile == NULL) {
41
42
43
44
		return NULL;
	}

	// Get the loadObject() function.  If it doesn't exist, return NULL.
45
#ifdef _WIN32
46
	#pragma warning(disable:4191) //GetProcAddress does NOT return a FARPROC, the warning misses it...
47
	const void (*loadSym)(const std::string&, const Config&) = (const void (*)(const std::string&, const Config&))GetProcAddress(objFile, "loadObject");
48
#else
49
	const void* loadSym = dlsym(objFile, "loadObject");
50
51
52
53
54
55
#endif

	if(loadSym == NULL) {
		return NULL;
	}

56
57
58
59
//HACK: this has to stay until c++ standard handles this case...
#ifdef __GNUC__
__extension__
#endif
60
	// Load a new instance of the requested class, and return it
61
	void* obj = ((void* (*)(const std::string&, const Config&))(loadSym))(name, cfg);
62
63
64
65
66
	return reinterpret_cast<PluginObject*>(obj);
}

DynamicLibrary* DynamicLoader::loadObjectFile(const std::string& file)
{
67
#ifdef _WIN32
68
69
70
71
72
73
74
75
76
77
78
79
80
	HINSTANCE objFile = LoadLibrary(TEXT(file.c_str()));
#else
	void* objFile = dlopen(file.c_str(), RTLD_NOW);
#endif

	if(objFile == NULL) {
		return NULL;
	}

	return new DynamicLibrary(objFile);
}

std::string DynamicLoader::getErrorMessage(){
81
#ifdef _WIN32
82
83
84
85
86
87
88
89
	std::stringstream ss;
	ss << GetLastError();
	return ss.str();
#else
	return std::string(dlerror());
#endif
}

90
PluginObject::PluginObject(void (*i_delObj)(void*)) : deleteObject(i_delObj){}
91
92
93
94
95

PluginObject::~PluginObject(void){}

void PluginObject::deleteSelf(void)
{
96
	(*deleteObject)(reinterpret_cast<void*>(this));
97
98
99
}

} //namespace