polymorphism with nested extensions (Qt/c++)
Hi
I'm new in protobuf and would need it to store different values in an
binary file.
I am using Qt 5.3 as development Environment.
My problem is to access and store the values inside the messages “Cat” and
“Dog”
I think that storing the data is working right because I am able to access
parts of it.
But when I try to go deeper it crashes :-(
extended adressbook.proto example
package dataLog;
//==============================================================================
Animal
message Animal
{
extensions 100 to max;
enum AnimalType
{
Type_Cat = 1;
Type_Dog = 2;
}
required AnimalType type = 1;
}
//==============================================================================
Cat
message Cat
{
extend Animal
{
required Cat beast = 100; // Unique Animal extension number
}
optional bool declawed = 1;
}
//==============================================================================
Dog
message Dog
{
extend Animal
{
required Dog beast = 101; // Unique Animal extension number
}
optional uint32 bones_buried = 1;
}
//==============================================================================
Person
message Person
{
required string name = 1;
required int32 id = 2;
required Animal pet = 3;
optional string email = 4;
enum PhoneType
{
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber
{
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 5;
}
//==============================================================================
Address Book
message AddressBook
{
repeated Person person = 1;
}
The new code based on the adressbook example:
Adding new People
//=============================================================================
add Person
void DataLog::addPerson( dataLog::Person* person )
{
person->set_id( 9 );
person->set_name( "Bob" );
person->set_email( "mail.com" );
dataLog::Person::PhoneNumber* phone_number = person->add_phone();
phone_number->set_number( "987654321" );
phone_number->set_type( dataLog::Person::MOBILE );
//person->set_allocated_pet( addAnimal(dataLog::Animal::Type_Dog) );
dataLog::Animal * pAnimal = person->mutable_pet(); //new
dataLog::Animal;
pAnimal->set_type( dataLog::Animal::Type_Dog );
dataLog::Dog *pDog = pAnimal->MutableExtension( dataLog::Dog::beast );
pDog->set_bones_buried( 989 );
//person->set_allocated_pet( pAnimal );
}
Reading the person data – ID, Name, email, and phone are working great but
not so the animal :-(
//=============================================================================
write People
void DataLog::writePeople()
{
for( int i=0; i<address_book.person_size(); i++ )
{
const dataLog::Person& person = address_book.person( i );
//... Name, Id, Phone
//Animal
qDebug() << " Has Pet:" << person.has_pet();
dataLog::Animal animal = person.pet();
switch( animal.type() )
{
case dataLog::Animal::Type_Cat:
break;
case dataLog::Animal::Type_Dog:
{
qDebug() << " Beast = Dog";
dataLog::Dog *pDog =
animal.MutableExtension(dataLog::Dog::beast);
qDebug() << " bones_buried: " <<
pDog->bones_buried(); //<<---- _CRASH !!!!!!
}
break;
defualt:
qDebug() << " Unknow Animal";
break;
}
} // for
qDebug() << "READ END";
}
--
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.