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

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

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

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下運(yùn)行 protoc.exe -I=c:/workspace/test/testprotobuf --cpp_out=c:/workspace/test/testprotobuf c:/workspace/test/testprotobuf/addressbook.proto
那么就可在c:/workspace/test/testprotobuf下產(chǎn)生addressbook.pb.h和addressbook.pb.cc。
如:在MSYS下運(yùn)行 protoc.exe -I=c:/workspace --cpp_out=c:/workspace/test/testprotobuf c:/workspace/test/testprotobuf/addressbook.proto
那么就可在c:/workspace/test/testprotobuf/test/testprotobuf下產(chǎn)生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宏。你最好像這樣——盡管這不是嚴(yán)格要求的——在使用C++ Protocol Buffer庫(kù)之前執(zhí)行該宏。它會(huì)檢查你是不是在無意中鏈接到了與你使用的頭文件不兼容的protocol buffer庫(kù)。如果檢測(cè)到了不匹配情況,程序會(huì)中止運(yùn)行下去。注意:每一個(gè).pb.cc文件在開始的時(shí)候都會(huì)自動(dòng)調(diào)用該宏。

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) 評(píng)論(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>
            欧美成人中文字幕| 翔田千里一区二区| 亚洲一区二区视频在线| 亚洲国产精品专区久久 | 亚洲性夜色噜噜噜7777| 最新高清无码专区| 欧美日韩精品国产| 亚洲自拍偷拍麻豆| 久久成人精品无人区| 伊人久久综合| 亚洲欧洲在线看| 国产精品久久久久久久app| 欧美一级网站| 久久久综合网站| 一区二区三区四区国产精品| 一区二区三区久久网| 韩日精品视频| 亚洲精品乱码| 国内成人精品2018免费看| 欧美 日韩 国产 一区| 欧美激情在线免费观看| 香蕉亚洲视频| 免费日韩av| 亚洲免费视频在线观看| 久久久久久久成人| 亚洲桃花岛网站| 久久久久免费视频| 亚洲一区日韩在线| 麻豆成人综合网| 亚洲欧美综合国产精品一区| 久久视频这里只有精品| 在线综合视频| 久久午夜色播影院免费高清| 亚洲女同性videos| 欧美成人精品一区二区三区| 性亚洲最疯狂xxxx高清| 欧美成人资源| 久久亚洲一区二区三区四区| 国产精品a级| 亚洲国产免费| 好吊一区二区三区| 一区二区三区四区国产| 91久久精品美女高潮| 欧美亚洲免费在线| 亚洲视频图片小说| 欧美成人一区二区三区片免费| 欧美在线观看视频一区二区三区| 欧美日韩成人在线视频| 六十路精品视频| 国产伪娘ts一区| 亚洲综合色丁香婷婷六月图片| 亚洲伦理精品| 牛夜精品久久久久久久99黑人| 久久精品亚洲一区二区三区浴池| 国产精品国产三级国产aⅴ浪潮| 欧美激情亚洲综合一区| 激情一区二区三区| 午夜精品成人在线| 欧美在线精品一区| 国产毛片精品视频| 亚洲综合日韩中文字幕v在线| 正在播放亚洲| 国产精品成人一区二区| 日韩午夜免费视频| 国产精品国产三级国产a| 亚洲美女免费精品视频在线观看| 亚洲精品国产系列| 欧美精品久久99| 亚洲欧洲在线视频| 亚洲视频1区2区| 国产精品看片你懂得| 亚洲天堂成人| 欧美亚洲在线视频| 国产一区在线播放| 久久久精品欧美丰满| 欧美高清在线一区二区| 亚洲美女在线一区| 欧美午夜激情在线| 欧美一区二区播放| 欧美成人免费网站| 一区二区三区|亚洲午夜| 欧美视频第二页| 亚洲女与黑人做爰| 美女福利精品视频| 亚洲全黄一级网站| 国产精品va在线| 欧美中文字幕在线| 91久久精品久久国产性色也91| 一区二区国产日产| 国产欧美日韩激情| 麻豆9191精品国产| 99精品视频免费观看视频| 亚洲欧美日韩综合aⅴ视频| 国产亚洲欧美日韩精品| 免费成人av资源网| 亚洲午夜一区二区三区| 久久久高清一区二区三区| 亚洲高清资源| 国产精品成人一区二区三区夜夜夜| 欧美一二三区在线观看| 亚洲承认在线| 亚洲电影专区| 欧美性一二三区| 久久亚洲私人国产精品va媚药 | 性色av一区二区三区红粉影视| 激情欧美日韩| 国产精品v欧美精品∨日韩| 欧美一区二区高清| 亚洲欧洲精品一区| 久久青青草原一区二区| 中文av一区二区| 亚洲国产精品123| 国产精品自拍在线| 欧美—级a级欧美特级ar全黄| 欧美在线视频观看免费网站| 亚洲精品看片| 欧美国产一区二区在线观看| 欧美夜福利tv在线| 一区二区精品| 亚洲高清三级视频| 国产午夜精品理论片a级探花 | 久久久一二三| 欧美日韩一区二区三区在线视频| 亚洲视频一区二区在线观看| 欧美激情综合| 亚洲午夜精品在线| 91久久久久久久久| 女人色偷偷aa久久天堂| 午夜欧美精品| 亚洲一区二区网站| 99成人在线| 日韩午夜精品视频| 亚洲黄色视屏| 亚洲黄色免费| 亚洲日产国产精品| 亚洲成色精品| 伊人色综合久久天天五月婷| 国内精品伊人久久久久av一坑| 国产裸体写真av一区二区| 欧美日韩一区在线| 国产精品福利在线| 国产精品三级视频| 国产精品入口夜色视频大尺度 | 麻豆成人av| 久久资源在线| 老鸭窝91久久精品色噜噜导演| 久久久久99精品国产片| 久久不射2019中文字幕| 翔田千里一区二区| 欧美一区成人| 亚洲免费视频网站| 亚洲欧美日韩国产中文在线| 亚洲你懂的在线视频| 亚洲欧美久久久| 欧美一区二区三区日韩视频| 久久成人免费电影| 久久一区二区三区av| 欧美国产亚洲精品久久久8v| 亚洲韩国日本中文字幕| 亚洲毛片网站| 亚洲欧美久久| 久久久国际精品| 欧美精品1区2区| 国产精品久久久久久妇女6080| 国产精品乱子久久久久| 红桃视频国产一区| 亚洲人成人一区二区三区| 日韩一级裸体免费视频| 亚洲欧美国产三级| 久久久免费精品视频| 亚洲国产天堂久久综合网| 99精品视频免费观看| 欧美一级久久久久久久大片| 噜噜噜在线观看免费视频日韩| 欧美连裤袜在线视频| 国产精品家庭影院| 黑人巨大精品欧美一区二区| 亚洲精选中文字幕| 欧美在线观看视频一区二区| 欧美激情第10页| 亚洲综合首页| 欧美a级理论片| 国产农村妇女毛片精品久久莱园子 | 美女图片一区二区| 国产在线视频不卡二| 在线观看欧美精品| 亚洲午夜精品在线| 欧美freesex交免费视频| 一区二区免费看| 久久色在线观看| 国产精品一区二区三区免费观看| 一区二区在线视频观看| 午夜精品久久久久| 亚洲国产欧美一区二区三区久久| 亚洲自拍16p| 欧美日韩高清不卡| 亚洲国产激情| 久久亚洲二区| 欧美在线观看www| 国产精品久久久久7777婷婷|