这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ cc_library(

cc_binary(
name = "test",
copts = [
"-DINVERSIFY_BINDING_INSPECTION",
],
srcs = glob([
"test/**/*.cpp",
]),
Expand Down
94 changes: 44 additions & 50 deletions include/mosure/binding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,58 @@

namespace mosure::inversify {

template <typename T, typename... SymbolTypes>
class BindingScope {
public:
void inSingletonScope() {
static_assert(
std::is_copy_constructible<T>::value,
"inversify::BindingScope singleton must have copy constructor"
);
template <typename T, typename... SymbolTypes>
class BindingScope {
public:
void inSingletonScope() {
resolver_ = std::make_shared<inversify::CachedResolver<T, SymbolTypes...>>(resolver_);
}

this->factory_ = [this, factory = std::move(factory_)](auto& context) {
if (!this->cached_set_) {
this->cached_ = factory(context);
this->cached_set_ = true;
}
#ifdef INVERSIFY_BINDING_INSPECTION
auto getResolver() const {
return resolver_;
}
#endif

return this->cached_;
};
}
protected:
inversify::ResolverPtr<T, SymbolTypes...> resolver_;
};

protected:
T cached_;
bool cached_set_ { false };
inversify::Factory<T, SymbolTypes...> factory_;
};
template <typename T, typename... SymbolTypes>
class BindingTo
: public BindingScope<T, SymbolTypes...>
{
public:
void toConstantValue(T&& value) {
this->resolver_ = std::make_shared<inversify::ConstantResolver<T, SymbolTypes...>>(value);
}

template <typename T, typename... SymbolTypes>
class BindingTo
: public BindingScope<T, SymbolTypes...>
{
public:
void toConstantValue(T&& value) {
this->factory_ = [val = std::move(value)](auto&) {
return val;
};
}
BindingScope<T, SymbolTypes...>& toDynamicValue(inversify::Factory<T, SymbolTypes...>&& factory) {
this->resolver_ = std::make_shared<inversify::DynamicResolver<T, SymbolTypes...>>(factory);

BindingScope<T, SymbolTypes...>& toDynamicValue(inversify::Factory<T, SymbolTypes...>&& factory) {
this->factory_ = std::move(factory);
return *this;
}

return *this;
}
template <typename U>
BindingScope<T, SymbolTypes...>& to() {
this->resolver_ = std::make_shared<inversify::AutoResolver<T, U, SymbolTypes...>>();

template <typename U>
BindingScope<T, SymbolTypes...>& to() {
this->factory_ = inversify::get_auto_resolver<T, U, SymbolTypes...>();
return *this;
}
};

return *this;
}
};
template <typename T, typename... SymbolTypes>
class Binding
: public BindingTo<typename T::value, SymbolTypes...>
{
public:
inline typename T::value resolve(const inversify::Context<SymbolTypes...>& context) {
if (!this->resolver_) {
throw inversify::exceptions::ResolutionException("inversify::Resolver not found. Malformed binding.");
}

template <typename T, typename... SymbolTypes>
class Binding
: public BindingTo<typename T::value, SymbolTypes...>
{
public:
inline typename T::value resolve(const inversify::Context<SymbolTypes...>& context) {
return this->factory_(context);
}
};
return this->resolver_->resolve(context);
}
};

}
86 changes: 43 additions & 43 deletions include/mosure/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,55 @@

namespace mosure::inversify {

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"
);
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"
);
static_assert(
!meta::is_empty_v<SymbolTypes...>,
"inversify::Container must register at least one symbol"
);

using BindingMap = std::tuple<
inversify::Binding<
SymbolTypes,
SymbolTypes...
>...
>;
using BindingMap = std::tuple<
inversify::Binding<
SymbolTypes,
SymbolTypes...
>...
>;

template <typename T>
inline inversify::BindingTo<typename T::value, SymbolTypes...>& bind() {
static_assert(
meta::contains_v<T, SymbolTypes...>,
"inversify::Container symbol not registered"
);
template <typename T>
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_);
}
return std::get<
inversify::Binding<T, SymbolTypes...>
>(bindings_);
}

template <typename T>
inline typename T::value get() {
static_assert(
meta::contains_v<T, SymbolTypes...>,
"inversify::Container symbol not registered"
);
template <typename T>
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_);
}
return std::get<
inversify::Binding<T, SymbolTypes...>
>(bindings_).resolve(context_);
}

private:
BindingMap bindings_ {};
inversify::Context<SymbolTypes...> context_ { *this };
};
private:
BindingMap bindings_ {};
inversify::Context<SymbolTypes...> context_ { *this };
};

}
12 changes: 6 additions & 6 deletions include/mosure/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

namespace mosure::inversify {

template <typename... SymbolTypes>
class Container;
template <typename... SymbolTypes>
class Container;

template <typename... SymbolTypes>
struct Context {
inversify::IContainer<Container, SymbolTypes...>& container;
};
template <typename... SymbolTypes>
struct Context {
inversify::IContainer<Container, SymbolTypes...>& container;
};

}
6 changes: 3 additions & 3 deletions include/mosure/exceptions/resolution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace mosure::inversify::exceptions {

struct ResolutionException : public std::runtime_error {
explicit ResolutionException(const std::string& msg) : std::runtime_error(msg) { }
};
struct ResolutionException : public std::runtime_error {
explicit ResolutionException(const std::string& msg) : std::runtime_error(msg) { }
};

}
4 changes: 2 additions & 2 deletions include/mosure/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace mosure::inversify {

template <typename T, typename... SymbolTypes>
using Factory = std::function<T(const inversify::Context<SymbolTypes...>&)>;
template <typename T, typename... SymbolTypes>
using Factory = std::function<T(const inversify::Context<SymbolTypes...>&)>;

}
34 changes: 19 additions & 15 deletions include/mosure/injectable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@

namespace mosure::inversify {

template <typename... Dependencies>
struct Inject {
static_assert(
meta::valid_symbol_types_v<Dependencies...>,
"inversify::Injectable dependencies must be of type inversify::Symbol"
);

template <typename... SymbolTypes>
inline static auto resolve(const inversify::Context<SymbolTypes...>& context) {
return std::make_tuple(context.container.template get<Dependencies>()...);
}
};

template <typename Implementation, typename Inject = inversify::Inject<>>
struct Injectable: Inject { };
template <typename... Dependencies>
struct Inject {
static_assert(
meta::valid_symbol_types_v<Dependencies...>,
"inversify::Injectable dependencies must be of type inversify::Symbol"
);

#ifdef INVERSIFY_BINDING_INSPECTION
inline static std::tuple<Dependencies...> dependencies;
#endif

template <typename... SymbolTypes>
inline static auto resolve(const inversify::Context<SymbolTypes...>& context) {
return std::make_tuple(context.container.template get<Dependencies>()...);
}
};

template <typename Implementation, typename Inject = inversify::Inject<>>
struct Injectable: Inject { };

}
52 changes: 26 additions & 26 deletions include/mosure/interfaces/icontainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@

namespace mosure::inversify {

template <
typename T,
typename... SymbolTypes
>
class BindingTo;

template <
template <typename...> class Implementation,
typename... SymbolTypes
>
class IContainer {
public:
template <typename T>
inversify::BindingTo<typename T::value, SymbolTypes...>& bind() {
auto crtpImplementation = static_cast<Implementation<SymbolTypes...> *>(this);

return crtpImplementation->template bind<T>();
}

template <typename T>
typename T::value get() {
auto crtpImplementation = static_cast<Implementation<SymbolTypes...> *>(this);

return crtpImplementation->template get<T>();
}
};
template <
typename T,
typename... SymbolTypes
>
class BindingTo;

template <
template <typename...> class Implementation,
typename... SymbolTypes
>
class IContainer {
public:
template <typename T>
inversify::BindingTo<typename T::value, SymbolTypes...>& bind() {
auto crtpImplementation = static_cast<Implementation<SymbolTypes...> *>(this);

return crtpImplementation->template bind<T>();
}

template <typename T>
typename T::value get() {
auto crtpImplementation = static_cast<Implementation<SymbolTypes...> *>(this);

return crtpImplementation->template get<T>();
}
};

}
Loading