summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/pdynlib.h
blob: b776d8e9b89c265022b90cac43846c7d3ea9ec26 (plain) (blame)
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
// license:GPL-2.0+
// copyright-holders:Couriersud

#ifndef PDYNLIB_H_
#define PDYNLIB_H_

///
/// \file pdynlib.h
///

#include "pstring.h"
#include "ptypes.h"

namespace plib {
// ----------------------------------------------------------------------------------------
// pdynlib: dynamic loading of libraries  ...
// ----------------------------------------------------------------------------------------

class dynlib : public nocopyassignmove
{
public:
	explicit dynlib(const pstring &libname);
	dynlib(const pstring &path, const pstring &libname);

	~dynlib();
	COPYASSIGNMOVE(dynlib, delete)

	bool isLoaded() const;

	template <typename T>
	T getsym(const pstring &name)
	{
		return reinterpret_cast<T>(getsym_p(name));
	}
private:
	void *getsym_p(const pstring &name);

	bool m_isLoaded;
	void *m_lib;
};

template <typename R, typename... Args>
class dynproc
{
public:
	using calltype = R(*) (Args... args);

	dynproc() : m_sym(nullptr) { }

	dynproc(dynlib &dl, const pstring &name)
	{
		m_sym = dl.getsym<calltype>(name);
	}

	void load(dynlib &dl, const pstring &name)
	{
		m_sym = dl.getsym<calltype>(name);
	}

	R operator ()(Args&&... args) const
	{
		return m_sym(std::forward<Args>(args)...);
		//return m_sym(args...);
	}

	bool resolved() const { return m_sym != nullptr; }
private:
	calltype m_sym;
};

} // namespace plib

#endif // PSTRING_H_