Skip to content

Commit

Permalink
Convert wait into member function
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier committed Jan 27, 2025
1 parent ac10a0b commit 0affa28
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
14 changes: 6 additions & 8 deletions cpp/include/Ice/CtrlCHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ namespace Ice
/// Obtains the current signal callback.
/// @return The callback.
[[nodiscard]] CtrlCHandlerCallback getCallback() const;
};

/// Waits until the CtrlC-C handler catches a signal.
/// @param handler The Ctrl-C handler.
/// @return The signal number that was caught.
/// @remark This function installs a callback in the CtrlCHandler object. It must not be called when a non-null
/// callback is already installed.
/// \headerfile Ice/Ice.h
ICE_API int wait(CtrlCHandler& handler);
/// Waits until this handler catches a signal.
/// @return The signal number that was caught.
/// @remark This function installs a signal callback. It must not be called when a non-null callback is already
/// installed.
int wait();
};
}

#endif
10 changes: 5 additions & 5 deletions cpp/src/Ice/CtrlCHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,20 @@ CtrlCHandler::~CtrlCHandler()
#endif

int
Ice::wait(CtrlCHandler& handler)
CtrlCHandler::wait()
{
promise<int> promise;

CtrlCHandlerCallback oldCallback = handler.setCallback(
[&promise, &handler](int sig)
CtrlCHandlerCallback oldCallback = setCallback(
[&promise, this](int sig)
{
handler.setCallback(nullptr); // ignore further signals
setCallback(nullptr); // ignore further signals
promise.set_value(sig);
});

if (oldCallback)
{
handler.setCallback(oldCallback);
setCallback(oldCallback);
throw Ice::LocalException{__FILE__, __LINE__, "do not call wait on a CtrlCHandler with a registered callback"};
}

Expand Down

0 comments on commit 0affa28

Please sign in to comment.