How to delete arbitrary objects in repeated field? (protobuf)

后端 未结 5 1235
广开言路
广开言路 2021-02-18 17:51

I have some entries in the repeated field in my proto. Now I want delete some of them. How can I accomplish this? There is a function to delete the last element, but I want to d

5条回答
  •  忘了有多久
    2021-02-18 18:24

    Here is example:

    message GuiChild
    {
        optional string widgetName = 1;
        //..
    }
    
    message GuiLayout
    {
        repeated ChildGuiElement children = 1;
        //..
    }
    
    typedef google_public::protobuf::RepeatedPtrField RepeatedField;
    typedef google_public::protobuf::Message Msg;
    
    GuiLayout guiLayout; 
    //Init children as necessary..
    
    GuiChild child;
    //Set child fileds..
    
    DeleteElementsFromRepeatedField(*child, guiLayout->mutable_children());
    
    void DeleteElementsFromRepeatedField(const Msg& msg, RepeatedField* repeatedField)
    {
        for (RepeatedField::iterator it = repeatedField->begin(); it != repeatedField->end(); it++)
        {
            if (google_public::protobuf::util::MessageDifferencer::Equals(*it, msg))
            {
                repeatedField->erase(it);
                break;
            }
        }
    }
    

提交回复
热议问题