From b00d0d3e825a398fac0989dfdea98319f8ff7216 Mon Sep 17 00:00:00 2001 From: Marco Poletti Date: Sun, 1 May 2016 11:44:50 +0100 Subject: [PATCH] Fix a bug in registerFactory(), where compilation would fail if there were multiple non-assisted parameters with different types. This is related (but not the same as) issue #12. --- include/fruit/impl/meta/component.h | 2 +- tests/CMakeLists.txt | 3 ++ tests/register_factory_autoinject3.cpp | 58 +++++++++++++++++++++++++ tests/register_factory_autoinject4.cpp | 59 ++++++++++++++++++++++++++ tests/register_factory_autoinject5.cpp | 56 ++++++++++++++++++++++++ 5 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 tests/register_factory_autoinject3.cpp create mode 100644 tests/register_factory_autoinject4.cpp create mode 100644 tests/register_factory_autoinject5.cpp diff --git a/include/fruit/impl/meta/component.h b/include/fruit/impl/meta/component.h index e237b781..117a08c2 100644 --- a/include/fruit/impl/meta/component.h +++ b/include/fruit/impl/meta/component.h @@ -243,7 +243,7 @@ struct RemoveAssisted { // Non-assisted case template struct apply { - using type = PushFront(CurrentResult, T); + using type = PushBack(CurrentResult, T); }; // Assisted case diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cca20ddd..78e6f6f0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -119,6 +119,9 @@ register_constructor_not_existing_with_annotation.cpp register_constructor_with_annotation.cpp register_factory_autoinject.cpp register_factory_autoinject2.cpp +register_factory_autoinject3.cpp +register_factory_autoinject4.cpp +register_factory_autoinject5.cpp register_factory_autoinject_from_provider.cpp register_factory_autoinject_from_provider_with_annotation.cpp register_factory_autoinject_with_binding.cpp diff --git a/tests/register_factory_autoinject3.cpp b/tests/register_factory_autoinject3.cpp new file mode 100644 index 00000000..fec7ff86 --- /dev/null +++ b/tests/register_factory_autoinject3.cpp @@ -0,0 +1,58 @@ +// expect-success +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "test_macros.h" + +using fruit::Component; +using fruit::Injector; +using fruit::Assisted; +using fruit::createComponent; + +struct X {}; +struct Y {}; + +struct Foo { + Foo(X x, Y y) { + (void)x; + (void)y; + } +}; + +using FooFactory = std::function; + +fruit::Component getComponent() { + static X x = X(); + static Y y = Y(); + return fruit::createComponent() + .bindInstance(x) + .bindInstance(y) + .registerFactory( + [](X x, Y y) { + return Foo(x, y); + }); +} + + +int main() { + fruit::Injector injector(getComponent()); + FooFactory fooFactory(injector); + Foo foo = fooFactory(); + (void)foo; + + return 0; +} diff --git a/tests/register_factory_autoinject4.cpp b/tests/register_factory_autoinject4.cpp new file mode 100644 index 00000000..cfef925c --- /dev/null +++ b/tests/register_factory_autoinject4.cpp @@ -0,0 +1,59 @@ +// expect-success +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "test_macros.h" + +using fruit::Component; +using fruit::Injector; +using fruit::Assisted; +using fruit::createComponent; + +struct X {}; +struct Y {}; +struct Z {}; + +struct Foo { + Foo(X, Y, int, float, Z) { + } +}; + +using FooFactory = std::function; + +fruit::Component getComponent() { + static X x = X(); + static Y y = Y(); + static Z z = Z(); + return fruit::createComponent() + .bindInstance(x) + .bindInstance(y) + .bindInstance(z) + .registerFactory, fruit::Assisted, Z)>( + [](X x, Y y, int n, float a, Z z) { + return Foo(x, y, n, a, z); + }); +} + + +int main() { + fruit::Injector injector(getComponent()); + FooFactory fooFactory(injector); + Foo foo = fooFactory(1, 3.4); + (void)foo; + + return 0; +} diff --git a/tests/register_factory_autoinject5.cpp b/tests/register_factory_autoinject5.cpp new file mode 100644 index 00000000..0bd001c1 --- /dev/null +++ b/tests/register_factory_autoinject5.cpp @@ -0,0 +1,56 @@ +// expect-success +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "test_macros.h" + +using fruit::Component; +using fruit::Injector; +using fruit::Assisted; +using fruit::createComponent; + +struct X {}; +struct Y {}; + +struct Foo { + Foo(int, float, X, Y, double) { + } +}; + +using FooFactory = std::function; + +fruit::Component getComponent() { + static X x = X(); + static Y y = Y(); + return fruit::createComponent() + .bindInstance(x) + .bindInstance(y) + .registerFactory, fruit::Assisted, X, Y, fruit::Assisted)>( + [](int n, float a, X x, Y y, double d) { + return Foo(n, a, x, y, d); + }); +} + + +int main() { + fruit::Injector injector(getComponent()); + FooFactory fooFactory(injector); + Foo foo = fooFactory(1, 3.4, 3.456); + (void)foo; + + return 0; +}