diff --git a/autowiring/CoreContext.h b/autowiring/CoreContext.h index 35f3d3d4f..f0810b88f 100644 --- a/autowiring/CoreContext.h +++ b/autowiring/CoreContext.h @@ -518,8 +518,10 @@ class CoreContext: /// /// Utility method which will inject the specified types into this context - /// Arguments will be passed to the T constructor if provided /// + /// + /// Arguments will be passed to the T constructor if provided + /// template std::shared_ptr Construct(Args&&... args) { // Add this type to the TypeRegistry diff --git a/autowiring/CreationRules.h b/autowiring/CreationRules.h index 00b5ad5a3..d92751657 100644 --- a/autowiring/CreationRules.h +++ b/autowiring/CreationRules.h @@ -33,7 +33,6 @@ struct CreationRules { static typename std::enable_if::value, U*>::type New(Args&&... args) { static_assert(!std::is_abstract::value, "Cannot create a type which is abstract"); static_assert(!has_static_new::value, "Can't inject member with arguments if it has a static new"); - static_assert(!sizeof...(Args) || !has_simple_constructor::value, "Can't inject member with arguments if it has a default constructor"); // Allocate slot first before registration auto* pSpace = Allocate(nullptr); diff --git a/autowiring/TypeUnifier.h b/autowiring/TypeUnifier.h index e0a46f118..b10028ad0 100644 --- a/autowiring/TypeUnifier.h +++ b/autowiring/TypeUnifier.h @@ -12,42 +12,29 @@ class TypeUnifierComplex: public TypeUnifier { public: - template - TypeUnifierComplex(Arg0&& arg0, Args&&... args) : - T(std::forward(arg0), std::forward(args)...) + template + TypeUnifierComplex(Args&&... args) : + T(std::forward(args)...) {} }; -template -class TypeUnifierSimple: - public T, - public TypeUnifier -{}; - /// /// Utility class which allows us to either use the pure type T, or a unifier, as appropriate /// template< class T, - bool inheritsObject = std::is_base_of::value, - bool hasSimpleCtor = has_simple_constructor::value + bool inheritsObject = std::is_base_of::value > struct SelectTypeUnifier; // Anyone already inheriting Object can just use Object -template -struct SelectTypeUnifier { - typedef T type; -}; - -// If T has a simple ctor, we don't want to confuse the compiler with the complex TypeUnifier template -struct SelectTypeUnifier { - typedef TypeUnifierSimple type; +struct SelectTypeUnifier { + typedef T type; }; // Otherwise, if there's a complex ctor, we have to use Args template -struct SelectTypeUnifier { +struct SelectTypeUnifier { typedef TypeUnifierComplex type; }; diff --git a/src/autowiring/test/AutoConstructTest.cpp b/src/autowiring/test/AutoConstructTest.cpp new file mode 100644 index 000000000..c64c0baf4 --- /dev/null +++ b/src/autowiring/test/AutoConstructTest.cpp @@ -0,0 +1,27 @@ +#include "stdafx.h" + +class AutoConstructTest: + public testing::Test +{}; + +class HasDefaultCtorAndOthers { +public: + HasDefaultCtorAndOthers(void) : + v(101) + {} + HasDefaultCtorAndOthers(int v) : + v(v) + {} + + const int v; +}; + +TEST_F(AutoConstructTest, AutoConstructNoArgs) { + AutoConstruct hdcao; + ASSERT_EQ(101, hdcao->v) << "Default constructor was not called as expected"; +} + +TEST_F(AutoConstructTest, AutoConstructWithArgs) { + AutoConstruct hdcao(495); + ASSERT_EQ(495, hdcao->v) << "Constructor call was not made as expected"; +} \ No newline at end of file diff --git a/src/autowiring/test/CMakeLists.txt b/src/autowiring/test/CMakeLists.txt index 4ad1bf22c..087d34955 100644 --- a/src/autowiring/test/CMakeLists.txt +++ b/src/autowiring/test/CMakeLists.txt @@ -1,6 +1,7 @@ set(AutowiringTest_SRCS AnySharedPointerTest.cpp AutoAnchorTest.cpp + AutoConstructTest.cpp AutoFilterTest.cpp AutoInjectableTest.cpp AutoPacketFactoryTest.cpp