File: mod_loader.h

package info (click to toggle)
firebird3.0 3.0.1.32609.ds4-14
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 42,212 kB
  • ctags: 51,764
  • sloc: ansic: 374,352; cpp: 314,303; sql: 14,317; pascal: 13,800; yacc: 7,513; fortran: 5,645; sh: 5,149; makefile: 1,124; perl: 97; sed: 83; xml: 36; csh: 15; awk: 1
file content (130 lines) | stat: -rw-r--r-- 4,528 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 *	PROGRAM:		JRD Module Loader
 *	MODULE:			mod_loader.h
 *	DESCRIPTION:	Abstract class for loadable modules.
 *
 *  The contents of this file are subject to the Initial
 *  Developer's Public License Version 1.0 (the "License");
 *  you may not use this file except in compliance with the
 *  License. You may obtain a copy of the License at
 *  http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
 *
 *  Software distributed under the License is distributed AS IS,
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied.
 *  See the License for the specific language governing rights
 *  and limitations under the License.
 *
 *  The Original Code was created by John Bellardo
 *  for the Firebird Open Source RDBMS project.
 *
 *  Copyright (c) 2002 John Bellardo <bellardo at cs.ucsd.edu>
 *  and all contributors signed below.
 *
 *  All Rights Reserved.
 *  Contributor(s): ______________________________________.
 *
 */

#ifndef JRD_OS_MOD_LOADER_H
#define JRD_OS_MOD_LOADER_H

#include "../common/classes/fb_string.h"

/***
	The ModuleLoader class is an abstraction of the dynamic code loading
	facilities provided by the host operating system.  The class provides
	functions to determine if the file at a given path is a loadable module,
	to load that module, and to modify the filename in a way that is
	appropiate to the host computer.

	All implementations of this interface are expected to provide definitions
	for the 3 static functions in the ModuleLoader class, and provide a
	subclass of ModuleLoader::Loader that implements findSymbol.
***/

class ModuleLoader
{
public:
	/** ModuleLoader::Module is the abstract base class for all disk
		based loadable modules after they are loaded into memory.
		It provides a method to locate a pointer for a given symbol,
		and automatically unloads the module from memory when the
		object is destructed.  Instances of this class are created
		using the ModuleLoader::loadModule function.
	**/
	class Module
	{
	public:
		/** findSymbol searchs through the module after it has been loaded into
			memory, and returns a pointer to that symbol's location in memory.
			If the symbol can't be found or doesn't exist the function returns
			NULL.
		**/
		virtual void* findSymbol(const Firebird::string&) = 0;

		template <typename T> T& findSymbol(const Firebird::string& symbol, T& ptr)
		{
			return (ptr = (T)(findSymbol(symbol)));
		}

		/// Destructor
		virtual ~Module() {}

		const Firebird::PathName fileName;

	protected:
		/// The constructor is protected so normal code can't allocate instances
		/// of the class, but the class itself is still able to be subclassed.
		Module(MemoryPool& pool, const Firebird::PathName& aFileName)
			: fileName(pool, aFileName)
		{
		}

	private:
		/// Copy construction is not supported, hence the copy constructor is private
		Module(const Module&);		// no impl
		/// assignment of Modules isn't supported so the assignment operator is private
		const Module& operator=(const Module&);		// no impl
	};

	/** loadModule is given as a string the path to the module to load.  It
		attempts to load the module.  If successful it returns the ModuleLoader::Module
		object that represents the loaded module in memory and can be used to
		perform symbol lookups on the module.  If unsuccessful it returns NULL.
		It is the callers responsibility to delete the returned module object
		when it is no longer needed.
	**/
	static Module* loadModule(const Firebird::PathName&);

	/** doctorModuleExtension modifies the given path name to add the platform
		specific module extention.  This allows the user to provide the root name
		of the file, and the code to append the correct extention regardless of the
		host operating system.  This function is typically called after a failed attempt
		to load the module without the extention.
	**/
	static void doctorModuleExtension(Firebird::PathName&);

	/** Almost like loadModule(), but in case of failure invokes doctorModuleExtension()
		and retries.
	**/
	static Module* fixAndLoadModule(const Firebird::PathName& modName)
	{
		Module* mod = loadModule(modName);
		if (!mod)
		{
			Firebird::PathName fixed(modName);
			doctorModuleExtension(fixed);
			mod = loadModule(fixed);
		}
		return mod;
	}

	/** isLoadableModule checks the given file to see if it is a loadable
		module.  This function is required because different operating
		systems require different checks.
	**/
	static bool isLoadableModule(const Firebird::PathName&);
};

#endif // JRD_OS_MOD_LOADER_H