Skip to content

Commit ec490b8

Browse files
committed
refactor: let ChangeCollection/ChangeIterator keep a copy of the Session
...so that we can (finally) implement some better locking. Change-Id: I9417abba054a4b4ec21b0873a4ec090cf7251d18
1 parent 3cbf4f6 commit ec490b8

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

include/sysrepo-cpp/Changes.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include <memory>
1111
#include <optional>
1212
#include <sysrepo-cpp/Enum.hpp>
13+
#include <sysrepo-cpp/Session.hpp>
1314
#include <string>
1415

15-
struct sr_session_ctx_s;
1616
struct sr_change_iter_s;
1717

1818
namespace sysrepo {
@@ -99,14 +99,15 @@ class ChangeIterator {
9999
struct iterator_end_tag{
100100
};
101101

102-
ChangeIterator(sr_change_iter_s* iter, std::shared_ptr<sr_session_ctx_s> sess);
102+
ChangeIterator(sr_change_iter_s* iter, Session sess);
103103
ChangeIterator(const iterator_end_tag);
104104
friend ChangeCollection;
105105

106106
std::optional<Change> m_current;
107107

108108
std::shared_ptr<sr_change_iter_s> m_iter;
109-
std::shared_ptr<sr_session_ctx_s> m_sess;
109+
// FIXME: get rid of the optional<> when we have a sentinel instead of an end iterator
110+
std::optional<Session> m_sess;
110111
};
111112

112113
/**
@@ -128,9 +129,9 @@ class ChangeCollection {
128129
ChangeIterator end() const;
129130

130131
private:
131-
ChangeCollection(const std::string& xpath, std::shared_ptr<sr_session_ctx_s> sess);
132+
ChangeCollection(const std::string& xpath, Session sess);
132133
friend Session;
133134
std::string m_xpath;
134-
std::shared_ptr<sr_session_ctx_s> m_sess;
135+
Session m_sess;
135136
};
136137
}

include/sysrepo-cpp/Session.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct sr_session_ctx_s;
2020
namespace sysrepo {
2121
class Connection;
2222
class ChangeCollection;
23+
class ChangeIterator;
2324
class DynamicSubscription;
2425
class Session;
2526
class Subscription;
@@ -178,6 +179,8 @@ class Session {
178179

179180
private:
180181
friend Connection;
182+
friend ChangeCollection;
183+
friend ChangeIterator;
181184
friend Session wrapUnmanagedSession(sr_session_ctx_s* session);
182185
friend Subscription;
183186
friend sr_session_ctx_s* getRawSession(Session sess);

src/Session.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ DynamicSubscription Session::subscribeNotifications(
695695
*/
696696
ChangeCollection Session::getChanges(const std::string& xpath)
697697
{
698-
return ChangeCollection{xpath, m_sess};
698+
return ChangeCollection{xpath, *this};
699699
}
700700

701701
/**

src/Subscription.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ Subscription::Subscription(Subscription&& other) noexcept = default;
293293

294294
Subscription& Subscription::operator=(Subscription&& other) noexcept = default;
295295

296-
ChangeCollection::ChangeCollection(const std::string& xpath, std::shared_ptr<sr_session_ctx_s> sess)
296+
ChangeCollection::ChangeCollection(const std::string& xpath, Session sess)
297297
: m_xpath(xpath)
298-
, m_sess(sess)
298+
, m_sess(std::move(sess))
299299
{
300300
}
301301

@@ -305,9 +305,9 @@ ChangeCollection::ChangeCollection(const std::string& xpath, std::shared_ptr<sr_
305305
ChangeIterator ChangeCollection::begin() const
306306
{
307307
sr_change_iter_t* iter;
308-
auto res = sr_get_changes_iter(m_sess.get(), m_xpath.c_str(), &iter);
308+
auto res = sr_get_changes_iter(m_sess.m_sess.get(), m_xpath.c_str(), &iter);
309309

310-
throwIfError(res, "Couldn't create an iterator for changes", m_sess.get());
310+
throwIfError(res, "Couldn't create an iterator for changes", m_sess.m_sess.get());
311311

312312
return ChangeIterator{iter, m_sess};
313313
}
@@ -323,17 +323,17 @@ ChangeIterator ChangeCollection::end() const
323323
/**
324324
* Wraps `sr_change_iter_s`.
325325
*/
326-
ChangeIterator::ChangeIterator(sr_change_iter_s* iter, std::shared_ptr<sr_session_ctx_s> sess)
326+
ChangeIterator::ChangeIterator(sr_change_iter_s* iter, Session sess)
327327
: m_iter(iter, sr_free_change_iter)
328-
, m_sess(sess)
328+
, m_sess(std::move(sess))
329329
{
330330
operator++();
331331
}
332332

333333
ChangeIterator::ChangeIterator(const iterator_end_tag)
334334
: m_current(std::nullopt)
335335
, m_iter(nullptr)
336-
, m_sess(nullptr)
336+
, m_sess(std::nullopt)
337337
{
338338
}
339339

@@ -347,14 +347,14 @@ ChangeIterator& ChangeIterator::operator++()
347347
const char* prevValue;
348348
const char* prevList;
349349
int prevDefault;
350-
auto ret = sr_get_change_tree_next(m_sess.get(), m_iter.get(), &operation, &node, &prevValue, &prevList, &prevDefault);
350+
auto ret = sr_get_change_tree_next(m_sess->m_sess.get(), m_iter.get(), &operation, &node, &prevValue, &prevList, &prevDefault);
351351

352352
if (ret == SR_ERR_NOT_FOUND) {
353353
m_current = std::nullopt;
354354
return *this;
355355
}
356356

357-
throwIfError(ret, "Could not iterate to the next change", m_sess.get());
357+
throwIfError(ret, "Could not iterate to the next change", m_sess->m_sess.get());
358358

359359
// I can safely "dereference" the change here, because last change is handled by the condition above.
360360
m_current.emplace(Change{

0 commit comments

Comments
 (0)