Skip to content

Commit a606bc1

Browse files
committed
🎨 Use unique ptr for sessions
1 parent c1d8a6e commit a606bc1

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/na/device/Device.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <cstddef>
2020
#include <spdlog/spdlog.h>
21+
#include <unordered_set>
2122

2223
namespace {
2324
/// The status of the session.
@@ -160,6 +161,20 @@ struct DecoherenceTimes {
160161
static DecoherenceTimes decoherenceTimes;
161162
return decoherenceTimes;
162163
}
164+
165+
/**
166+
* @brief Provides access to the list of device sessions.
167+
* @return a reference to a static vector of unique pointers to
168+
* MQT_NA_QDMI_Device_Session_impl_d.
169+
*/
170+
[[nodiscard]] auto sessions()
171+
-> std::unordered_map<MQT_NA_QDMI_Device_Session,
172+
std::unique_ptr<MQT_NA_QDMI_Device_Session_impl_d>>& {
173+
static std::unordered_map<MQT_NA_QDMI_Device_Session,
174+
std::unique_ptr<MQT_NA_QDMI_Device_Session_impl_d>>
175+
sessions;
176+
return sessions;
177+
}
163178
} // namespace
164179

165180
// NOLINTBEGIN(bugprone-macro-parentheses)
@@ -228,14 +243,21 @@ int MQT_NA_QDMI_device_initialize() {
228243
return QDMI_SUCCESS;
229244
}
230245

231-
int MQT_NA_QDMI_device_finalize() { return QDMI_SUCCESS; }
246+
int MQT_NA_QDMI_device_finalize() {
247+
while (!sessions().empty()) {
248+
MQT_NA_QDMI_device_session_free(sessions().begin()->first);
249+
}
250+
return QDMI_SUCCESS;
251+
}
232252

233253
int MQT_NA_QDMI_device_session_alloc(MQT_NA_QDMI_Device_Session* session) {
234254
if (session == nullptr) {
235255
return QDMI_ERROR_INVALIDARGUMENT;
236256
}
237-
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
238-
*session = new MQT_NA_QDMI_Device_Session_impl_d();
257+
auto uniqueSession = std::make_unique<MQT_NA_QDMI_Device_Session_impl_d>();
258+
*session = sessions()
259+
.emplace(uniqueSession.get(), std::move(uniqueSession))
260+
.first->first;
239261
return QDMI_SUCCESS;
240262
}
241263

@@ -251,8 +273,7 @@ int MQT_NA_QDMI_device_session_init(MQT_NA_QDMI_Device_Session session) {
251273
}
252274

253275
void MQT_NA_QDMI_device_session_free(MQT_NA_QDMI_Device_Session session) {
254-
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
255-
delete session;
276+
sessions().erase(session);
256277
}
257278

258279
int MQT_NA_QDMI_device_session_set_parameter(

0 commit comments

Comments
 (0)