Skip to content

Commit

Permalink
Introducing named-as-itself component registration
Browse files Browse the repository at this point in the history
  • Loading branch information
ybainier committed May 10, 2017
1 parent 96693d4 commit 581333c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
15 changes: 15 additions & 0 deletions Hypodermic.Tests/NamedTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ namespace Testing
BOOST_CHECK(instance != nullptr);
}

BOOST_AUTO_TEST_CASE(should_resolve_named_component_as_itself)
{
// Arrange
ContainerBuilder builder;

// Act
builder.registerType< DefaultConstructible1 >().named("default1");

auto container = builder.build();

// Assert
auto instance = container->resolveNamed< DefaultConstructible1 >("default1");
BOOST_CHECK(instance != nullptr);
}

BOOST_AUTO_TEST_CASE(should_not_resolve_named_component_without_its_name)
{
// Arrange
Expand Down
27 changes: 25 additions & 2 deletions Hypodermic/Named.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,35 @@ namespace RegistrationDescriptorOperations
};

public:
template <class TBase, class TDelayedDescriptor = TDescriptor>
// This template avoids Early Template Instantiation issue
template <class TDelayedDescriptor = TDescriptor>
typename TDelayedDescriptor::template UpdateDescriptor
<
typename TDescriptorInfo::template RegisterBase< TBase >::Type
typename TDescriptorInfo::SelfRegistered::Type
>
::Type& named(const std::string& name)
{
auto descriptor = static_cast< TDescriptor* >(this);
descriptor->addTypeIfMissing(createKeyForNamedType< InstanceType >(name));

auto updatedDescriptor = descriptor->template createUpdate< typename TDescriptorInfo::SelfRegistered::Type >();
descriptor->registrationDescriptorUpdated()(updatedDescriptor);

return *updatedDescriptor;
}

// This template avoids Early Template Instantiation issue
template <class TBase, class TDelayedDescriptor = TDescriptor>
typename std::enable_if
<
!std::is_same< TBase, InstanceType >::value,
typename TDelayedDescriptor::template UpdateDescriptor
<
typename TDescriptorInfo::template RegisterBase< TBase >::Type
>
::Type&
>
::type named(const std::string& name)
{
EnforceBaseOf< TBase, InstanceType >::act();
EnforceBaseNotAlreadyRegistered< TBase >::act();
Expand Down

1 comment on commit 581333c

@ybainier
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes issue #22

Please sign in to comment.