Getting Started#

Supported Platforms#

7bitDI requires client code and compiler compatible with the C++17 standard or newer.

The library is officially supported on the following platforms:

Operating systems:

  • Linux

  • macOS

  • Windows

Compilers:

  • gcc 7.0+

  • clang 6.0+

  • MSVC 2015+

If you notice any problems/bugs, please file an issue on the 7bitDI GitHub Issue Tracker. Pull requests containing fixes are welcome!

Installation#

There are a few ways of installation:

  1. Using Cmake fetch content api - Recommended

    Update CMakeLists.txt file with following code

    include(FetchContent)
    FetchContent_Declare(
            7bitDI
            GIT_REPOSITORY https://github.com/7bitcoder/7bitDI.git
            GIT_TAG 86228173f14f449dde88a84c549474ba43c2fd25 # proper release tag for example 1.0.0
    )
    FetchContent_MakeAvailable(7bitDI)
    
  2. Using Conan.io package manager

    Download and install Conan, and create conanfile.txt in the root of your project for example:

    [requires]
    7bitdi/2.0.0
    

    change the version to newer if available, then run the command:

    conan install . --output-folder=build --build=missing
    
  3. Header only

    Download source code from the most recent release, copy include folder into your project location, for example copy into the ‘/SevenBitDI’ folder. Include this folder into the project, with CMake, u can use:

    include_directories(/SevenBitDI/Include)
    
  4. Header only - Single file

    Download SevenBitDI.hpp header file from the most recent release, copy this file into your project location and include it.

  5. Building library as Static/Shared

    Download source code from the most recent release, build or install the project using CMake, for more details see the Building Library guide.

Example Usage#

The next chapters will in detail, step by step explain what is going on in this example

Examples/Guides/InjectingServices#
#include <SevenBit/DI.hpp>
#include <iostream>

using namespace sb::di;

struct IServiceA
{
    virtual std::string actionA() = 0;

    virtual ~IServiceA() = default;
};

struct IServiceB
{
    virtual std::string actionB() = 0;

    virtual ~IServiceB() = default;
};

struct ServiceA final : IServiceA
{
    std::string actionA() override { return "actionA"; }
};

struct ServiceB final : IServiceB
{
    std::string actionB() override { return "actionB"; }
};

class ServiceExecutor
{
    IServiceA &_serviceA;
    std::unique_ptr<IServiceB> _serviceB;

  public:
    ServiceExecutor(IServiceA &serviceA, std::unique_ptr<IServiceB> serviceB)
        : _serviceA(serviceA), _serviceB(std::move(serviceB))
    {
    }

    [[nodiscard]] std::string execute() const
    {
        return _serviceA.actionA() + ", " + _serviceB->actionB() + " executed.";
    }
};
int main()
{
    ServiceProvider provider = ServiceCollection{}
                                   .addSingleton<IServiceA, ServiceA>()
                                   .addTransient<IServiceB, ServiceB>()
                                   .addScoped<ServiceExecutor>()
                                   .buildServiceProvider();

    const auto &executor = provider.getService<ServiceExecutor>();

    std::cout << executor.execute();
    return 0;
}
Output#
actionA, actionB executed.