{"id":13620498,"url":"https://github.com/gracicot/kangaru","last_synced_at":"2025-04-14T21:31:19.054Z","repository":{"id":29443842,"uuid":"32979989","full_name":"gracicot/kangaru","owner":"gracicot","description":"🦘 A dependency injection container for C++11, C++14 and later","archived":false,"fork":false,"pushed_at":"2024-07-01T13:57:36.000Z","size":1608,"stargazers_count":470,"open_issues_count":13,"forks_count":39,"subscribers_count":27,"default_branch":"master","last_synced_at":"2024-07-14T09:10:49.126Z","etag":null,"topics":["c-plus-plus","dependency-injection","injection","inversion-of-control","ioc","ioc-container"],"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/gracicot.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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}},"created_at":"2015-03-27T10:21:01.000Z","updated_at":"2024-07-23T06:05:55.729Z","dependencies_parsed_at":"2023-01-16T23:01:16.662Z","dependency_job_id":"942d6369-d667-4cec-bf10-559ad736e148","html_url":"https://github.com/gracicot/kangaru","commit_stats":null,"previous_names":[],"tags_count":49,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gracicot%2Fkangaru","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gracicot%2Fkangaru/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gracicot%2Fkangaru/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gracicot%2Fkangaru/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gracicot","download_url":"https://codeload.github.com/gracicot/kangaru/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223647938,"owners_count":17179348,"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","dependency-injection","injection","inversion-of-control","ioc","ioc-container"],"created_at":"2024-08-01T21:00:56.528Z","updated_at":"2025-04-14T21:31:19.044Z","avatar_url":"https://github.com/gracicot.png","language":"C++","funding_links":[],"categories":["Miscellaneous","C++","排序"],"sub_categories":["多项混杂"],"readme":"# kangaru 🦘\n\n[![Build status](https://ci.appveyor.com/api/projects/status/8gv9iapt3g7mgc4l?svg=true)](https://ci.appveyor.com/project/gracicot/kangaru) [![All OS](https://github.com/gracicot/kangaru/actions/workflows/all-os.yml/badge.svg)](https://github.com/gracicot/kangaru/actions/workflows/all-os.yml) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/f50382d94f894dc981334308a5e709f6)](https://app.codacy.com/gh/gracicot/kangaru/dashboard?utm_source=gh\u0026utm_medium=referral\u0026utm_content=\u0026utm_campaign=Badge_grade) [![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/1401/badge)](https://bestpractices.coreinfrastructure.org/projects/1401) [![Join the chat at https://gitter.im/gracicot/kangaru](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gracicot/kangaru?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/gracicot/kangaru/master/LICENSE) [![GitHub Releases](https://img.shields.io/github/release/gracicot/kangaru.svg)](https://github.com/gracicot/kangaru/releases) [![GitHub Issues](https://img.shields.io/github/issues/gracicot/kangaru.svg)](http://github.com/gracicot/kangaru/issues) [![Try online](https://img.shields.io/badge/try-online-blue.svg)](https://wandbox.org/permlink/SVZduLAhH0dACDlj)\n\nKangaru is an inversion of control container for C++11, C++14 and later. It provides many features to automate dependency injection and reduce the amount of wiring boilerplate in your code. We are achieving that by exposing in code configuration for autowiring, constructor and function parameters injection. We aim to keep the simplest interface possible and keep boilerplate to a minimum. On top of that, we don't want to be intrusive into user/library code.\n\nKangaru is a header only library because of its extensive use of templates.\nThe name kangaru comes from the container's feature to inject itself into a service as a dependency, and because kangaroos are awesome.\n\n----\n\n[Documentation and tutorial](https://github.com/gracicot/kangaru/wiki) is in the wiki and the `doc` folder!\n\nLooking for the latest stable version? Check out our [release page](https://github.com/gracicot/kangaru/releases).\n\n## Overview\n\nHere's a quick demo to show usage of this library. This is some basic usage of the library with two user classes.\n\n```c++\n#include \u003ckangaru/kangaru.hpp\u003e\n#include \u003ccassert\u003e\n\n// We define some normal classes with dependencies between them\nstruct Camera {};\n\nstruct Scene {\n    Camera\u0026 camera;\n};\n\n// The following is the configuration of our user classes above.\n// The structure and dependency graph is defined by these configs.\n\n// Camera is a single service so the service has a shared instance.\n// It will be injected and returned as a reference.\nstruct CameraService : kgr::single_service\u003cCamera\u003e {};\n\n// Scene is not single, so the container returns scenes by value.\n// Also, we depend on a camera to be constructed.\nstruct SceneService : kgr::service\u003cScene, kgr::dependency\u003cCameraService\u003e\u003e {};\n\nint main()\n{\n    kgr::container container;\n    \n    // The service function returns instances of the normal classes.\n    Scene scene = container.service\u003cSceneService\u003e();\n    Camera\u0026 camera = container.service\u003cCameraService\u003e();\n    \n    assert(\u0026scene.camera == \u0026camera); // passes, both cameras are the same instance.\n}\n```\n\n[Try this example online](https://wandbox.org/permlink/3ekQZXqTFGRlj8ZG) to see how it runs.\n\n## Autowire API\n\nSince recent versions of kangaru, we support autowire api. The following is the same example as above, using autowire.\n\n```c++\n#include \u003ckangaru/kangaru.hpp\u003e\n#include \u003ccassert\u003e\n\n// We define some normal classes with dependencies between them\n// And we added the autowire configuration\nstruct Camera {\n    // friend functions are faster to lookup than plain free functions\n    friend auto service_map(Camera const\u0026) -\u003e kgr::autowire_single;\n};\n\nstruct Scene {\n    Camera\u0026 camera;\n    \n    friend auto service_map(Scene const\u0026) -\u003e kgr::autowire;\n};\n\n// No need for service definitions\n\nint main()\n{\n    kgr::container container;\n    \n    // We invoke a lambda that receives injected parameters.\n    // The container will figure how to wire the classes using\n    // either the constructor parameters or aggregate initialization\n    container.invoke([](Scene scene, Camera\u0026 camera) {\n        assert(\u0026scene.camera == \u0026camera); // passes, both cameras are the same instance.\n    });\n}\n```\n\n## Features\n\n* Non intrusive, no existing classes need modification\n* You tell the container how to construct your types, store and inject them\n* Injection by setters\n* Autowiring by class constructors\n* Function parameter injection\n* Clean and simple API for simple cases, flexible enough for complex cases\n* Low runtime overhead\n* Header only library\n* Clean diagnostics at compile-time\n\n## Installation\n\n### Build kangaru yourself\n\nTo make kangaru available on your machine, you must first clone the repository:\n\n```sh\n$ git clone https://github.com/gracicot/kangaru.git \u0026\u0026 cd kangaru\n```\n\nThen use cmake to generate the project and export the package information:\n\n```sh\n$ cmake --preset export # -DKANGARU_HASH_TYPE_ID=OFF # uncomment for older compiler support\n```\n\nThat's it! Link it to your project using cmake and you can already include and code!\n\nOptionally, you can also install kangaru on your system:\n\n```sh\n$ sudo cmake --build --preset export --target install # optional step\n```\n\n### Install with vcpkg\n\nTo make kangaru available on your machine, [install vcpkg](https://vcpkg.io/en/getting-started.html). Then install the appropriate architecture. For the default, enter the following:\n\n```cmd\nvcpkg install kangaru\n```\n\nor if you want 64-bit Windows, for example, enter:\n\n```cmd\nvcpkg install kangaru:x64-windows\n```\n\n## Adding Include Path\n\nYou must use the `find_package` function:\n\n```cmake\nfind_package(kangaru 4.3 REQUIRED)\n```\n\nAnd then add the include dirs to your target:\n\n```cmake\ntarget_link_libraries(\u003cYOUR TARGET\u003e PUBLIC kangaru::kangaru)\n```\n\nThen you can include the library as follows:\n\n```c++\n#include \u003ckangaru/kangaru.hpp\u003e\n```\n\nIf you skip the installation, simply tell CMake where to find kangaru:\n\n```sh\n# in your project build directory\n$ cmake .. -DCMAKE_PREFIX_PATH=../../path/to/kangaru/build\n```\n\n## Compiler Requirement\n\nKangaru is tested by our continuous integration with all major compiler versions. The minimum required versions are:\n\n* MSVC: 2015 update 3 or better\n* GCC: 4.8.5 or better\n* Clang: 3.6 or better\n* AppleClang: 7.0 or better\n\n## What's Next\n\nThere is some feature I would like to see become real. Here's a list of those,\nfeel free to contribute!\n\n* Tests for compile-time errors\n* Better messages for compile-time errors (ongoing)\n* Service sources, more detail here: [#41](https://github.com/gracicot/kangaru/issues/41)\n* Even better performance (ongoing)\n* Expose a zero-overhead interface for cases it can apply\n* Move service definitions to service map.\n\nGot suggestions or questions? Discovered a bug? Please open an issue and we'll gladly respond!\n\n## Contributing\n\nTo contribute, simply open a pull request or an issue and we'll discuss together about how to make this library even more awesome! See our complete [contribution guideline](https://github.com/gracicot/kangaru/blob/master/CONTRIBUTING.md) for more details.\n\nWant to help? Pick an issue on our [issue tracker](https://github.com/gracicot/kangaru/issues)!\n\nFound an issue? Have an idea to make this library better? Please [submit an issue](https://github.com/gracicot/kangaru/issues/new) and we will respond within a few days, and commit to address the needs.\n\n### Running Tests\n\nTests are enabled using the cmake profile `dev`. Enabling this will make our CMake scripts to try finding the Catch2 library. We also contain a submodule for this library in our git repository in case you don't have it available in a prefix directory. You can also enable vcpkg to download the dependencies.\n\nUsing this option adds the the `test` target.\n\nYou can run the tests like this:\n\n```cmake\ncmake --preset dev\ncmake --build --preset debug\nctest --preset debug\n```\n\n## Who's Using Kangaru\n\nHere's a list of projects making use of kangaru:\n\n* Our team's game engine\n* The people I helped integrating this library into their projects\n* Surely more!\n\n### Using kangaru?\n\nLet me know of your projects using kangaru! I'll be glad to fill the list above with your project's name.\n\n## Acknowledgements\n\nA big thanks to [Louis-Alexandre Vallières-Lavoie](https://github.com/Louis-Alexandre) for reviewing and proposing various improvements to our documentation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgracicot%2Fkangaru","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgracicot%2Fkangaru","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgracicot%2Fkangaru/lists"}