OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_FORMATTING_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_FORMATTING_H_ | |
7 | |
8 #include <iosfwd> | |
9 | |
10 #include "mojo/public/cpp/bindings/array.h" | |
11 #include "mojo/public/cpp/bindings/map.h" | |
12 #include "mojo/public/cpp/bindings/struct_ptr.h" | |
13 | |
14 namespace mojo { | |
15 | |
16 // Prints the contents of an array to an output stream in a human-readable | |
17 // format. | |
18 template <typename T> | |
19 std::ostream& operator<<(std::ostream& os, const mojo::Array<T>& array) { | |
20 if (array) { | |
21 os << "["; | |
22 bool first = true; | |
23 for (auto it = array.storage().cbegin(); it != array.storage().cend(); | |
24 ++it) { | |
25 if (first) | |
26 first = false; | |
27 else | |
28 os << ", "; | |
29 os << *it; | |
30 } | |
31 os << "]"; | |
32 } else { | |
33 os << "null"; | |
34 } | |
35 return os; | |
36 } | |
37 | |
38 // Prints the contents of a map to an output stream in a human-readable | |
39 // format. | |
40 template <typename Key, typename Value> | |
41 std::ostream& operator<<(std::ostream& os, const mojo::Map<Key, Value>& map) { | |
42 if (map) { | |
43 os << "{"; | |
44 bool first = true; | |
45 for (auto it = map.cbegin(); it != map.cend(); ++it) { | |
46 if (first) | |
47 first = false; | |
48 else | |
49 os << ", "; | |
50 os << it.GetKey() << ": " << it.GetValue(); | |
51 } | |
52 os << "}"; | |
53 } else { | |
54 os << "null"; | |
55 } | |
56 return os; | |
57 } | |
58 | |
59 // Prints the pointee of a Mojo structure pointer to an output stream | |
60 // assuming there exists an operator<< overload that accepts a const | |
61 // reference to the object. | |
62 template <typename T, typename = typename T::Data_> | |
63 std::ostream& operator<<(std::ostream& os, const T* value) { | |
64 return value ? os << *value : os << "null"; | |
65 } | |
66 template <typename T> | |
67 std::ostream& operator<<(std::ostream& os, const StructPtr<T>& value) { | |
68 return os << value.get(); | |
69 } | |
70 template <typename T> | |
71 std::ostream& operator<<(std::ostream& os, const InlinedStructPtr<T>& value) { | |
72 return os << value.get(); | |
73 } | |
74 | |
75 } // namespace mojo | |
76 | |
77 #endif // MOJO_PUBLIC_CPP_BINDINGS_FORMATTING_H_ | |
OLD | NEW |