Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
xorz57 committed Nov 4, 2023
1 parent 520c5f7 commit acb7365
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.26)
project(ServiceLocator)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(${PROJECT_NAME} src/Example.cpp)
add_library(ServiceLocator INTERFACE)
target_include_directories(ServiceLocator INTERFACE ${CMAKE_SOURCE_DIR}/include)

add_executable(Example examples/Example.cpp)
target_link_libraries(Example PRIVATE ServiceLocator)
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,81 @@
</a>
</div>

## Example

```cpp
#include "ServiceLocator/ServiceLocator.hpp"

#include <iostream>

class A {
public:
A() {
std::cout << __func__ << std::endl;
}
~A() {
std::cout << __func__ << std::endl;
}

A(const A &other) = delete;
A(A &&other) = delete;

A &operator=(const A &other) = delete;
A &operator=(A &&other) = delete;
};

class B {
public:
B() {
std::cout << __func__ << std::endl;
}
~B() {
std::cout << __func__ << std::endl;
}

B(const B &other) = delete;
B(B &&other) = delete;

B &operator=(const B &other) = delete;
B &operator=(B &&other) = delete;
};

int main() {
ServiceLocator serviceLocator;

serviceLocator.SetInstance<A>();
serviceLocator.SetInstance<B>();

std::cout << serviceLocator.GetInstance<A>() << std::endl;
std::cout << serviceLocator.GetInstance<B>() << std::endl;

std::cout << serviceLocator.GetInstance<A>() << std::endl;
std::cout << serviceLocator.GetInstance<B>() << std::endl;

serviceLocator.Clear();

std::cout << serviceLocator.GetInstance<A>() << std::endl;
std::cout << serviceLocator.GetInstance<B>() << std::endl;

return 0;
}
```
## Output
```console
A
B
00000241A0CA9850
00000241A0CA9550
00000241A0CA9850
00000241A0CA9550
~B
~A
0000000000000000
0000000000000000
```

## How to Build using CMake

```bash
Expand Down
2 changes: 1 addition & 1 deletion src/Example.cpp → examples/Example.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ServiceLocator.hpp"
#include "ServiceLocator/ServiceLocator.hpp"

#include <iostream>

Expand Down
File renamed without changes.

0 comments on commit acb7365

Please sign in to comment.