Add defer helper.
This commit is contained in:
parent
e5740ccb32
commit
4f0ecc4506
@ -81,11 +81,11 @@ bool FileSystem::copyFile(const char * src, const char * dst) {
|
|||||||
|
|
||||||
FILE * fsrc = fileOpen(src, "rb");
|
FILE * fsrc = fileOpen(src, "rb");
|
||||||
if (fsrc == NULL) return false;
|
if (fsrc == NULL) return false;
|
||||||
NV_ON_RETURN(fclose(fsrc));
|
defer{ fclose(fsrc); };
|
||||||
|
|
||||||
FILE * fdst = fileOpen(dst, "wb");
|
FILE * fdst = fileOpen(dst, "wb");
|
||||||
if (fdst == NULL) return false;
|
if (fdst == NULL) return false;
|
||||||
NV_ON_RETURN(fclose(fdst));
|
defer{ fclose(fdst); };
|
||||||
|
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
size_t n;
|
size_t n;
|
||||||
|
@ -326,14 +326,13 @@ namespace nv {
|
|||||||
F f;
|
F f;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename F>
|
struct ExitScopeHelp {
|
||||||
ScopeExit<F> MakeScopeExit(F f) {
|
template<typename T>
|
||||||
return ScopeExit<F>(f);
|
ScopeExit<T> operator+(T t) { return t; }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#define NV_ON_RETURN(code) \
|
#define defer const auto& __attribute__((unused)) NV_STRING_JOIN2(defer__, __LINE__) = nv::ExitScopeHelp() + [&]()
|
||||||
auto NV_STRING_JOIN2(scope_exit_, __LINE__) = nv::MakeScopeExit([=](){code;})
|
|
||||||
|
|
||||||
|
|
||||||
// Indicate the compiler that the parameter is not used to suppress compier warnings.
|
// Indicate the compiler that the parameter is not used to suppress compier warnings.
|
||||||
|
Loading…
Reference in New Issue
Block a user