Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(521)

Side by Side Diff: mojo/public/bindings/sample/sample_test.cc

Issue 27034003: Mojo C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compilation issue on Official Linux builder Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 <stdio.h>
6
7 #include <string>
8
9 #include "mojo/public/bindings/sample/generated/sample_service.h"
10 #include "mojo/public/bindings/sample/generated/sample_service_proxy.h"
11 #include "mojo/public/bindings/sample/generated/sample_service_stub.h"
12
13 namespace sample {
14
15 static void PrintSpacer(int depth) {
16 for (int i = 0; i < depth; ++i)
17 printf(" ");
18 }
19
20 static void Print(int depth, const char* name, bool value) {
21 PrintSpacer(depth);
22 printf("%s: %s\n", name, value ? "true" : "false");
23 }
24
25 static void Print(int depth, const char* name, int32_t value) {
26 PrintSpacer(depth);
27 printf("%s: %d\n", name, value);
28 }
29
30 static void Print(int depth, const char* name, uint8_t value) {
31 PrintSpacer(depth);
32 printf("%s: %u\n", name, value);
33 }
34
35 static void Print(int depth, const char* name, mojo::Handle value) {
36 PrintSpacer(depth);
37 printf("%s: 0x%x\n", name, value.value);
38 }
39
40 static void Print(int depth, const char* name, const mojo::String* str) {
41 PrintSpacer(depth);
42 printf("%s: \"%*s\"\n", name, static_cast<int>(str->size()), &str->at(0));
43 }
44
45 static void Print(int depth, const char* name, const Bar* bar) {
46 PrintSpacer(depth);
47 printf("%s: %p\n", name, bar);
48 if (bar) {
49 ++depth;
50 Print(depth, "alpha", bar->alpha());
51 Print(depth, "beta", bar->beta());
52 Print(depth, "gamma", bar->gamma());
53 --depth;
54 }
55 }
56
57 template <typename T>
58 static void Print(int depth, const char* name, const mojo::Array<T>* array) {
59 PrintSpacer(depth);
60 printf("%s: %p\n", name, array);
61 if (array) {
62 ++depth;
63 for (size_t i = 0; i < array->size(); ++i) {
64 char buf[32];
65 sprintf(buf, "%lu", static_cast<unsigned long>(i));
66 Print(depth, buf, array->at(i));
67 }
68 --depth;
69 }
70 }
71
72 static void Print(int depth, const char* name, const Foo* foo) {
73 PrintSpacer(depth);
74 printf("%s: %p\n", name, foo);
75 if (foo) {
76 ++depth;
77 Print(depth, "name", foo->name());
78 Print(depth, "x", foo->x());
79 Print(depth, "y", foo->y());
80 Print(depth, "a", foo->a());
81 Print(depth, "b", foo->b());
82 Print(depth, "c", foo->c());
83 Print(depth, "bar", foo->bar());
84 Print(depth, "extra_bars", foo->extra_bars());
85 Print(depth, "data", foo->data());
86 Print(depth, "files", foo->files());
87 --depth;
88 }
89 }
90
91 class ServiceImpl : public ServiceStub {
92 public:
93 virtual void Frobinate(const Foo* foo, bool baz, mojo::Handle port)
94 MOJO_OVERRIDE {
95 // Users code goes here to handle the incoming Frobinate message.
96 // We'll just dump the Foo structure and all of its members.
97
98 printf("Frobinate:\n");
99
100 int depth = 1;
101 Print(depth, "foo", foo);
102 Print(depth, "baz", baz);
103 Print(depth, "port", port);
104 }
105 };
106
107 class SimpleMessageReceiver : public mojo::MessageReceiver {
108 public:
109 virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE {
110 // Imagine some IPC happened here.
111
112 // In the receiving process, an implementation of ServiceStub is known to
113 // the system. It receives the incoming message.
114 ServiceImpl impl;
115
116 ServiceStub* stub = &impl;
117 return stub->Accept(message);
118 }
119 };
120
121 void Exercise() {
122 SimpleMessageReceiver receiver;
123
124 // User has a proxy to a Service somehow.
125 Service* service = new ServiceProxy(&receiver);
126
127 // User constructs a message to send.
128
129 // Notice that it doesn't matter in what order the structs / arrays are
130 // allocated. Here, the various members of Foo are allocated before Foo is
131 // allocated.
132
133 mojo::ScratchBuffer buf;
134
135 Bar* bar = Bar::New(&buf);
136 bar->set_alpha(20);
137 bar->set_beta(40);
138 bar->set_gamma(60);
139
140 const char kName[] = "foopy";
141 mojo::String* name = mojo::String::NewCopyOf(&buf, std::string(kName));
142
143 mojo::Array<Bar*>* extra_bars = mojo::Array<Bar*>::New(&buf, 3);
144 for (size_t i = 0; i < extra_bars->size(); ++i) {
145 Bar* bar = Bar::New(&buf);
146 uint8_t base = static_cast<uint8_t>(i * 100);
147 bar->set_alpha(base);
148 bar->set_beta(base + 20);
149 bar->set_gamma(base + 40);
150 (*extra_bars)[i] = bar;
151 }
152
153 mojo::Array<uint8_t>* data = mojo::Array<uint8_t>::New(&buf, 10);
154 for (size_t i = 0; i < data->size(); ++i)
155 (*data)[i] = static_cast<uint8_t>(data->size() - i);
156
157 mojo::Array<mojo::Handle>* files = mojo::Array<mojo::Handle>::New(&buf, 4);
158 for (size_t i = 0; i < files->size(); ++i)
159 (*files)[i].value = static_cast<MojoHandle>(0xFFFF - i);
160
161 Foo* foo = Foo::New(&buf);
162 foo->set_name(name);
163 foo->set_x(1);
164 foo->set_y(2);
165 foo->set_a(false);
166 foo->set_b(true);
167 foo->set_c(false);
168 foo->set_bar(bar);
169 foo->set_extra_bars(extra_bars);
170 foo->set_data(data);
171 foo->set_files(files);
172
173 mojo::Handle port = { 10 };
174
175 service->Frobinate(foo, true, port);
176 }
177
178 } // namespace sample
179
180 int main() {
181 sample::Exercise();
182 return 0;
183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698