OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 21 matching lines...) Expand all Loading... |
32 // kenton@google.com (Kenton Varda) | 32 // kenton@google.com (Kenton Varda) |
33 // Based on original Protocol Buffers design by | 33 // Based on original Protocol Buffers design by |
34 // Sanjay Ghemawat, Jeff Dean, and others. | 34 // Sanjay Ghemawat, Jeff Dean, and others. |
35 // | 35 // |
36 // Defines MessageLite, the abstract interface implemented by all (lite | 36 // Defines MessageLite, the abstract interface implemented by all (lite |
37 // and non-lite) protocol message objects. | 37 // and non-lite) protocol message objects. |
38 | 38 |
39 #ifndef GOOGLE_PROTOBUF_MESSAGE_LITE_H__ | 39 #ifndef GOOGLE_PROTOBUF_MESSAGE_LITE_H__ |
40 #define GOOGLE_PROTOBUF_MESSAGE_LITE_H__ | 40 #define GOOGLE_PROTOBUF_MESSAGE_LITE_H__ |
41 | 41 |
| 42 #include <climits> |
42 #include <google/protobuf/stubs/common.h> | 43 #include <google/protobuf/stubs/common.h> |
| 44 #include <google/protobuf/stubs/logging.h> |
43 | 45 |
44 | 46 |
45 namespace google { | 47 namespace google { |
46 namespace protobuf { | 48 namespace protobuf { |
47 class Arena; | 49 class Arena; |
48 namespace io { | 50 namespace io { |
49 class CodedInputStream; | 51 class CodedInputStream; |
50 class CodedOutputStream; | 52 class CodedOutputStream; |
51 class ZeroCopyInputStream; | 53 class ZeroCopyInputStream; |
52 class ZeroCopyOutputStream; | 54 class ZeroCopyOutputStream; |
53 } | 55 } |
| 56 namespace internal { |
| 57 class WireFormatLite; |
| 58 } |
54 | 59 |
55 // Interface to light weight protocol messages. | 60 // Interface to light weight protocol messages. |
56 // | 61 // |
57 // This interface is implemented by all protocol message objects. Non-lite | 62 // This interface is implemented by all protocol message objects. Non-lite |
58 // messages additionally implement the Message interface, which is a | 63 // messages additionally implement the Message interface, which is a |
59 // subclass of MessageLite. Use MessageLite instead when you only need | 64 // subclass of MessageLite. Use MessageLite instead when you only need |
60 // the subset of features which it supports -- namely, nothing that uses | 65 // the subset of features which it supports -- namely, nothing that uses |
61 // descriptors or reflection. You can instruct the protocol compiler | 66 // descriptors or reflection. You can instruct the protocol compiler |
62 // to generate classes which implement only MessageLite, not the full | 67 // to generate classes which implement only MessageLite, not the full |
63 // Message interface, by adding the following line to the .proto file: | 68 // Message interface, by adding the following line to the .proto file: |
64 // | 69 // |
65 // option optimize_for = LITE_RUNTIME; | 70 // option optimize_for = LITE_RUNTIME; |
66 // | 71 // |
67 // This is particularly useful on resource-constrained systems where | 72 // This is particularly useful on resource-constrained systems where |
68 // the full protocol buffers runtime library is too big. | 73 // the full protocol buffers runtime library is too big. |
69 // | 74 // |
70 // Note that on non-constrained systems (e.g. servers) when you need | 75 // Note that on non-constrained systems (e.g. servers) when you need |
71 // to link in lots of protocol definitions, a better way to reduce | 76 // to link in lots of protocol definitions, a better way to reduce |
72 // total code footprint is to use optimize_for = CODE_SIZE. This | 77 // total code footprint is to use optimize_for = CODE_SIZE. This |
73 // will make the generated code smaller while still supporting all the | 78 // will make the generated code smaller while still supporting all the |
74 // same features (at the expense of speed). optimize_for = LITE_RUNTIME | 79 // same features (at the expense of speed). optimize_for = LITE_RUNTIME |
75 // is best when you only have a small number of message types linked | 80 // is best when you only have a small number of message types linked |
76 // into your binary, in which case the size of the protocol buffers | 81 // into your binary, in which case the size of the protocol buffers |
77 // runtime itself is the biggest problem. | 82 // runtime itself is the biggest problem. |
78 class LIBPROTOBUF_EXPORT MessageLite { | 83 class LIBPROTOBUF_EXPORT MessageLite { |
79 public: | 84 public: |
80 inline MessageLite() {} | 85 inline MessageLite() {} |
81 virtual ~MessageLite(); | 86 virtual ~MessageLite() {} |
82 | 87 |
83 // Basic Operations ------------------------------------------------ | 88 // Basic Operations ------------------------------------------------ |
84 | 89 |
85 // Get the name of this message type, e.g. "foo.bar.BazProto". | 90 // Get the name of this message type, e.g. "foo.bar.BazProto". |
86 virtual string GetTypeName() const = 0; | 91 virtual string GetTypeName() const = 0; |
87 | 92 |
88 // Construct a new instance of the same type. Ownership is passed to the | 93 // Construct a new instance of the same type. Ownership is passed to the |
89 // caller. | 94 // caller. |
90 virtual MessageLite* New() const = 0; | 95 virtual MessageLite* New() const = 0; |
91 | 96 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 // Like SerializeAsString(), but allows missing required fields. | 234 // Like SerializeAsString(), but allows missing required fields. |
230 string SerializePartialAsString() const; | 235 string SerializePartialAsString() const; |
231 | 236 |
232 // Like SerializeToString(), but appends to the data to the string's existing | 237 // Like SerializeToString(), but appends to the data to the string's existing |
233 // contents. All required fields must be set. | 238 // contents. All required fields must be set. |
234 bool AppendToString(string* output) const; | 239 bool AppendToString(string* output) const; |
235 // Like AppendToString(), but allows missing required fields. | 240 // Like AppendToString(), but allows missing required fields. |
236 bool AppendPartialToString(string* output) const; | 241 bool AppendPartialToString(string* output) const; |
237 | 242 |
238 // Computes the serialized size of the message. This recursively calls | 243 // Computes the serialized size of the message. This recursively calls |
239 // ByteSize() on all embedded messages. If a subclass does not override | 244 // ByteSizeLong() on all embedded messages. |
240 // this, it MUST override SetCachedSize(). | |
241 // | 245 // |
242 // ByteSize() is generally linear in the number of fields defined for the | 246 // ByteSizeLong() is generally linear in the number of fields defined for the |
243 // proto. | 247 // proto. |
244 virtual int ByteSize() const = 0; | 248 virtual size_t ByteSizeLong() const = 0; |
245 | 249 |
246 // Serializes the message without recomputing the size. The message must | 250 // Legacy ByteSize() API. |
247 // not have changed since the last call to ByteSize(); if it has, the results | 251 int ByteSize() const { |
248 // are undefined. | 252 size_t result = ByteSizeLong(); |
| 253 GOOGLE_DCHECK_LE(result, static_cast<size_t>(INT_MAX)); |
| 254 return static_cast<int>(result); |
| 255 } |
| 256 |
| 257 // Serializes the message without recomputing the size. The message must not |
| 258 // have changed since the last call to ByteSize(), and the value returned by |
| 259 // ByteSize must be non-negative. Otherwise the results are undefined. |
249 virtual void SerializeWithCachedSizes( | 260 virtual void SerializeWithCachedSizes( |
250 io::CodedOutputStream* output) const = 0; | 261 io::CodedOutputStream* output) const = 0; |
251 | 262 |
252 // Like SerializeWithCachedSizes, but writes directly to *target, returning | 263 // A version of SerializeWithCachedSizesToArray, below, that does |
253 // a pointer to the byte immediately after the last byte written. "target" | 264 // not guarantee deterministic serialization. |
254 // must point at a byte array of at least ByteSize() bytes. | 265 virtual uint8* SerializeWithCachedSizesToArray(uint8* target) const { |
255 virtual uint8* SerializeWithCachedSizesToArray(uint8* target) const; | 266 return InternalSerializeWithCachedSizesToArray(false, target); |
| 267 } |
256 | 268 |
257 // Returns the result of the last call to ByteSize(). An embedded message's | 269 // Returns the result of the last call to ByteSize(). An embedded message's |
258 // size is needed both to serialize it (because embedded messages are | 270 // size is needed both to serialize it (because embedded messages are |
259 // length-delimited) and to compute the outer message's size. Caching | 271 // length-delimited) and to compute the outer message's size. Caching |
260 // the size avoids computing it multiple times. | 272 // the size avoids computing it multiple times. |
261 // | 273 // |
262 // ByteSize() does not automatically use the cached size when available | 274 // ByteSize() does not automatically use the cached size when available |
263 // because this would require invalidating it every time the message was | 275 // because this would require invalidating it every time the message was |
264 // modified, which would be too hard and expensive. (E.g. if a deeply-nested | 276 // modified, which would be too hard and expensive. (E.g. if a deeply-nested |
265 // sub-message is changed, all of its parents' cached sizes would need to be | 277 // sub-message is changed, all of its parents' cached sizes would need to be |
266 // invalidated, which is too much work for an otherwise inlined setter | 278 // invalidated, which is too much work for an otherwise inlined setter |
267 // method.) | 279 // method.) |
268 virtual int GetCachedSize() const = 0; | 280 virtual int GetCachedSize() const = 0; |
269 | 281 |
| 282 // Functions below here are not part of the public interface. It isn't |
| 283 // enforced, but they should be treated as private, and will be private |
| 284 // at some future time. Unfortunately the implementation of the "friend" |
| 285 // keyword in GCC is broken at the moment, but we expect it will be fixed. |
| 286 |
| 287 // Like SerializeWithCachedSizes, but writes directly to *target, returning |
| 288 // a pointer to the byte immediately after the last byte written. "target" |
| 289 // must point at a byte array of at least ByteSize() bytes. If deterministic |
| 290 // is true then we use deterministic serialization, e.g., map keys are sorted. |
| 291 // FOR INTERNAL USE ONLY! |
| 292 virtual uint8* InternalSerializeWithCachedSizesToArray(bool deterministic, |
| 293 uint8* target) const; |
| 294 |
270 private: | 295 private: |
| 296 friend class internal::WireFormatLite; |
| 297 |
271 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageLite); | 298 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageLite); |
272 }; | 299 }; |
273 | 300 |
274 } // namespace protobuf | 301 } // namespace protobuf |
275 | 302 |
276 } // namespace google | 303 } // namespace google |
277 #endif // GOOGLE_PROTOBUF_MESSAGE_LITE_H__ | 304 #endif // GOOGLE_PROTOBUF_MESSAGE_LITE_H__ |
OLD | NEW |