Chromium Code Reviews| Index: mojo/public/cpp/bindings/message.h |
| diff --git a/mojo/public/cpp/bindings/message.h b/mojo/public/cpp/bindings/message.h |
| index b0a403b3cdf9c64c0117cd03d692549dcac8cbcd..839544f896a02171bfe69c6a9ae8facd1b509a8b 100644 |
| --- a/mojo/public/cpp/bindings/message.h |
| +++ b/mojo/public/cpp/bindings/message.h |
| @@ -7,7 +7,9 @@ |
| #include <vector> |
| +#include "base/memory/scoped_ptr.h" |
| #include "mojo/public/cpp/bindings/lib/message_internal.h" |
| +#include "mojo/public/cpp/bindings/lib/pickle_buffer.h" |
| #include "mojo/public/cpp/environment/logging.h" |
| namespace mojo { |
| @@ -23,64 +25,73 @@ class Message { |
| void Reset(); |
| + // These methods preallocate capacity in underlying storage, but they *DO NOT* |
| + // claim that space. Use the interface exposed by |buffer()| to claim bytes. |
| void AllocData(uint32_t num_bytes); |
| void AllocUninitializedData(uint32_t num_bytes); |
| // Transfers data and handles to |destination|. |
| void MoveTo(Message* destination); |
| - uint32_t data_num_bytes() const { return data_num_bytes_; } |
| + uint32_t data_num_bytes() const { return buffer_->data_num_bytes(); } |
| // Access the raw bytes of the message. |
| const uint8_t* data() const { |
| - return reinterpret_cast<const uint8_t*>(data_); |
| + return reinterpret_cast<const uint8_t*>(buffer_->data()); |
| + } |
| + uint8_t* mutable_data() { |
| + return reinterpret_cast<uint8_t*>(buffer_->data()); |
| } |
| - uint8_t* mutable_data() { return reinterpret_cast<uint8_t*>(data_); } |
| // Access the header. |
| - const internal::MessageHeader* header() const { return &data_->header; } |
| + const internal::MessageHeader* header() const { |
| + return reinterpret_cast<const internal::MessageHeader*>(buffer_->data()); |
|
yzshen1
2015/12/15 19:07:02
nit: static_cast here and the two methods above?
|
| + } |
| + |
| + internal::MessageHeader* header() { |
| + return const_cast<internal::MessageHeader*>( |
| + static_cast<const Message*>(this)->header()); |
| + } |
| - uint32_t interface_id() const { return data_->header.interface_id; } |
| - void set_interface_id(uint32_t id) { data_->header.interface_id = id; } |
| + uint32_t interface_id() const { return header()->interface_id; } |
| + void set_interface_id(uint32_t id) { header()->interface_id = id; } |
| - uint32_t name() const { return data_->header.name; } |
| - bool has_flag(uint32_t flag) const { return !!(data_->header.flags & flag); } |
| + uint32_t name() const { return header()->name; } |
| + bool has_flag(uint32_t flag) const { return !!(header()->flags & flag); } |
| // Access the request_id field (if present). |
| - bool has_request_id() const { return data_->header.version >= 1; } |
| + bool has_request_id() const { return header()->version >= 1; } |
| uint64_t request_id() const { |
| MOJO_DCHECK(has_request_id()); |
| return static_cast<const internal::MessageHeaderWithRequestID*>( |
| - &data_->header)->request_id; |
| + header())->request_id; |
| } |
| void set_request_id(uint64_t request_id) { |
| MOJO_DCHECK(has_request_id()); |
| - static_cast<internal::MessageHeaderWithRequestID*>(&data_->header) |
| + static_cast<internal::MessageHeaderWithRequestID*>(header()) |
| ->request_id = request_id; |
| } |
| // Access the payload. |
| - const uint8_t* payload() const { |
| - return reinterpret_cast<const uint8_t*>(data_) + data_->header.num_bytes; |
| - } |
| - uint8_t* mutable_payload() { |
| - return reinterpret_cast<uint8_t*>(data_) + data_->header.num_bytes; |
| - } |
| + const uint8_t* payload() const { return data() + header()->num_bytes; } |
| + uint8_t* mutable_payload() { return const_cast<uint8_t*>(payload()); } |
| uint32_t payload_num_bytes() const { |
| - MOJO_DCHECK(data_num_bytes_ >= data_->header.num_bytes); |
| - return data_num_bytes_ - data_->header.num_bytes; |
| + MOJO_DCHECK(buffer_->data_num_bytes() >= header()->num_bytes); |
| + return buffer_->data_num_bytes() - header()->num_bytes; |
| } |
| // Access the handles. |
| const std::vector<Handle>* handles() const { return &handles_; } |
| std::vector<Handle>* mutable_handles() { return &handles_; } |
| + // Access the underlying Buffer interface. |
| + internal::Buffer* buffer() { return buffer_.get(); } |
| + |
| private: |
| void Initialize(); |
| - void FreeDataAndCloseHandles(); |
| + void CloseHandles(); |
| - uint32_t data_num_bytes_; |
| - internal::MessageData* data_; |
| + scoped_ptr<internal::PickleBuffer> buffer_; |
| std::vector<Handle> handles_; |
| MOJO_DISALLOW_COPY_AND_ASSIGN(Message); |