More Related Content More from Ciklum Ukraine (20) CiklumCPPSat24032012:SergeyBratus-GoogleProtocolBuffers2. Как все это работает
• Создайте .proto файл в котором будет информация о
том что вы будете сериализовать
• Сгенерируйте файлы с учетом нужного языка
программирования используя утилиту protoc.exe
• Добавьте код для вызова функций сериализации
4. Other Languages
• Action Script
•C
• C#
• Haskell
• Javascript
• Objective C
• Perl
• PHP
• Ruby
• Scala
• Visual Basic
5. Proto файл
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
6. С чего начинается .proto
Message
message SearchRequest
{
optional int32 page_number = 1;
}
7. Message Field Types
• double, float, int32, int64, uint32, uint64, sint32, sint64,
fixed32, fixed64, sfixed32, sfixed64, bool, string, bytes
• enum Smth
{
UNIVERSAL = 0;
WEB = 1;
IMAGES = 2;
}
• Other Messages
10. Optional Field
optional Int32 foo = 1;
• bool has_foo() const
• int32 foo() const
• void set_foo(int32 value)
• void clear_foo()
IsInitialized = true;
foo вернет дефолтное значение
11. String Field
optional string foo = 1;
required string foo = 1;
• bool has_foo() const
• const string& foo() const
• void set_foo(const string& value)
• void set_foo(const char* value)
• void set_foo(const char* value, int size)
• string* mutable_foo()
• void clear_foo()
• string* release_foo()
12. Enum Fields
enum Bar { BAR_VALUE = 1; }
optional Bar foo = 1;
required Bar foo = 1;
• bool has_foo() const
• Bar foo() const
• void set_foo(Bar value)
• void clear_foo()
13. Message Fields
message Bar {}
optional Bar foo = 1;
required Bar foo = 1;
• bool has_foo() const
• const Bar& foo() const
• Bar* mutable_foo()
• void clear_foo()
• Bar* release_foo()
14. Repeated Numeric Fields
repeated int32 foo = 1;
• int foo_size() const
• int32 foo(int index) const
• void set_foo(int index, int32 value)
• void add_foo(int32 value)
• void clear_foo()
• const RepeatedField<int32>& foo() const
• RepeatedField<int32>* mutable_foo()
15. Repeated String Fields
repeated string foo = 1;
repeated bytes foo = 1;
• int foo_size() const
• const string& foo(int index) const
• void set_foo(int index, const string& value)
• void set_foo(int index, const char* value)
• void set_foo(int index, const char* value, int size)
• string* mutable_foo(int index)
• void add_foo(const string& value)
• void add_foo(const char* value)
• void add_foo(const char* value, int size)
• string* add_foo()
• void clear_foo()
• const RepeatedPtrField<string>& foo() const
• RepeatedPtrField<string>* mutable_foo()
16. Repeated Enum Fields
enum Bar { BAR_VALUE = 1; }
repeated Bar foo = 1;
• int foo_size() const
• Bar foo(int index) const
• void set_foo(int index, Bar value)
• void add_foo(Bar value)
• void clear_foo()
• const RepeatedField<int>& foo() const
• RepeatedField<int>* mutable_foo()
17. Repeated Embedded Message Fields
message Bar {}
repeated Bar foo = 1;
• int int foo_size() const
• const Bar& foo(int index) const
• Bar* mutable_foo(int index)
• Bar* add_foo()
• void clear_foo()
• const RepeatedPtrField<Bar>& foo() const
• RepeatedPtrField<Bar>* mutable_foo()
18. Enumerations
enum Foo {
VALUE_A = 1;
VALUE_B = 5;
}
• const const EnumDescriptor* Foo_descriptor()
• bool Foo_IsValid(int value)
20. String
String => std::string
Bytes => std::string
25. Pack Functions
Bool SerializeToCodedStream(io::CodedOutputStream * output)
const
Bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream *
output) const
Bool SerializeToString(string * output) const
Bool SerializeToArray(void * data, int size) const
String SerializeAsString() const
Bool AppendToString(string * output) const
26. Cookies
• Поддержка extensions
message Foo { // ... extensions 100 to 199; }
extend Foo { optional int32 bar = 126; }
• Packages
package foo.bar;
message Open { ... }
• Options
option optimize_for = CODE_SIZE;
option optimize_for = SPEED;