From e5434fd7d15d2a3c0b13ee5113995753ab386e2f Mon Sep 17 00:00:00 2001 From: Lord Turmoil Date: Sun, 24 Sep 2023 16:43:36 +0800 Subject: [PATCH] update: change to static lib --- mioc/CMakeLists.txt | 9 +++++++-- mioc/include/mioc/ServiceContainer.h | 7 +------ mioc/include/mioc/SingletonContainer.h | 26 ++++++++------------------ mioc/src/ServiceContainer.cpp | 12 ++++++++++++ mioc/src/SingletonContainer.cpp | 25 +++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 mioc/src/ServiceContainer.cpp create mode 100644 mioc/src/SingletonContainer.cpp diff --git a/mioc/CMakeLists.txt b/mioc/CMakeLists.txt index 6b1bd0b..3ae4123 100644 --- a/mioc/CMakeLists.txt +++ b/mioc/CMakeLists.txt @@ -1,5 +1,10 @@ # Copyright (C) 2018 - 2023 Tony's Studio. All rights reserved. # CMakeLists for MinIoc. -add_library(mioc INTERFACE) -target_include_directories(mioc INTERFACE include) +# Add all source files +file(GLOB_RECURSE SRC_LIST CONFIGURE_DEPENDS src/*.cpp) + +# Add source to this project's executable. +add_library(mioc STATIC ${SRC_LIST} "src/ServiceContainer.cpp") + +target_include_directories(mioc PUBLIC include) diff --git a/mioc/include/mioc/ServiceContainer.h b/mioc/include/mioc/ServiceContainer.h index 2a984ad..31e0a40 100644 --- a/mioc/include/mioc/ServiceContainer.h +++ b/mioc/include/mioc/ServiceContainer.h @@ -20,9 +20,7 @@ using ServiceContainerPtr = std::shared_ptr; class ServiceContainer final { public: - explicit ServiceContainer(bool lazy = DEFAULT_LAZINESS) : _lazy(lazy) - { - } + explicit ServiceContainer(bool lazy = DEFAULT_LAZINESS); // Any copy of service container is not allowed. ServiceContainer(const ServiceContainer&) = delete; @@ -125,9 +123,6 @@ class ServiceContainer final bool _lazy; }; -// This is a random number, change it as you wish. -int ServiceContainer::_nextTypeId = 75159; - MIOC_END diff --git a/mioc/include/mioc/SingletonContainer.h b/mioc/include/mioc/SingletonContainer.h index 95f0a9a..a4ac15e 100644 --- a/mioc/include/mioc/SingletonContainer.h +++ b/mioc/include/mioc/SingletonContainer.h @@ -13,25 +13,18 @@ MIOC_BEGIN -class SingletonContainer +class SingletonContainer final { public: + SingletonContainer(const SingletonContainer&) = delete; + SingletonContainer& operator=(const SingletonContainer&) = delete; + SingletonContainer(SingletonContainer&&) = delete; + SingletonContainer& operator=(SingletonContainer&&) = delete; + ~SingletonContainer() = default; - static ServiceContainerPtr GetContainer() - { - if (_container == nullptr) - { - _container = ServiceContainer::New(_lazy); - } - return _container; - } - - static ServiceContainerPtr GetContainer(bool lazy) - { - _lazy = lazy; - return GetContainer(); - } + static ServiceContainerPtr GetContainer(); + static ServiceContainerPtr GetContainer(bool lazy); private: // Prevent instantiation. @@ -41,9 +34,6 @@ class SingletonContainer static bool _lazy; }; -std::shared_ptr SingletonContainer::_container; -bool SingletonContainer::_lazy = DEFAULT_LAZINESS; - MIOC_END diff --git a/mioc/src/ServiceContainer.cpp b/mioc/src/ServiceContainer.cpp new file mode 100644 index 0000000..ff16c6f --- /dev/null +++ b/mioc/src/ServiceContainer.cpp @@ -0,0 +1,12 @@ +#include + +MIOC_BEGIN + +ServiceContainer::ServiceContainer(bool lazy) : _lazy(lazy) +{ +} + +// This is a random number, change it as you wish. +int ServiceContainer::_nextTypeId = 75159; + +MIOC_END diff --git a/mioc/src/SingletonContainer.cpp b/mioc/src/SingletonContainer.cpp new file mode 100644 index 0000000..3fe16d9 --- /dev/null +++ b/mioc/src/SingletonContainer.cpp @@ -0,0 +1,25 @@ +#include + +MIOC_BEGIN + + +ServiceContainerPtr SingletonContainer::GetContainer() +{ + if (_container == nullptr) + { + _container = ServiceContainer::New(_lazy); + } + return _container; +} + +ServiceContainerPtr SingletonContainer::GetContainer(bool lazy) +{ + _lazy = lazy; + return GetContainer(); +} + +std::shared_ptr SingletonContainer::_container; +bool SingletonContainer::_lazy = DEFAULT_LAZINESS; + + +MIOC_END