rename DEFER to DEFER_HPP

This commit is contained in:
BlackMATov
2020-10-29 04:58:07 +07:00
parent 6ea555b98c
commit 9fbd7a34cf
4 changed files with 36 additions and 36 deletions

View File

@@ -51,7 +51,7 @@ target_link_libraries(your_project_target defer.hpp)
```cpp
if ( FILE *file = std::fopen("output.txt", "a") ) {
// defer will close the file after scope or on exception
DEFER([file]{ std::fclose(file); });
DEFER_HPP([file]{ std::fclose(file); });
const char buffer[] = "hello world\n";
if ( 12 != std::fwrite(buffer, sizeof(buffer[0]), std::strlen(buffer), file) ) {
@@ -65,10 +65,10 @@ if ( FILE *file = std::fopen("output.txt", "a") ) {
```cpp
if ( FILE *file = std::fopen("output.txt", "a") ) {
// defer will close the file after scope or on exception
DEFER([file]{ std::fclose(file); });
DEFER_HPP([file]{ std::fclose(file); });
// error defer will be called on exception
ERROR_DEFER([]{
ERROR_DEFER_HPP([]{
std::cerr << "there is something wrong" << std::endl;
});
@@ -84,10 +84,10 @@ if ( FILE *file = std::fopen("output.txt", "a") ) {
```cpp
if ( FILE *file = std::fopen("output.txt", "a") ) {
// defer will close the file after scope or on exception
DEFER([file]{ std::fclose(file); });
DEFER_HPP([file]{ std::fclose(file); });
// return defer will be called on successful scope exit
RETURN_DEFER([]{
RETURN_DEFER_HPP([]{
std::cout << "all is ok!" << std::endl;
});

View File

@@ -120,31 +120,31 @@ namespace defer_hpp
}
}
#define DEFER_IMPL_PP_CAT(x, y) DEFER_IMPL_PP_CAT_I(x, y)
#define DEFER_IMPL_PP_CAT_I(x, y) x ## y
#define DEFER_HPP_IMPL_PP_CAT(x, y) DEFER_HPP_IMPL_PP_CAT_I(x, y)
#define DEFER_HPP_IMPL_PP_CAT_I(x, y) x ## y
#ifdef __COUNTER__
#define DEFER(...)\
auto DEFER_IMPL_PP_CAT(generated_defer_, __COUNTER__) =\
#define DEFER_HPP(...)\
auto DEFER_HPP_IMPL_PP_CAT(generated_defer_, __COUNTER__) =\
::defer_hpp::make_defer(__VA_ARGS__)
#define ERROR_DEFER(...)\
auto DEFER_IMPL_PP_CAT(generated_error_defer_, __COUNTER__) =\
#define ERROR_DEFER_HPP(...)\
auto DEFER_HPP_IMPL_PP_CAT(generated_error_defer_, __COUNTER__) =\
::defer_hpp::make_error_defer(__VA_ARGS__)
#define RETURN_DEFER(...)\
auto DEFER_IMPL_PP_CAT(generated_return_defer_, __COUNTER__) =\
#define RETURN_DEFER_HPP(...)\
auto DEFER_HPP_IMPL_PP_CAT(generated_return_defer_, __COUNTER__) =\
::defer_hpp::make_return_defer(__VA_ARGS__)
#else
#define DEFER(...)\
auto DEFER_IMPL_PP_CAT(generated_defer_, __LINE__) =\
#define DEFER_HPP(...)\
auto DEFER_HPP_IMPL_PP_CAT(generated_defer_, __LINE__) =\
::defer_hpp::make_defer(__VA_ARGS__)
#define ERROR_DEFER(...)\
auto DEFER_IMPL_PP_CAT(generated_error_defer_, __LINE__) =\
#define ERROR_DEFER_HPP(...)\
auto DEFER_HPP_IMPL_PP_CAT(generated_error_defer_, __LINE__) =\
::defer_hpp::make_error_defer(__VA_ARGS__)
#define RETURN_DEFER(...)\
auto DEFER_IMPL_PP_CAT(generated_return_defer_, __LINE__) =\
#define RETURN_DEFER_HPP(...)\
auto DEFER_HPP_IMPL_PP_CAT(generated_return_defer_, __LINE__) =\
::defer_hpp::make_return_defer(__VA_ARGS__)
#endif

View File

@@ -17,7 +17,7 @@ TEST_CASE("examples") {
SECTION("basic_defer") {
if ( FILE *file = std::fopen("output.txt", "a") ) {
// defer will close the file after scope or on exception
DEFER([file]{ std::fclose(file); });
DEFER_HPP([file]{ std::fclose(file); });
const char buffer[] = "hello world\n";
if ( 12 != std::fwrite(buffer, sizeof(buffer[0]), std::strlen(buffer), file) ) {
@@ -29,10 +29,10 @@ TEST_CASE("examples") {
SECTION("error_defer") {
if ( FILE *file = std::fopen("output.txt", "a") ) {
// defer will close the file after scope or on exception
DEFER([file]{ std::fclose(file); });
DEFER_HPP([file]{ std::fclose(file); });
// error defer will be called on exception
ERROR_DEFER([]{
ERROR_DEFER_HPP([]{
std::cerr << "there is something wrong" << std::endl;
});
@@ -46,10 +46,10 @@ TEST_CASE("examples") {
SECTION("return_defer") {
if ( FILE *file = std::fopen("output.txt", "a") ) {
// defer will close the file after scope or on exception
DEFER([file]{ std::fclose(file); });
DEFER_HPP([file]{ std::fclose(file); });
// return defer will be called on successful scope exit
RETURN_DEFER([]{
RETURN_DEFER_HPP([]{
std::cout << "all is ok!" << std::endl;
});

View File

@@ -17,7 +17,7 @@ TEST_CASE("defer") {
SECTION("simple") {
int i = 0;
{
DEFER([&i]{ ++i; });
DEFER_HPP([&i]{ ++i; });
REQUIRE(i == 0);
}
REQUIRE(i == 1);
@@ -26,7 +26,7 @@ TEST_CASE("defer") {
SECTION("simple_with_arg") {
int i = 0;
{
DEFER([](int& i){ ++i; }, std::ref(i));
DEFER_HPP([](int& i){ ++i; }, std::ref(i));
REQUIRE(i == 0);
}
REQUIRE(i == 1);
@@ -35,7 +35,7 @@ TEST_CASE("defer") {
SECTION("simple_with_args") {
int i = 0, j = 0;
{
DEFER([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
DEFER_HPP([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
REQUIRE(i == 0);
REQUIRE(j == 0);
}
@@ -46,7 +46,7 @@ TEST_CASE("defer") {
SECTION("simple_with_exception") {
int i = 0;
try {
DEFER([&i]{ ++i; });
DEFER_HPP([&i]{ ++i; });
REQUIRE(i == 0);
throw std::exception();
} catch (...) {
@@ -59,7 +59,7 @@ TEST_CASE("error_defer") {
SECTION("simple") {
int i = 0;
{
ERROR_DEFER([&i]{ ++i; });
ERROR_DEFER_HPP([&i]{ ++i; });
REQUIRE(i == 0);
}
REQUIRE(i == 0);
@@ -68,7 +68,7 @@ TEST_CASE("error_defer") {
SECTION("simple_with_arg") {
int i = 0;
{
ERROR_DEFER([](int& i){ ++i; }, std::ref(i));
ERROR_DEFER_HPP([](int& i){ ++i; }, std::ref(i));
REQUIRE(i == 0);
}
REQUIRE(i == 0);
@@ -77,7 +77,7 @@ TEST_CASE("error_defer") {
SECTION("simple_with_args") {
int i = 0, j = 0;
{
ERROR_DEFER([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
ERROR_DEFER_HPP([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
REQUIRE(i == 0);
REQUIRE(j == 0);
}
@@ -88,7 +88,7 @@ TEST_CASE("error_defer") {
SECTION("simple_with_exception") {
int i = 0;
try {
ERROR_DEFER([&i]{ ++i; });
ERROR_DEFER_HPP([&i]{ ++i; });
REQUIRE(i == 0);
throw std::exception();
} catch (...) {
@@ -101,7 +101,7 @@ TEST_CASE("return_defer") {
SECTION("simple") {
int i = 0;
{
RETURN_DEFER([&i]{ ++i; });
RETURN_DEFER_HPP([&i]{ ++i; });
REQUIRE(i == 0);
}
REQUIRE(i == 1);
@@ -110,7 +110,7 @@ TEST_CASE("return_defer") {
SECTION("simple_with_arg") {
int i = 0;
{
RETURN_DEFER([](int& i){ ++i; }, std::ref(i));
RETURN_DEFER_HPP([](int& i){ ++i; }, std::ref(i));
REQUIRE(i == 0);
}
REQUIRE(i == 1);
@@ -119,7 +119,7 @@ TEST_CASE("return_defer") {
SECTION("simple_with_args") {
int i = 0, j = 0;
{
RETURN_DEFER([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
RETURN_DEFER_HPP([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
REQUIRE(i == 0);
REQUIRE(j == 0);
}
@@ -130,7 +130,7 @@ TEST_CASE("return_defer") {
SECTION("simple_with_exception") {
int i = 0;
try {
RETURN_DEFER([&i]{ ++i; });
RETURN_DEFER_HPP([&i]{ ++i; });
REQUIRE(i == 0);
throw std::exception();
} catch (...) {