|
42 | 42 | #include <unordered_map>
|
43 | 43 | #include <utility>
|
44 | 44 |
|
| 45 | +#ifdef __GNUC__ |
| 46 | +#define MUST_USE_RESULT __attribute__((warn_unused_result)) |
| 47 | +#else |
| 48 | +#define MUST_USE_RESULT |
| 49 | +#endif |
| 50 | + |
45 | 51 | namespace node {
|
46 | 52 |
|
47 | 53 | // Maybe remove kPathSeparator when cpp17 is ready
|
@@ -494,14 +500,37 @@ class BufferValue : public MaybeStackBuffer<char> {
|
494 | 500 | // silence a compiler warning about that.
|
495 | 501 | template <typename T> inline void USE(T&&) {}
|
496 | 502 |
|
497 |
| -// Run a function when exiting the current scope. |
498 |
| -struct OnScopeLeave { |
499 |
| - std::function<void()> fn_; |
| 503 | +template <typename Fn> |
| 504 | +struct OnScopeLeaveImpl { |
| 505 | + Fn fn_; |
| 506 | + bool active_; |
| 507 | + |
| 508 | + explicit OnScopeLeaveImpl(Fn&& fn) : fn_(std::move(fn)), active_(true) {} |
| 509 | + ~OnScopeLeaveImpl() { if (active_) fn_(); } |
500 | 510 |
|
501 |
| - explicit OnScopeLeave(std::function<void()> fn) : fn_(std::move(fn)) {} |
502 |
| - ~OnScopeLeave() { fn_(); } |
| 511 | + OnScopeLeaveImpl(const OnScopeLeaveImpl& other) = delete; |
| 512 | + OnScopeLeaveImpl& operator=(const OnScopeLeaveImpl& other) = delete; |
| 513 | + OnScopeLeaveImpl(OnScopeLeaveImpl&& other) |
| 514 | + : fn_(std::move(other.fn_)), active_(other.active_) { |
| 515 | + other.active_ = false; |
| 516 | + } |
| 517 | + OnScopeLeaveImpl& operator=(OnScopeLeaveImpl&& other) { |
| 518 | + if (this == &other) return *this; |
| 519 | + this->~OnScopeLeave(); |
| 520 | + new (this)OnScopeLeaveImpl(std::move(other)); |
| 521 | + return *this; |
| 522 | + } |
503 | 523 | };
|
504 | 524 |
|
| 525 | +// Run a function when exiting the current scope. Used like this: |
| 526 | +// auto on_scope_leave = OnScopeLeave([&] { |
| 527 | +// // ... run some code ... |
| 528 | +// }); |
| 529 | +template <typename Fn> |
| 530 | +inline MUST_USE_RESULT OnScopeLeaveImpl<Fn> OnScopeLeave(Fn&& fn) { |
| 531 | + return OnScopeLeaveImpl<Fn>{std::move(fn)}; |
| 532 | +} |
| 533 | + |
505 | 534 | // Simple RAII wrapper for contiguous data that uses malloc()/free().
|
506 | 535 | template <typename T>
|
507 | 536 | struct MallocedBuffer {
|
@@ -679,12 +708,6 @@ constexpr T RoundUp(T a, T b) {
|
679 | 708 | return a % b != 0 ? a + b - (a % b) : a;
|
680 | 709 | }
|
681 | 710 |
|
682 |
| -#ifdef __GNUC__ |
683 |
| -#define MUST_USE_RESULT __attribute__((warn_unused_result)) |
684 |
| -#else |
685 |
| -#define MUST_USE_RESULT |
686 |
| -#endif |
687 |
| - |
688 | 711 | class SlicedArguments : public MaybeStackBuffer<v8::Local<v8::Value>> {
|
689 | 712 | public:
|
690 | 713 | inline explicit SlicedArguments(
|
|
0 commit comments