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

隨筆 - 70, 文章 - 0, 評論 - 9, 引用 - 0
數(shù)據(jù)加載中……

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

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庫之前執(zhí)行該宏。它會檢查你是不是在無意中鏈接到了與你使用的頭文件不兼容的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)  編輯 收藏 引用 所屬分類: 數(shù)據(jù)

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            久久精品成人一区二区三区蜜臀| 欧美在线在线| 欧美三日本三级少妇三2023| 久久人人爽国产| 久久综合99re88久久爱| 蜜桃av一区二区| 欧美精品日韩一本| 国产精品久久久久毛片软件| 国产区日韩欧美| 1024成人网色www| 一本大道久久a久久精二百| 亚洲婷婷综合色高清在线| 午夜精品久久久久久久白皮肤| 欧美一区二区视频观看视频| 久久综合导航| 亚洲卡通欧美制服中文| 亚洲免费影视第一页| 久久久久九九视频| 欧美三级韩国三级日本三斤| 国产精品一区二区你懂的| 在线观看成人av电影| 亚洲专区在线视频| 女女同性女同一区二区三区91| 亚洲精品欧美极品| 久久福利电影| 欧美日韩一区二区国产| 黑人操亚洲美女惩罚| 亚洲天堂黄色| 欧美国产日本韩| 亚洲欧美日韩综合| 欧美日韩成人在线| 伊人精品视频| 性8sex亚洲区入口| 亚洲精品欧美激情| 久久久噜噜噜久久中文字幕色伊伊| 欧美日本一区二区视频在线观看| 国产欧美日韩一区| 亚洲视频在线视频| 亚洲国产精品久久| 久久er精品视频| 国产精品嫩草99av在线| 日韩一二三区视频| 亚洲成色www久久网站| 欧美一区二区三区四区在线观看 | 中国av一区| 免费成人黄色| 国产精品捆绑调教| 99av国产精品欲麻豆| 欧美aⅴ一区二区三区视频| 亚洲免费在线| 国产精品视频九色porn| 一区二区三区精品视频| 亚洲国产一区二区视频| 老鸭窝91久久精品色噜噜导演| 国产色婷婷国产综合在线理论片a| 亚洲天堂偷拍| 中文亚洲欧美| 国产精品香蕉在线观看| 先锋影院在线亚洲| 亚洲欧美日韩网| 国产一区二区在线免费观看| 羞羞漫画18久久大片| 亚洲影视在线播放| 国产婷婷色一区二区三区| 欧美在线观看网站| 欧美综合77777色婷婷| 国产在线不卡| 欧美成人精品福利| 欧美日韩xxxxx| 亚洲自拍偷拍麻豆| 午夜精品久久久| 尤物99国产成人精品视频| 欧美大成色www永久网站婷| 免费观看成人| 亚洲图片欧洲图片av| 亚洲一区二区欧美| 国产一区二区三区自拍| 免费不卡中文字幕视频| 欧美国产日本韩| 亚洲欧美日韩国产一区| 久久国产精品久久久久久电车| 在线观看中文字幕不卡| 亚洲国产日本| 国产精品视频精品视频| 欧美88av| 国产精品久久一区主播| 久久久另类综合| 欧美精品v日韩精品v韩国精品v | 欧美日韩欧美一区二区| 午夜国产精品影院在线观看| 欧美一区二区成人| 亚洲精品美女久久7777777| 夜夜嗨一区二区三区| 狠狠色狠狠色综合日日小说| 亚洲啪啪91| 国产一区二区三区最好精华液| 亚洲国产精品v| 国产欧美日韩综合一区在线播放 | 亚洲精品看片| 亚洲午夜精品一区二区| 韩国精品在线观看| 亚洲欧洲在线一区| 国产日韩欧美在线播放不卡| 欧美顶级大胆免费视频| 国产精品美女在线观看| 1769国内精品视频在线播放| 99国产精品久久久| 伊人久久亚洲美女图片| 在线亚洲伦理| 日韩一级网站| 久久综合亚州| 久久不见久久见免费视频1| 欧美激情综合在线| 久久综合影视| 国产精品自拍视频| 最新精品在线| 91久久精品日日躁夜夜躁国产| 亚洲免费视频网站| 宅男噜噜噜66国产日韩在线观看| 久久久蜜桃一区二区人| 欧美一级一区| 欧美三级电影精品| 91久久久久| 亚洲人妖在线| 欧美18av| 亚洲国产mv| 亚洲国产精品视频一区| 久久九九久精品国产免费直播| 午夜精品久久久久久99热| 欧美日韩精品免费看 | 亚洲亚洲精品在线观看| 嫩草影视亚洲| 亚洲成人中文| 亚洲精品久久久久久下一站 | 一本色道久久88精品综合| 久久视频精品在线| 美女999久久久精品视频| 国产一区二区久久久| 欧美一区二区三区婷婷月色 | 欧美国产视频日韩| 欧美激情一区二区三区在线| 在线免费观看欧美| 久久天天躁狠狠躁夜夜爽蜜月| 久久久噜噜噜久久久| 国产亚洲欧洲| 老司机成人在线视频| 亚洲成色777777在线观看影院| 亚洲电影观看| 欧美另类女人| 亚洲一区国产| 久久最新视频| 99精品国产在热久久下载| 欧美日韩亚洲成人| 亚洲欧美999| 欧美暴力喷水在线| 亚洲线精品一区二区三区八戒| 国产精品久久久久久久久久免费看| 亚洲一区二区3| 久久这里有精品视频| 亚洲日本欧美天堂| 国产精品久久久久久五月尺| 羞羞答答国产精品www一本| 蜜桃久久av一区| 亚洲在线播放电影| 亚洲第一毛片| 国产精品你懂的在线欣赏| 久久丁香综合五月国产三级网站| 国产精品青草久久| 久久嫩草精品久久久精品一| 亚洲高清不卡| 欧美一区在线直播| 91久久精品国产91性色tv| 欧美日韩在线免费| 久久国产色av| 日韩一区二区高清| 久久手机精品视频| 在线中文字幕一区| 精品粉嫩aⅴ一区二区三区四区| 欧美韩国在线| 欧美亚洲日本一区| 亚洲老板91色精品久久| 久久久亚洲高清| 亚洲一区在线观看视频| 在线免费观看日韩欧美| 国产精品伊人日日| 欧美日韩三区四区| 麻豆成人在线播放| 午夜伦理片一区| 日韩视频一区二区三区| 久久九九精品| 午夜精品视频一区| 一区二区动漫| 亚洲国产精品久久久久婷婷老年| 国产精品久久久久婷婷| 欧美国产一区二区| 久久免费视频这里只有精品| 亚洲一级电影| 一区二区三区欧美在线| 亚洲福利视频在线| 免费在线欧美黄色|