C ++ protobuf is not just serialization

Source: Internet
Author: User

What is the relationship between cppblog and blog Park? Do I have to send communications with people on both sides?

In C ++, protobuf is a common serialization library. It can send network messages and parse messages conveniently. xml can be used and can be used. But it is not just a serialization library.


Simply put, protobuf adds the C # attribute function to C ++. C ++ now has metadata! C # students must have understood the meaning of this sentence.


1. Use protobuf as the configuration file:
Protobuf provides a serialization format of textformat, similar to the json format, which is clear and easy to read. For example, a behavior Tree node description file:


Data is defined:


Message BehaviorNodeConf
{
Required int32 type = 1;
// Parameters required by the condition
Repeated int32 args = 2;
// Contains multiple subnodes
Repeated BehaviorNodeConf node = 3;
};


Message BehaviorTreeConf
{
// Behavior tree type: AI, ON_HIT, ON_HITTED...
Required int32 type = 1;
// Behavior Tree node
Required BehaviorNodeConf node = 2;
}; The configuration file is:


Type: 5
Node:
{
Type: 1
Node:
{
Type: 101
Args: 2
}
Node:
{
Type: 1
Node:
{
Type: 1001
Args: 0
Args: 100
}
Node:
{
Type: 1001
Args: 1
Args:-1, 100
}
}
} The following two lines of code can parse the configuration file: BehaviorTreeConf;
Google: protobuf: TextFormat: ParseFromString (fileContent, & conf); II. Reflection usage of protobuf
Many people say that C ++ is difficult to do Orm, because there is no reflection, etc. With protobuf, it is not a problem, as shown below:


Select Operation: The select statement is automatically generated. query a record and save it to the person variable.


Try
{
DbHelper dbHelper ("tcp: // 127.0.0.1: 3306", "root", "111111 ");


DbResultPtr result = dbHelper. Execute (
Select <Person> (Column ("*")).
Where (Column ("age")> 10)
);


Auto person = boost: make_shared <Person> ();
Result-> One (person );
ASSERT_EQ (26, person-> age ());
}
Catch (const Exception & e)
{
} Update operation: the update statement is automatically generated. This code is extracted from my unit test. You can understand it in TEST_F (UpdateTest, Update_Where)
{
Std: string expectedSql;
ExpectedSql =
"Update Egametang. Person"
"Set guid = 1, age = 18, comment = 'a good student! '"
"Where age> 10 ";
Person person;
Person. set_guid (1 );
Person. set_age (18 );
Person. set_comment ("a good student! ");
Update update (person );
Update. Where (Column ("age")> 10 );
EXPECT_EQ (expectedSql, update. ToString ());
} 3. protbuf is similar to c # attribute.
See the following protobuf definition: import "google/protobuf/descriptor. proto ";


Extend google. protobuf. FileOptions
{
Optional string my_file_option = 50000;
}
Extend google. protobuf. MessageOptions
{
Optional int32 my_message_option = 50001;
}
Extend google. protobuf. FieldOptions
{
Optional onal float my_field_option = 50002;
}
Extend google. protobuf. EnumOptions
{
Optional bool my_enum_option = 50003;
}
Extend google. protobuf. EnumValueOptions
{
Optional uint32 my_enum_value_option = 50004;
}
Extend google. protobuf. ServiceOptions
{
Optional MyEnum my_service_option = 50005;
}
Extend google. protobuf. MethodOptions
{
Optional MyMessage my_method_option = 50006;
}


Option (my_file_option) = "Hello world! ";


Message MyMessage
{
Option (my_message_option) = 1234;


Optional int32 foo = 1 [(my_field_option) = 4.5];
Optional string bar = 2;
}


Enum MyEnum
{
Option (my_enum_option) = true;


FOO = 1 [(my_enum_value_option) = 321];
BAR = 2;
}


Message RequestType {}
Message ResponseType {}


Service MyService
{
Option (my_service_option) = FOO;


Rpc MyMethod (RequestType) returns (ResponseType)
{
// Note: my_method_option has type MyMessage. We can set each field
// Within it using a separate "option" line.
Option (my_method_option). foo = 567;
Option (my_method_option). bar = "Some string ";
}
} In protobuf, option is the attribute in C #. option can also be placed on the message (class OF c #) service (Method of c #) and field of message.


4. How can we use the protobuf function in game development?
1. Plan to use protobuf as the configuration file. In data definition, I can set the option control of a field to C,


The editor can directly generate a control when reading the data definition. Therefore, you can generate an editor based on the data definition to fill in the data for the planning. For example:


Message BehaviorNodeConf
{
Required int32 type = 1 [control = "textbox" max = 100 min = 0];
// Parameters required by the condition
Repeated int32 args = 2 [control = "textbox"];
}; 2. another example: a skill can be used to cast a buff. Each buff should have an associated skill. Therefore, when planning to fill out a skill, you must enter a proto table and a buff proto table, we can add a constraint for the two table plans to check by the editor: message Spell.
{
Required int32 id = 1;
Optional int32 buffId = 2 [ref = "Buff. id"];
}


Message Buff
{
Required int32 id = 1 [ref = "Spell. buffId"];
Optional int32 time = 2;
} If you fill in a buff in the editor, the editor will check whether there are any skills associated with this buff. When you fill in the skill, if you fill in the buff, You Need To forcibly configure the corresponding buff, which greatly reduces configuration errors, the plan was freed from the error in Table filling. It's easy to plan. Is it far from a programmer? He is so nice to me! Protobuf write a simple orm, the current implementation of the select and update, other subsequent additions, the principle is the same Code address: https://github.com/egametang/Egametang/tree/master/Cpp/Platform/Orm



Author Tang Hai

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.