Maestro 0.3.11
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{
60
62{
63 public:
64 Library(const Library &) = delete;
65 Library &operator=(const Library &) = delete;
66 Library(Library &&) = delete;
67 Library &operator=(Library &&) = delete;
68
69 Library() noexcept
70 {
71 }
72
73 virtual ~Library()
74 {
75 if (handle)
76#if defined(__linux__) || defined(__APPLE__)
77 dlclose(handle);
78#elif defined(_WIN32)
79 FreeLibrary(handle);
80#endif
81 }
82
83 virtual bool Init(const char *libName) noexcept
84 {
85 if (handle)
86 return true;
87#if defined(__linux__) || defined(__APPLE__)
88 handle = dlopen(libName, RTLD_NOW);
89
90 if (handle == nullptr)
91 {
92 const char *dlsym_error = dlerror();
93 if (!mute && dlsym_error)
94 std::cerr << "Library: Unable to load library, error: " << dlsym_error << std::endl;
95
96 return false;
97 }
98#elif defined(_WIN32)
99 handle = LoadLibraryA(libName);
100 if (handle == nullptr)
101 {
102 const DWORD error = GetLastError();
103 if (!mute)
104 std::cerr << "Library: Unable to load library, error code: " << error << std::endl;
105 return false;
106 }
107#endif
108
109 return true;
110 }
111
112 void *GetFunction(const char *funcName) noexcept
113 {
114#if defined(__linux__) || defined(__APPLE__)
115 return dlsym(handle, funcName);
116#elif defined(_WIN32)
117 return GetProcAddress(handle, funcName);
118#endif
119 }
120
121 const void *GetHandle() const noexcept
122 {
123 return handle;
124 }
125
126 bool IsMuted() const noexcept
127 {
128 return mute;
129 }
130
131 void SetMute(bool m) noexcept
132 {
133 mute = m;
134 }
135
136 private:
137#if defined(__linux__) || defined(__APPLE__)
138 void *handle = nullptr;
139#elif defined(_WIN32)
140 HINSTANCE handle = nullptr;
141#endif
142 bool mute = false;
143};
144
145} // namespace Utils
146
147#endif
void * GetFunction(const char *funcName) noexcept
Definition Library.h:112
const void * GetHandle() const noexcept
Definition Library.h:121
Library(const Library &)=delete
void SetMute(bool m) noexcept
Definition Library.h:131
virtual ~Library()
Definition Library.h:73
virtual bool Init(const char *libName) noexcept
Definition Library.h:83
Library(Library &&)=delete
Library & operator=(Library &&)=delete
Library & operator=(const Library &)=delete
Library() noexcept
Definition Library.h:69
bool IsMuted() const noexcept
Definition Library.h:126
Portable dense contraction kernels.