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

Unified Diff: mojo/public/bindings/sample_test.cc

Issue 23913008: C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/bindings/sample_test.cc
diff --git a/mojo/public/bindings/sample_test.cc b/mojo/public/bindings/sample_test.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f0df6e5dcb4b9ca7a3c201e84b332388f576e43d
--- /dev/null
+++ b/mojo/public/bindings/sample_test.cc
@@ -0,0 +1,161 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <assert.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <new>
+#include <vector>
+
+#include "mojo/public/bindings/generated/sample_service.h"
+#include "mojo/public/bindings/generated/sample_service_proxy.h"
+#include "mojo/public/bindings/generated/sample_service_stub.h"
+
+static void PrintSpacer(int depth) {
+ for (int i = 0; i < depth; ++i)
+ printf(" ");
+}
+
+static void Print(int depth, const char* name, bool value) {
+ PrintSpacer(depth);
+ printf("%s: %s\n", name, value ? "true" : "false");
+}
+
+static void Print(int depth, const char* name, int32_t value) {
+ PrintSpacer(depth);
+ printf("%s: %d\n", name, value);
+}
+
+static void Print(int depth, const char* name, uint8_t value) {
+ PrintSpacer(depth);
+ printf("%s: %u\n", name, value);
+}
+
+static void Print(int depth, const char* name, const sample::Bar* bar) {
+ PrintSpacer(depth);
+ printf("%s: %p\n", name, bar);
+ if (bar) {
+ ++depth;
+ Print(depth, "alpha", bar->alpha());
+ Print(depth, "beta", bar->beta());
+ Print(depth, "gamma", bar->gamma());
+ --depth;
+ }
+}
+
+template <typename T>
+static void Print(int depth, const char* name, const mojo::Array<T>* array) {
+ PrintSpacer(depth);
+ printf("%s: %p\n", name, array);
+ if (array) {
+ ++depth;
+ for (size_t i = 0; i < array->size(); ++i) {
+ char buf[32];
+ sprintf(buf, "%lu", i);
+ Print(depth, buf, array->at(i));
+ }
+ --depth;
+ }
+}
+
+static void Print(int depth, const char* name, const sample::Foo* foo) {
+ PrintSpacer(depth);
+ printf("%s: %p\n", name, foo);
+ if (foo) {
+ ++depth;
+ Print(depth, "x", foo->x());
+ Print(depth, "y", foo->y());
+ Print(depth, "a", foo->a());
+ Print(depth, "b", foo->b());
+ Print(depth, "c", foo->c());
+ Print(depth, "bar", foo->bar());
+ Print(depth, "data", foo->data());
+ Print(depth, "extra_bars", foo->extra_bars());
+ --depth;
+ }
+}
+
+class ServiceImpl : public sample::ServiceStub {
+ public:
+ virtual void Frobinate(const sample::Foo* foo, bool baz) {
+ // Users code goes here to handle the incoming Frobinate message.
+ // We'll just dump the Foo structure and all of its members.
+
+ printf("Frobinate:\n");
+
+ int depth = 1;
+ Print(depth, "foo", foo);
+ Print(depth, "baz", baz);
+ }
+};
+
+//----
+
+class SimpleMessageSender : public mojo::MessageSender {
+ public:
+ virtual void Send(const mojo::Message& message) {
+ // Imagine some IPC happened here.
+
+ // In the receiving process, an implementation of ServiceStub is known to
+ // the system. It receives the incoming message.
+ ServiceImpl impl;
+
+ sample::ServiceStub* stub = &impl;
+ stub->OnMessageReceived(message);
+ }
+};
+
+int main() {
+ SimpleMessageSender sender;
+
+ // User has a proxy to a Service somehow.
+ sample::Service* service = new sample::ServiceProxy(&sender);
+
+ // User constructs a message to send.
+
+ // Notice that it doesn't matter in what order the structs / arrays are
+ // allocated. Here, the various members of Foo are allocated before Foo is
+ // allocated.
+
+ mojo::MessageBuffer buf;
+
+ sample::Bar* bar = buf.Alloc<sample::Bar>();
+ bar->set_alpha(20);
+ bar->set_beta(40);
+ bar->set_gamma(60);
+
+ const size_t kNumDataElements = 10;
+ mojo::Array<uint8_t>* data = buf.AllocArray<uint8_t>(kNumDataElements);
+ for (size_t i = 0; i < kNumDataElements; ++i)
+ (*data)[i] = static_cast<uint8_t>(kNumDataElements - i);
+
+ const size_t kNumExtraBarsElements = 3;
+ mojo::Array<sample::Bar*>* extra_bars =
+ buf.AllocArray<sample::Bar*>(kNumExtraBarsElements);
+ for (size_t i = 0; i < kNumExtraBarsElements; ++i) {
+ sample::Bar* bar = buf.Alloc<sample::Bar>();
+ bar->set_alpha(i * 100);
+ bar->set_beta(i * 100 + 20);
+ bar->set_gamma(i * 100 + 40);
+ (*extra_bars)[i] = bar;
+ }
+
+ sample::Foo* foo = buf.Alloc<sample::Foo>();
+ foo->set_x(1);
+ foo->set_y(2);
+ foo->set_a(false);
+ foo->set_b(true);
+ foo->set_c(false);
+ foo->set_bar(bar);
viettrungluu 2013/10/01 00:51:04 I'm a little concerned that this and (even more so
+ foo->set_data(data);
+ foo->set_extra_bars(extra_bars);
+
+ service->Frobinate(foo, true);
+
+ return 0;
+}
« mojo/public/bindings/sample_service.idl ('K') | « mojo/public/bindings/sample_service.idl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698