cyringe pronunciation /sɪˈrɪndʒ/
- Description
- Improve legacy code
- How to clone, build, test and install
- How to declare as CMake link-dependency
- How to use
- Requires
- License
cyringe is an easy to use dependency injection framework for C++. It allows to distribute/provide objects within the whole application-process borders. The framework can provide single-instance of one type or multiple-instances of a type
- In case of a single-instance, which is singleton-compatible, is no need to define the ID-Type and also no need to set the argument id when calling the cyringe-methods.
- In case of a multi-instance, a proper ID-Type has to be defined (must be an integral-type or enum-type) and an unique ID per object has to be passed with every method call.
Availability or globally available instances, similar to a Singleton pattern but WITHOUT of it's drawbacks for testing. In fact it makes testing even easier.
Globally availability can be misused to shortcut some architectural rules. Good news; you can search for usage and define rules in your merge/pull-request process.
Within legacy code often Singletons are used all over the place. One good step forward is to get rid of the singletons by simply replacing them with cyringe.
Clone the project from here: https://github.com/vahid-dzanic/cyringe
# clone
git clone https://github.com/vahid-dzanic/cyringe.git
# build
mkdir -p cyringe/build
cd cyringe/build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
# test
ctest -j$(nproc)
# install
sudo make install
In case your build system is CMake, you can setup the cyringe Header-only-library as link-dependency.
...
find_package(cyringe REQUIRED)
add_executable(my_app
...)
target_link_libraries(my_app
PRIVATE
vdc::cyringe
...
... )
#include <cyringe/cyringe.h>
class XYZ {
float origin();
}
int main() {
XYZ xyz;
cyringe<XYZ>::setup(xyz);
...
}
#include <cyringe/cyringe.h>
int main() {
XYZ* xyz = new XYZ();
cyringe<XYZ>::setup(*xyz);
...
cyringe<XYZ>::release();
delete xyz;
xyz = nullptr;
}
#include "XYZ.h"
#include <cyringe/cyringe.h>
float someCodeSomewhere() {
auto origin = cyringe<XYZ>::get().origin()
do something with origin...
}
#include <cyringe/cyringe.h>
class XYZ {
XYZ() {
cyringe<XYZ>::setup(*this);
}
~XYZ() {
cyringe<XYZ>::release();
}
float origin();
}
C++11 capable compiler
Mozilla Public License Version 2.0