我试图实现DependencyInjectable行为,并且在实例化时可以使用适当的依赖项注入从此行为派生的类。
下面我尝试从更大的项目中提取和压缩代码示例,以说明我遇到的问题。它有点冗长,我为此道歉。
#include <tuple>
#include <type_traits>
template <typename, typename> struct tuple_has_type;
template <typename T> struct tuple_has_type<T, std::tuple<>> : std::false_type { };
template <typename T, typename U, typename ... Args> struct tuple_has_type<T, std::tuple<U, Args ...>> : tuple_has_type<T, std::tuple<Args ...>> { };
template <typename T, typename ... Args> struct tuple_has_type<T, std::tuple<T, Args ...>> : std::true_type { };
template<typename ... Dependencies>
class DependencyInjectable
{
private:
template<int index, typename ... Args>
struct assign_dependencies
{
void operator () (std::tuple<Dependencies ...> &lhs, std::tuple<Args ...> &&rhs)
{
typedef typename std::tuple_element<index, std::tuple<Dependencies ...>>::type T;
std::get<T>(lhs) = std::get<T>(rhs);
assign_dependencies<index - 1, Args ...> { } (lhs, std::forward<std::tuple<Args ...>>(rhs));
}
};
template<typename ... Args>
struct assign_dependencies<0, Args ...>
{
void operator() (std::tuple<Dependencies ...> &lhs, std::tuple<Args ...> &&rhs)
{
typedef typename std::tuple_element<0, std::tuple<Dependencies ...>>::type T;
std::get<T>(lhs) = std::get<T>(rhs);
}
};
public:
template<typename ... Args>
DependencyInjectable(Args ... dependencies)
{
setDependencies(std::forward<Args>(dependencies) ...);
}
virtual ~DependencyInjectable(void) { }
template<typename T> auto getDependency(void) const ->
typename std::enable_if<tuple_has_type<T, std::tuple<Dependencies ...>>::value, T>::type
{
return std::get<T>(m_dependencies);
}
template<typename T, typename U = typename std::decay<T>::type>
auto setDependency(T &&dependency) ->
typename std::enable_if<tuple_has_type<U, std::tuple<Dependencies ...>>::value, void>::type
{
std::get<U>(m_dependencies) = dependency;
}
template<typename ... Args>
void setDependencies(Args ... dependencies)
{
constexpr auto size = std::tuple_size<std::tuple<Dependencies ...>>::value;
static_assert(size > 0, "List of dependencies must be specified.");
assign_dependencies<size - 1, Args ...> { } (m_dependencies, std::forward_as_tuple(
std::forward<Args>(dependencies) ...));
}
private:
std::tuple<Dependencies ...> m_dependencies; // an std::tuple containing injection dependencies
};
class DependencyOfBase { };
class DependencyOfDerived { };
class Base
: public DependencyInjectable<DependencyOfBase *>
{
public:
Base(DependencyOfBase *pDependencyOfBase)
: DependencyInjectable<DependencyOfBase *>(pDependencyOfBase)
{
}
virtual ~Base(void) { }
};
class Derived
: public Base, public DependencyInjectable<DependencyOfDerived *>
{
public:
using Base::getDependency;
using DependencyInjectable<DependencyOfDerived *>::getDependency;
Derived(DependencyOfBase *pDependencyOfBase, DependencyOfDerived *pDependencyOfDerived)
: Base(pDependencyOfBase),
DependencyInjectable<DependencyOfDerived *>(pDependencyOfDerived)
{
}
virtual ~Derived(void) { }
};
int main(int argc, char **argv)
{
DependencyOfBase dependencyOfBase;
DependencyOfDerived dependencyOfDerived;
Base base(&dependencyOfBase);
Derived derived(&dependencyOfBase, &dependencyOfDerived);
auto *pDependencyOfBase = base.getDependency<DependencyOfBase *>();
auto *pDependencyOfDerived = derived.getDependency<DependencyOfDerived *>();
return (pDependencyOfBase != nullptr && pDependencyOfDerived != nullptr) ? 0 : 1;
}
特别是,我使用std :: tuple在DependencyInjectable类中存储依赖项,并且我试图在元组不包含请求的类型时使用enable_if来禁用getDependency()函数。在main中,我调用Derived的getDependency()函数的实例,并指定&#34; DependencyOfDerived *&#34;。只要我指定
,这就有效using Base::getDependency;
using DependencyInjectable<DependencyOfDerived *>::getDependency;
在Derived类中的,但我认为enable_if会在&#34; DependencyOfDerived *&#34;的基础中禁用getDependency()函数。模板参数,因为Base元组不包含该类型。我在这里缺少什么?
如果我评论using语句,这是我从gcc获得的以下输出:
在函数&#39; int main(int,char **)&#39;: 116:42:错误:请求成员&#39; getDependency&#39;很暧昧 45:31:注意:候选者是:template typename std :: enable_if&gt; :: value,T&gt; :: type DependencyInjectable :: getDependency()const [with T = T;依赖关系= {DependencyOfDerived *}] 45:31:注意:模板typename std :: enable_if&gt; :: value,T&gt; :: type DependencyInjectable :: getDependency()const [with T = T;依赖关系= {DependencyOfBase *}] 116:76:错误:在&#39; *&#39;之前预期的主要表达代币 116:77:错误:在&#39;&gt;&#39;之前预期的主要表达代币 116:79:错误:在&#39;)之前预期的主要表达式令牌