{"id":17362713,"url":"https://github.com/kdewald/safe_callback","last_synced_at":"2026-03-01T05:36:25.934Z","repository":{"id":42815688,"uuid":"469580625","full_name":"kdewald/safe_callback","owner":"kdewald","description":"A wrapper around std::function with some thread-safety guarantees.","archived":false,"fork":false,"pushed_at":"2022-05-01T01:01:39.000Z","size":16,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-23T03:11:21.170Z","etag":null,"topics":["callback-functions","cpp","thread-safe"],"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/kdewald.png","metadata":{"files":{"readme":"README.rst","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}},"created_at":"2022-03-14T04:31:06.000Z","updated_at":"2024-06-25T09:44:32.000Z","dependencies_parsed_at":"2022-08-22T05:20:27.448Z","dependency_job_id":null,"html_url":"https://github.com/kdewald/safe_callback","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kdewald/safe_callback","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdewald%2Fsafe_callback","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdewald%2Fsafe_callback/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdewald%2Fsafe_callback/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdewald%2Fsafe_callback/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kdewald","download_url":"https://codeload.github.com/kdewald/safe_callback/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdewald%2Fsafe_callback/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29960478,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-01T01:47:18.291Z","status":"online","status_checked_at":"2026-03-01T02:00:07.437Z","response_time":124,"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":["callback-functions","cpp","thread-safe"],"created_at":"2024-10-15T19:40:07.176Z","updated_at":"2026-03-01T05:36:25.866Z","avatar_url":"https://github.com/kdewald.png","language":"C++","readme":"Safe Callback\n==========\n\nA wrapper around std::function with some thread-safety guarantees.\n\nOverview\n--------\n\nThis library was conceived to solve the problem of using callbacks in systems\nwhere objects can be destroyed while a callback is still pending or running,\nwhich can happen in multithreaded applications and will lead to a crash.\n\nThis library is header-only and does not depend on any other library, so feel\nfree to just copy it into your project or append the `include` directory to\nyour project's include path.\n\nIf you need help using this library, **please do not hesitate to reach out!**\n\n* Contact me: ``kevin at dewald dot me``\n\nUsage\n-----\n\nThe following code briefly describes how to use the safe callback. All functions\nof interest to the user are presented here:::\n\n    #include \u003ckvn_safe_callback.hpp\u003e\n\n    // Create a callback wrapper\n    kvn::safe_callback\u003cint(int)\u003e cb;\n\n    // Load a lambda function into the callback wrapper\n    cb.load([] (int i) -\u003e int {\n        std::cout \u003c\u003c \"Callback called with \" \u003c\u003c i \u003c\u003c std::endl;\n        return i * 2;\n    });\n\n    // Check if the callback is loaded\n    if (cb.is_loaded()) {\n        std::cout \u003c\u003c \"Callback is loaded\" \u003c\u003c std::endl;\n    }\n\n    // Another way of checking if the callback is loaded.\n    if (cb) {\n        std::cout \u003c\u003c \"Callback is loaded\" \u003c\u003c std::endl;\n    }\n\n    // Call the callback and print the result\n    int result = cb(42);\n    std::cout \u003c\u003c \"Result: \" \u003c\u003c result \u003c\u003c std::endl;\n\n    // Unload the callback\n    cb.unload();\n\nA few notes that need to be highlighted on the behavior of the safe callback:\n\n* If the callback wrapper is not loaded, attempting to call the callback will\n  be a no-op.\n* In the cases where the callback wrapper is not loaded and the internal function\n  returns a value, the return value will be the default value of the return type.\n  This means that all return types need to be default constructible.\n  If you use a non-default-constructible type, the following error message will\n  appear during compilation: \n  ``error: no matching function for call to 'YourClass::YourClass()'``\n\nLicense\n-------\n\nAll components within this project that have not been bundled from\nexternal creators, are licensed under the terms of the `MIT Licence`_.\n\n.. Links\n\n.. _MIT Licence: LICENCE.md\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkdewald%2Fsafe_callback","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkdewald%2Fsafe_callback","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkdewald%2Fsafe_callback/lists"}