refactored code

This commit is contained in:
Emiel Bruijntjes 2018-06-10 20:46:11 +02:00
parent a3a6a27efc
commit 2430da76c3
1 changed files with 19 additions and 12 deletions

View File

@ -66,22 +66,29 @@ void TcpConnection::process(int fd, int flags)
// monitor the object for destruction, because you never know what the user // monitor the object for destruction, because you never know what the user
Monitor monitor(this); Monitor monitor(this);
// store the old state
auto *oldstate = _state.get();
// pass on the the state, that returns a new impl // pass on the the state, that returns a new impl
auto *result = _state->process(monitor, fd, flags); auto *newstate = _state->process(monitor, fd, flags);
// are we still valid // if the state did not change, we do not have to update a member,
if (!monitor.valid() && result != _state.get()) // when the newstate is nullptr, the object is (being) destructed
// and we do not have to do anything else either
if (oldstate == newstate || newstate == nullptr) return;
// in a bizarre set of circumstances, the user may have implemented the
// handler in such a way that the connection object was destructed
if (!monitor.valid())
{ {
delete result; // ok, user code is weird, connection object no longer exist, get rid of the state too
return; delete newstate;
}
else
{
// replace it with the new implementation
_state.reset(newstate);
} }
// skip if the same state is continued to be used, or when the process()
// method returns nullptr (which only happens when the object is destructed,
// and "this" is no longer valid)
if (!result || result == _state.get()) return;
// replace it with the new implementation
_state.reset(result);
} }
/** /**