Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

Implementation of *Scope Guards* (`scope_exit`, `scope_success`, `scope_fail`) and `unique_resource` as proposed in [**P0052R10**][1].

- **`scope_exit`:** Executes a callable when the current scope is left
- **`scope_success`:** Executes a callable only if the scope exits normally (no exception)
- **`scope_fail`:** Executes a callable only if an exception is thrown during the scope
- **`unique_resource`:** RAII wrapper that owns a resource and calls a deleter on destruction or reset
- **`make_unique_resource_checked()`:** Helper that creates a `unique_resource` and releases it if the resource equals an invalid value


## Compatibility

Expand All @@ -30,6 +36,96 @@ The filenames contain a `.h` extension. To enable the compatible header as speci
- [*N4853 – C++ Extensions for Library Fundamentals, Version 3*][2] (T. Köppe)


## Usage Examples

### scope_exit

```cpp
#include "scope_exit.h"

void example() {
sr::scope_exit guard([] {
std::cout << "Cleanup executed when scope exits\n";
});

// Do work ...
}
```

### scope_success

```cpp
#include "scope_success.h"

void example() {
sr::scope_success guard{[] {
std::cout << "Success: no exception was thrown\n";
}};

// Do work that might throw ...
}
```

### scope_fail

```cpp
#include "scope_fail.h"

void example() {
sr::scope_fail guard{[] {
std::cout << "Failure: an exception was thrown\n";
}};

// do work that might throw ...
}
```

### unique_resource

```cpp
#include "unique_resource.h"

class Resource {
// ...
};

void example() {
sr::unique_resource resource{
Resource{"Example resource"},
[](Resource r) {
// Delete r
std::cout << "Resource deleted\n";
}};

auto& res = resource.get();
// Use resource ...
}
```

### make_unique_resource_checked()

```cpp
#include "unique_resource.h"

class Resource {
// ...
};

void example() {
auto resource = sr::make_unique_resource_checked(
Resource{"invalid"}, // invalid value
[](Resource r) {
// Delete r
std::cout << "Resource deleted\n";
});
// Resource is released if it equals the invalid value

auto& res = resource.get();
// Use resource ...
}
```


## License

**MIT License**
Expand Down
Loading