Skip to content

Commit

Permalink
feat: allow copy and move for service container
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-Turmoil committed Nov 30, 2024
1 parent 4986e7b commit 7d68b97
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
name: CMake on multiple platforms
name: Multi-platform Build and Test

on:
push:
Expand Down
8 changes: 4 additions & 4 deletions include/mioc/detail/ServiceContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class ServiceContainer final
}

// Any copy of service container is not allowed.
ServiceContainer(const ServiceContainer&) = delete;
ServiceContainer(const ServiceContainer&&) = delete;
ServiceContainer& operator=(const ServiceContainer&) = delete;
ServiceContainer& operator=(const ServiceContainer&&) = delete;
ServiceContainer(const ServiceContainer& other) = default;
ServiceContainer& operator=(const ServiceContainer& other) = default;
ServiceContainer(ServiceContainer&& other) noexcept = default;
ServiceContainer& operator=(ServiceContainer&& other) noexcept = default;

~ServiceContainer() = default;

Expand Down
3 changes: 0 additions & 3 deletions include/mioc/detail/SingletonContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ class SingletonContainer final
private:
// Prevent instantiation.
SingletonContainer() = default;

static std::shared_ptr<ServiceContainer> _container;
static bool _lazy;
};

MIOC_END
Expand Down
2 changes: 2 additions & 0 deletions src/ServiceContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <mioc/detail/ServiceContainer.h>

#include <utility>

MIOC_BEGIN

// This is a random number, change it as you wish.
Expand Down
13 changes: 3 additions & 10 deletions src/SingletonContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@ MIOC_BEGIN

ServiceContainerPtr SingletonContainer::GetContainer()
{
if (_container == nullptr)
{
_container = ServiceContainer::New(_lazy);
}
return _container;
return GetContainer(DEFAULT_LAZINESS);
}

ServiceContainerPtr SingletonContainer::GetContainer(bool lazy)
{
_lazy = lazy;
return GetContainer();
static std::shared_ptr<ServiceContainer> container = ServiceContainer::New(lazy);
return container;
}

std::shared_ptr<ServiceContainer> SingletonContainer::_container;
bool SingletonContainer::_lazy = DEFAULT_LAZINESS;

MIOC_END
37 changes: 37 additions & 0 deletions tests/AssignmentTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <mioc/mioc.h>
#include "Test.h"
#include "doctest.h"

TEST_CASE("Assignment")
{
mioc::ServiceContainerPtr containerA = mioc::ServiceContainer::New();
mioc::ServiceContainerPtr containerB = mioc::ServiceContainer::New();

// Register interfaces and implementations.
containerA->AddSingleton<IA, A>();
containerA->AddSingleton<IB, B, IA>();

// Copy container.
*containerB = *containerA;

auto a1 = containerA->Resolve<IA>();
auto a2 = containerB->Resolve<IA>();
CHECK_EQ(a1->ToString(), a2->ToString());

const auto b1 = containerA->Resolve<IB>();
const auto b2 = containerB->Resolve<IB>();
CHECK_EQ(b1->ToString(), b2->ToString());

// Register new instance.
const std::shared_ptr<IC> c = std::make_shared<C>(b1);
containerA->AddSingleton(c);
CHECK_NE(containerA->Resolve<IC>(), nullptr);
CHECK_EQ(containerB->Resolve<IC>(), nullptr);

// Move container.
mioc::ServiceContainerPtr containerC = mioc::ServiceContainer::New();
*containerC = std::move(*containerA);
CHECK_EQ(containerC->Resolve<IA>()->ToString(), a1->ToString());
CHECK_EQ(containerC->Resolve<IB>()->ToString(), b1->ToString());
CHECK_EQ(containerC->Resolve<IC>(), nullptr);
}
15 changes: 8 additions & 7 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
aux_source_directory(. TEST_FILES)
add_executable(tests ${TEST_FILES})
set(LIBRARIES mioc)
set(MIOC_TESTS SingletonTest TransientTest AssignmentTest)

target_include_directories(tests PRIVATE .)

target_link_libraries(tests mioc)

add_test(NAME TestAll COMMAND tests)
foreach(test ${MIOC_TESTS})
add_executable(${test} doctest.cpp ${test}.cpp)
target_include_directories(${test} PRIVATE .)
target_link_libraries(${test} ${LIBRARIES})
add_test(NAME ${test} COMMAND ${test})
endforeach(test ${MIOC_TESTS})
File renamed without changes.

0 comments on commit 7d68b97

Please sign in to comment.