mirror of
https://github.com/NixOS/nix.git
synced 2025-12-17 14:31:06 +01:00
Move /src to /subprojects
This will facilitate breaking up Nix into multiple packages for each component with Meson.
This commit is contained in:
parent
4db9487823
commit
84e2963f8e
737 changed files with 504 additions and 505 deletions
47
subprojects/libutil/finally.hh
Normal file
47
subprojects/libutil/finally.hh
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include <utility>
|
||||
#include <cassert>
|
||||
#include <exception>
|
||||
|
||||
/**
|
||||
* A trivial class to run a function at the end of a scope.
|
||||
*/
|
||||
template<typename Fn>
|
||||
class [[nodiscard("Finally values must be used")]] Finally
|
||||
{
|
||||
private:
|
||||
Fn fun;
|
||||
bool movedFrom = false;
|
||||
|
||||
public:
|
||||
Finally(Fn fun) : fun(std::move(fun)) { }
|
||||
// Copying Finallys is definitely not a good idea and will cause them to be
|
||||
// called twice.
|
||||
Finally(Finally &other) = delete;
|
||||
Finally(Finally &&other) : fun(std::move(other.fun)) {
|
||||
other.movedFrom = true;
|
||||
}
|
||||
~Finally() noexcept(false)
|
||||
{
|
||||
try {
|
||||
if (!movedFrom)
|
||||
fun();
|
||||
} catch (...) {
|
||||
// finally may only throw an exception if exception handling is not already
|
||||
// in progress. if handling *is* in progress we have to return cleanly here
|
||||
// but are still prohibited from doing so since eating the exception would,
|
||||
// in almost all cases, mess up error handling even more. the only good way
|
||||
// to handle this is to abort entirely and leave a message, so we'll assert
|
||||
// (and rethrow anyway, just as a defense against possible NASSERT builds.)
|
||||
if (std::uncaught_exceptions()) {
|
||||
assert(false &&
|
||||
"Finally function threw an exception during exception handling. "
|
||||
"this is not what you want, please use some other methods (like "
|
||||
"std::promise or async) instead.");
|
||||
}
|
||||
throw;
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue