Add defer helper.

pull/310/head
Ignacio 4 years ago
parent e5740ccb32
commit 4f0ecc4506

@ -81,11 +81,11 @@ bool FileSystem::copyFile(const char * src, const char * dst) {
FILE * fsrc = fileOpen(src, "rb");
if (fsrc == NULL) return false;
NV_ON_RETURN(fclose(fsrc));
defer{ fclose(fsrc); };
FILE * fdst = fileOpen(dst, "wb");
if (fdst == NULL) return false;
NV_ON_RETURN(fclose(fdst));
defer{ fclose(fdst); };
char buffer[1024];
size_t n;

@ -326,14 +326,13 @@ namespace nv {
F f;
};
template <typename F>
ScopeExit<F> MakeScopeExit(F f) {
return ScopeExit<F>(f);
};
struct ExitScopeHelp {
template<typename T>
ScopeExit<T> operator+(T t) { return t; }
};
}
#define NV_ON_RETURN(code) \
auto NV_STRING_JOIN2(scope_exit_, __LINE__) = nv::MakeScopeExit([=](){code;})
#define defer const auto& __attribute__((unused)) NV_STRING_JOIN2(defer__, __LINE__) = nv::ExitScopeHelp() + [&]()
// Indicate the compiler that the parameter is not used to suppress compier warnings.

Loading…
Cancel
Save