Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change receivers to share_ptr #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/net/include/scy/net/socketadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ class Net_API SocketAdapter

struct Ref
{
typedef std::shared_ptr<Ref> ptr_t;

SocketAdapter* ptr;
bool alive;
};

SocketAdapter* _sender;
std::vector<Ref*> _receivers;
std::vector<Ref::ptr_t> _receivers;
bool _dirty = false;
};

Expand Down
11 changes: 7 additions & 4 deletions src/net/src/socketadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ void SocketAdapter::addReceiver(SocketAdapter* adapter)
// Note that we insert new adapters in the back of the queue,
// and iterate in reverse to ensure calling order is preserved.
_dirty = true;
_receivers.push_back(new Ref{ adapter, true });
auto ptrRef = std::make_shared<Ref>();
ptrRef->ptr = adapter;
ptrRef->alive = true;
_receivers.push_back(ptrRef);
// _receivers.insert(_receivers.begin(), new Ref{ adapter, false }); // insert front
// std::sort(_receivers.begin(), _receivers.end(),
// [](SocketAdapter const& l, SocketAdapter const& r) {
Expand All @@ -204,7 +207,7 @@ void SocketAdapter::removeReceiver(SocketAdapter* adapter)
{
assert(adapter != this);
auto it = std::find_if(_receivers.begin(), _receivers.end(),
[&](const Ref* ref) { return ref->ptr == adapter; });
[&](const Ref::ptr_t& ref) { return ref->ptr == adapter; });
if (it != _receivers.end()) { (*it)->alive = false; }
}

Expand All @@ -215,7 +218,7 @@ void SocketAdapter::cleanupReceivers()
for (auto it = _receivers.begin(); it != _receivers.end();) {
auto ref = *it;
if (!ref->alive) {
delete ref;
//delete ref;
it = _receivers.erase(it);
}
else ++it;
Expand All @@ -234,7 +237,7 @@ std::vector<SocketAdapter*> SocketAdapter::receivers()
{
std::vector<SocketAdapter*> items;
std::transform(_receivers.begin(), _receivers.end(), std::back_inserter(items),
[](const Ref* ref) { return ref->ptr; });
[](const Ref::ptr_t& ref) { return ref->ptr; });
return items;
}

Expand Down