Skip to content

Commit defbeb1

Browse files
authored
Merge pull request #16 from 7bitcoder/dev
update documentation
2 parents cb7d025 + 41a3232 commit defbeb1

File tree

5 files changed

+56
-45
lines changed

5 files changed

+56
-45
lines changed

Docs/advanced-guides/using-factories.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ Using Factories
22
========================================
33

44
Factory functor can be provided to manually create a service.
5-
Functor should return unique_ptr and as an argument should optionally take other services ().
6-
Functor scheme (Services...) -> std::unique_ptr
5+
Functor should return unique_ptr or T if object is movable/copyable and should optionally take other services as arguments.
6+
Functor scheme (Services...) -> std::unique_ptr< T> | T
77
Where Services are pointers, in place objects (if object is movable or copyable), unique pointers, references, vectors with pointers or unique pointers
88

9-
109
.. literalinclude:: ../../Examples/Guides/FactoryFunctions.cpp
1110
:caption: Examples/Guides/FactoryFunctions
1211
:language: C++

Docs/basic-guides/injection-rules.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ General
1212

1313
Injecting Services
1414
---------------------
15-
* Services cannot be injected by value: (T)
1615
* Singleton/scoped services can be injected using one of:
17-
18-
* References: (T&)
19-
* Const references: (const T&)
20-
* Pointers: (T*)
21-
* Const pointer: (T* const)
22-
* Pointer to const object: (const T*)
23-
* Const pointer to const object: (const T* const)
24-
* Transient services can be injected using std::unique_ptr: (unique_ptr<T>) or directly T if object is movable or copyable
16+
* References: (T&)
17+
* Const references: (const T&)
18+
* Pointers: (T*)
19+
* Const pointer: (T* const)
20+
* Pointer to const object: (const T*)
21+
* Const pointer to const object: (const T* const)
22+
* Transient services can be injected using one of:
23+
* std::unique_ptr: (unique_ptr<T>)
24+
* In place object if type is movable or copyable: T
2525
* Multiple services implementing specified interface can be injected using std::vector:
26-
* Transient (std::vector<std::unique_ptr<T>>)
27-
* Singleton/scoped (std::vector<T*>)
26+
* Transient (std::vector<std::unique_ptr<T>>)
27+
* Singleton/scoped (std::vector<T*>)
2828

2929
.. list-table:: Injection table
3030
:widths: 50 50

Docs/basic-guides/services-lifetime.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Services LifeTime
22
========================================
33

4-
Service providers can create scoped validInstance providers:
4+
Service providers can create scoped service providers:
55

66
.. code-block:: cpp
77
@@ -11,7 +11,7 @@ Service providers can create scoped validInstance providers:
1111
1212
Service can be registered as a singleton, scoped, or transient.
1313

14-
* Singleton: validInstance provider will create only one instance of this validInstance (accessible via the getService method)
14+
* Singleton: service provider will create only one instance of this service (accessible via the getService method)
1515
* Scoped: instance provider will create only one instance of this instance for each scope (accessible via the getService method)
1616
* Transient: services are always unique, a new service is provided every time it is requested, and the service provider returns, in this case, std::unique_ptr (accessible via createService method)
1717

Examples/Guides/FactoryFunctions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ServiceB
2121

2222
public:
2323
explicit ServiceB(ServiceA &serviceA) : _serviceA(serviceA) {}
24+
ServiceB(ServiceB &&) = default;
2425

2526
[[nodiscard]] std::string message() const { return _serviceA.message(); }
2627
};
@@ -30,7 +31,7 @@ int main()
3031
ServiceProvider provider =
3132
ServiceCollection{}
3233
.addSingleton<ServiceA>([] { return std::make_unique<ServiceA>("Hello from service!"); })
33-
.addSingleton<ServiceB>([](ServiceA &serviceA) { return std::make_unique<ServiceB>(serviceA); })
34+
.addSingleton<ServiceB>([](ServiceA &serviceA) { return ServiceB(serviceA); })
3435
.buildServiceProvider();
3536

3637
auto &serviceA = provider.getService<ServiceA>();

README.md

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fixes are welcome!
4848

4949
## Installation
5050

51-
**There are a few ways of installation:**
51+
### There are a few ways of installation:
5252

5353
### 1. Using Cmake fetch content api - Recommended
5454

@@ -68,16 +68,16 @@ FetchContent_MakeAvailable(7bitDI)
6868

6969
Download and install A [Conan](https://conan.io/), and create conanfile.txt in the root of your project for example:
7070

71-
```txt
72-
[requires]
73-
7bitdi/2.0.0
74-
```
71+
```txt
72+
[requires]
73+
7bitdi/2.0.0
74+
```
7575

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

78-
```sh
79-
conan install . --output-folder=build --build=missing
80-
```
78+
```console
79+
conan install . --output-folder=build --build=missing
80+
```
8181

8282
### 3. Header only
8383

@@ -86,9 +86,9 @@ copy include folder into your project location,
8686
for example copy into the '/SevenBitDI' folder.
8787
Include this folder into the project, with [Cmake](https://cmake.org/), u can use:
8888

89-
```cmake
90-
include_directories(/SevenBitDI/Include)
91-
```
89+
```cmake
90+
include_directories(/SevenBitDI/Include)
91+
```
9292

9393
### 4. Header only - Single file
9494

@@ -97,7 +97,7 @@ copy this file into your project location and include it.
9797

9898
### 5. Building library as Static/Shared
9999

100-
Download source code from the most recent release, build or install the project using [Cmake](https://cmake.org/)_,
100+
Download source code from the most recent release, build or install the project using [Cmake](https://cmake.org/),
101101
for more details see the Building Library guide
102102
in [Documentation](https://7bitdi.readthedocs.io/en/latest/getting-started.html).
103103

@@ -108,42 +108,53 @@ in [Documentation](https://7bitdi.readthedocs.io/en/latest/getting-started.html)
108108

109109
## Injection Rules
110110

111-
The dependency injection mechanism relies heavily on template metaprogramming and it has some limitations.
111+
The dependency injection system relies heavily on template metaprogramming and it has some limitations.
112112

113113
### General
114114

115115
* Only one constructor should be defined for each instance implementation
116116
* If the service is registered with interface and implementation, the interface should have a virtual destructor
117-
* If multiple services are registered by the same interface, all should have the same lifetime (the build method will
118-
throw an exception)
117+
* If multiple services are registered by the same interface, all should have the same lifetime singleton, scoped or
118+
transient (the build method will throw an exception)
119119
* Only one service implementation can be registered (the build method will throw an exception)
120120

121+
### Service lifetime
122+
123+
Service can be registered as a singleton, scoped, or transient.
124+
125+
* Singleton: service provider will create only one instance of this service (accessible via the getService
126+
method)
127+
* Scoped: instance provider will create only one instance of this instance for each scope (accessible via the getService
128+
method)
129+
* Transient: services are always unique, a new service is provided every time it is requested, and the service provider
130+
returns, in this case, std::unique_ptr (accessible via createService method)
131+
121132
### Injecting Services
122133

123-
* Services cannot be injected by value: (T)
124134
* Singleton/scoped services can be injected using one of:
125135
* References: (T&)
126136
* Const references: (const T&)
127137
* Pointers: (T*)
128138
* Const pointer: (T* const)
129139
* Pointer to const object: (const T*)
130140
* Const pointer to const object: (const T* const)
131-
* Transient services can be injected using std::unique_ptr: (unique_ptr<T>) or directly T if object is movable or
132-
copyable
141+
* Transient services can be injected using one of:
142+
* std::unique_ptr: (unique_ptr< T>)
143+
* In place object if type is movable or copyable: T
133144
* Multiple services implementing specified interface can be injected using std::vector:
134-
* Transient (std::vector<std::unique_ptr<T>>)
135-
* Singleton/scoped (std::vector<T*>)
145+
* Transient (std::vector<std::unique_ptr < T>>)
146+
* Singleton/scoped (std::vector<T *>)
136147

137148
### Injection Table
138149

139-
| Constructor param type | ServiceProvider method used |
140-
|---------------------------------|------------------------------------|
141-
| T - if movable or copyable | provider.createServiceInPlace<T>() |
142-
| std::unique_ptr<T> | provider.createService<T>() |
143-
| T& | provider.getService<T>() |
144-
| T* | provider.tryGetService<T>() |
145-
| std::vector<T*> | provider.getServices<T>() |
146-
| std::vector<std::unique_ptr<T>> | provider.createServices<T>() |
150+
| Constructor param type | ServiceProvider method used |
151+
|----------------------------------|-------------------------------------|
152+
| T - if movable or copyable | provider.createServiceInPlace< T>() |
153+
| std::unique_ptr< T> | provider.createService< T>() |
154+
| T& | provider.getService< T>() |
155+
| T* | provider.tryGetService< T>() |
156+
| std::vector<T*> | provider.getServices< T>() |
157+
| std::vector<std::unique_ptr< T>> | provider.createServices< T>() |
147158

148159
### Sample Usage
149160

0 commit comments

Comments
 (0)