forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AK: Add a ScopeGuard helper that invokes a callback when destroyed.
This is useful when you want to ensure some little thing happens when you exit a certain scope. This patch makes use of it in LibC's netdb code to make sure we close the connection to the LookupServer.
- Loading branch information
1 parent
4edc73a
commit 69a6ce9
Showing
2 changed files
with
40 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
namespace AK { | ||
|
||
template<typename Callback> | ||
class ScopeGuard { | ||
public: | ||
ScopeGuard(Callback callback) | ||
: m_callback(move(callback)) | ||
{ | ||
} | ||
|
||
~ScopeGuard() | ||
{ | ||
m_callback(); | ||
} | ||
|
||
private: | ||
Callback m_callback; | ||
}; | ||
|
||
} | ||
|
||
using AK::ScopeGuard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters