aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/dynlib.h
blob: 1a5015f7f4fa01dfa9effe66666ad53175848998 (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
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
#pragma once

#include "defaults.h" 
 
#include <util/generic/ptr.h>
#include <util/generic/string.h>
 
#define Y_GET_FUNC(dll, name) FUNC_##name((dll).Sym(#name))
#define Y_GET_FUNC_OPTIONAL(dll, name) FUNC_##name((dll).SymOptional(#name))

#ifdef _win32_
    #define DEFAULT_DLLOPEN_FLAGS 0 
#else
    #include <dlfcn.h> 

    #ifndef RTLD_GLOBAL 
        #define RTLD_GLOBAL (0) 
    #endif 

    #define DEFAULT_DLLOPEN_FLAGS (RTLD_NOW | RTLD_GLOBAL) 
#endif

class TDynamicLibrary { 
public: 
    TDynamicLibrary() noexcept;
    TDynamicLibrary(const TString& path, int flags = DEFAULT_DLLOPEN_FLAGS);
    ~TDynamicLibrary();

    void Open(const char* path, int flags = DEFAULT_DLLOPEN_FLAGS); 
    void Close() noexcept;
    void* SymOptional(const char* name) noexcept;
    void* Sym(const char* name); 
    bool IsLoaded() const noexcept;
    void SetUnloadable(bool unloadable); // Set to false to avoid unloading on destructor 

private: 
    class TImpl; 
    THolder<TImpl> Impl_; 
};

// a wrapper for a symbol
template <class TLib>
class TExternalSymbol { 
private:
    TLib* PLib; 
    TDynamicLibrary* DLib; 
    TString lname;
    TString vname;
 
public:
    TExternalSymbol() noexcept {
        PLib = nullptr;
        DLib = nullptr;
    }
    TExternalSymbol(const TExternalSymbol& es) { 
        PLib = nullptr;
        DLib = nullptr;
        if (es.IsDynamic())
            Open(es.LibName().data(), es.VtblName().data());
        else if (es.IsStatic())
            SetSym(es.Symbol());
    }
    TExternalSymbol& operator=(const TExternalSymbol& es) { 
        if (this != &es) {
            Close();
            if (es.IsDynamic())
                Open(es.LibName().data(), es.VtblName().data());
            else if (es.IsStatic())
                SetSym(es.Symbol());
        }
        return *this;
    }
    ~TExternalSymbol() {
        delete DLib;
    }
    // set the symbol from dynamic source
    void Open(const char* lib_name, const char* vtbl_name) { 
        if (DLib != nullptr || PLib != nullptr)
            return;
        try {
            DLib = new TDynamicLibrary();
            DLib->Open(lib_name);
            PLib = (TLib*)DLib->Sym(vtbl_name); 
        } catch (...) { 
            delete DLib;
            DLib = nullptr;
            throw;
        }
        lname = lib_name;
        vname = vtbl_name;
    }
    // set the symbol from static source
    void SetSym(TLib* pl) noexcept {
        if (DLib == nullptr && PLib == nullptr)
            PLib = pl;
    }
    void Close() noexcept {
        delete DLib;
        DLib = 0;
        PLib = 0;
        lname.remove();
        vname.remove();
    }
    TLib* Symbol() const noexcept {
        return PLib;
    }
    const TString& LibName() const noexcept {
        return lname;
    }
    const TString& VtblName() const noexcept {
        return vname;
    }
    bool IsStatic() const noexcept {
        return DLib == nullptr && PLib != nullptr;
    }
    bool IsDynamic() const noexcept {
        return DLib && DLib->IsLoaded() && PLib != nullptr;
    }
};