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

Unified Diff: mojo/public/cpp/bindings/formatting.h

Issue 1532993002: Add output formatters for more mojom types. (Closed) Base URL: git@github.com:domokit/mojo.git@moz-9
Patch Set: rebase Created 4 years, 11 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/cpp/bindings/formatting.h
diff --git a/mojo/public/cpp/bindings/formatting.h b/mojo/public/cpp/bindings/formatting.h
new file mode 100644
index 0000000000000000000000000000000000000000..97b1aedc2894587384289484c99c1218da6da33a
--- /dev/null
+++ b/mojo/public/cpp/bindings/formatting.h
@@ -0,0 +1,77 @@
+// Copyright 2016 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.
+
+#ifndef MOJO_PUBLIC_CPP_BINDINGS_FORMATTING_H_
+#define MOJO_PUBLIC_CPP_BINDINGS_FORMATTING_H_
+
+#include <iosfwd>
+
+#include "mojo/public/cpp/bindings/array.h"
+#include "mojo/public/cpp/bindings/map.h"
+#include "mojo/public/cpp/bindings/struct_ptr.h"
+
+namespace mojo {
+
+// Prints the contents of an array to an output stream in a human-readable
+// format.
+template <typename T>
+std::ostream& operator<<(std::ostream& os, const mojo::Array<T>& array) {
+ if (array) {
+ os << "[";
+ bool first = true;
+ for (auto it = array.storage().cbegin(); it != array.storage().cend();
+ ++it) {
+ if (first)
+ first = false;
+ else
+ os << ", ";
+ os << *it;
+ }
+ os << "]";
+ } else {
+ os << "null";
+ }
+ return os;
+}
+
+// Prints the contents of a map to an output stream in a human-readable
+// format.
+template <typename Key, typename Value>
viettrungluu 2016/01/26 18:44:05 This is maybe a bit bikesheddy, but I think that t
jeffbrown 2016/01/26 22:57:59 Done.
+std::ostream& operator<<(std::ostream& os, const mojo::Map<Key, Value>& map) {
+ if (map) {
+ os << "[";
+ bool first = true;
+ for (auto it = map.cbegin(); it != map.cend(); ++it) {
+ if (first)
+ first = false;
+ else
+ os << ", ";
+ os << "{" << it.GetKey() << ": " << it.GetValue() << "}";
+ }
+ os << "]";
+ } else {
+ os << "null";
+ }
+ return os;
+}
+
+// Prints the pointee of a Mojo structure pointer to an output stream
+// assuming there exists an operator<< overload that accepts a const
+// reference to the object.
+template <typename T, typename = typename T::Data_>
+auto operator<<(std::ostream& os, const T* value) -> decltype(os << *value) {
+ return value ? os << *value : os << "null";
+}
+template <typename T>
+std::ostream& operator<<(std::ostream& os, const StructPtr<T>& value) {
+ return os << value.get();
+}
+template <typename T>
+std::ostream& operator<<(std::ostream& os, const InlinedStructPtr<T>& value) {
+ return os << value.get();
+}
+
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_CPP_BINDINGS_FORMATTING_H_

Powered by Google App Engine
This is Rietveld 408576698