From acb7365216c437947e8745a423d708258582f677 Mon Sep 17 00:00:00 2001 From: George Fotopoulos Date: Sat, 4 Nov 2023 23:36:59 +0200 Subject: [PATCH] Update --- CMakeLists.txt | 8 +- README.md | 75 +++++++++++++++++++ {src => examples}/Example.cpp | 2 +- .../ServiceLocator}/ServiceLocator.hpp | 0 4 files changed, 82 insertions(+), 3 deletions(-) rename {src => examples}/Example.cpp (96%) rename {src => include/ServiceLocator}/ServiceLocator.hpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3acb876..aa4ae42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/README.md b/README.md index f7b28f5..26a848e 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,81 @@ +## Example + +```cpp +#include "ServiceLocator/ServiceLocator.hpp" + +#include + +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(); + serviceLocator.SetInstance(); + + std::cout << serviceLocator.GetInstance() << std::endl; + std::cout << serviceLocator.GetInstance() << std::endl; + + std::cout << serviceLocator.GetInstance() << std::endl; + std::cout << serviceLocator.GetInstance() << std::endl; + + serviceLocator.Clear(); + + std::cout << serviceLocator.GetInstance() << std::endl; + std::cout << serviceLocator.GetInstance() << std::endl; + + return 0; +} +``` + +## Output + +```console +A +B +00000241A0CA9850 +00000241A0CA9550 +00000241A0CA9850 +00000241A0CA9550 +~B +~A +0000000000000000 +0000000000000000 +``` + ## How to Build using CMake ```bash diff --git a/src/Example.cpp b/examples/Example.cpp similarity index 96% rename from src/Example.cpp rename to examples/Example.cpp index 6c5fb5d..19ecb0f 100644 --- a/src/Example.cpp +++ b/examples/Example.cpp @@ -1,4 +1,4 @@ -#include "ServiceLocator.hpp" +#include "ServiceLocator/ServiceLocator.hpp" #include diff --git a/src/ServiceLocator.hpp b/include/ServiceLocator/ServiceLocator.hpp similarity index 100% rename from src/ServiceLocator.hpp rename to include/ServiceLocator/ServiceLocator.hpp