Maestro 0.2.5
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Library.h
Go to the documentation of this file.
1
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 &&) = default;
65 Library &operator=(Library &&) = default;
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 defined(__linux__) || defined(__APPLE__)
80 handle = dlopen(libName, RTLD_NOW);
81
82 if (handle == nullptr) {
83 const char *dlsym_error = dlerror();
84 if (!mute && dlsym_error)
85 std::cerr << "Library: Unable to load library, error: " << dlsym_error
86 << std::endl;
87
88 return false;
89 }
90#elif defined(_WIN32)
91 handle = LoadLibraryA(libName);
92 if (handle == nullptr) {
93 const DWORD error = GetLastError();
94 if (!mute)
95 std::cerr << "Library: Unable to load library, error code: " << error
96 << std::endl;
97 return false;
98 }
99#endif
100
101 return true;
102 }
103
104 void *GetFunction(const char *funcName) noexcept {
105#if defined(__linux__) || defined(__APPLE__)
106 return dlsym(handle, funcName);
107#elif defined(_WIN32)
108 return GetProcAddress(handle, funcName);
109#endif
110 }
111
112 const void *GetHandle() const noexcept { return handle; }
113
114 bool IsMuted() const noexcept { return mute; }
115
116 void SetMute(bool m) noexcept { mute = m; }
117
118 private:
119#if defined(__linux__) || defined(__APPLE__)
120 void *handle = nullptr;
121#elif defined(_WIN32)
122 HINSTANCE handle = nullptr;
123#endif
124 bool mute = false;
125};
126
127} // namespace Utils
128
129#endif
void * GetFunction(const char *funcName) noexcept
Definition Library.h:104
Library(Library &&)=default
const void * GetHandle() const noexcept
Definition Library.h:112
Library(const Library &)=delete
void SetMute(bool m) noexcept
Definition Library.h:116
virtual ~Library()
Definition Library.h:69
virtual bool Init(const char *libName) noexcept
Definition Library.h:78
Library & operator=(const Library &)=delete
Library() noexcept
Definition Library.h:67
bool IsMuted() const noexcept
Definition Library.h:114
Library & operator=(Library &&)=default
Definition Alias.h:20