Chromium Code Reviews| 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 #include <assert.h> | |
| 6 #include <stddef.h> | |
| 7 #include <stdint.h> | |
| 8 #include <stdio.h> | |
| 9 #include <stdlib.h> | |
| 10 #include <string.h> | |
| 11 | |
| 12 #include <new> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "mojo/public/bindings/generated/sample_service.h" | |
| 16 #include "mojo/public/bindings/generated/sample_service_proxy.h" | |
| 17 #include "mojo/public/bindings/generated/sample_service_stub.h" | |
| 18 | |
| 19 static void PrintSpacer(int depth) { | |
| 20 for (int i = 0; i < depth; ++i) | |
| 21 printf(" "); | |
| 22 } | |
| 23 | |
| 24 static void Print(int depth, const char* name, bool value) { | |
| 25 PrintSpacer(depth); | |
| 26 printf("%s: %s\n", name, value ? "true" : "false"); | |
| 27 } | |
| 28 | |
| 29 static void Print(int depth, const char* name, int32_t value) { | |
| 30 PrintSpacer(depth); | |
| 31 printf("%s: %d\n", name, value); | |
| 32 } | |
| 33 | |
| 34 static void Print(int depth, const char* name, uint8_t value) { | |
| 35 PrintSpacer(depth); | |
| 36 printf("%s: %u\n", name, value); | |
| 37 } | |
| 38 | |
| 39 static void Print(int depth, const char* name, const sample::Bar* bar) { | |
| 40 PrintSpacer(depth); | |
| 41 printf("%s: %p\n", name, bar); | |
| 42 if (bar) { | |
| 43 ++depth; | |
| 44 Print(depth, "alpha", bar->alpha()); | |
| 45 Print(depth, "beta", bar->beta()); | |
| 46 Print(depth, "gamma", bar->gamma()); | |
| 47 --depth; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 template <typename T> | |
| 52 static void Print(int depth, const char* name, const mojo::Array<T>* array) { | |
| 53 PrintSpacer(depth); | |
| 54 printf("%s: %p\n", name, array); | |
| 55 if (array) { | |
| 56 ++depth; | |
| 57 for (size_t i = 0; i < array->size(); ++i) { | |
| 58 char buf[32]; | |
| 59 sprintf(buf, "%lu", i); | |
| 60 Print(depth, buf, array->at(i)); | |
| 61 } | |
| 62 --depth; | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 static void Print(int depth, const char* name, const sample::Foo* foo) { | |
| 67 PrintSpacer(depth); | |
| 68 printf("%s: %p\n", name, foo); | |
| 69 if (foo) { | |
| 70 ++depth; | |
| 71 Print(depth, "x", foo->x()); | |
| 72 Print(depth, "y", foo->y()); | |
| 73 Print(depth, "a", foo->a()); | |
| 74 Print(depth, "b", foo->b()); | |
| 75 Print(depth, "c", foo->c()); | |
| 76 Print(depth, "bar", foo->bar()); | |
| 77 Print(depth, "data", foo->data()); | |
| 78 Print(depth, "extra_bars", foo->extra_bars()); | |
| 79 --depth; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 class ServiceImpl : public sample::ServiceStub { | |
| 84 public: | |
| 85 virtual void Frobinate(const sample::Foo* foo, bool baz) { | |
| 86 // Users code goes here to handle the incoming Frobinate message. | |
| 87 // We'll just dump the Foo structure and all of its members. | |
| 88 | |
| 89 printf("Frobinate:\n"); | |
| 90 | |
| 91 int depth = 1; | |
| 92 Print(depth, "foo", foo); | |
| 93 Print(depth, "baz", baz); | |
| 94 } | |
| 95 }; | |
| 96 | |
| 97 //---- | |
| 98 | |
| 99 class SimpleMessageSender : public mojo::MessageSender { | |
| 100 public: | |
| 101 virtual void Send(const mojo::Message& message) { | |
| 102 // Imagine some IPC happened here. | |
| 103 | |
| 104 // In the receiving process, an implementation of ServiceStub is known to | |
| 105 // the system. It receives the incoming message. | |
| 106 ServiceImpl impl; | |
| 107 | |
| 108 sample::ServiceStub* stub = &impl; | |
| 109 stub->OnMessageReceived(message); | |
| 110 } | |
| 111 }; | |
| 112 | |
| 113 int main() { | |
| 114 SimpleMessageSender sender; | |
| 115 | |
| 116 // User has a proxy to a Service somehow. | |
| 117 sample::Service* service = new sample::ServiceProxy(&sender); | |
| 118 | |
| 119 // User constructs a message to send. | |
| 120 | |
| 121 // Notice that it doesn't matter in what order the structs / arrays are | |
| 122 // allocated. Here, the various members of Foo are allocated before Foo is | |
| 123 // allocated. | |
| 124 | |
| 125 mojo::MessageBuffer buf; | |
| 126 | |
| 127 sample::Bar* bar = buf.Alloc<sample::Bar>(); | |
| 128 bar->set_alpha(20); | |
| 129 bar->set_beta(40); | |
| 130 bar->set_gamma(60); | |
| 131 | |
| 132 const size_t kNumDataElements = 10; | |
| 133 mojo::Array<uint8_t>* data = buf.AllocArray<uint8_t>(kNumDataElements); | |
| 134 for (size_t i = 0; i < kNumDataElements; ++i) | |
| 135 (*data)[i] = static_cast<uint8_t>(kNumDataElements - i); | |
| 136 | |
| 137 const size_t kNumExtraBarsElements = 3; | |
| 138 mojo::Array<sample::Bar*>* extra_bars = | |
| 139 buf.AllocArray<sample::Bar*>(kNumExtraBarsElements); | |
| 140 for (size_t i = 0; i < kNumExtraBarsElements; ++i) { | |
| 141 sample::Bar* bar = buf.Alloc<sample::Bar>(); | |
| 142 bar->set_alpha(i * 100); | |
| 143 bar->set_beta(i * 100 + 20); | |
| 144 bar->set_gamma(i * 100 + 40); | |
| 145 (*extra_bars)[i] = bar; | |
| 146 } | |
| 147 | |
| 148 sample::Foo* foo = buf.Alloc<sample::Foo>(); | |
| 149 foo->set_x(1); | |
| 150 foo->set_y(2); | |
| 151 foo->set_a(false); | |
| 152 foo->set_b(true); | |
| 153 foo->set_c(false); | |
| 154 foo->set_bar(bar); | |
|
viettrungluu
2013/10/01 00:51:04
I'm a little concerned that this and (even more so
| |
| 155 foo->set_data(data); | |
| 156 foo->set_extra_bars(extra_bars); | |
| 157 | |
| 158 service->Frobinate(foo, true); | |
| 159 | |
| 160 return 0; | |
| 161 } | |
| OLD | NEW |