指针函数的映射与参数宏

时间:2014-12-19 10:31:14

标签: c++ templates dictionary macros function-pointers

我正在尝试创建一个包含构造函数的地图。为此,我使用这种方法:

automatic registration of object creator function with a macro

所以在DLL中我有这样的数据结构:

namespace pods
{


struct PODS_API StructExample{
    double  d;
    bool    b;
    int     i;
    StructExample() : d(0), b(false), i(0) {};
    StructExample(QList<QVariant> paramList) : 
        d(paramList[0].toDouble()), 
        b(paramList[1].toBool()), 
        i(paramList[2].toInt())
    {};
    StructExample(double param_d, bool param_b, int param_i) : d(param_d), b(param_b), i(param_i) {};
};
REGISTER_VARIABLE_TYPE(StructExample);
}

我正在尝试创建一个将调用正确构造函数的工厂(实现正确的结构)。

这些结构包含在我称之为VariableT的对象中。这是一个类似的模板类:

namespace variable{

template <class T>
class VariableT : public AbstractVariableT<T>, public IVariable {

public:
VariableT() {};
VariableT(const QString& name, const QList<QVariant> listParam) : name(name), item(T(listParam)) {};

~VariableT(){};

T* getData()
{
    return &item;
};

void setData(T* t)
{
    item = *t;
};


private:
T item;

};

}

当使用正确的T(即StructExample)调用VariableT时,我可以为这些结构实现容器。 VariableT继承自IVariable,因此我可以在程序的任何地图中使用对这些容器的引用作为IVariable。

现在我正试图通过我之前引用的例子来启发一个功能。

这将允许我添加结构并在我的程序中使用它们,只需更改代码。

我使我的VariableT(struct的容器)继承自AbstractVariableT。

namespace variable
{

#define REGISTER_VARIABLE_TYPE(T) bool isRegistered_##T =     \
VariableFactory::instance()->                                 \
regVariable<T>(#T,  (VariableT<T>::createVariable)(const QString&, const QList<QVariant>&));

template <class T>
class  AbstractVariableT : public QObject
{
public:
static IVariable* createVariable(const QString& name, const QList<QVariant>& listParam) { return new VariableT<T>(name,listParam); }

virtual ~AbstractVariableT(){};


protected: 
QString name;

};

}

在本课程中,我使用函数createVariable来调用VariableT的构造函数方法。由于AbstractVariableT是一个模板类,因此根据所使用的Struct的类型,它将调用另一个构造函数。

为了调用createVariable函数,我使用的是一个名为VariableFactory的类:

namespace variable
{

class  VariableFactory
{

public:

static VariableFactory* instance();


static IVariable* createVariable(const QString& idVariable, const QString& name, const QList<QVariant>& listParam);

template <class T>
bool regVariable(const QString& idVariable, IVariable* (*creator)(const QString&, const QList<QVariant>&))
{
    variableCreators[idVariable] = creator;
    return true;
}


private:
QMap<QString, IVariable* (*)(const QString&, const QList<QVariant>&)> variableCreators;
};
}

在这个类中,我存储了一个名为我的CreateVariable方法的variableCreator的地图。 (即一个地图键/值将是:

"StructExample"/AbstractVariableT::CreateVariable<StructExample>(const QString& name, const QList<QVariant>& listParam);   

或类似的......)。

为了在地图中添加构造函数,我使用函数regVariable。

为了自动添加不同StructContainer的每个构造函数,我想使用一个调用regVariable的宏。

#define REGISTER_VARIABLE_TYPE(T) bool isRegistered_##T = VariableFactory::instance()->regVariable<T>(#T,  (VariableT<T>::createVariable)(const QString&, const QList<QVariant>&));

我在我的工厂使用过这个(取决于我的类型,它从我的地图中获取正确的函数并使用这些参数来实现该功能:

IVariable* VariableFactory::createVariable(const QString& typeVariable, const QString& name, const QList<QVariant>& listParam)
{
    return instance()->variableCreators[typeVariable](name, listParam);
}

我无法找到正确的语法来执行此操作。或者我可以这样做吗?

当我尝试在Struct(StructExample)下使用宏时。我得到了不同的错误。

看起来我无法正确注册构造函数:

Error   1   error C2653: 'VariableFactory' : is not a class or namespace name   
Error   5   error C2275: 'pods::StructExample' : illegal use of this type as an expression  
Error   2   error C2227: left of '->regVariable' must point to class/struct/union/generic type  
Error   8   error C2143: syntax error : missing ')' before 'const'  
Error   4   error C2065: 'VariableT' : undeclared identifier    
Error   7   error C2065: 'createVariable' : undeclared identifier   
Error   9   error C2059: syntax error : ')' 
Error   3   error C2039: 'VariableT' : is not a member of 'variable'    
Error   6   error C2039: 'createVariable' : is not a member of '`global namespace'' 
Error   10  error C1903: unable to recover from previous error(s); stopping compilation 

此问题已修改。

感谢您的帮助。 埃里克

0 个答案:

没有答案