-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
63 lines (52 loc) · 2.37 KB
/
main.cpp
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
#include <cstdlib>
#include <iostream>
#include <function_loader/exceptions.hpp>
#include <function_loader/function_loader.hpp>
static int print_error_and_exit(const std::exception & error)
{
std::cerr << error.what() << std::endl;
return EXIT_FAILURE;
}
static int show_usage()
{
namespace function_loader = burda::function_loader;
try
{
function_loader::function_loader loader{ "./demo-library.dll" };
// function_loader supports move semantics, so we can safely do e.g. "const auto other = std::move(loader)"
// get procedures at runtime from the shared library
// see "demo-library.hpp" and "demo-library.cpp" in the "demo-library" directory
const auto func_void_no_params = loader.get_function<void()>("function_with_no_params");
const auto func_with_return_value_and_params = loader.get_function<int(float, const char *)>("function_with_return_value_and_params");
// don't have to check for call-ability, otherwise the "function_does_not_exist" would be thrown
func_void_no_params();
std::clog << "func_with_return_value_and_params returned " << func_with_return_value_and_params(99.0, "foo");
// if the "loader" object went out of scope in here, it would free all resources and unload the library handle,
// but for demo purposes, we'll move the instance
const auto another_loader = std::move(loader);
// do whatever actions you like with the "another_loader"
// the "another_loader" goes out of scope
}
catch (const function_loader::exceptions::library_load_failed & error)
{
// library load failed upon construction of the function_loader
return print_error_and_exit(error);
}
catch (const function_loader::exceptions::library_handle_invalid & error)
{
// happens when "get_function" called on the function_loader with invalid library handle
// (may happen after the object was moved)
return print_error_and_exit(error);
}
catch (const function_loader::exceptions::function_does_not_exist & error)
{
// given function not found in the library, might be caused by incorrect signature,
// or function is not exported (visible) from outside
return print_error_and_exit(error);
}
return EXIT_SUCCESS;
}
int main(int /*argc*/, char ** /*argv*/)
{
return show_usage();
}