{"id":13729528,"url":"https://github.com/Neargye/scope_guard","last_synced_at":"2025-05-08T01:32:47.364Z","repository":{"id":94806821,"uuid":"128187440","full_name":"Neargye/scope_guard","owner":"Neargye","description":"Scope Guard \u0026 Defer C++","archived":false,"fork":false,"pushed_at":"2024-01-23T11:56:48.000Z","size":327,"stargazers_count":156,"open_issues_count":0,"forks_count":16,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-11-14T00:41:00.471Z","etag":null,"topics":["c-plus-plus","c-plus-plus-11","cplusplus","cplusplus-11","cpp","cpp11","defer-operator","header-only","no-dependencies","scope-guard","single-file"],"latest_commit_sha":null,"homepage":"","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/Neargye.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":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-04-05T09:51:02.000Z","updated_at":"2024-11-13T00:28:13.000Z","dependencies_parsed_at":"2024-01-24T19:16:10.609Z","dependency_job_id":null,"html_url":"https://github.com/Neargye/scope_guard","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neargye%2Fscope_guard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neargye%2Fscope_guard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neargye%2Fscope_guard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neargye%2Fscope_guard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Neargye","download_url":"https://codeload.github.com/Neargye/scope_guard/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224689018,"owners_count":17353308,"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","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":["c-plus-plus","c-plus-plus-11","cplusplus","cplusplus-11","cpp","cpp11","defer-operator","header-only","no-dependencies","scope-guard","single-file"],"created_at":"2024-08-03T02:01:01.771Z","updated_at":"2024-11-14T20:30:54.321Z","avatar_url":"https://github.com/Neargye.png","language":"C++","readme":"[![Github Releases](https://img.shields.io/github/release/Neargye/scope_guard.svg)](https://github.com/Neargye/scope_guard/releases)\n[![License](https://img.shields.io/github/license/Neargye/scope_guard.svg)](LICENSE)\n\n# Scope Guard \u0026 Defer C++\n\nScope Guard statement invokes a function with deferred execution until surrounding function returns in cases:\n\n* scope_exit - executing action on scope exit.\n\n* scope_fail - executing action on scope exit when an exception has been thrown.\n\n* scope_success - executing action on scope exit when no exceptions have been thrown.\n\nProgram control transferring does not influence Scope Guard statement execution. Hence, Scope Guard statement can be used to perform manual resource management, such as file descriptors closing, and to perform actions even if an error occurs.\n\n## Features\n\n* C++11\n* Header-only\n* Dependency-free\n* Thin callback wrapping, no added std::function or virtual table penalties\n* No implicitly ignored return, check callback return void\n* Defer or Scope Guard syntax and \"With\" syntax\n\n## [Examples](example)\n\n* [Scope Guard on exit](example/scope_exit_example.cpp)\n\n  ```cpp\n  std::fstream file(\"test.txt\");\n  SCOPE_EXIT{ file.close(); }; // File closes when exit the enclosing scope or errors occur.\n  ```\n\n* [Scope Guard on fail](example/scope_fail_example.cpp)\n\n  ```cpp\n  persons.push_back(person); // Add the person to db.\n  SCOPE_FAIL{ persons.pop_back(); }; // If errors occur, we should roll back.\n  ```\n\n* [Scope Guard on success](example/scope_success_example.cpp)\n\n  ```cpp\n  person = new Person{/*...*/};\n  // ...\n  SCOPE_SUCCESS{ persons.push_back(person); }; // If no errors occur, we should add the person to db.\n  ```\n\n* Custom Scope Guard\n\n  ```cpp\n  persons.push_back(person); // Add the person to db.\n\n  MAKE_SCOPE_EXIT(scope_exit) { // Following block is executed when exit the enclosing scope or errors occur.\n    persons.pop_back(); // If the db insertion fails, we should roll back.\n  };\n  // MAKE_SCOPE_EXIT(name) {action} - macro is used to create a new scope_exit object.\n  scope_exit.dismiss(); // An exception was not thrown, so don't execute the scope_exit.\n  ```\n\n  ```cpp\n  persons.push_back(person); // Add the person to db.\n\n  auto scope_exit = make_scope_exit([]() { persons.pop_back(); });\n  // make_scope_exit(A\u0026\u0026 action) - function is used to create a new scope_exit object. It can be instantiated with a lambda function, a std::function\u003cvoid()\u003e, a functor, or a void(*)() function pointer.\n  // ...\n  scope_exit.dismiss(); // An exception was not thrown, so don't execute the scope_exit.\n  ```\n\n* With Scope Guard\n\n  ```cpp\n  std::fstream file(\"test.txt\");\n  WITH_SCOPE_EXIT({ file.close(); }) { // File closes when exit the enclosing with scope or errors occur.\n    // ...\n  };\n  ```\n\n## Synopsis\n\n### Reference\n\n#### scope_exit\n\n* `scope_exit\u003cF\u003e make_scope_exit(F\u0026\u0026 action);` - return scope_exit with the action.\n* `SCOPE_EXIT{action};` - macro for creating scope_exit with the action.\n* `MAKE_SCOPE_EXIT(name) {action};` - macro for creating named scope_exit with the action.\n* `WITH_SCOPE_EXIT({action}) {/*...*/};` - macro for creating scope with scope_exit with the action.\n\n#### scope_fail\n\n* `scope_fail\u003cF\u003e make_scope_fail(F\u0026\u0026 action);` - return scope_fail with the action.\n* `SCOPE_FAIL{action};` - macro for creating scope_fail with the action.\n* `MAKE_SCOPE_FAIL(name) {action};` - macro for creating named scope_fail with the action.\n* `WITH_SCOPE_FAIL({action}) {/*...*/};` - macro for creating scope with scope_fail with the action.\n\n#### scope_success\n\n* `scope_success\u003cF\u003e make_scope_success(F\u0026\u0026 action);` - return scope_success with the action.\n* `SCOPE_SUCCESS{action};` - macro for creating scope_success with the action.\n* `MAKE_SCOPE_SUCCESS(name) {action};` - macro for creating named scope_success with the action.\n* `WITH_SCOPE_SUCCESS({action}) {/*...*/};` - macro for creating scope with scope_success with the action.\n\n#### defer\n\n* `DEFER{action};` - macro for creating defer with the action.\n* `MAKE_DEFER(name) {action};` - macro for creating named defer with the action.\n* `WITH_DEFER({action}) {/*...*/};` - macro for creating scope with defer with the action.\n\n### Interface of scope_guard\n\nscope_exit, scope_fail, scope_success implement scope_guard interface.\n\n* `dismiss()` - dismiss executing action on scope exit.\n\n#### Throwable settings\n\n* `SCOPE_GUARD_NOTHROW_CONSTRUCTIBLE` define this to require nothrow constructible action.\n\n* `SCOPE_GUARD_MAY_THROW_ACTION` define this to action may throw exceptions.\n\n* `SCOPE_GUARD_NO_THROW_ACTION` define this to require noexcept action.\n\n* `SCOPE_GUARD_SUPPRESS_THROW_ACTIONS` define this to exceptions during action will be suppressed.\n\n* By default using `SCOPE_GUARD_MAY_THROW_ACTION`.\n\n* `SCOPE_GUARD_CATCH_HANDLER` define this to add exceptions handler. If `SCOPE_GUARD_SUPPRESS_THROW_ACTIONS` is not defined, it will do nothing.\n\n### Remarks\n\n* If multiple Scope Guard statements appear in the same scope, the order they appear is the reverse of the order they are executed.\n\n  ```cpp\n  void f() {\n    SCOPE_EXIT{ std::cout \u003c\u003c \"First\" \u003c\u003c std::endl; };\n    SCOPE_EXIT{ std::cout \u003c\u003c \"Second\" \u003c\u003c std::endl; };\n    SCOPE_EXIT{ std::cout \u003c\u003c \"Third\" \u003c\u003c std::endl; };\n    ... // Other code.\n    // Prints \"Third\".\n    // Prints \"Second\".\n    // Prints \"First\".\n  }\n  ```\n\n## Integration\n\nYou should add required file [scope_guard.hpp](include/scope_guard.hpp).\n\n## References\n\n* [Andrei Alexandrescu \"Systematic Error Handling in C++\"](https://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C)\n* [Andrei Alexandrescu “Declarative Control Flow\"](https://youtu.be/WjTrfoiB0MQ)\n\n## Licensed under the [MIT License](LICENSE)\n","funding_links":[],"categories":["Libraries \u0026 Frameworks:","C++"],"sub_categories":["Other"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNeargye%2Fscope_guard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNeargye%2Fscope_guard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNeargye%2Fscope_guard/lists"}