Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我是否可以注册一个函数,该函数根据定义其类型的字符串调用特定模板函数?_C++_Qt_Templates_Metadata - Fatal编程技术网

C++ 我是否可以注册一个函数,该函数根据定义其类型的字符串调用特定模板函数?

C++ 我是否可以注册一个函数,该函数根据定义其类型的字符串调用特定模板函数?,c++,qt,templates,metadata,C++,Qt,Templates,Metadata,我为Qt编写了一个小编辑器。它接收一个指针,并根据属性的类型(由字符串确定),使用模板函数从中提取数据 (例如,如果类型为“point”,它将调用指针上的getBoundValue。) 这在实践中效果很好。但我将它设置为一个库,以便其他人可以使用它,我假设他们希望添加对其他类型的支持。问题是我不希望他们必须修改原始源代码,而是从类继承,或者注册新类型的回调。我不知道如何实现后一个选项,因为我不能同时传递字符串类型和类类型,可以吗 如果我想允许用户添加对自己类型的支持,他们是否必须实现处理这些值的

我为Qt编写了一个小编辑器。它接收一个指针,并根据属性的类型(由字符串确定),使用模板函数从中提取数据

(例如,如果类型为“point”,它将调用指针上的
getBoundValue
。)

这在实践中效果很好。但我将它设置为一个库,以便其他人可以使用它,我假设他们希望添加对其他类型的支持。问题是我不希望他们必须修改原始源代码,而是从类继承,或者注册新类型的回调。我不知道如何实现后一个选项,因为我不能同时传递字符串类型和类类型,可以吗

如果我想允许用户添加对自己类型的支持,他们是否必须实现处理这些值的属性编辑器的子类?或者,是否有一种方法可以重新组织代码,以便用户可以传入处理新类型(如“矩形”)的功能,以及调用
getBoundValue()
的类型矩形

下面是一些示例代码,我在其中查看对象是否属于某种类型。如果是这样,我调用它的匹配模板函数:

AttributeEditor::update(Bindable* instance) {
    _instance = instance;
    clear();

    for (int i = 0; i < _instance->attributeCount(); i++) {
        Attribute attr = _instance->at(i);

        QList<QStandardItem*> attributeRow;
        QStandardItem* nameItem = new QStandardItem(
            attribute->property("name").toString()
        );

        attributeRow << nameItem;

        QList< QList<QStandardItem*> > subRows;

        if (attr->property("getter").isValid()) {

            std::cout << attr->type().toStdString() << std::endl;

            QString value = "?";
            if (attr->type() == "point3" || attr->type() == "vector3") {
                QVector3D p = getBoundValue<QVector3D>(_instance, attr);

                /* ... */
            }

    /* ... */
}
attributeditor::update(可绑定*实例){
_实例=实例;
清除();
对于(int i=0;i<\u实例->属性计数();i++){
属性属性attr=\u实例->at(i);
QList attributeRow;
QStandardItem*nameItem=新QStandardItem(
属性->属性(“名称”).toString()
);
attributeRow子窗口;
如果(attr->property(“getter”).isValid()){
std::cout type().toStdString()type()=“point3”| | attr->type()=“vector3”){
QVector3D p=getBoundValue(_实例,attr);
/* ... */
}
/* ... */
}

由于您的代码不知道外部类、模板或其他,因此您需要公开一个接受以下内容的注册机制:

  • 类型名称(字符串)。例如“矩形”
  • 接口
  • 该接口应该有一个名为
    getBoundValue
    的纯虚拟方法。所有需要该功能的类都应该实现该接口

    例如:

    struct IShape {
       virtual QVector3D getBoundValue() = 0;
       // more shape methods
       ...
    };
    
    class Rectangle {
       QVector3D getBoundValue();
    };
    
    // register interface prototype - exported from your library
    void RegisterShape( IShape* pShape, const QString& name );
    

    需要记住的是,模板是一种编译时现象。如果您发现自己试图在运行时使用模板进行操作…我建议您可能使用了错误的工具。在任何情况下…为了完整性,您是否可以提供
    属性
    的定义,以及您真正尝试的内容的更多上下文我最终做了类似的事情,但使用了一个“处理程序”类,因为每个属性类型都需要实现一组函数,用户不必单独注册每个函数。就是这样做的,接口包含所有方法,所以只需注册接口即可。