-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tuple map bindings + lambda resolvers (#6)
* feat: use variant bindings in container's symbol map This support faster lookup speeds as we are no longer using `std::any`. * reduce test flake across platforms * update single_includes * fix: windows optimize copts * docs: remove old api * lower the bar a bit (speed test) * refactor: tuple >>> map + variant (for binding map) * remove unused include * only use container.template get... call when necessary * add consumed std types * fix: allow for duplicate symbol types + allow struct symbols * docs: refer to the containerless branch * test: use catch2 BENCHMARK * refactor: remove resolver classes * remove unused meta structs * refactor: cache/constant resolver lambdas * fix: codacy extra newline
- Loading branch information
Showing
35 changed files
with
744 additions
and
509 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
build:windows --copt="/std:c++17" --copt="/W4" | ||
build:windows --copt="/std:c++17" --copt="/W4" --copt="/O2" | ||
build:linux --copt="-std=c++17" --copt="-O3" | ||
build:macos --copt="-std=c++17" | ||
build:macos --copt="-std=c++17" --copt="-O3" |
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 |
---|---|---|
|
@@ -33,3 +33,5 @@ | |
|
||
.vscode/ | ||
bazel-* | ||
|
||
callgrind.out.* |
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ cc_library( | |
"-Wall", | ||
"-Wextra", | ||
"-Wshadow", | ||
"-Wunused", | ||
], | ||
hdrs = glob([ | ||
"include/**/*.hpp", | ||
|
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,13 @@ | ||
load("@rules_cc//cc:defs.bzl", "cc_binary") | ||
|
||
|
||
cc_binary( | ||
name = "profiling", | ||
srcs = [ | ||
"main.cpp", | ||
], | ||
deps = [ | ||
"//:inversify", | ||
], | ||
visibility = ["//visibility:public"] | ||
) |
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,46 @@ | ||
#include <iostream> | ||
|
||
#include <mosure/inversify.hpp> | ||
|
||
|
||
namespace inversify = mosure::inversify; | ||
|
||
using int_symbol = inversify::Symbol<int>; | ||
|
||
struct Service { | ||
int val; | ||
|
||
Service() : val(0) { } | ||
Service(int val) : val(val) { } | ||
|
||
void foo() { | ||
std::cout << "hello: " << val << std::endl; | ||
} | ||
}; | ||
|
||
template <> | ||
struct inversify::Injectable<Service> | ||
: inversify::Inject< | ||
int_symbol | ||
> | ||
{ }; | ||
|
||
using test_symbol = inversify::Symbol<Service>; | ||
|
||
|
||
template <typename Container> | ||
void to_profile(Container& container) { | ||
container.template get<test_symbol>().foo(); | ||
} | ||
|
||
int main() { | ||
inversify::Container< | ||
int_symbol, | ||
test_symbol | ||
> container; | ||
|
||
container.bind<int_symbol>().toConstantValue(3); | ||
container.bind<test_symbol>().to<Service>(); | ||
|
||
to_profile(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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,31 +1,64 @@ | ||
#pragma once | ||
|
||
#include <tuple> | ||
|
||
#include <mosure/binding.hpp> | ||
#include <mosure/context.hpp> | ||
#include <mosure/interfaces/icontainer.hpp> | ||
#include <mosure/meta.hpp> | ||
|
||
|
||
namespace mosure::inversify { | ||
|
||
template <typename Symbol> | ||
struct BindingLookup { | ||
inline static inversify::Binding<typename Symbol::value> binding {}; | ||
}; | ||
|
||
class Container : public inversify::IContainer<Container> { | ||
template <typename... SymbolTypes> | ||
class Container | ||
: public inversify::IContainer<Container, SymbolTypes...> | ||
{ | ||
public: | ||
static_assert( | ||
meta::valid_symbol_types_v<SymbolTypes...>, | ||
"inversify::Container symbols must be of type inversify::Symbol" | ||
); | ||
|
||
static_assert( | ||
!meta::is_empty_v<SymbolTypes...>, | ||
"inversify::Container must register at least one symbol" | ||
); | ||
|
||
using BindingMap = std::tuple< | ||
inversify::Binding< | ||
SymbolTypes, | ||
SymbolTypes... | ||
>... | ||
>; | ||
|
||
template <typename T> | ||
inline inversify::BindingTo<typename T::value>& bind() { | ||
return BindingLookup<T>::binding; | ||
inline inversify::BindingTo<typename T::value, SymbolTypes...>& bind() { | ||
static_assert( | ||
meta::contains_v<T, SymbolTypes...>, | ||
"inversify::Container symbol not registered" | ||
); | ||
|
||
return std::get< | ||
inversify::Binding<T, SymbolTypes...> | ||
>(bindings_); | ||
} | ||
|
||
template <typename T> | ||
inline typename T::value get() const { | ||
return BindingLookup<T>::binding.resolve(context_); | ||
inline typename T::value get() { | ||
static_assert( | ||
meta::contains_v<T, SymbolTypes...>, | ||
"inversify::Container symbol not registered" | ||
); | ||
|
||
return std::get< | ||
inversify::Binding<T, SymbolTypes...> | ||
>(bindings_).resolve(context_); | ||
} | ||
|
||
private: | ||
inversify::Context context_ { *this }; | ||
BindingMap bindings_ {}; | ||
inversify::Context<SymbolTypes...> context_ { *this }; | ||
}; | ||
|
||
} |
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
Oops, something went wrong.