{"id":17912728,"url":"https://github.com/stypox/event-notifier","last_synced_at":"2025-08-15T08:31:15.007Z","repository":{"id":128744039,"uuid":"171675606","full_name":"Stypox/event-notifier","owner":"Stypox","description":"Connect events to functions with an intuitive C++ interface","archived":false,"fork":false,"pushed_at":"2019-03-12T14:22:21.000Z","size":39,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-28T19:55:22.820Z","etag":null,"topics":["event","event-driven","event-handler","event-management","event-notification","events","header-only","simple"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Stypox.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2019-02-20T13:11:53.000Z","updated_at":"2024-03-27T18:14:46.000Z","dependencies_parsed_at":"2023-03-26T01:34:56.422Z","dependency_job_id":null,"html_url":"https://github.com/Stypox/event-notifier","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stypox%2Fevent-notifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stypox%2Fevent-notifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stypox%2Fevent-notifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stypox%2Fevent-notifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Stypox","download_url":"https://codeload.github.com/Stypox/event-notifier/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229901666,"owners_count":18141740,"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":["event","event-driven","event-handler","event-management","event-notification","events","header-only","simple"],"created_at":"2024-10-28T19:46:44.521Z","updated_at":"2024-12-16T03:10:18.508Z","avatar_url":"https://github.com/Stypox.png","language":"C++","readme":"# Event notifier\nA **C++ header-only** library that lets you **connect events to functions** with a **small** and **intuitive** interface. When an event occours, all the functions connected to it are called. **Member functions** (like `game.pause()`) are supported, too!\n# Connect events to functions\nWhen connecting some events to a function, you can choose whether to **connect all events** of that type to the function **or only some** (based on `std::hash\u003cEvent\u003e`) [``en`` is an EventNotifier]:\n- `en.connect\u003cEvent\u003e(function)`: connects every event of type `Event` to `function`. E.g. `en.connect\u003cClick\u003e(function)` would connect every event of type `Click` to the function.\n- `en.connect(function, events...)`: connects every event in `events...` to `function`. `std::hash` is used to compare for equality. E.g. `en.connect(function, Click{right})` would connect every event that has the same hash as `Click{right}` to the function.\n\n`function` can be any function convertible to `std::function` (normal functions and lambdas are ok). It must take either 0 arguments or 1 argument (the event).\n\nIf you need to connect to a member function, you can use `en.connect_member()` [in the examples `game` is of type `Game`]:\n- `en.connect_member\u003cEvent\u003e(object, \u0026object_type::member_function)`: connects every event of type `Event` to `(object.*member_function)()`. E.g. `en.connect_member\u003cClick\u003e(game, \u0026Game::pause)`\n- `en.connect_member(object, \u0026object_type::member_function, events...)`: connects every event in `events...` to `(object.*member_function)()`. E.g. `en.connect_member(game, \u0026Game::pause, Click{right})`\n\nEvery connect\\[_member\\]() call returns a `EventNotifier::Handler`, that can be used to **disconnect the function** when it is not needed / it can not be used anymore. It uses **RAII**, so its destructor takes care of disconnecting the function it handles, even though the disconnection can be done manually using ``handler.disconnect()``. If you do not need this kind of handling (because the connected function will always be valid) you can do `handler.keep()`, that will transfer the ownership to the EventNotifier. For example: `en.connect(closeGame, WindowButton{X}).keep()`. The compiler will warn you if you do nothing with the returned Handler.\n\n# Event notification\nWhen you want an event to occour, you have to **pass it to the EventNotifier** through the `en.notify(event)` function.    \nFor example: `en.notify(Click{right})`\n\n# Installation\nTo use this library just download the header file `event_notifier.hpp` and `#include` it in your project! If you want to `#include` it as `\u003cstypox/event_notifier.hpp\u003e` you need to add `-IPATH/TO/event-notifier/include` to your compiler options. Note: it requires C++17, so add to your compiler options `-std=c++17`.\n\n# Example\n```cpp\n#include \u003cstypox/event_notifier.hpp\u003e\n#include \u003ciostream\u003e\nusing stypox::EventNotifier;\n\nenum Click { right, left };\nstruct MouseScroll { int deltaPixels; };\n\nclass Game {\n\tEventNotifier::Handler m_rightClickHandler;\n\t// ^ will automatically be disconnected when Game is destructed.\npublic:\n\tGame(EventNotifier\u0026 en) :\n\t\tm_rightClickHandler{en.connect_member(*this, \u0026Game::onRightClick, Click::right)}\t{}\n\tvoid onRightClick() { std::cout \u003c\u003c \"Game received a right click!\\n\"; }\n};\n\nint main() {\n\tEventNotifier en;\n\ten.connect\u003cMouseScroll\u003e([](MouseScroll ms){ std::cout \u003c\u003c \"Scrolled by \" \u003c\u003c ms.deltaPixels \u003c\u003c \" pixels\\n\"; }).keep();\n\t// ^ here `.keep()` is used since the lambda will always be available: it does not depend on any object that may be destructed before `en` is.\n\n\ten.notify(MouseScroll{17}); // prints \"Scrolled by 17 pixels\"\n\n\t{\n\t\tGame game{en};\n\t\ten.notify(Click::right); // prints \"Game received a right click!\"\n\t\ten.notify(Click::left); // does nothing\n\t}\n\ten.notify(Click::right); // does nothing since `game` has already been destructed\n\ten.notify(MouseScroll{23}); // prints \"Scrolled by 23 pixels\"\n\n\t// no leaks: when `en` is destructed all functions are disconnected.\n}\n```\nThe complete output will be:\n```\nScrolled by 17 pixels\nGame received a right click!\nScrolled by 23 pixels\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstypox%2Fevent-notifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstypox%2Fevent-notifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstypox%2Fevent-notifier/lists"}