forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyModule.hpp
More file actions
79 lines (62 loc) · 2.43 KB
/
DependencyModule.hpp
File metadata and controls
79 lines (62 loc) · 2.43 KB
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
/*******************************************************
* Copyright (c) 2018, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#pragma once
#include <common/Logger.hpp>
#include <common/defines.hpp>
#include <common/module_loading.hpp>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
namespace spdlog {
class logger;
}
namespace common {
using Version = std::tuple<int, int, int>; // major, minor, patch
/// Allows you to create classes which dynamically load dependencies at runtime
///
/// Creates a dependency module which will dynamically load a library
/// at runtime instead of at link time. This class will be a component of a
/// module class which will have member functions for each of the functions
/// we use in ArrayFire
class DependencyModule {
LibHandle handle;
std::shared_ptr<spdlog::logger> logger;
std::vector<void*> functions;
public:
DependencyModule(const char* plugin_file_name,
const char** paths = nullptr);
DependencyModule(const std::vector<std::string>& plugin_base_file_name,
const std::vector<std::string>& suffixes,
const std::vector<std::string>& paths,
const size_t verListSize = 0,
const Version* versions = nullptr);
~DependencyModule() noexcept;
/// Returns a function pointer to the function with the name symbol_name
template<typename T>
T getSymbol(const char* symbol_name) {
functions.push_back(getFunctionPointer(handle, symbol_name));
return (T)functions.back();
}
/// Returns true if the module was successfully loaded
bool isLoaded() const noexcept;
/// Returns true if all of the symbols for the module were loaded
bool symbolsLoaded() const noexcept;
/// Returns the last error message that occurred because of loading the
/// library
static std::string getErrorMessage() noexcept;
spdlog::logger* getLogger() const noexcept;
};
} // namespace common
/// Creates a function pointer
#define MODULE_MEMBER(NAME) decltype(&::NAME) NAME
/// Dynamically loads the function pointer at runtime
#define MODULE_FUNCTION_INIT(NAME) \
NAME = module.getSymbol<decltype(&::NAME)>(#NAME);