Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue resolving const dependencies #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Hypodermic.Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(HypodermicTests_sources
ProvidedInstanceFactoryRegistrationTests.cpp
ProvidedInstanceRegistrationTests.cpp
RegistrationTests.cpp
ResolveConstPtrTests.cpp
RuntimeRegistrationTests.cpp
UseIfNoneTests.cpp
)
Expand Down
91 changes: 91 additions & 0 deletions Hypodermic.Tests/ResolveConstPtrTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "stdafx.h"

#include "Hypodermic/ContainerBuilder.h"


namespace Hypodermic
{
namespace Testing
{
namespace
{

class Foo
{
public:
int value() const
{
return 42;
}
};

class Bar
{
public:
explicit Bar(std::shared_ptr<Foo> foo) noexcept : _foo(std::move(foo))
{ }

const std::shared_ptr<Foo>& foo() const noexcept
{
return _foo;
}

private:
std::shared_ptr<Foo> _foo;
};

class Baz
{
public:
explicit Baz(std::shared_ptr<const Foo> foo) noexcept : _foo(std::move(foo))
{ }

const std::shared_ptr<const Foo>& foo() const noexcept
{
return _foo;
}

private:
std::shared_ptr<const Foo> _foo;
};

} // namespace

BOOST_AUTO_TEST_SUITE(ResolveConstPtrTests)

BOOST_AUTO_TEST_CASE(should_resolve_non_const_ptr)
{
// Arrange
ContainerBuilder builder;
builder.registerType<Foo>();
builder.registerType<Bar>();
auto container = builder.build();

// Act
const auto bar = container->resolve<Bar>();
auto value = bar->foo()->value();

// Assert
BOOST_CHECK_EQUAL(value, 42);
}

BOOST_AUTO_TEST_CASE(should_resolve_const_ptr)
{
// Arrange
ContainerBuilder builder;
builder.registerType<Foo>();
builder.registerType<Baz>();
auto container = builder.build();

// Act
const auto baz = container->resolve<Bar>();
auto value = baz->foo()->value();

// Assert
BOOST_CHECK_EQUAL(value, 42);
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace Testing
} // namespace Hypodermic
19 changes: 19 additions & 0 deletions Hypodermic/ArgumentResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ namespace Traits
};


template <class TArg>
struct ArgumentResolver< std::shared_ptr< const TArg > >
{
typedef std::shared_ptr< const TArg > Type;

template <class TRegistration, class TResolutionContext>
static Type resolve(const TRegistration& registration, TResolutionContext& resolutionContext)
{
static_assert(IsComplete< TArg >::value, "TArg should be a complete type");

auto&& factory = registration.getDependencyFactory(Utils::getMetaTypeInfo< TArg >());
if (factory)
return std::shared_ptr< const TArg >(std::static_pointer_cast<TArg>(factory(resolutionContext.componentContext())));

return std::shared_ptr< const TArg >(resolutionContext.componentContext().template resolve< TArg >());
}
};


template <class TArg>
struct ArgumentResolver< std::vector< std::shared_ptr< TArg > > >
{
Expand Down