Skip to content

Commit 20dc706

Browse files
committed
Let Session keep a Connection reference
The Connection class is meant as a lightweight wrapper anyway, so let's not bother with an explicit shared_ptr<sr_conn_ctx_s>, and keep a full-blown C++ reference. The `Session::Session(..., const unmanaged_tag)` is still correct, AFAICT: it will not attempt to call sr_disconnect() because the destructor of the shared_ptr<sr_conn_ctx_t> has an empty lambda as its destructor. That's (still) by design. Change-Id: I91efc186ff93e7ca27830baa3cbe24ee4c08fba7
1 parent 98d5406 commit 20dc706

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

include/sysrepo-cpp/Connection.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
*/
88
#pragma once
99

10+
#include <chrono>
1011
#include <memory>
11-
#include <sysrepo-cpp/Session.hpp>
12+
#include <string>
13+
#include <sysrepo-cpp/Enum.hpp>
1214

1315
struct sr_conn_ctx_s;
1416

@@ -42,7 +44,7 @@ class Connection {
4244
friend Session;
4345

4446
private:
45-
Connection(std::shared_ptr<sr_conn_ctx_s> ctx);
47+
explicit Connection(std::shared_ptr<sr_conn_ctx_s> ctx);
4648
std::shared_ptr<sr_conn_ctx_s> ctx;
4749
};
4850
}

include/sysrepo-cpp/Session.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
*/
88
#pragma once
99

10-
#include <chrono>
1110
#include <iosfwd>
12-
#include <memory>
1311
#include <optional>
1412
#include <libyang-cpp/Context.hpp>
1513
#include <libyang-cpp/DataNode.hpp>
16-
#include <sysrepo-cpp/Enum.hpp>
14+
#include <sysrepo-cpp/Connection.hpp>
1715
#include <sysrepo-cpp/Subscription.hpp>
1816

19-
struct sr_conn_ctx_s;
2017
struct sr_session_ctx_s;
2118

2219
namespace sysrepo {
@@ -176,10 +173,10 @@ class Session {
176173
friend Session wrapUnmanagedSession(sr_session_ctx_s* session);
177174
friend sr_session_ctx_s* getRawSession(Session sess);
178175

179-
Session(sr_session_ctx_s* sess, std::shared_ptr<sr_conn_ctx_s> conn);
176+
Session(sr_session_ctx_s* sess, Connection conn);
180177
explicit Session(sr_session_ctx_s* unmanagedSession, const unmanaged_tag);
181178

182-
std::shared_ptr<sr_conn_ctx_s> m_conn;
179+
Connection m_conn;
183180
std::shared_ptr<sr_session_ctx_s> m_sess;
184181
};
185182

src/Connection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Session Connection::sessionStart(sysrepo::Datastore datastore)
5454
auto res = sr_session_start(ctx.get(), toDatastore(datastore), &sess);
5555

5656
throwIfError(res, "Couldn't start sysrepo session");
57-
return Session{sess, ctx};
57+
return Session{sess, *this};
5858
}
5959

6060
/**

src/Session.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ std::optional<std::string> constructXPathFilter(const std::optional<std::variant
8181
*
8282
* Internal use only.
8383
*/
84-
Session::Session(sr_session_ctx_s* sess, std::shared_ptr<sr_conn_ctx_s> conn)
85-
: m_conn(conn)
86-
// The connection `conn` is saved here in the deleter (as a capture). This means that copies of this shared_ptr will
87-
// automatically hold a reference to `conn`.
88-
, m_sess(sess, [extend_connection_lifetime = conn] (auto* sess) {
84+
Session::Session(sr_session_ctx_s* sess, Connection conn)
85+
: m_conn(std::move(conn))
86+
// The connection is saved here in the deleter (as a capture). This means that copies of this shared_ptr will
87+
// automatically hold a reference to `m_conn`.
88+
, m_sess(sess, [extend_connection_lifetime = m_conn] (auto* sess) {
8989
sr_session_stop(sess);
9090
})
9191
{
@@ -944,7 +944,7 @@ void Session::setOriginatorName(const std::string& originatorName)
944944
*/
945945
Connection Session::getConnection()
946946
{
947-
return Connection{m_conn};
947+
return m_conn;
948948
}
949949

950950
/**

0 commit comments

Comments
 (0)