Maestro 0.3.1
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Library.h
Go to the documentation of this file.
1
38
39#pragma once
40
41#ifndef _LIBRARY_H
42#define _LIBRARY_H
43
44#include <iostream>
45
46#if defined(__linux__) || defined(__APPLE__)
47
48#include <dlfcn.h>
49
50#elif defined(_WIN32)
51
52#include <windows.h>
53#undef min
54#undef max
55
56#endif
57
58namespace Utils {
59
60class Library {
61 public:
62 Library(const Library &) = delete;
63 Library &operator=(const Library &) = delete;
64 Library(Library &&) = delete;
65 Library &operator=(Library &&) = delete;
66
67 Library() noexcept {}
68
69 virtual ~Library() {
70 if (handle)
71#if defined(__linux__) || defined(__APPLE__)
72 dlclose(handle);
73#elif defined(_WIN32)
74 FreeLibrary(handle);
75#endif
76 }
77
78 virtual bool Init(const char *libName) noexcept {
79 if (handle) return true;
80#if defined(__linux__) || defined(__APPLE__)
81 handle = dlopen(libName, RTLD_NOW);
82
83 if (handle == nullptr) {
84 const char *dlsym_error = dlerror();
85 if (!mute && dlsym_error)
86 std::cerr << "Library: Unable to load library, error: " << dlsym_error
87 << std::endl;
88
89 return false;
90 }
91#elif defined(_WIN32)
92 handle = LoadLibraryA(libName);
93 if (handle == nullptr) {
94 const DWORD error = GetLastError();
95 if (!mute)
96 std::cerr << "Library: Unable to load library, error code: " << error
97 << std::endl;
98 return false;
99 }
100#endif
101
102 return true;
103 }
104
105 void *GetFunction(const char *funcName) noexcept {
106#if defined(__linux__) || defined(__APPLE__)
107 return dlsym(handle, funcName);
108#elif defined(_WIN32)
109 return GetProcAddress(handle, funcName);
110#endif
111 }
112
113 const void *GetHandle() const noexcept { return handle; }
114
115 bool IsMuted() const noexcept { return mute; }
116
117 void SetMute(bool m) noexcept { mute = m; }
118
119 private:
120#if defined(__linux__) || defined(__APPLE__)
121 void *handle = nullptr;
122#elif defined(_WIN32)
123 HINSTANCE handle = nullptr;
124#endif
125 bool mute = false;
126};
127
128} // namespace Utils
129
130#endif
void * GetFunction(const char *funcName) noexcept
Definition Library.h:105
const void * GetHandle() const noexcept
Definition Library.h:113
Library(const Library &)=delete
void SetMute(bool m) noexcept
Definition Library.h:117
virtual ~Library()
Definition Library.h:69
virtual bool Init(const char *libName) noexcept
Definition Library.h:78
Library(Library &&)=delete
Library & operator=(Library &&)=delete
Library & operator=(const Library &)=delete
Library() noexcept
Definition Library.h:67
bool IsMuted() const noexcept
Definition Library.h:115
Definition Alias.h:22