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

隨筆 - 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 閱讀(2298) 評論(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>
            国产日韩精品入口| 国产日韩欧美综合| 极品av少妇一区二区| 亚洲欧美网站| 9l国产精品久久久久麻豆| 欧美高清在线一区| 国产一区二区三区四区老人| 亚洲影院污污.| 91久久精品www人人做人人爽| 久久久久成人精品免费播放动漫| 国产人成一区二区三区影院| 亚洲午夜免费福利视频| 一本久久综合| 久久国产精品毛片| 樱桃国产成人精品视频| 亚洲欧美制服另类日韩| 99精品欧美一区| 欧美天堂在线观看| 亚洲专区一区二区三区| 中文日韩电影网站| 美女国内精品自产拍在线播放| 尤物在线观看一区| 亚洲福利视频网| 欧美精品久久天天躁| 一区二区三区视频在线播放| 亚洲精品中文字| 国产精品乱人伦中文| 久久成人精品无人区| 午夜精品福利在线| 一区视频在线看| 欧美黄色一级视频| 欧美日韩在线精品一区二区三区| 亚洲在线视频观看| 午夜精品在线| 亚洲第一中文字幕| 亚洲麻豆视频| 国产精品一区三区| 欧美电影美腿模特1979在线看| 免费成人高清视频| 亚洲愉拍自拍另类高清精品| 亚洲人成在线播放| 国产欧美日韩一区二区三区| 美女日韩在线中文字幕| 欧美日韩视频一区二区三区| 久久国产精品一区二区| 欧美国产精品va在线观看| 午夜欧美精品| 欧美电影免费观看高清| 9色精品在线| 欧美在线影院| 亚洲一区国产| 欧美a级片一区| 亚洲精品一区二区三区福利| 亚洲一品av免费观看| 1204国产成人精品视频| 欧美大片免费久久精品三p| 欧美午夜无遮挡| 欧美波霸影院| 国产日韩欧美一区二区三区在线观看 | 亚洲欧美日韩综合一区| 亚洲午夜高清视频| 日韩亚洲欧美精品| 久久综合久久久久88| 99综合电影在线视频| 久久久久青草大香线综合精品| 亚洲一二三区在线观看| 麻豆国产精品一区二区三区| 久久成人免费日本黄色| 欧美日韩99| 久久精品伊人| 欧美日韩亚洲成人| 在线亚洲欧美视频| 亚洲欧美视频一区| 国产精品综合| 欧美中文字幕不卡| 欧美刺激午夜性久久久久久久| 一区在线播放视频| 久久影院午夜片一区| 亚洲电影第三页| 亚洲久久一区| 国产精品成人免费视频| 亚洲欧美综合国产精品一区| 欧美在线观看视频| 亚洲成色777777在线观看影院| 久久久久久一区二区三区| 女同性一区二区三区人了人一 | 欧美另类videos死尸| 亚洲精品日产精品乱码不卡| 99精品视频免费| 国产精品进线69影院| 性娇小13――14欧美| 美女性感视频久久久| 亚洲精品视频啊美女在线直播| 欧美日韩成人激情| 亚洲一区影院| 欧美国产日韩a欧美在线观看| 一区二区三区免费观看| 国产精品毛片a∨一区二区三区|国| 亚洲欧美日韩精品久久久| 蜜桃av噜噜一区二区三区| 夜夜嗨av色一区二区不卡| 国产视频在线一区二区| 免费久久精品视频| 亚洲免费一在线| 欧美高清视频在线播放| 午夜精彩视频在线观看不卡| 激情自拍一区| 国产精品久久久99| 免费久久99精品国产自在现线| 一区二区三区视频在线播放| 免费在线视频一区| 亚洲伊人观看| 亚洲人成亚洲人成在线观看| 国产精品综合网站| 欧美日韩免费区域视频在线观看| 午夜免费电影一区在线观看| 亚洲激情第一页| 久久男女视频| 欧美一级淫片aaaaaaa视频| 在线观看一区二区视频| 国产欧美日韩在线| 欧美日韩色综合| 欧美超级免费视 在线| 欧美一区高清| 亚洲欧美国产高清| 日韩一区二区精品| 欧美成人在线免费视频| 久久精品在线| 久久福利视频导航| 亚洲中无吗在线| 一区二区三区.www| 亚洲精品乱码久久久久久蜜桃麻豆| 国产欧美一区二区视频| 国产精品欧美在线| 欧美体内谢she精2性欧美| 欧美理论在线| 欧美精品123区| 欧美岛国激情| 欧美激情一区二区久久久| 蜜桃精品一区二区三区| 久久久久国产免费免费| 久久久国产成人精品| 欧美在线黄色| 欧美一区二区三区另类| 欧美一区二区三区久久精品 | 久久三级视频| 久久精品一区中文字幕| 欧美在线在线| 久久久福利视频| 久久久蜜臀国产一区二区| 久久久久国产精品厨房| 老巨人导航500精品| 欧美99久久| 亚洲黄一区二区| 亚洲美女中出| 亚洲一区二区三区777| 亚洲欧美日韩在线高清直播| 亚洲资源av| 欧美制服丝袜第一页| 久久久免费av| 欧美成年人在线观看| 欧美日韩精品综合| 欧美性猛交视频| 国产日韩精品在线观看| 国模私拍一区二区三区| 激情五月***国产精品| 91久久在线视频| 亚洲专区国产精品| 久久久久久穴| 亚洲三级影院| 亚洲男人影院| 久久欧美中文字幕| 免费观看欧美在线视频的网站| 欧美日韩 国产精品| 国产欧美欧洲在线观看| 在线成人免费观看| 在线视频免费在线观看一区二区| 欧美一区不卡| 欧美激情片在线观看| 一区二区欧美在线| 久久久久综合网| 欧美激情一区| 国产视频欧美视频| 日韩一级精品| 久久久久久久999| 日韩图片一区| 亚洲女人天堂av| 麻豆乱码国产一区二区三区| 欧美丝袜第一区| 亚洲国产日韩欧美在线动漫| 亚洲在线播放| 亚洲国产美国国产综合一区二区| 午夜精品久久久久久久99热浪潮 | 亚洲色图在线视频| 久久综合色影院| 国产欧美日韩在线视频| 亚洲精品之草原avav久久| 久久精品国产亚洲高清剧情介绍| 亚洲精品日韩久久| 久久日韩精品|