#ifndef MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_
#define MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_H_
#include <assert.h>
#include <vector>
#include "mojo/public/cpp/bindings/lib/message_internal.h"
namespace mojo {
class Message {
public:
Message();
~Message();
void AllocUninitializedData(uint32_t num_bytes);
void AdoptData(uint32_t num_bytes, internal::MessageData* data);
void Swap(Message* other);
uint32_t data_num_bytes() const { return data_num_bytes_; }
const uint8_t* data() const { return
reinterpret_cast<const uint8_t*>(data_);
}
uint8_t* mutable_data() { return reinterpret_cast<uint8_t*>(data_); }
const internal::MessageHeader* header() const { return &data_->header; }
uint32_t name() const { return data_->header.name; }
bool has_flag(uint32_t flag) const { return !!(data_->header.flags & flag); }
bool has_request_id() const { return data_->header.num_fields >= 3; }
uint64_t request_id() const {
assert(has_request_id());
return static_cast<const internal::MessageHeaderWithRequestID*>(
&data_->header)->request_id;
}
void set_request_id(uint64_t request_id) {
assert(has_request_id());
static_cast<internal::MessageHeaderWithRequestID*>(&data_->header)->
request_id = request_id;
}
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 std::vector<Handle>* handles() const { return &handles_; }
std::vector<Handle>* mutable_handles() { return &handles_; }
private:
uint32_t data_num_bytes_;
internal::MessageData* data_;
std::vector<Handle> handles_;
MOJO_DISALLOW_COPY_AND_ASSIGN(Message);
};
class MessageReceiver {
public:
virtual ~MessageReceiver() {}
virtual bool Accept(Message* message) = 0;
virtual bool AcceptWithResponder(Message* message,
MessageReceiver* responder) = 0;
};
MojoResult ReadAndDispatchMessage(MessagePipeHandle handle,
MessageReceiver* receiver,
bool* receiver_result);
}
#endif