{"id":25523300,"url":"https://github.com/sjinks/scope-action-cpp","last_synced_at":"2025-07-25T09:09:56.821Z","repository":{"id":278367252,"uuid":"934585813","full_name":"sjinks/scope-action-cpp","owner":"sjinks","description":"Scope guard utilities for managing exit actions in C++","archived":false,"fork":false,"pushed_at":"2025-07-22T07:15:39.000Z","size":111,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-22T09:37:07.557Z","etag":null,"topics":["cpp","cpp20","header-only","raii","scope-exit","scope-fail","scope-guard","scope-success"],"latest_commit_sha":null,"homepage":"https://sjinks.github.io/scope-action-cpp/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sjinks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"custom":["https://www.paypal.com/donate/?hosted_button_id=SAG6877JDJ3KU","https://send.monobank.ua/jar/7rosVfiwKM"]}},"created_at":"2025-02-18T04:33:48.000Z","updated_at":"2025-07-22T07:15:41.000Z","dependencies_parsed_at":"2025-05-21T16:09:41.848Z","dependency_job_id":"7d48d535-6318-4fa9-b1b2-345395dc60f2","html_url":"https://github.com/sjinks/scope-action-cpp","commit_stats":null,"previous_names":["sjinks/scope-action-cpp"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/sjinks/scope-action-cpp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjinks%2Fscope-action-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjinks%2Fscope-action-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjinks%2Fscope-action-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjinks%2Fscope-action-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sjinks","download_url":"https://codeload.github.com/sjinks/scope-action-cpp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjinks%2Fscope-action-cpp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266983523,"owners_count":24016559,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","cpp20","header-only","raii","scope-exit","scope-fail","scope-guard","scope-success"],"created_at":"2025-02-19T19:18:40.399Z","updated_at":"2025-07-25T09:09:56.772Z","avatar_url":"https://github.com/sjinks.png","language":"C++","funding_links":["https://www.paypal.com/donate/?hosted_button_id=SAG6877JDJ3KU","https://send.monobank.ua/jar/7rosVfiwKM"],"categories":[],"sub_categories":[],"readme":"# Scope Action Utilities\n\n[![Build and Test](https://github.com/sjinks/scope-action-cpp/actions/workflows/ci.yml/badge.svg)](https://github.com/sjinks/scope-action-cpp/actions/workflows/ci.yml)\n[![CodeQL](https://github.com/sjinks/scope-action-cpp/actions/workflows/codeql.yml/badge.svg)](https://github.com/sjinks/scope-action-cpp/actions/workflows/codeql.yml)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=sjinks_scope-action-cpp\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=sjinks_scope-action-cpp)\n\n## Overview\n\nThis project provides a set of scope guard utilities for managing exit actions in C++. These utilities ensure that specified actions are executed when a scope is exited, regardless of how the exit occurs. The scope guards include:\n\n- `exit_action`: Executes an action when the scope is exited.\n- `fail_action`: Executes an action when the scope is exited due to an exception.\n- `success_action`: Executes an action when the scope is exited normally.\n\nThese utilities are useful for ensuring that resources are properly released or actions are taken when a scope is exited.\n\n## Features\n\n- **exit_action**: Calls its exit function on destruction, when a scope is exited.\n- **fail_action**: Calls its exit function when a scope is exited via an exception.\n- **success_action**: Calls its exit function when a scope is exited normally.\n\n## Usage\n\n### Example Usage\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cscope_action.h\u003e\n\nvoid maybe_throw(bool should_throw)\n{\n    if (should_throw) {\n        throw std::runtime_error(\"An error occurred\");\n    }\n}\n\nint main()\n{\n    bool exit_status = false;\n    bool did_throw = false;\n\n    auto func = [\u0026exit_status]() { exit_status = true; };\n\n    // Using exit_action: runs on scope exit (success or exception)\n    try {\n        auto guard = wwa::utils::exit_action(func);\n        maybe_throw(false);\n    } catch (...) {\n        did_throw = true;\n    }\n    std::cout \u003c\u003c \"exit_action: \" \u003c\u003c exit_status \u003c\u003c \", did_throw: \" \u003c\u003c did_throw \u003c\u003c std::endl;\n\n    exit_status = did_throw = false;\n\n    // Using fail_action: runs only if an exception occurs\n    try {\n        auto guard = wwa::utils::fail_action(func);\n        maybe_throw(true);\n    } catch (...) {\n        did_throw = true;\n    }\n    std::cout \u003c\u003c \"fail_action: \" \u003c\u003c exit_status \u003c\u003c \", did_throw: \" \u003c\u003c did_throw \u003c\u003c std::endl;\n\n    exit_status = did_throw = false;\n\n    // Using success_action: runs only if no exception occurs\n    try {\n        auto guard = wwa::utils::success_action(func);\n        maybe_throw(false);\n    } catch (...) {\n        did_throw = true;\n    }\n    std::cout \u003c\u003c \"success_action: \" \u003c\u003c exit_status \u003c\u003c \", did_throw: \" \u003c\u003c did_throw \u003c\u003c std::endl;\n\n    return 0;\n}\n```\n\n## API Documentation\n\nThe documentation is available at [https://sjinks.github.io/scope-action-cpp/](https://sjinks.github.io/scope-action-cpp/).\n\n### `exit_action`\n\nA scope guard that calls its exit function on destruction, when a scope is exited.\n\n```cpp\ntemplate\u003ctypename ExitFunc\u003e\nclass [[nodiscard]] exit_action {\npublic:\n    template\u003ctypename Func\u003e\n    explicit exit_action(Func\u0026\u0026 fn)\n    noexcept(std::is_nothrow_constructible_v\u003cExitFunc, Func\u003e || std::is_nothrow_constructible_v\u003cExitFunc, Func\u0026\u003e);\n\n    exit_action(exit_action\u0026\u0026 other)\n    noexcept(std::is_nothrow_move_constructible_v\u003cExitFunc\u003e || std::is_nothrow_copy_constructible_v\u003cExitFunc\u003e);\n\n    ~exit_action() noexcept;\n\n    void release() noexcept;\n};\n```\n\n### `fail_action`\n\nA scope guard that calls its exit function when a scope is exited via an exception.\n\n```cpp\ntemplate\u003ctypename ExitFunc\u003e\nclass [[nodiscard]] fail_action {\npublic:\n    template\u003ctypename Func\u003e\n    explicit fail_action(Func\u0026\u0026 fn)\n    noexcept(std::is_nothrow_constructible_v\u003cExitFunc, Func\u003e || std::is_nothrow_constructible_v\u003cExitFunc, Func\u0026\u003e);\n\n    fail_action(fail_action\u0026\u0026 other)\n    noexcept(std::is_nothrow_move_constructible_v\u003cExitFunc\u003e || std::is_nothrow_copy_constructible_v\u003cExitFunc\u003e);\n\n    ~fail_action() noexcept;\n\n    void release() noexcept;\n};\n```\n\n### `success_action`\n\nA scope guard that calls its exit function when a scope is exited normally.\n\n```cpp\ntemplate\u003ctypename ExitFunc\u003e\nclass [[nodiscard]] success_action {\npublic:\n    template\u003ctypename Func\u003e\n    explicit success_action(Func\u0026\u0026 fn)\n    noexcept(std::is_nothrow_constructible_v\u003cExitFunc, Func\u003e || std::is_nothrow_constructible_v\u003cExitFunc, Func\u0026\u003e);\n\n    success_action(success_action\u0026\u0026 other)\n    noexcept(std::is_nothrow_move_constructible_v\u003cExitFunc\u003e || std::is_nothrow_copy_constructible_v\u003cExitFunc\u003e);\n\n    ~success_action() noexcept(noexcept(this-\u003em_exit_function()));\n\n    void release() noexcept;\n};\n```\n\n## Building and Testing\n\n### Prerequisites\n\n  * C++20 compatible compiler;\n  * CMake;\n  * Google Test (for running tests).\n\n### Installation\n\n```sh\ncmake -B build\ncmake --build build\nsudo cmake --install build\n```\n\n#### Configuration Options\n\n| Option                  | Description                                                               | Default |\n|-------------------------|---------------------------------------------------------------------------|---------|\n| `BUILD_TESTS`           | Build tests                                                               | `ON`    |\n| `BUILD_EXAMPLES`        | Build examples                                                            | `ON`    |\n| `BUILD_DOCS`            | Build documentation                                                       | `ON`    |\n| `BUILD_INTERNAL_DOCS`   | Build internal documentation                                              | `OFF`   |\n| `ENABLE_MAINTAINER_MODE`| Maintainer mode (enable more compiler warnings, treat warnings as errors) | `OFF`   |\n| `USE_CLANG_TIDY`        | Use `clang-tidy` during build                                             | `OFF`   |\n\nThe `BUILD_DOCS` (public API documentation) and `BUILD_INTERNAL_DOCS` (public and private API documentation) require [Doxygen](https://www.doxygen.nl/)\nand, optionally, `dot` (a part of [Graphviz](https://graphviz.org/)).\n\nThe `USE_CLANG_TIDY` option requires [`clang-tidy`](https://clang.llvm.org/extra/clang-tidy/).\n\n#### Build Types\n\n| Build Type       | Description                                                                     |\n|------------------|---------------------------------------------------------------------------------|\n| `Debug`          | Build with debugging information and no optimization.                           |\n| `Release`        | Build with optimization for maximum performance and no debugging information.   |\n| `RelWithDebInfo` | Build with optimization and include debugging information.                      |\n| `MinSizeRel`     | Build with optimization for minimum size.                                       |\n| `ASAN`           | Build with AddressSanitizer enabled for detecting memory errors.                |\n| `LSAN`           | Build with LeakSanitizer enabled for detecting memory leaks.                    |\n| `UBSAN`          | Build with UndefinedBehaviorSanitizer enabled for detecting undefined behavior. |\n| `TSAN`           | Build with ThreadSanitizer enabled for detecting data races.                    |\n| `Coverage`       | Build with code coverage analysis enabled.                                      |\n\nASAN, LSAN, UBSAN, TSAN, and Coverage builds are only supported with GCC or clang.\n\nCoverage build requires `gcov` (GCC) or `llvm-gcov` (clang) and [`gcovr`](https://gcovr.com/en/stable/).\n\n### Running Tests\n\nTo run the tests, use the following command:\n\n```sh\nctest -T test --test-dir build/test\n```\n\nor run the following binary:\n\n```sh\n./build/test/test_scope_action\n```\n\nThe test binary uses [Google Test](http://google.github.io/googletest/) library.\nIts behavior [can be controlled](http://google.github.io/googletest/advanced.html#running-test-programs-advanced-options)\nvia environment variables and/or command line flags.\n\nRun `test_scope_action --help` for the list of available options.\n\n## License\n\nThis project is licensed under the MIT License.\n\n## Acknowledgements\n\nThis project is inspired by the scope guard utilities from the (experimental) [Version 3 of the C++ Extensions for Library Fundamentals](https://en.cppreference.com/w/cpp/experimental/lib_extensions_3) and the [Guidelines Support Library (GSL)](https://github.com/microsoft/GSL).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsjinks%2Fscope-action-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsjinks%2Fscope-action-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsjinks%2Fscope-action-cpp/lists"}