Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Library.h
Go to the documentation of this file.
1
11
12#pragma once
13
14#ifndef _LIBRARY_H
15#define _LIBRARY_H
16
17#include <iostream>
18
19#ifdef __linux__
20
21#include <dlfcn.h>
22
23#elif defined(_WIN32)
24
25#include <windows.h>
26
27#endif
28
29namespace Utils {
30
31class Library {
32public:
33 Library(const Library &) = delete;
34 Library &operator=(const Library &) = delete;
35 Library(Library &&) = default;
36 Library &operator=(Library &&) = default;
37
38 Library() noexcept {}
39
40 virtual ~Library() {
41 if (handle)
42#ifdef __linux__
43 dlclose(handle);
44#elif defined(_WIN32)
45 FreeLibrary(handle);
46#endif
47 }
48
49 virtual bool Init(const char *libName) noexcept {
50#ifdef __linux__
51 handle = dlopen(libName, RTLD_NOW);
52
53 if (handle == nullptr) {
54 const char *dlsym_error = dlerror();
55 if (!mute && dlsym_error)
56 std::cout << "Library: Unable to load library, error: " << dlsym_error
57 << std::endl;
58
59 return false;
60 }
61#elif defined(_WIN32)
62 handle = LoadLibraryA(libName);
63 if (handle == nullptr) {
64 const DWORD error = GetLastError();
65 if (!mute)
66 std::cout << "Library: Unable to load library, error code: " << error
67 << std::endl;
68 return false;
69 }
70#endif
71
72 return true;
73 }
74
75 void *GetFunction(const char *funcName) noexcept {
76#ifdef __linux__
77 return dlsym(handle, funcName);
78#elif defined(_WIN32)
79 return GetProcAddress(handle, funcName);
80#endif
81 }
82
83 const void *GetHandle() const noexcept { return handle; }
84
85 bool IsMuted() const noexcept { return mute; }
86
87 void SetMute(bool m) noexcept { mute = m; }
88
89private:
90#ifdef __linux__
91 void *handle = nullptr;
92#elif defined(_WIN32)
93 HINSTANCE handle = nullptr;
94#endif
95 bool mute = false;
96};
97
98} // namespace Utils
99
100#endif
void * GetFunction(const char *funcName) noexcept
Definition Library.h:75
Library(Library &&)=default
const void * GetHandle() const noexcept
Definition Library.h:83
Library(const Library &)=delete
void SetMute(bool m) noexcept
Definition Library.h:87
virtual ~Library()
Definition Library.h:40
virtual bool Init(const char *libName) noexcept
Definition Library.h:49
Library & operator=(const Library &)=delete
Library() noexcept
Definition Library.h:38
bool IsMuted() const noexcept
Definition Library.h:85
Library & operator=(Library &&)=default
Definition Alias.h:20