WSL/SLF GitLab Repository

DynamicLibrary.h 3.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/***********************************************************************************/
/*  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/>.
*/
#ifndef __DYNAMICLIB_H__
#define __DYNAMICLIB_H__

21
#include <meteoio/Config.h>
22
23

#ifndef USE_PRECOMPILED_HEADERS
24
#ifdef _WIN32
25
26
27
28
	#include <direct.h>
	#include <windows.h>
	#undef max
	#undef min
29
#else
30
	#include <dlfcn.h>
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#endif
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#endif

namespace mio {


/**
 * @class PluginObject
 * @brief The PluginObject is an interface for all dynamically loadable Objects, its main task is to register a callback destructor function
 *        for the object dynamically allocated
 *
 * @author Thomas Egger
 * @date   2009-03-10
 */
class PluginObject {
	private:
		// Callback function that should be called to delete dynamic object
52
		void (*deleteObject)(void*);
53
54
	public:
		// The constructor sets the callback function to use
55
		PluginObject(void (*i_delObj)(void*));
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

		// The destructor
		virtual ~PluginObject(void);

		// Sends "this" to the callback destructor function.
		void deleteSelf(void);
};

/**
 * @class DynamicLibrary
 * @brief Manages lifetime of an open dynamic library
 *
 * @author Thomas Egger
 * @date   2009-03-10
 */
class DynamicLibrary {
	protected:
		// The handle to the shared library that was opened
74
#ifdef _WIN32
75
		HINSTANCE objFile;
76
#else
77
		void *objFile;
78
79
80
81
82
83
84
85
86
87
88
#endif

		// Since an instance of DynamicLibrary manages lifetime of an open 
		// library, it is important to make sure that the object isn't 
		// copied.
		DynamicLibrary(const DynamicLibrary&) {}
		DynamicLibrary& operator=(const DynamicLibrary&) {return *this;}

		// Creates a new library, with the object file handle that is passed 
		// in. Protected so that only the DynamicLoader can create an 
		// instance (since it is declared friend.
89
#ifdef _WIN32
90
		DynamicLibrary(HINSTANCE i_objFile);
91
#else
92
		DynamicLibrary(void* i_objFile);
93
94
95
96
97
98
99
100
#endif

	public:
		// Destructor, closes the open shared library
		~DynamicLibrary(void);

		// Creates a new instance of the named class, or returns NULL is the 
		// class isn't found. 
101
		PluginObject* newObject(const std::string& name, const Config& cfg);
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

		friend class DynamicLoader; ///< The friend class DynamicLoader can solely instantiate the DynamicLibrary class (protected constructor)
};

/**
 * @class DynamicLoader
 * @brief The dynamic loader class, used for loading DynamicLibraries.
 *
 * @author Thomas Egger
 * @date   2009-03-10
 */
class DynamicLoader {
	public:
		static DynamicLibrary* loadObjectFile(const std::string& file);
		// Loads a DynamicLibrary, given the shared library file
		// "file", with the dlopen flags supplied.

		//Return last error message of dynamic link libraries
		static std::string getErrorMessage();
};
} //end namespace

#endif