|
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
|
@@ -489,14 +495,37 @@ class BufferValue : public MaybeStackBuffer<char> {
|
489 | 495 | // silence a compiler warning about that.
|
490 | 496 | template <typename T> inline void USE(T&&) {}
|
491 | 497 |
|
492 |
| -// Run a function when exiting the current scope. |
493 |
| -struct OnScopeLeave { |
494 |
| - std::function<void()> fn_; |
| 498 | +template <typename Fn> |
| 499 | +struct OnScopeLeaveImpl { |
| 500 | + Fn fn_; |
| 501 | + bool active_; |
| 502 | + |
| 503 | + explicit OnScopeLeaveImpl(Fn&& fn) : fn_(std::move(fn)), active_(true) {} |
| 504 | + ~OnScopeLeaveImpl() { if (active_) fn_(); } |
495 | 505 |
|
496 |
| - explicit OnScopeLeave(std::function<void()> fn) : fn_(std::move(fn)) {} |
497 |
| - ~OnScopeLeave() { fn_(); } |
| 506 | + OnScopeLeaveImpl(const OnScopeLeaveImpl& other) = delete; |
| 507 | + OnScopeLeaveImpl& operator=(const OnScopeLeaveImpl& other) = delete; |
| 508 | + OnScopeLeaveImpl(OnScopeLeaveImpl&& other) |
| 509 | + : fn_(std::move(other.fn_)), active_(other.active_) { |
| 510 | + other.active_ = false; |
| 511 | + } |
| 512 | + OnScopeLeaveImpl& operator=(OnScopeLeaveImpl&& other) { |
| 513 | + if (this == &other) return *this; |
| 514 | + this->~OnScopeLeave(); |
| 515 | + new (this)OnScopeLeaveImpl(std::move(other)); |
| 516 | + return *this; |
| 517 | + } |
498 | 518 | };
|
499 | 519 |
|
| 520 | +// Run a function when exiting the current scope. Used like this: |
| 521 | +// auto on_scope_leave = OnScopeLeave([&] { |
| 522 | +// // ... run some code ... |
| 523 | +// }); |
| 524 | +template <typename Fn> |
| 525 | +inline MUST_USE_RESULT OnScopeLeaveImpl<Fn> OnScopeLeave(Fn&& fn) { |
| 526 | + return OnScopeLeaveImpl<Fn>{std::move(fn)}; |
| 527 | +} |
| 528 | + |
500 | 529 | // Simple RAII wrapper for contiguous data that uses malloc()/free().
|
501 | 530 | template <typename T>
|
502 | 531 | struct MallocedBuffer {
|
@@ -674,12 +703,6 @@ constexpr T RoundUp(T a, T b) {
|
674 | 703 | return a % b != 0 ? a + b - (a % b) : a;
|
675 | 704 | }
|
676 | 705 |
|
677 |
| -#ifdef __GNUC__ |
678 |
| -#define MUST_USE_RESULT __attribute__((warn_unused_result)) |
679 |
| -#else |
680 |
| -#define MUST_USE_RESULT |
681 |
| -#endif |
682 |
| - |
683 | 706 | class SlicedArguments : public MaybeStackBuffer<v8::Local<v8::Value>> {
|
684 | 707 | public:
|
685 | 708 | inline explicit SlicedArguments(
|
|
0 commit comments