OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // These are helper classes for allocating, aligning, and framing a | |
6 // |mojo::Message|. They are mainly used from within the generated C++ bindings. | |
7 | |
8 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MESSAGE_BUILDER_H_ | |
9 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MESSAGE_BUILDER_H_ | |
10 | |
11 #include <stdint.h> | |
12 | |
13 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | |
14 #include "mojo/public/cpp/bindings/lib/message_internal.h" | |
15 #include "mojo/public/cpp/bindings/message.h" | |
16 | |
17 namespace mojo { | |
18 | |
19 class Message; | |
20 | |
21 // MessageBuilder helps initialize and frame a |mojo::Message| that does not | |
22 // expect a response message, and therefore does not tag the message with a | |
23 // request id (which may save some bytes). | |
24 // | |
25 // The underlying |Message| is owned by MessageBuilder, but can be permanently | |
26 // moved by accessing |message()| and calling its |MoveTo()|. | |
27 class MessageBuilder { | |
28 public: | |
29 // This frames and configures a |mojo::Message| with the given message name. | |
30 MessageBuilder(uint32_t name, size_t payload_size); | |
31 ~MessageBuilder(); | |
32 | |
33 Message* message() { return &message_; } | |
34 | |
35 // TODO(vardhan): |buffer()| is internal and only consumed by internal classes | |
36 // and unittests. Consider making it private + friend class its consumers? | |
37 internal::Buffer* buffer() { return &buf_; } | |
38 | |
39 protected: | |
40 MessageBuilder(); | |
41 void Initialize(size_t size); | |
42 | |
43 Message message_; | |
44 internal::FixedBuffer buf_; | |
45 | |
46 MOJO_DISALLOW_COPY_AND_ASSIGN(MessageBuilder); | |
47 }; | |
48 | |
49 namespace internal { | |
50 | |
51 class MessageWithRequestIDBuilder : public MessageBuilder { | |
52 public: | |
53 MessageWithRequestIDBuilder(uint32_t name, | |
54 size_t payload_size, | |
55 uint32_t flags, | |
56 uint64_t request_id); | |
57 }; | |
58 | |
59 } // namespace internal | |
60 | |
61 // Builds a |mojo::Message| that is a "request" message that expects a response | |
62 // message. You can give it a unique |request_id| (with which you can construct | |
63 // a response message) by calling |message()->set_request_id()|. | |
64 // | |
65 // Has the same interface as |mojo::MessageBuilder|. | |
66 class RequestMessageBuilder : public internal::MessageWithRequestIDBuilder { | |
67 public: | |
68 RequestMessageBuilder(uint32_t name, size_t payload_size) | |
69 : MessageWithRequestIDBuilder(name, | |
70 payload_size, | |
71 internal::kMessageExpectsResponse, | |
72 0) {} | |
73 }; | |
74 | |
75 // Builds a |mojo::Message| that is a "response" message which pertains to a | |
76 // |request_id|. | |
77 // | |
78 // Has the same interface as |mojo::MessageBuilder|. | |
79 class ResponseMessageBuilder : public internal::MessageWithRequestIDBuilder { | |
80 public: | |
81 ResponseMessageBuilder(uint32_t name, | |
82 size_t payload_size, | |
83 uint64_t request_id) | |
84 : MessageWithRequestIDBuilder(name, | |
85 payload_size, | |
86 internal::kMessageIsResponse, | |
87 request_id) {} | |
88 }; | |
89 | |
90 } // namespace mojo | |
91 | |
92 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MESSAGE_BUILDER_H_ | |
OLD | NEW |