{"id":17038608,"url":"https://github.com/darky-lucera/delegate","last_synced_at":"2026-02-11T21:35:05.353Z","repository":{"id":212994420,"uuid":"732745234","full_name":"Darky-Lucera/delegate","owner":"Darky-Lucera","description":"Versatile, easy-to-use and header-only implementation for delegates.","archived":false,"fork":false,"pushed_at":"2024-12-22T18:05:49.000Z","size":13,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-14T18:42:54.491Z","etag":null,"topics":["callback","delegate","event-handler","listener"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Darky-Lucera.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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-12-17T17:35:31.000Z","updated_at":"2025-09-10T14:11:35.000Z","dependencies_parsed_at":"2025-09-14T18:24:07.249Z","dependency_job_id":"acbca015-3553-4c6b-8d02-bba62ddca594","html_url":"https://github.com/Darky-Lucera/delegate","commit_stats":null,"previous_names":["darky-lucera/delegate"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Darky-Lucera/delegate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Darky-Lucera%2Fdelegate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Darky-Lucera%2Fdelegate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Darky-Lucera%2Fdelegate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Darky-Lucera%2Fdelegate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Darky-Lucera","download_url":"https://codeload.github.com/Darky-Lucera/delegate/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Darky-Lucera%2Fdelegate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29345610,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-11T20:11:40.865Z","status":"ssl_error","status_checked_at":"2026-02-11T20:10:41.637Z","response_time":97,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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","delegate","event-handler","listener"],"created_at":"2024-10-14T08:57:16.099Z","updated_at":"2026-02-11T21:35:05.333Z","avatar_url":"https://github.com/Darky-Lucera.png","language":"C++","readme":"# Delegate Class\r\n\r\nThe Delegate class in C++ is a versatile, easy-to-use, type-safe and header-only implementation for delegates. It empowers you to craft delegate / callback / listener / event-handler objects capable of holding multiple references to a variety of entities:\r\n\r\n- Free functions\r\n- Static member functions\r\n- Functors\r\n- Non-static member functions (const or non-const)\r\n- Lambda functions (with and without captures)\r\n- std::function\r\n- std::bind\r\n\r\nThis utility is derived from an earlier version employed in the **MindShake** video game engine from **Lucera Project**. While I can't recall the precise inception date of the initial class, I developed it prior to the establishment of Lucera in 2009, utilizing C++98. Subsequently, I enhanced it to leverage the features introduced in C++11, a transformation that took place several years ago.\r\n\r\n**Note:** In all my tests, the Delegate has demonstrated comparable speed to std::function and, in some cases, even faster performance (depending on the specific scenario and compiler flags). Additionally, it is capable of holding multiple functions at the same time.\r\n\r\n## Usage\r\n\r\nJust drop the class into your code folder and include it.\r\n\r\n**Note:** We now capture exceptions on every call and show information using fprintf. Change it by your own logger.\r\n\r\n## Interface\r\n\r\n**Note:** The interface has been changed and the flag lazy for removing a function it is not needed anymore.\r\n\r\n - `id       Add(function)`: Adds a function.\r\n - `bool     Remove(function)`: If the delegate is not being executed the function is removed. If it is being executed the function will be disabled and removed when the execution will finish.\r\n - `bool     RemoveById(id)`: Removes a funtion given its id.\r\n - `void     Clear()`: Removes all functions.\r\n - `void     operator(...) const`: Runs all the functions added.\r\n - `size_t   GetNumDelegates() const`: Get the number of delegates added.\r\n\r\n## Examples\r\n\r\nThe main.cpp contains an exhaustive example of how to use this class.\r\n\r\n## Snippets\r\n\r\nHere are some basic examples of how to use the `Delegate` class:\r\n\r\n```cpp\r\n#include \"Delegate.h\"\r\n\r\nvoid mousePosition(int x, int y) {\r\n    // ...\r\n}\r\n\r\nclass MyClass {\r\npublic:\r\n    void mousePosition(int x, int y) {\r\n        // ...\r\n    }\r\n};\r\n\r\nint main() {\r\n    MindShake::Delegate\u003cvoid(int x, int y)\u003e delegate;\r\n\r\n    delegate.Add(mousePosition);\r\n\r\n    MyClass myObject;\r\n    delegate.Add(\u0026myObject, \u0026MyClass::mousePosition);\r\n\r\n    delegate.Add([](int x, int y) {\r\n        // ...\r\n    });\r\n\r\n    delegate(2, 3);\r\n\r\n    // ...\r\n}\r\n```\r\n\r\nIf you find yourself needing to distinguish between calling a const or a non-const function, you can employ the helper functions: `getNonConstMethod` and `getConstMethod`:\r\n\r\n```cpp\r\n\r\nclass MyClass {\r\npublic:\r\n    void memberFunction() {\r\n        // ...\r\n    }\r\n\r\n    void memberFunction() const {\r\n        // ...\r\n    }\r\n};\r\n\r\nint kk() {\r\n    MindShake::Delegate\u003cvoid()\u003e delegate;\r\n\r\n    MyClass myObject;\r\n    delegate.Add(\u0026myObject, MindShake::getNonConstMethod(\u0026MyClass::memberFunction));\r\n\r\n    delegate.Add(\u0026myObject, MindShake::getConstMethod(\u0026MyClass::memberFunction));\r\n\r\n    // ...\r\n}\r\n\r\n```\r\n\r\nIf you no longer wish to receive additional events, or if you are in the process of destroying the class that holds the delegate, it is advisable to remove it:\r\n\r\n```cpp\r\nstruct Holder {\r\n    static MindShake::Delegate\u003cvoid(int)\u003e delegate;\r\n};\r\n\r\nclass MyClass {\r\npublic:\r\n    MyClass() {\r\n        Holder::delegate.Add(this, \u0026MyClass::memberFunction);\r\n    }\r\n\r\n    virtual ~MyClass() {\r\n        Holder::delegate.Remove(this, \u0026MyClass::memberFunction);\r\n    }\r\n\r\n    void memberFunction(int value) {\r\n        // ...\r\n    }\r\n};\r\n\r\nint main() {\r\n    MyClass myObject;\r\n    // ...\r\n    Holder::delegate(123);\r\n    // ...\r\n}\r\n```\r\n\r\nIf you wish to cease receiving further events for a lambda function, take note that the Add function returns an ID, facilitating the straightforward identification of the delegate to be removed:\r\n\r\n```cpp\r\nDelegate\u003cvoid()\u003e delegate;\r\n\r\nauto lambdaId = delegate.Add([]() {\r\n    // ...\r\n});\r\n\r\n// ...\r\ndelegate.RemoveById(lambdaId);\r\n```\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarky-lucera%2Fdelegate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarky-lucera%2Fdelegate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarky-lucera%2Fdelegate/lists"}