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 Conan.io package manager - Recommended

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

    [requires]
    7bitdi/1.0.0
    

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

    conan install . --output-folder=build --build=missing
    
  2. 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)
    
  3. Header only - Single file

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

  4. 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 : public IServiceA
{
    std::string actionA() { return "actionA"; }
};

struct ServiceB final : public IServiceB
{
    std::string actionB() { 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);
    }

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

    ServiceExecutor &executor = provider->getService<ServiceExecutor>();

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