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

Resolve const #55

Open
wants to merge 3 commits 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
121 changes: 121 additions & 0 deletions Hypodermic.Tests/ResolveConstPtrTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#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>();
auto container = builder.build();

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

// Assert
BOOST_CHECK_EQUAL(value, 42);
}

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

// Act
const auto cfoo = container->resolve<const Foo>();
auto value = cfoo->value();

// Assert
BOOST_CHECK_EQUAL(value, 42);
}

BOOST_AUTO_TEST_CASE(should_resolve_non_const_ptr_argument)
{
// 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_argument)
{
// Arrange
ContainerBuilder builder;
builder.registerType<Foo>();
builder.registerType<Baz>();
auto container = builder.build();

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

// Assert
BOOST_CHECK_EQUAL(value, 42);
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace Testing
} // namespace Hypodermic
5 changes: 3 additions & 2 deletions Hypodermic/ComponentContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ namespace Hypodermic
template <class T>
bool tryToRegisterType(IRegistrationScope& scope, std::true_type /* T has autowireable constructor */)
{
auto&& factory = Traits::ConstructorDescriptor< T >::describe();
using MutableT = std::remove_const_t<T>;
auto&& factory = Traits::ConstructorDescriptor< MutableT >::describe();

scope.addRegistration(m_runtimeRegistrationBuilder->build
(
Utils::getMetaTypeInfo< T >(),
Utils::getMetaTypeInfo< MutableT >(),
[factory](const IRegistration& r, IResolutionContext& c) { return std::static_pointer_cast< void >(factory(r, c)); }
));

Expand Down