-
Notifications
You must be signed in to change notification settings - Fork 1
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
8 changed files
with
205 additions
and
15 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,33 @@ | ||
// | ||
// This file is part of dingo project <https://github.com/romanpauk/dingo> | ||
// | ||
// See LICENSE for license and copyright information | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
#include <dingo/container.h> | ||
#include <dingo/storage/shared.h> | ||
|
||
// struct A that will be registered with the container | ||
struct A {}; | ||
|
||
//// | ||
// struct B that will be constructed using container | ||
struct B { | ||
A& a; | ||
static B factory(A& a) { return B{a}; } | ||
}; | ||
//// | ||
int main() { | ||
using namespace dingo; | ||
container<> container; | ||
// Register struct A with shared scope | ||
container.register_type<scope<shared>, storage<A>>(); | ||
//// | ||
// Construct instance of B, injecting shared instance of A | ||
B b1 = container.invoke([&](A& a) { return B{a}; }); | ||
B b2 = container.invoke(std::function<B(A&)>([](auto& a) { return B{a}; })); | ||
B b3 = container.invoke(B::factory); | ||
//// | ||
} | ||
|
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,44 @@ | ||
// | ||
// This file is part of dingo project <https://github.com/romanpauk/dingo> | ||
// | ||
// See LICENSE for license and copyright information | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
#pragma once | ||
|
||
#include <dingo/config.h> | ||
#include <dingo/factory/constructor.h> | ||
|
||
#include <functional> | ||
|
||
namespace dingo { | ||
template< typename T > struct invoke: invoke< decltype(&T::operator()) > {}; | ||
|
||
template< typename T, typename R, typename... Args > struct invoke< R(T::*)(Args...) >: invoke< R(T::*)(Args...) const > {}; | ||
|
||
template< typename T, typename R, typename... Args > struct invoke< R(T::*)(Args...) const > { | ||
template< typename Context, typename Container, typename Callable > static R construct(Context& ctx, Container& container, Callable&& callable) { | ||
return callable(((void)sizeof(Args), detail::constructor_argument_impl< void, Context, Container, detail::reference >(ctx, container))...); | ||
} | ||
}; | ||
|
||
template< typename R, typename... Args > struct invoke< std::function<R(Args...)> > { | ||
template< typename Context, typename Container, typename Callable > static R construct(Context& ctx, Container& container, Callable&& callable) { | ||
return callable(((void)sizeof(Args), detail::constructor_argument_impl< void, Context, Container , detail::reference >(ctx, container))...); | ||
} | ||
}; | ||
|
||
template< typename R, typename... Args > struct invoke< R(*)(Args...) > { | ||
template< typename Context, typename Container, typename Callable > static R construct(Context& ctx, Container& container, Callable&& callable) { | ||
return callable(((void)sizeof(Args), detail::constructor_argument_impl< void, Context, Container, detail::reference >(ctx, container))...); | ||
} | ||
}; | ||
|
||
template< typename R, typename... Args > struct invoke< R(Args...) > { | ||
template< typename Context, typename Container, typename Callable > static R construct(Context& ctx, Container& container, Callable&& callable) { | ||
return callable(((void)sizeof(Args), detail::constructor_argument_impl< void, Context, Container, detail::reference >(ctx, container))...); | ||
} | ||
}; | ||
|
||
} |
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,73 @@ | ||
// | ||
// This file is part of dingo project <https://github.com/romanpauk/dingo> | ||
// | ||
// See LICENSE for license and copyright information | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
#include <dingo/container.h> | ||
#include <dingo/storage/external.h> | ||
#include <dingo/storage/shared.h> | ||
#include <dingo/storage/unique.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "containers.h" | ||
#include "test.h" | ||
|
||
namespace dingo { | ||
|
||
template <typename T> struct invoke_test : public test<T> {}; | ||
TYPED_TEST_SUITE(invoke_test, container_types); | ||
|
||
template< typename T = void > struct invoke_test_functions { | ||
static void call(int arg) { result = arg * 2; } | ||
static int result; | ||
|
||
static int call_return(int arg) { return arg * 2; } | ||
}; | ||
|
||
template< typename T > int invoke_test_functions<T>::result; | ||
|
||
TYPED_TEST(invoke_test, invoke) { | ||
using container_type = TypeParam; | ||
|
||
container_type container; | ||
container.template register_type<scope<external>, storage<int>>(2); | ||
|
||
{ | ||
std::function<int(int)> fn = [](int arg) { return arg * 2; }; | ||
ASSERT_EQ(container.invoke(fn), 4); | ||
} | ||
|
||
{ | ||
int result = 0; | ||
std::function<void(int)> fn = [&](int arg) { result = arg * 2; }; | ||
container.invoke(fn); | ||
ASSERT_EQ(result, 4); | ||
} | ||
|
||
{ | ||
ASSERT_EQ(container.invoke([](int arg) { return arg * 2; }), 4); | ||
} | ||
|
||
{ | ||
int result = 0; | ||
container.invoke([&](int arg) { result = arg * 2; }); | ||
ASSERT_EQ(result, 4); | ||
} | ||
|
||
{ | ||
container.invoke(&invoke_test_functions<>::call); | ||
ASSERT_EQ(invoke_test_functions<>::result, 4); | ||
} | ||
|
||
{ | ||
ASSERT_EQ(container.invoke(invoke_test_functions<>::call_return), 4); | ||
ASSERT_EQ(container.invoke(&invoke_test_functions<>::call_return), 4); | ||
} | ||
} | ||
|
||
} | ||
|
||
|