Well I am about to expose my ignorance on so many levels.
I have a message, that contains repeated messages, something like this:
message person {
optional string personName =1;
optional float age =2;
optional int32 ssn =3;
}
message simpleDB {
optional string dbName =1;
repeated person people =2;
}
Now I have read that PB's don't have a "delete[arrayIndex]" for performance
reasons. Instead the idea is too swap messages, so the message to delete is
at the end of the list, and do a removeLast();
So I can swap messages via something like:
sally->Swap(dick);
Where sally and dick are mutable pointers of type person. (
sally=myDB->mutable_people(i); )
But now how I do I do the final act of removing the last message from the
list of "people" messages?
I have attached "easy to follow" code to help explain what I am doing.
(seems I am not the only one who struggles with this)
Thanks.
Regards
Carl
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
#include <iostream>
#include "simple_pb.h"
int main()
{
int i;
std::cout << "Start\n";
simpleDB *myDB;
person *personPB;
myDB= new simpleDB;
myDB->set_dbname("Overlord");
std::cout << "DB name=" << myDB->dbname() << "\n";
personPB=myDB->add_people();
personPB->set_personname("Tom");
personPB->set_age(1.);
personPB->set_ssn(10);
personPB=myDB->add_people();
personPB->set_personname("Dick");
personPB->set_age(2.);
personPB->set_ssn(9);
personPB=myDB->add_people();
personPB->set_personname("Harry");
personPB->set_age(3.);
personPB->set_ssn(8);
personPB=myDB->add_people();
personPB->set_personname("Sally");
personPB->set_age(4.);
personPB->set_ssn(7);
std::cout << "Current order:\n";
for(i=0;i<myDB->people_size();i++)
std::cout << "\t" << myDB->people(i).personname()
<< "\t" << myDB->people(i).age()
<< "\t" << myDB->people(i).ssn()
<< "\n";
// swap Sally and Dick
person *dick;
person *sally;
for(i=0;i<4;i++)
{
if(myDB->people(i).personname() == "Dick")
{
std::cout << "Found Dick..." << i << "\n";
dick=myDB->mutable_people(i);
}
if(myDB->people(i).personname() == "Sally")
{
std::cout << "Found Sally..." << i << "\n";
sally=myDB->mutable_people(i);
}
}
std::cout << "Swapping Sally and Dick:\n";
sally->Swap(dick);
std::cout << "Current order:\n";
for(i=0;i<myDB->people_size();i++)
std::cout << "\t" << myDB->people(i).personname()
<< "\t" << myDB->people(i).age()
<< "\t" << myDB->people(i).ssn()
<< "\n";
std::cout << "Removing Dick:\n";
// What goes here?
std::cout << "Final order:\n";
for(i=0;i<myDB->people_size();i++)
std::cout << "\t" << myDB->people(i).personname()
<< "\t" << myDB->people(i).age()
<< "\t" << myDB->people(i).ssn()
<< "\n";
std::cout << "End\n";
return 0;
}