青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆 - 70, 文章 - 0, 評論 - 9, 引用 - 0
數據加載中……

Protocol Buffers (協議緩沖) 簡單使用

Each field must be annotated with one of the following modifiers:
required: a value for the field must be provided, otherwise the message will be considered "uninitialized". If libprotobuf is compiled in debug mode, serializing an uninitialized message will cause an assertion failure. In optimized builds, the check is skipped and the message will be written anyway. However, parsing an uninitialized message will always fail (by returning false from the parse method). Other than this, a required field behaves exactly like an optional field.
optional: the field may or may not be set. If an optional field value isn't set, a default value is used. For simple types, you can specify your own default value, as we've done for the phone number type in the example. Otherwise, a system default is used: zero for numeric types, the empty string for strings, false for bools. For embedded messages, the default value is always the "default instance" or "prototype" of the message, which has none of its fields set. Calling the accessor to get the value of an optional (or required) field which has not been explicitly set always returns that field's default value.
repeated: the field may be repeated any number of times (including zero). The order of the repeated values will be preserved in the protocol buffer. Think of repeated fields as dynamically sized arrays.

Now that you have a .proto, the next thing you need to do is generate the classes you'll need to read and write AddressBook (and hence Person and PhoneNumber) messages. To do this, you need to run the protocol buffer compiler protoc on your .proto:
If you haven't installed the compiler, download the package and follow the instructions in the README.
Now run the compiler, specifying the source directory (where your application's source code lives – the current directory is used if you don't provide a value), the destination directory (where you want the generated code to go; often the same as $SRC_DIR), and the path to your .proto. In this case, you...:
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/addressbook.proto
Because you want C++ classes, you use the --cpp_out option – similar options are provided for other supported languages.
This generates the following files in your specified destination directory:
addressbook.pb.h, the header which declares your generated classes.
addressbook.pb.cc, which contains the implementation of your classes.
如:在MSYS下運行 protoc.exe -I=c:/workspace/test/testprotobuf --cpp_out=c:/workspace/test/testprotobuf c:/workspace/test/testprotobuf/addressbook.proto
那么就可在c:/workspace/test/testprotobuf下產生addressbook.pb.h和addressbook.pb.cc。
如:在MSYS下運行 protoc.exe -I=c:/workspace --cpp_out=c:/workspace/test/testprotobuf c:/workspace/test/testprotobuf/addressbook.proto
那么就可在c:/workspace/test/testprotobuf/test/testprotobuf下產生addressbook.pb.h和addressbook.pb.cc。
可以看出-I的作用。


While the numeric id field just has the basic accessor set described above, the name and email fields have a couple of extra methods because they're strings – a mutable_ getter that lets you get a direct pointer to the string, and an extra setter. Note that you can call mutable_email() even if email is not already set; it will be initialized to an empty string automatically. If you had a singular message field in this example, it would also have a mutable_ method but not a set_ method.

Nested Classes
The compiler has also generated a nested class for you called Person::PhoneNumber. If you look at the code, you can see that the "real" class is actually called Person_PhoneNumber, but a typedef defined inside Person allows you to treat it as if it were a nested class. The only case where this makes a difference is if you want to forward-declare the class in another file – you cannot forward-declare nested types in C++, but you can forward-declare Person_PhoneNumber.

Standard Message Methods
Each message class also contains a number of other methods that let you check or manipulate the entire message, including:
bool IsInitialized() const;: checks if all the required fields have been set.
string DebugString() const;: returns a human-readable representation of the message, particularly useful for debugging.
void CopyFrom(const Person& from);: overwrites the message with the given message's values.
void Clear();: clears all the elements back to the empty state.

Parsing and Serialization
Each protocol buffer class has methods for writing and reading messages of your chosen type using the protocol buffer binary format. These include:
bool SerializeToString(string* output) const;: serializes the message and stores the bytes in the given string. Note that the bytes are binary, not text; we only use the string class as a convenient container.
bool ParseFromString(const string& data);: parses a message from the given string.
bool SerializeToOstream(ostream* output) const;: writes the message to the given C++ ostream.
bool ParseFromIstream(istream* input);: parses a message from the given C++ istream.

Protocol Buffers and O-O Design. Protocol buffer classes are basically dumb data holders (like structs in C++); they don't make good first class citizens in an object model. If you want to add richer behaviour to a generated class, the best way to do this is to wrap the generated protocol buffer class in an application-specific class. Wrapping protocol buffers is also a good idea if you don't have control over the design of the .proto file (if, say, you're reusing one from another project). In that case, you can use the wrapper class to craft an interface better suited to the unique environment of your application: hiding some data and methods, exposing convenience functions, etc. You should never add behaviour to the generated classes by inheriting from them. This will break internal mechanisms and is not good object-oriented practice anyway.

Notice the GOOGLE_PROTOBUF_VERIFY_VERSION macro. It is good practice – though not strictly necessary – to execute this macro before using the C++ Protocol Buffer library. It verifies that you have not accidentally linked against a version of the library which is incompatible with the version of the headers you compiled with. If a version mismatch is detected, the program will abort. Note that every .pb.cc file automatically invokes this macro on startup.
注意GOOGLE_PROTOBUF_VERIFY_VERSION宏。你最好像這樣——盡管這不是嚴格要求的——在使用C++ Protocol Buffer庫之前執行該宏。它會檢查你是不是在無意中鏈接到了與你使用的頭文件不兼容的protocol buffer庫。如果檢測到了不匹配情況,程序會中止運行下去。注意:每一個.pb.cc文件在開始的時候都會自動調用該宏。

Also notice the call to ShutdownProtobufLibrary() at the end of the program. All this does is delete any global objects that were allocated by the Protocol Buffer library. This is unnecessary for most programs, since the process is just going to exit anyway and the OS will take care of reclaiming all of its memory. However, if you use a memory leak checker that requires that every last object be freed, or if you are writing a library which may be loaded and unloaded multiple times by a single process, then you may want to force Protocol Buffers to clean up everything.

Extending a Protocol Buffer
Sooner or later after you release the code that uses your protocol buffer, you will undoubtedly want to "improve" the protocol buffer's definition. If you want your new buffers to be backwards-compatible, and your old buffers to be forward-compatible – and you almost certainly do want this – then there are some rules you need to follow. In the new version of the protocol buffer:
you must not change the tag numbers of any existing fields.
you must not add or delete any required fields.
you may delete optional or repeated fields.
you may add new optional or repeated fields but you must use fresh tag numbers (i.e. tag numbers that were never used in this protocol buffer, not even by deleted fields).
If you follow these rules, old code will happily read new messages and simply ignore any new fields. To the old code, optional fields that were deleted will simply have their default value, and deleted repeated fields will be empty. New code will also transparently read old messages. However, keep in mind that new optional fields will not be present in old messages, so you will need to either check explicitly whether they're set with has_, or provide a reasonable default value in your .proto file with [default = value] after the tag number. If the default value is not specified for an optional element, a type-specific default value is used instead: for strings, the default value is the empty string. For booleans, the default value is false. For numeric types, the default value is zero. Note also that if you added a new repeated field, your new code will not be able to tell whether it was left empty (by new code) or never set at all (by old code) since there is no has_ flag for it.

Optimization Tips
The C++ Protocol Buffers library is extremely heavily optimized. However, proper usage can improve performance even more. Here are some tips for squeezing every last drop of speed out of the library:
Reuse message objects when possible. Messages try to keep around any memory they allocate for reuse, even when they are cleared. Thus, if you are handling many messages with the same type and similar structure in succession, it is a good idea to reuse the same message object each time to take load off the memory allocator. However, objects can become bloated over time, especially if your messages vary in "shape" or if you occasionally construct a message that is much larger than usual. You should monitor the sizes of your message objects by calling the SpaceUsed method and delete them once they get too big.
Your system's memory allocator may not be well-optimized for allocating lots of small objects from multiple threads. Try using Google's tcmalloc instead.

Advanced Usage
Protocol buffers have uses that go beyond simple accessors and serialization. Be sure to explore the C++ API reference to see what else you can do with them.
One key feature provided by protocol message classes is reflection. You can iterate over the fields of a message and manipulate their values without writing your code against any specific message type. One very useful way to use reflection is for converting protocol messages to and from other encodings, such as XML or JSON. A more advanced use of reflection might be to find differences between two messages of the same type, or to develop a sort of "regular expressions for protocol messages" in which you can write expressions that match certain message contents. If you use your imagination, it's possible to apply Protocol Buffers to a much wider range of problems than you might initially expect!


參考:http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/cpptutorial.html  (示例)

posted on 2011-01-24 09:27 seahouse 閱讀(2286) 評論(0)  編輯 收藏 引用 所屬分類: 數據

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲国产精品视频| 久久国产精品99国产| 亚洲欧美日韩一区二区| 一本色道久久88精品综合| 亚洲毛片一区| 亚洲一区二区视频| 亚洲一区二区三区在线| 先锋亚洲精品| 麻豆久久精品| 亚洲精品综合精品自拍| 亚洲免费影院| 久久裸体艺术| 欧美日韩成人网| 免费在线日韩av| 欧美一级精品大片| 韩国av一区二区三区四区| 亚洲高清久久| 亚洲一区二区精品视频| 久久精品成人一区二区三区| 欧美成人综合| 亚洲主播在线观看| 久久久蜜桃一区二区人| 欧美日韩国产高清| 影音先锋一区| 午夜免费久久久久| 亚洲电影欧美电影有声小说| 99成人在线| 久久九九免费| 国产精品久久久爽爽爽麻豆色哟哟| 激情综合视频| 午夜精品婷婷| 亚洲三级影片| 久久久综合精品| 国产欧美日韩综合一区在线播放| 亚洲国产精品久久久久久女王| 午夜精品一区二区三区电影天堂 | 久久中文在线| 一区二区三区四区五区视频| 久久午夜精品| 激情校园亚洲| 久久影院亚洲| 欧美一级一区| 国产精品一区二区久久国产| 一道本一区二区| 亚洲国产黄色| 狂野欧美激情性xxxx欧美| 国产午夜精品美女视频明星a级| 亚洲午夜久久久久久尤物 | 久久最新视频| 亚洲欧美日韩国产一区二区三区| 欧美日韩精品是欧美日韩精品| 亚洲黄色性网站| 欧美粗暴jizz性欧美20| 免费观看日韩av| 91久久国产综合久久蜜月精品 | 亚洲视频图片小说| 欧美精品免费在线观看| 亚洲美女电影在线| 亚洲国产精品久久久久秋霞影院| 久久蜜桃精品| 亚洲欧洲午夜| 亚洲毛片av| 亚洲国产日本| 久久一区精品| 久久精品女人天堂| 国产亚洲欧洲997久久综合| 久久精视频免费在线久久完整在线看 | 久久久精品一品道一区| 激情文学一区| 欧美黑人在线播放| 欧美日本网站| 亚洲制服av| 午夜国产欧美理论在线播放 | 一区二区三区精品视频| 日韩视频免费观看高清在线视频| 欧美日韩高清在线一区| 亚洲欧美日韩在线高清直播| 欧美亚洲网站| 亚洲高清影视| 日韩网站在线看片你懂的| 国产精品视频免费观看www| 欧美专区福利在线| 麻豆91精品91久久久的内涵| 一区二区欧美亚洲| 性色av香蕉一区二区| 亚洲国产高清aⅴ视频| 亚洲免费电影在线观看| 国产亚洲欧美一级| 亚洲三级毛片| 国产一区二区三区在线观看视频 | 性色av一区二区三区在线观看| 欧美在线视频播放| 亚洲精品在线免费| 欧美亚洲视频| 99精品国产在热久久婷婷| 亚洲欧美一区二区原创| 亚洲国内在线| 午夜精品久久久久久久99樱桃 | 99re8这里有精品热视频免费| 国产精品影视天天线| 欧美激情一区二区久久久| 国产精品每日更新| 亚洲高清视频中文字幕| 国产欧美日韩视频在线观看| 亚洲欧洲精品一区二区| 国产综合久久久久久鬼色| 亚洲精品麻豆| 亚洲电影欧美电影有声小说| 先锋a资源在线看亚洲| 一二三区精品福利视频| 久久久久久久久久看片| 欧美在线free| 欧美一区二视频| 尤物yw午夜国产精品视频明星| 亚洲精品永久免费| 在线看国产一区| 亚洲欧美资源在线| 亚洲深夜福利视频| 美女露胸一区二区三区| 久久久国产精品一区二区三区| 欧美色另类天堂2015| 亚洲第一色在线| 在线观看视频一区二区| 欧美影院在线播放| 久久国产直播| 国产拍揄自揄精品视频麻豆| 99re在线精品| 一本色道久久综合精品竹菊| 男人的天堂亚洲| 欧美成人午夜视频| 伊人久久久大香线蕉综合直播| 午夜老司机精品| 久久成人综合网| 国产亚洲欧美日韩一区二区| 性欧美videos另类喷潮| 久久精品国产久精国产爱| 国产伦精品一区二区三区高清 | 亚洲精品自在久久| 99re这里只有精品6| 欧美久久久久中文字幕| 亚洲国产午夜| 99re亚洲国产精品| 欧美日韩日日骚| 亚洲一级电影| 久久久久国产精品一区三寸 | 亚洲欧美日韩第一区| 欧美午夜在线观看| 亚洲免费在线| 老牛嫩草一区二区三区日本| 在线成人激情视频| 欧美国产欧美亚洲国产日韩mv天天看完整 | 亚洲综合精品自拍| 国产视频观看一区| 久久久久9999亚洲精品| 欧美成人一区二区在线| 日韩天堂在线视频| 国产精品丝袜白浆摸在线| 久久av资源网站| 亚洲国产欧美不卡在线观看| 国产精品一区一区| 亚洲综合日韩| 久久av一区二区三区亚洲| 国产日韩av一区二区| 久久成人亚洲| 91久久久国产精品| 欧美成人激情视频| 一区二区三区国产盗摄| 国产精品高潮久久| 久久av一区二区三区亚洲| 欧美大学生性色视频| 一区二区久久久久| 国产午夜精品麻豆| 欧美激情偷拍| 欧美一区三区二区在线观看| 亚洲国产精品日韩| 欧美在线播放一区二区| 最新国产拍偷乱拍精品 | 亚洲激情一区| 亚洲综合色丁香婷婷六月图片| 欧美在线视频免费播放| 激情成人av| 欧美日韩久久不卡| 久久精品99国产精品| aa级大片欧美三级| 久久综合中文字幕| 午夜日韩av| 99日韩精品| 在线观看欧美日本| 国产欧美日韩综合一区在线观看 | 国产精品色午夜在线观看| 久久综合九色欧美综合狠狠| 亚洲一区视频在线| 99亚洲精品| 亚洲片在线资源| 欧美激情欧美狂野欧美精品| 久久国产夜色精品鲁鲁99| 亚洲专区在线视频| 99精品视频免费全部在线| 欧美三级视频在线| 欧美成年人视频网站|