我有这个课程:
template<class C>
class OperatorsMap
{
typedef void (C::*voidFunctionType)(void);
private:
std::map<long, voidFunctionType> m;
public:
template<typename T>
void Insert(long id, T f1)
m.insert(std::make_pair(id,(voidFunctionType)f1));
template<typename O, typename... Args>
bool SearchAndCall(long id, O *obj, Args&&... args)
{
auto mapIter = m.find(id);
if(mapIter == m.end()) return false;
auto mapVal = mapIter->second;
auto Fun = (bool(C::*)(Args ...))(mapVal);
return (obj->*Fun)(std::forward<Args>(args)...);
}
};
class IConditionBase
{
public:
virtual ConditionInfo GetInfo() = 0;
virtual void ClearOperators() = 0;
};
template<class C>
class ConditionBase : public IConditionBase
{
public:
template<typename O>
ConditionBase(O object){mObject = object;}
~ConditionBase(){}
ConditionInfo GetInfo(){return mInfo;}
template<typename F>
void AddOperator(long id, const char* name, F fun)
{
mInfo.AddOperatorInfo(name, id);
mOperators.Insert(id, fun);
}
template<typename... Args>
bool CallOperator(long id, Args&&... args)
{
return mOperators.SearchAndCall(id, mObject, std::forward<Args>(args)...);
}
private:
ConditionInfo mInfo;
OperatorsMap<C> mOperators;
C* mObject;
class ModuleConditionService
{
public:
ModuleConditionService();
~ModuleConditionService();
template<typename... Args>
bool EvaluateCondition(long conditionID, long operatorID, Args&&... args)
{
bool ret = false;
IConditionBase* cnd = GetCondition(conditionID);
switch(conditionID)
{
case ID_ACTUAL_TRIP_ID: (static_cast<cndActualTripID>(cnd))->CallOperator(operatorID, std::forward<Args>(args)...);
default: cout << "Condition with ID " << conditionID << " not found!";
}
return ret;
}
private:
void AddCondition(IConditionBase* condition);
IConditionBase *GetCondition(long conditionID);
vector<IConditionBase*> mConditionContainer;
};
class cndActualTripID : public ConditionBase<cndActualTripID>
{
public:
cndActualTripID();
~cndActualTripID();
using ConditionBase<cndActualTripID>::ConditionBase;
bool operator_Equal(long id);
bool operator_Greater(long id);
bool operator_IN(unsigned int idsCount, long ids[]);
};
在ModuleConditionService
的构造函数中,我创建了这样的条件:
AddCondition(new cndActualTripID());
然后在main中我调用EvaluateCondition:
ModuleConditionService* service = new ModuleConditionService();
long arr[] = {111,122,125,0,129};
bool ret1 = service->EvaluateCondition(1, 10, 5, arr);
bool ret2 = service->EvaluateCondition(1, 9, 89);
bool ret3 = service->EvaluateCondition(1, 8, 122);
一切正常,但我认为函数EvaluateCondition
并不是很好。现在只有一个条件cndActualTripID
但是当有100个条件时,那个类型转换(static_cast<cndActualTripID>(cnd))->CallOperator(operatorID, std::forward<Args>(args)...);
将会非常混乱,并且.h文件中的类型转换我认为并不是一个好主意。当函数CallOperator
位于IConditionBase
virtual
时,它会有所帮助,但它是模板函数:(我必须从args
的参数得到EvaluateCondition
SearchAndCall
中的OperatorsMap
,但我不知道如何。请帮助我或者您有其他想法,如何致电CallOperator
上课cndActualTripID
(和其他)基于ID
(ID
是继承属性,每个ConditionBase
)。我使用c ++ 11。它只是代码的重要部分,如果缺少,请告诉我发布什么。
非常感谢。
答案 0 :(得分:0)
我将我的程序重新设计为写入n.m.没有可变参数但只有一个固定(向量)。现在我有类ConditionBaseOperators,我存储和调用函数。它比以前更好(更安全)吗? 感谢。
template<class C>
class MODULECONDITIONDOMAIN_API ConditionBaseOperators : public ConditionBase
{
typedef bool (C::*Operators)(vector<string>);
public:
template<typename O>
ConditionBaseOperators(O obj){mObject = obj;}
~ConditionBaseOperators(){}
void AddOperator(long id, string name, Operators function)
{
AddOperatorInfo(id, name.data());
mOperators.insert(make_pair(id, function));
}
bool CallOperator(long id, vector<string> args)
{
bool ret = false;
auto it = mOperators.find(id);
if(it != mOperators.end())
{
auto fun = (bool(C::*)(vector<string>))(it->second);
ret = (mObject->*fun)(args);
}
else
EERROR("Operator with ID %d not found in module %s", id, GetInfo().GetName().c_str());
return ret;
}
private:
map<long,Operators> mOperators;
C* mObject;
};
class MODULECONDITIONDOMAIN_API ConditionBase : public Wertyz::DomainLayer::Core::DomainObject
{
public:
ConditionBase(){}
~ConditionBase(){}
ConditionInfo GetInfo();
void SetInfo(ConditionInfo info);
void SetInfo(long id, string name);
void AddOperatorInfo(long id, const char* name);
virtual bool CallOperator(long id, vector<string> args) = 0;
long VtoL(vector<string> vec, unsigned int idx);
unsigned long VtoUL(vector<string> vec, unsigned int idx);
double VtoD(vector<string> vec, unsigned int idx);
int VtoI(vector<string> vec, unsigned int idx);
long long VtoLL(vector<string> vec, unsigned int idx);
vector<long> VtoVL(vector<string> vec);
private:
ConditionInfo mInfo;
};
class SERVICECORE_API ModuleConditionService
{
public:
ModuleConditionService();
~ModuleConditionService();
bool EvaluateCondition(long conditionID, long operatorID, vector<string> args);
private:
void AddCondition(ConditionBase *condition);
void AddAction(ActionBase* action);
ActionBase *GetAction(long idx);
ConditionBase *GetCondition(long conditionID);
vector<ConditionBase*> mConditionContainer;
vector<ActionBase*> mActionContainer;
};
class MODULECONDITIONDOMAIN_API cndActualTripID : public ConditionBaseOperators<cndActualTripID>
{
public:
cndActualTripID();
~cndActualTripID();
using ConditionBaseOperators<cndActualTripID>::ConditionBaseOperators;
private:
bool operator_Equal(vector<string> args);
bool operator_Greater(vector<string> args);
bool operator_IN(vector<string> args);
long GetValue();
};
ModuleCondition.cpp
bool ModuleConditionService::EvaluateCondition(long conditionID, long operatorID, vector<string> args)
{
return GetCondition(conditionID)->CallOperator(operatorID, args);
}
cndActualTripID.cpp
cndActualTripID::cndActualTripID() : ConditionBaseOperators<cndActualTripID>::ConditionBaseOperators(this)
{
SetInfo(ID_ACTUAL_TRIP_ID, "ActualTripID");
AddOperator(1, "Greater", &cndActualTripID::operator_Greater);
AddOperator(2, "Equal", &cndActualTripID::operator_Equal);
AddOperator(3, "IN", &cndActualTripID::operator_IN);
}