fix move-ctor functions

This commit is contained in:
Timur A. Fatkhullin 2024-11-05 12:07:38 +03:00
parent 39b0fad13d
commit 1794de6acd

View File

@ -103,29 +103,13 @@ protected:
AdcNetSessionManager(const AdcNetSessionManager&) = delete;
AdcNetSessionManager(AdcNetSessionManager&& other)
{
if (this == &other) {
return;
}
for (auto& func : _moveCtorFunc) {
func(this);
}
_stopSessionFunc = std::move(other._stopSessionFunc);
_moveCtorFunc = std::move(other._moveCtorFunc);
moveInstFunc(&other, this);
}
AdcNetSessionManager& operator=(const AdcNetSessionManager&) = delete;
AdcNetSessionManager& operator=(AdcNetSessionManager&& other)
{
if (this != &other) {
for (auto& func : _moveCtorFunc) {
func(this);
}
_stopSessionFunc = std::move(other._stopSessionFunc);
_moveCtorFunc = std::move(other._moveCtorFunc);
}
moveInstFunc(&other, this);
return *this;
};
@ -139,11 +123,10 @@ protected:
// started sessions weak pointers
template <interfaces::adc_netsession_c SessionT>
// static std::unordered_map<const AdcNetSessionManager*, std::unordered_set<std::weak_ptr<SessionT>>>
// _serverSessions;
static inline std::unordered_map<const AdcNetSessionManager*, std::list<std::weak_ptr<SessionT>>> _serverSessions{};
std::vector<std::function<bool()>> _stopSessionFunc;
std::vector<std::function<void(const AdcNetSessionManager*)>> _moveCtorFunc;
std::vector<std::function<bool(const AdcNetSessionManager*)>> _stopSessionFunc;
std::vector<std::function<void(const AdcNetSessionManager*, const AdcNetSessionManager*)>> _moveCtorFunc;
template <interfaces::adc_netsession_c SessionT>
void startSession(std::shared_ptr<SessionT>& sess_ptr)
@ -152,11 +135,11 @@ protected:
it = _serverSessions<SessionT>[this].emplace(it, sess_ptr);
sess_ptr->start();
_stopSessionFunc.emplace_back([it, this]() {
_stopSessionFunc.emplace_back([it](const AdcNetSessionManager* inst) {
if (!it->expired()) { // session is still existing
auto sess = it->lock();
sess->stop();
_serverSessions<SessionT>[this].erase(it);
_serverSessions<SessionT>[inst].erase(it);
return true;
} else {
return false;
@ -164,27 +147,12 @@ protected:
});
// auto res = _serverSessions<SessionT>[this].emplace(sess_ptr);
// if (res.second) {
// sess_ptr->start();
// _stopSessionFunc.emplace_back([res, this]() {
// if (!res.first->expired()) { // session is still existing
// auto sess = res.first->lock();
// sess->stop();
// _serverSessions<SessionT>[this].erase(res.first);
// return true;
// } else {
// return false;
// }
// });
// }
// define move-function only once per SessionT!
if (_serverSessions<SessionT>[this].size() == 1) {
_moveCtorFunc.emplace_back([this](const AdcNetSessionManager* new_instance) {
_serverSessions<SessionT>[new_instance] = std::move(_serverSessions<SessionT>[this]);
});
_moveCtorFunc.emplace_back(
[](const AdcNetSessionManager* new_instance, const AdcNetSessionManager* from_inst) {
_serverSessions<SessionT>[new_instance] = std::move(_serverSessions<SessionT>[from_inst]);
});
}
}
@ -229,7 +197,7 @@ protected:
size_t N = 0;
for (auto& func : _stopSessionFunc) {
func() ? ++N : 0;
func(this) ? ++N : 0;
}
_stopSessionFunc.clear();
@ -237,6 +205,19 @@ protected:
return N;
}
void moveInstFunc(const AdcNetSessionManager* to, const AdcNetSessionManager* from)
{
if (from != to) {
for (auto& func : _moveCtorFunc) {
func(to, from);
}
_stopSessionFunc = std::move(from->_stopSessionFunc);
_moveCtorFunc = std::move(from->_moveCtorFunc);
}
}
};
@ -271,7 +252,7 @@ public:
_stopListenFunc = std::move(other._stopListenFunc);
for (auto& func : other._moveCtorFunc) {
func(this);
func(&other, this);
}
_moveCtorFunc = std::move(other._moveCtorFunc);
@ -290,7 +271,7 @@ public:
_stopListenFunc = std::move(other._stopListenFunc);
for (auto& func : other._moveCtorFunc) {
func(this);
func(&other, this);
}
_moveCtorFunc = std::move(other._moveCtorFunc);
@ -313,11 +294,11 @@ public:
auto acceptor = std::make_shared<typename SessionT::netservice_t::acceptor_t>(
std::forward<AcceptorCtorArgTs>(ctor_args)...);
_stopListenFunc.emplace_back([acceptor, id, this]() {
_stopListenFunc.emplace_back([acceptor, id](const AdcGenericNetServer* inst) {
std::error_code ec;
acceptor->close(ec);
_isListening<SessionT>[this][id] = false;
_isListening<SessionT>[inst][id] = false;
});
doAccept<SessionT>(acceptor, std::move(id), std::move(sess_ctx));
@ -325,9 +306,10 @@ public:
// only once per SessionT
if (_isListening<SessionT>[this].size() == 1) {
_moveCtorFunc.emplace_back([this](const AdcGenericNetServer* new_instance) {
_isListening<SessionT>[new_instance] = std::move(_isListening<SessionT>[this]);
});
_moveCtorFunc.emplace_back(
[](const AdcGenericNetServer* new_instance, const AdcGenericNetServer* from_inst) {
_isListening<SessionT>[new_instance] = std::move(_isListening<SessionT>[from_inst]);
});
}
};
@ -343,7 +325,7 @@ public:
virtual void stop()
{
for (auto& func : _stopListenFunc) {
func();
func(this);
}
_stopListenFunc.clear();
@ -361,8 +343,8 @@ protected:
std::map<const typename SessionT::netsession_ident_t, bool>>
_isListening{};
std::vector<std::function<void()>> _stopListenFunc;
std::vector<std::function<void(const AdcGenericNetServer*)>> _moveCtorFunc;
std::vector<std::function<void(const AdcGenericNetServer*)>> _stopListenFunc;
std::vector<std::function<void(const AdcGenericNetServer*, const AdcGenericNetServer*)>> _moveCtorFunc;
server_ident_t _serverIdent;