mirror of
https://github.com/BlackMATov/defer.hpp.git
synced 2025-12-16 22:19:19 +07:00
fix CI compilation
This commit is contained in:
18
README.md
18
README.md
@@ -49,12 +49,12 @@ target_link_libraries(your_project_target defer.hpp)
|
|||||||
### Basic Defer
|
### Basic Defer
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
if ( FILE *file = ::fopen("output.txt", "a") ) {
|
if ( FILE *file = std::fopen("output.txt", "a") ) {
|
||||||
// defer will close the file after scope or on exception
|
// defer will close the file after scope or on exception
|
||||||
DEFER([file]{ ::fclose(file); });
|
DEFER([file]{ std::fclose(file); });
|
||||||
|
|
||||||
const char buffer[] = "hello world\n";
|
const char buffer[] = "hello world\n";
|
||||||
if ( 12 != ::fwrite(buffer, sizeof(buffer[0]), ::strlen(buffer), file) ) {
|
if ( 12 != std::fwrite(buffer, sizeof(buffer[0]), std::strlen(buffer), file) ) {
|
||||||
throw std::runtime_error("some exception");
|
throw std::runtime_error("some exception");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,9 +63,9 @@ if ( FILE *file = ::fopen("output.txt", "a") ) {
|
|||||||
### Error Defer
|
### Error Defer
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
if ( FILE *file = ::fopen("output.txt", "a") ) {
|
if ( FILE *file = std::fopen("output.txt", "a") ) {
|
||||||
// defer will close the file after scope or on exception
|
// defer will close the file after scope or on exception
|
||||||
DEFER([file]{ ::fclose(file); });
|
DEFER([file]{ std::fclose(file); });
|
||||||
|
|
||||||
// error defer will be called on exception
|
// error defer will be called on exception
|
||||||
ERROR_DEFER([]{
|
ERROR_DEFER([]{
|
||||||
@@ -73,7 +73,7 @@ if ( FILE *file = ::fopen("output.txt", "a") ) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const char buffer[] = "hello world\n";
|
const char buffer[] = "hello world\n";
|
||||||
if ( 12 != ::fwrite(buffer, sizeof(buffer[0]), ::strlen(buffer), file) ) {
|
if ( 12 != std::fwrite(buffer, sizeof(buffer[0]), std::strlen(buffer), file) ) {
|
||||||
throw std::runtime_error("some exception");
|
throw std::runtime_error("some exception");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,9 +82,9 @@ if ( FILE *file = ::fopen("output.txt", "a") ) {
|
|||||||
### Return Defer
|
### Return Defer
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
if ( FILE *file = ::fopen("output.txt", "a") ) {
|
if ( FILE *file = std::fopen("output.txt", "a") ) {
|
||||||
// defer will close the file after scope or on exception
|
// defer will close the file after scope or on exception
|
||||||
DEFER([file]{ ::fclose(file); });
|
DEFER([file]{ std::fclose(file); });
|
||||||
|
|
||||||
// return defer will be called on successful scope exit
|
// return defer will be called on successful scope exit
|
||||||
RETURN_DEFER([]{
|
RETURN_DEFER([]{
|
||||||
@@ -92,7 +92,7 @@ if ( FILE *file = ::fopen("output.txt", "a") ) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const char buffer[] = "hello world\n";
|
const char buffer[] = "hello world\n";
|
||||||
if ( 12 != ::fwrite(buffer, sizeof(buffer[0]), ::strlen(buffer), file) ) {
|
if ( 12 != std::fwrite(buffer, sizeof(buffer[0]), std::strlen(buffer), file) ) {
|
||||||
throw std::runtime_error("some exception");
|
throw std::runtime_error("some exception");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,26 +7,29 @@
|
|||||||
#define CATCH_CONFIG_FAST_COMPILE
|
#define CATCH_CONFIG_FAST_COMPILE
|
||||||
#include <catch2/catch.hpp>
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <defer.hpp/defer.hpp>
|
#include <defer.hpp/defer.hpp>
|
||||||
|
|
||||||
TEST_CASE("examples") {
|
TEST_CASE("examples") {
|
||||||
SECTION("basic_defer") {
|
SECTION("basic_defer") {
|
||||||
if ( FILE *file = ::fopen("output.txt", "a") ) {
|
if ( FILE *file = std::fopen("output.txt", "a") ) {
|
||||||
// defer will close the file after scope or on exception
|
// defer will close the file after scope or on exception
|
||||||
DEFER([file]{ ::fclose(file); });
|
DEFER([file]{ std::fclose(file); });
|
||||||
|
|
||||||
const char buffer[] = "hello world\n";
|
const char buffer[] = "hello world\n";
|
||||||
if ( 12 != ::fwrite(buffer, sizeof(buffer[0]), ::strlen(buffer), file) ) {
|
if ( 12 != std::fwrite(buffer, sizeof(buffer[0]), std::strlen(buffer), file) ) {
|
||||||
throw std::runtime_error("some exception");
|
throw std::runtime_error("some exception");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("error_defer") {
|
SECTION("error_defer") {
|
||||||
if ( FILE *file = ::fopen("output.txt", "a") ) {
|
if ( FILE *file = std::fopen("output.txt", "a") ) {
|
||||||
// defer will close the file after scope or on exception
|
// defer will close the file after scope or on exception
|
||||||
DEFER([file]{ ::fclose(file); });
|
DEFER([file]{ std::fclose(file); });
|
||||||
|
|
||||||
// error defer will be called on exception
|
// error defer will be called on exception
|
||||||
ERROR_DEFER([]{
|
ERROR_DEFER([]{
|
||||||
@@ -34,16 +37,16 @@ TEST_CASE("examples") {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const char buffer[] = "hello world\n";
|
const char buffer[] = "hello world\n";
|
||||||
if ( 12 != ::fwrite(buffer, sizeof(buffer[0]), ::strlen(buffer), file) ) {
|
if ( 12 != std::fwrite(buffer, sizeof(buffer[0]), std::strlen(buffer), file) ) {
|
||||||
throw std::runtime_error("some exception");
|
throw std::runtime_error("some exception");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("return_defer") {
|
SECTION("return_defer") {
|
||||||
if ( FILE *file = ::fopen("output.txt", "a") ) {
|
if ( FILE *file = std::fopen("output.txt", "a") ) {
|
||||||
// defer will close the file after scope or on exception
|
// defer will close the file after scope or on exception
|
||||||
DEFER([file]{ ::fclose(file); });
|
DEFER([file]{ std::fclose(file); });
|
||||||
|
|
||||||
// return defer will be called on successful scope exit
|
// return defer will be called on successful scope exit
|
||||||
RETURN_DEFER([]{
|
RETURN_DEFER([]{
|
||||||
@@ -51,7 +54,7 @@ TEST_CASE("examples") {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const char buffer[] = "hello world\n";
|
const char buffer[] = "hello world\n";
|
||||||
if ( 12 != ::fwrite(buffer, sizeof(buffer[0]), ::strlen(buffer), file) ) {
|
if ( 12 != std::fwrite(buffer, sizeof(buffer[0]), std::strlen(buffer), file) ) {
|
||||||
throw std::runtime_error("some exception");
|
throw std::runtime_error("some exception");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user