在函数体内定义函数

函数指针的使用

Posted by 雯饰太一 on June 25, 2023

在函数体内定义函数

使用lambda表达式,可以临时建立指定类型的函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
AbsBehaviorTree BuildTreeFromXML(const QDomElement& bt_root, const NodeModels& models )
{
    AbsBehaviorTree tree;

    if( bt_root.tagName() != "BehaviorTree" )
    {
        throw std::runtime_error( "Expecting a node called <BehaviorTree>");
    }

    //-------------------------------------定义lambda表达式,在函数内定义函数
    std::function<void(AbstractTreeNode* parent, QDomElement)> recursiveStep;
    recursiveStep = [&](AbstractTreeNode* parent, QDomElement xml_node)
    {
        // The nodes with a ID used that QString to insert into the registry()
        QString modelID = xml_node.tagName();
        if( xml_node.hasAttribute("ID") )
        {
            modelID = xml_node.attribute("ID");
        }

        AbstractTreeNode tree_node;

        auto model_it = models.find(modelID);
        if( model_it ==  models.end() )
        {
             throw std::runtime_error( (QString("This model has not been registered: ") + modelID).toStdString() );
        }
        tree_node.model = model_it->second;

        if( xml_node.hasAttribute("name") )
        {
            tree_node.instance_name = ( xml_node.attribute("name") );
        }
        else{
            tree_node.instance_name = modelID;
        }

        auto attributes = xml_node.attributes();
        for( int attr=0; attr < attributes.size(); attr++ )
        {
            auto attribute = attributes.item(attr).toAttr();
            if( attribute.name() != "ID" && attribute.name() != "name")
            {
                tree_node.ports_mapping.insert( { attribute.name(), attribute.value() } );
            }
        }

        auto added_node = tree.addNode(parent, std::move(tree_node));

        for (QDomElement  child = xml_node.firstChildElement( )  ;
             !child.isNull();
             child = child.nextSiblingElement( ) )
        {
            recursiveStep( added_node, child );
        }
    };

    auto first_child = bt_root.firstChildElement();
    if( first_child.tagName() == "Root")
    {
        QMessageBox::question(nullptr,
                              "Fix your file!",
                              "Please remove the node <Root> from your <BehaviorTree>",
                              QMessageBox::Ok );
        first_child = first_child.firstChildElement();
    }

    // start recursion
    recursiveStep( nullptr, first_child );

    return tree;
}

std::function这个关键字很好用,配合lambda表达式,可以再一个函数内容很方便的封装功能,并且可以让代码看着很整洁,但是这样的代码并不能向前兼容,如果有些工作环境要求同时兼容不同的版本,就要注意少用。

如果在多个函数中,有着公用的部分,也是需要进行局部重构,将这些公用的部分提成私有成员函数,或者是调整为inline类型的全局函数。