OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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_COMMON_LOGGING_H_ | |
6 #define MOJO_COMMON_LOGGING_H_ | |
7 | |
8 #include <ostream> | |
vardhan
2015/12/11 00:12:20
nit: blank line after this include
| |
9 #include "mojo/public/cpp/bindings/array.h" | |
10 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
11 #include "mojo/public/cpp/bindings/map.h" | |
12 | |
13 // Mojom Logging helpers. | |
vardhan
2015/12/11 00:12:20
Probably "mojom debug string" is more clarifying;
| |
14 | |
15 namespace mojo { | |
16 | |
17 // Support output formatting of Mojo structs by value or by reference | |
18 // assuming there exists an operator<< overload that accepts a const reference. | |
19 template <typename T, typename = typename T::Data_> | |
vardhan
2015/12/11 00:12:20
Hm, I was hoping you could use IsStructPtr<> in bi
| |
20 auto operator<<(std::ostream& os, const T* value) -> decltype(os << *value) { | |
vardhan
2015/12/11 00:12:20
What do you think about defining:
template <Mojom
| |
21 return value ? os << *value : os << "null"; | |
22 } | |
23 | |
24 template <typename T> | |
25 std::ostream& operator<<(std::ostream& os, const mojo::StructPtr<T>& value) { | |
26 return os << value.get(); | |
27 } | |
28 | |
29 template <typename T> | |
30 std::ostream& operator<<(std::ostream& os, | |
31 const mojo::InlinedStructPtr<T>& value) { | |
32 return os << value.get(); | |
33 } | |
34 | |
35 template <typename T> | |
36 std::ostream& operator<<(std::ostream& os, const mojo::InterfacePtr<T>& iface) { | |
37 return os << "{impl=" << iface.get() << ", bound=" << iface.is_bound() << "}"; | |
38 } | |
39 | |
40 template <typename T> | |
41 std::ostream& operator<<(std::ostream& os, const mojo::Array<T>& array) { | |
42 if (array) { | |
43 os << "["; | |
44 bool first = true; | |
45 for (auto it = array.storage().cbegin(); it != array.storage().cend(); | |
46 ++it) { | |
47 if (first) | |
48 first = false; | |
49 else | |
50 os << ", "; | |
51 os << *it; | |
52 } | |
53 os << "]"; | |
54 } else { | |
55 os << "null"; | |
56 } | |
57 return os; | |
58 } | |
59 | |
60 template <typename Key, typename Value> | |
61 std::ostream& operator<<(std::ostream& os, const mojo::Map<Key, Value>& map) { | |
62 if (map) { | |
63 os << "["; | |
64 bool first = true; | |
65 for (auto it = map.cbegin(); it != map.cend(); ++it) { | |
66 if (first) | |
67 first = false; | |
68 else | |
69 os << ", "; | |
70 os << "{" << it.GetKey() << ": " << it.GetValue() << "}"; | |
71 } | |
72 os << "]"; | |
73 } else { | |
74 os << "null"; | |
75 } | |
76 return os; | |
77 } | |
78 | |
79 } // namespace mojo | |
80 | |
81 #endif // MOJO_COMMON_LOGGING_H_ | |
OLD | NEW |