File: SharedLib.cpp

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (82 lines) | stat: -rw-r--r-- 2,083 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#if defined BUILDING_AI
#include "System/StringUtil.h"
#else // defined BUILDING_AI
#include "System/Log/ILog.h"
#endif // defined BUILDING_AI

// OS dependent stuff
#ifdef _WIN32
	#include "Win/DllLib.h"
	#define SL_IMPL_CLS DllLib
	#define SL_IMPL_EXT "dll"
#else // _WIN32
	#include "Linux/SoLib.h"
	#define SL_IMPL_CLS SoLib
	#if defined __APPLE__
		#define SL_IMPL_EXT "dylib"
	#else // defined __APPLE__
		#define SL_IMPL_EXT "so"
	#endif // defined __APPLE__
#endif // _WIN32

/**
 * Used to create a platform-specific shared library handler.
 */
SharedLib* SharedLib::Instantiate(const char* fileName)
{
	SharedLib* lib = NULL;

	lib = new SL_IMPL_CLS(fileName);

	if (lib == NULL || lib->LoadFailed()) {
		// loading failed
		delete lib;
		lib = NULL;
	}

	return lib;
}

/**
 * Used to create a platform-specific shared library handler.
 */
SharedLib* SharedLib::Instantiate(const std::string& fileName)
{
	return Instantiate(fileName.c_str());
}


const char* SharedLib::GetLibExtension() 
{
	return SL_IMPL_EXT;
}

void SharedLib::reportError(const char* errorMsg, const char* fileName, int lineNumber, const char* function) {

#if defined BUILDING_AI
	// when used by an AI Interface eg, we define a macro, eg. this:
	// EXTERNAL_LOGGER(msg) printf(msg);
	// so we can log SharedLib errors/failures in our AI if we want so.
	// see the C AI Interface for an example
	// Note: the msg passed to the macro is of type: const char*
	#if defined EXTERNAL_LOGGER
		const int MAX_MSG_LENGTH = 511;
		char s_msg[MAX_MSG_LENGTH + 1];
		SNPRINTF(s_msg, MAX_MSG_LENGTH, "%s:%d: %s: %s", fileName, lineNumber, function, errorMsg);
		EXTERNAL_LOGGER(s_msg)
	#else // defined EXTERNAL_LOGGER
		// DO NOTHING
	#endif // defined EXTERNAL_LOGGER
#else // defined BUILDING_AI
	LOG_L(L_ERROR, "%s:%d: %s: %s", fileName, lineNumber, function, errorMsg);
#endif // defined BUILDING_AI
}

SharedLib::~SharedLib() {
	// subclasses will call Unload in their destructors.
}

#undef SL_IMPL_CLS
#undef SL_IMPL_EXT