-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
282 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "stdafx.h" | ||
|
||
#include "Hypodermic/ContainerBuilder.h" | ||
|
||
#include "TestingTypes.h" | ||
|
||
|
||
namespace Hypodermic | ||
{ | ||
namespace Testing | ||
{ | ||
|
||
BOOST_AUTO_TEST_SUITE(NamedTests) | ||
|
||
BOOST_AUTO_TEST_CASE(should_resolve_named_component) | ||
{ | ||
// Arrange | ||
ContainerBuilder builder; | ||
|
||
// Act | ||
builder.registerType< DefaultConstructible1 >().named< DefaultConstructibleBase >("default1"); | ||
|
||
auto container = builder.build(); | ||
|
||
// Assert | ||
auto instance = container->resolveNamed< DefaultConstructibleBase >("default1"); | ||
BOOST_CHECK(instance != nullptr); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(should_not_resolve_named_component_without_its_name) | ||
{ | ||
// Arrange | ||
ContainerBuilder builder; | ||
|
||
// Act | ||
builder.registerType< DefaultConstructible1 >().named< DefaultConstructibleBase >("default1"); | ||
|
||
auto container = builder.build(); | ||
|
||
// Assert | ||
auto instance = container->resolve< DefaultConstructibleBase >(); | ||
BOOST_CHECK(instance == nullptr); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(should_resolve_the_right_named_component) | ||
{ | ||
// Arrange | ||
ContainerBuilder builder; | ||
|
||
// Act | ||
builder.registerType< DefaultConstructible1 >().named< DefaultConstructibleBase >("default1"); | ||
builder.registerType< DefaultConstructible2 >().named< DefaultConstructibleBase >("default2"); | ||
|
||
auto container = builder.build(); | ||
|
||
// Assert | ||
auto instance = container->resolveNamed< DefaultConstructibleBase >("default1"); | ||
BOOST_CHECK(instance != nullptr); | ||
BOOST_CHECK(std::dynamic_pointer_cast< DefaultConstructible1 >(instance) == instance); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(should_not_conflict_with_anonymous_registrations) | ||
{ | ||
// Arrange | ||
ContainerBuilder builder; | ||
|
||
// Act | ||
auto instance = std::make_shared< DefaultConstructible1 >(); | ||
builder.registerInstance(instance).as< DefaultConstructibleBase >(); | ||
|
||
builder.registerType< DefaultConstructible1 >().named< DefaultConstructibleBase >("default"); | ||
|
||
auto container = builder.build(); | ||
|
||
// Assert | ||
auto resolvedInstance = container->resolve< DefaultConstructibleBase >(); | ||
auto namedInstance1 = container->resolveNamed< DefaultConstructibleBase >("default"); | ||
auto namedInstance2 = container->resolveNamed< DefaultConstructibleBase >("default"); | ||
|
||
BOOST_CHECK(resolvedInstance == instance); | ||
|
||
BOOST_CHECK(namedInstance1 != nullptr); | ||
BOOST_CHECK(namedInstance1 != instance); | ||
|
||
BOOST_CHECK(namedInstance2 != nullptr); | ||
BOOST_CHECK(namedInstance2 != instance); | ||
|
||
BOOST_CHECK(namedInstance1 != namedInstance2); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() | ||
|
||
} // namespace Testing | ||
} // namespace Hypodermic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#pragma once | ||
|
||
#include <type_traits> | ||
|
||
#include "Hypodermic/TypeAliasKey.h" | ||
|
||
|
||
namespace Hypodermic | ||
{ | ||
namespace RegistrationDescriptorOperations | ||
{ | ||
|
||
template | ||
< | ||
class TDescriptor, | ||
class TDescriptorInfo | ||
> | ||
class Named | ||
{ | ||
private: | ||
typedef typename TDescriptorInfo::InstanceType InstanceType; | ||
|
||
template <class TBase, class T> | ||
struct EnforceBaseOf | ||
{ | ||
static_assert(std::is_base_of< TBase, T >::value && !std::is_same< TBase, T >::value, "TBase should be a base of T"); | ||
|
||
static void act() {} | ||
}; | ||
|
||
template <class TBase> | ||
struct EnforceBaseNotAlreadyRegistered | ||
{ | ||
static_assert(!TDescriptorInfo::template IsBaseRegistered< TBase >::value, "TBase is already registered for instance T"); | ||
|
||
static void act() {} | ||
}; | ||
|
||
public: | ||
template <class TBase, class TDelayedDescriptor = TDescriptor> | ||
typename TDelayedDescriptor::template UpdateDescriptor | ||
< | ||
typename TDescriptorInfo::template RegisterBase< TBase >::Type | ||
> | ||
::Type& named(const std::string& name) | ||
{ | ||
EnforceBaseOf< TBase, InstanceType >::act(); | ||
EnforceBaseNotAlreadyRegistered< TBase >::act(); | ||
|
||
auto descriptor = static_cast< TDescriptor* >(this); | ||
descriptor->addTypeIfMissing(createKeyForNamedType< TBase >(name), [](const std::shared_ptr< void >& x) | ||
{ | ||
auto instanceDynamicType = std::static_pointer_cast< InstanceType >(x); | ||
auto instanceStaticType = std::static_pointer_cast< TBase >(instanceDynamicType); | ||
return instanceStaticType; | ||
}); | ||
|
||
auto updatedDescriptor = descriptor->template createUpdate< typename TDescriptorInfo::template RegisterBase< TBase >::Type >(); | ||
descriptor->registrationDescriptorUpdated()(updatedDescriptor); | ||
|
||
return *updatedDescriptor; | ||
} | ||
|
||
protected: | ||
virtual ~Named() {} | ||
}; | ||
|
||
} // namespace RegistrationDescriptorOperations | ||
} // namespace Hypodermic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#include "Hypodermic/ITypeAlias.h" | ||
#include "Hypodermic/TypeInfo.h" | ||
|
||
|
||
namespace Hypodermic | ||
{ | ||
|
||
class NamedTypeAlias : public ITypeAlias | ||
{ | ||
public: | ||
explicit NamedTypeAlias(const TypeInfo& typeInfo, const std::string& name) | ||
: m_typeInfo(typeInfo) | ||
, m_name(name) | ||
{ | ||
} | ||
|
||
bool operator==(const ITypeAlias& rhs) const override | ||
{ | ||
const ITypeAlias* self = this; | ||
if (self == &rhs) | ||
return true; | ||
|
||
auto rhsTypeAlias = dynamic_cast< const NamedTypeAlias* >(&rhs); | ||
if (rhsTypeAlias == nullptr) | ||
return false; | ||
|
||
return m_typeInfo == rhsTypeAlias->m_typeInfo && m_name == rhsTypeAlias->m_name; | ||
} | ||
|
||
std::size_t hashCode() const override | ||
{ | ||
auto hashCode = std::hash< std::type_index >()(m_typeInfo.intrinsicTypeInfo()); | ||
return (hashCode * 397) ^ std::hash< std::string >()(m_name); | ||
} | ||
|
||
const TypeInfo& typeInfo() const override | ||
{ | ||
return m_typeInfo; | ||
} | ||
|
||
private: | ||
TypeInfo m_typeInfo; | ||
std::string m_name; | ||
}; | ||
|
||
} // namespace Hypodermic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.