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> | |
43 #include <google/protobuf/stubs/common.h> | 42 #include <google/protobuf/stubs/common.h> |
44 #include <google/protobuf/stubs/logging.h> | |
45 | 43 |
46 | 44 |
47 namespace google { | 45 namespace google { |
48 namespace protobuf { | 46 namespace protobuf { |
49 class Arena; | 47 class Arena; |
50 namespace io { | 48 namespace io { |
51 class CodedInputStream; | 49 class CodedInputStream; |
52 class CodedOutputStream; | 50 class CodedOutputStream; |
53 class ZeroCopyInputStream; | 51 class ZeroCopyInputStream; |
54 class ZeroCopyOutputStream; | 52 class ZeroCopyOutputStream; |
55 } | 53 } |
56 namespace internal { | |
57 class WireFormatLite; | |
58 } | |
59 | 54 |
60 // Interface to light weight protocol messages. | 55 // Interface to light weight protocol messages. |
61 // | 56 // |
62 // This interface is implemented by all protocol message objects. Non-lite | 57 // This interface is implemented by all protocol message objects. Non-lite |
63 // messages additionally implement the Message interface, which is a | 58 // messages additionally implement the Message interface, which is a |
64 // subclass of MessageLite. Use MessageLite instead when you only need | 59 // subclass of MessageLite. Use MessageLite instead when you only need |
65 // the subset of features which it supports -- namely, nothing that uses | 60 // the subset of features which it supports -- namely, nothing that uses |
66 // descriptors or reflection. You can instruct the protocol compiler | 61 // descriptors or reflection. You can instruct the protocol compiler |
67 // to generate classes which implement only MessageLite, not the full | 62 // to generate classes which implement only MessageLite, not the full |
68 // Message interface, by adding the following line to the .proto file: | 63 // Message interface, by adding the following line to the .proto file: |
69 // | 64 // |
70 // option optimize_for = LITE_RUNTIME; | 65 // option optimize_for = LITE_RUNTIME; |
71 // | 66 // |
72 // This is particularly useful on resource-constrained systems where | 67 // This is particularly useful on resource-constrained systems where |
73 // the full protocol buffers runtime library is too big. | 68 // the full protocol buffers runtime library is too big. |
74 // | 69 // |
75 // Note that on non-constrained systems (e.g. servers) when you need | 70 // Note that on non-constrained systems (e.g. servers) when you need |
76 // to link in lots of protocol definitions, a better way to reduce | 71 // to link in lots of protocol definitions, a better way to reduce |
77 // total code footprint is to use optimize_for = CODE_SIZE. This | 72 // total code footprint is to use optimize_for = CODE_SIZE. This |
78 // will make the generated code smaller while still supporting all the | 73 // will make the generated code smaller while still supporting all the |
79 // same features (at the expense of speed). optimize_for = LITE_RUNTIME | 74 // same features (at the expense of speed). optimize_for = LITE_RUNTIME |
80 // is best when you only have a small number of message types linked | 75 // is best when you only have a small number of message types linked |
81 // into your binary, in which case the size of the protocol buffers | 76 // into your binary, in which case the size of the protocol buffers |
82 // runtime itself is the biggest problem. | 77 // runtime itself is the biggest problem. |
83 class LIBPROTOBUF_EXPORT MessageLite { | 78 class LIBPROTOBUF_EXPORT MessageLite { |
84 public: | 79 public: |
85 inline MessageLite() {} | 80 inline MessageLite() {} |
86 virtual ~MessageLite() {} | 81 virtual ~MessageLite(); |
87 | 82 |
88 // Basic Operations ------------------------------------------------ | 83 // Basic Operations ------------------------------------------------ |
89 | 84 |
90 // Get the name of this message type, e.g. "foo.bar.BazProto". | 85 // Get the name of this message type, e.g. "foo.bar.BazProto". |
91 virtual string GetTypeName() const = 0; | 86 virtual string GetTypeName() const = 0; |
92 | 87 |
93 // Construct a new instance of the same type. Ownership is passed to the | 88 // Construct a new instance of the same type. Ownership is passed to the |
94 // caller. | 89 // caller. |
95 virtual MessageLite* New() const = 0; | 90 virtual MessageLite* New() const = 0; |
96 | 91 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 // Like SerializeAsString(), but allows missing required fields. | 229 // Like SerializeAsString(), but allows missing required fields. |
235 string SerializePartialAsString() const; | 230 string SerializePartialAsString() const; |
236 | 231 |
237 // Like SerializeToString(), but appends to the data to the string's existing | 232 // Like SerializeToString(), but appends to the data to the string's existing |
238 // contents. All required fields must be set. | 233 // contents. All required fields must be set. |
239 bool AppendToString(string* output) const; | 234 bool AppendToString(string* output) const; |
240 // Like AppendToString(), but allows missing required fields. | 235 // Like AppendToString(), but allows missing required fields. |
241 bool AppendPartialToString(string* output) const; | 236 bool AppendPartialToString(string* output) const; |
242 | 237 |
243 // Computes the serialized size of the message. This recursively calls | 238 // Computes the serialized size of the message. This recursively calls |
244 // ByteSizeLong() on all embedded messages. | 239 // ByteSize() on all embedded messages. If a subclass does not override |
| 240 // this, it MUST override SetCachedSize(). |
245 // | 241 // |
246 // ByteSizeLong() is generally linear in the number of fields defined for the | 242 // ByteSize() is generally linear in the number of fields defined for the |
247 // proto. | 243 // proto. |
248 virtual size_t ByteSizeLong() const = 0; | 244 virtual int ByteSize() const = 0; |
249 | 245 |
250 // Legacy ByteSize() API. | 246 // Serializes the message without recomputing the size. The message must |
251 int ByteSize() const { | 247 // not have changed since the last call to ByteSize(); if it has, the results |
252 size_t result = ByteSizeLong(); | 248 // are undefined. |
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. | |
260 virtual void SerializeWithCachedSizes( | 249 virtual void SerializeWithCachedSizes( |
261 io::CodedOutputStream* output) const = 0; | 250 io::CodedOutputStream* output) const = 0; |
262 | 251 |
263 // A version of SerializeWithCachedSizesToArray, below, that does | 252 // Like SerializeWithCachedSizes, but writes directly to *target, returning |
264 // not guarantee deterministic serialization. | 253 // a pointer to the byte immediately after the last byte written. "target" |
265 virtual uint8* SerializeWithCachedSizesToArray(uint8* target) const { | 254 // must point at a byte array of at least ByteSize() bytes. |
266 return InternalSerializeWithCachedSizesToArray(false, target); | 255 virtual uint8* SerializeWithCachedSizesToArray(uint8* target) const; |
267 } | |
268 | 256 |
269 // Returns the result of the last call to ByteSize(). An embedded message's | 257 // Returns the result of the last call to ByteSize(). An embedded message's |
270 // size is needed both to serialize it (because embedded messages are | 258 // size is needed both to serialize it (because embedded messages are |
271 // length-delimited) and to compute the outer message's size. Caching | 259 // length-delimited) and to compute the outer message's size. Caching |
272 // the size avoids computing it multiple times. | 260 // the size avoids computing it multiple times. |
273 // | 261 // |
274 // ByteSize() does not automatically use the cached size when available | 262 // ByteSize() does not automatically use the cached size when available |
275 // because this would require invalidating it every time the message was | 263 // because this would require invalidating it every time the message was |
276 // modified, which would be too hard and expensive. (E.g. if a deeply-nested | 264 // modified, which would be too hard and expensive. (E.g. if a deeply-nested |
277 // sub-message is changed, all of its parents' cached sizes would need to be | 265 // sub-message is changed, all of its parents' cached sizes would need to be |
278 // invalidated, which is too much work for an otherwise inlined setter | 266 // invalidated, which is too much work for an otherwise inlined setter |
279 // method.) | 267 // method.) |
280 virtual int GetCachedSize() const = 0; | 268 virtual int GetCachedSize() const = 0; |
281 | 269 |
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 | |
295 private: | 270 private: |
296 friend class internal::WireFormatLite; | |
297 | |
298 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageLite); | 271 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageLite); |
299 }; | 272 }; |
300 | 273 |
301 } // namespace protobuf | 274 } // namespace protobuf |
302 | 275 |
303 } // namespace google | 276 } // namespace google |
304 #endif // GOOGLE_PROTOBUF_MESSAGE_LITE_H__ | 277 #endif // GOOGLE_PROTOBUF_MESSAGE_LITE_H__ |
OLD | NEW |