Last active
May 24, 2024 01:29
-
-
Save p2004a/045726d70a490d12ad62 to your computer and use it in GitHub Desktop.
Simple defer macro for c++
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
// SPDX-FileCopyrightText: 2015 Marek Rusinowski | |
// SPDX-License-Identifier: MIT | |
#include <memory> | |
#include <cstdio> | |
template<typename F> | |
class defer_finalizer { | |
F f; | |
bool moved; | |
public: | |
template<typename T> | |
defer_finalizer(T && f_) : f(std::forward<T>(f_)), moved(false) { } | |
defer_finalizer(const defer_finalizer &) = delete; | |
defer_finalizer(defer_finalizer && other) : f(std::move(other.f)), moved(other.moved) { | |
other.moved = true; | |
} | |
~defer_finalizer() { | |
if (!moved) f(); | |
} | |
}; | |
struct { | |
template<typename F> | |
defer_finalizer<F> operator<<(F && f) { | |
return defer_finalizer<F>(std::forward<F>(f)); | |
} | |
} deferrer; | |
#define TOKENPASTE(x, y) x ## y | |
#define TOKENPASTE2(x, y) TOKENPASTE(x, y) | |
#define defer auto TOKENPASTE2(__deferred_lambda_call, __COUNTER__) = deferrer << [&] | |
void f() { | |
FILE *f = fopen("asdasd", "r"); | |
defer { | |
fclose(f); | |
}; | |
} | |
// Compiler optimizes it nicely: http://goo.gl/XAGl6A |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dolmens I know about 2 ways:
Add
static
to thedeferrer
singleton as you suggest.and then have a single c++ file with
deferrer deferrer_object;
The 1 is cleaner IMHO ;)