1/*
2 * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/CommaPrinter.h>
29#include <wtf/PrintStream.h>
30#include <wtf/StringPrintStream.h>
31
32namespace WTF {
33
34template<typename T>
35class ListDump {
36public:
37 ListDump(const T& list, const char* comma)
38 : m_list(list)
39 , m_comma(comma)
40 {
41 }
42
43 void dump(PrintStream& out) const
44 {
45 for (auto iter = m_list.begin(); iter != m_list.end(); ++iter)
46 out.print(m_comma, *iter);
47 }
48
49private:
50 const T& m_list;
51 CommaPrinter m_comma;
52};
53
54template<typename T>
55class PointerListDump {
56public:
57 PointerListDump(const T& list, const char* comma)
58 : m_list(list)
59 , m_comma(comma)
60 {
61 }
62
63 void dump(PrintStream& out) const
64 {
65 for (auto iter = m_list.begin(); iter != m_list.end(); ++iter)
66 out.print(m_comma, pointerDump(*iter));
67 }
68
69private:
70 const T& m_list;
71 CommaPrinter m_comma;
72};
73
74template<typename T>
75class MapDump {
76public:
77 MapDump(const T& map, const char* arrow, const char* comma)
78 : m_map(map)
79 , m_arrow(arrow)
80 , m_comma(comma)
81 {
82 }
83
84 void dump(PrintStream& out) const
85 {
86 for (auto iter = m_map.begin(); iter != m_map.end(); ++iter)
87 out.print(m_comma, iter->key, m_arrow, iter->value);
88 }
89
90private:
91 const T& m_map;
92 const char* m_arrow;
93 CommaPrinter m_comma;
94};
95
96template<typename T>
97ListDump<T> listDump(const T& list, const char* comma = ", ")
98{
99 return ListDump<T>(list, comma);
100}
101
102template<typename T>
103PointerListDump<T> pointerListDump(const T& list, const char* comma = ", ")
104{
105 return PointerListDump<T>(list, comma);
106}
107
108template<typename T, typename Comparator>
109CString sortedListDump(const T& list, const Comparator& comparator, const char* comma = ", ")
110{
111 Vector<typename T::ValueType> myList;
112 myList.appendRange(list.begin(), list.end());
113 std::sort(myList.begin(), myList.end(), comparator);
114 StringPrintStream out;
115 CommaPrinter commaPrinter(comma);
116 for (unsigned i = 0; i < myList.size(); ++i)
117 out.print(commaPrinter, myList[i]);
118 return out.toCString();
119}
120
121template<typename T>
122CString sortedListDump(const T& list, const char* comma = ", ")
123{
124 return sortedListDump(list, std::less<typename T::ValueType>(), comma);
125}
126
127template<typename T>
128MapDump<T> mapDump(const T& map, const char* arrow = "=>", const char* comma = ", ")
129{
130 return MapDump<T>(map, arrow, comma);
131}
132
133template<typename T, typename Comparator>
134CString sortedMapDump(const T& map, const Comparator& comparator, const char* arrow = "=>", const char* comma = ", ")
135{
136 Vector<typename T::KeyType> keys;
137 for (auto iter = map.begin(); iter != map.end(); ++iter)
138 keys.append(iter->key);
139 std::sort(keys.begin(), keys.end(), comparator);
140 StringPrintStream out;
141 CommaPrinter commaPrinter(comma);
142 for (unsigned i = 0; i < keys.size(); ++i)
143 out.print(commaPrinter, keys[i], arrow, map.get(keys[i]));
144 return out.toCString();
145}
146
147template<typename T, typename U>
148class ListDumpInContext {
149public:
150 ListDumpInContext(const T& list, U* context, const char* comma)
151 : m_list(list)
152 , m_context(context)
153 , m_comma(comma)
154 {
155 }
156
157 void dump(PrintStream& out) const
158 {
159 for (auto iter = m_list.begin(); iter != m_list.end(); ++iter)
160 out.print(m_comma, inContext(*iter, m_context));
161 }
162
163private:
164 const T& m_list;
165 U* m_context;
166 CommaPrinter m_comma;
167};
168
169template<typename T, typename U>
170ListDumpInContext<T, U> listDumpInContext(
171 const T& list, U* context, const char* comma = ", ")
172{
173 return ListDumpInContext<T, U>(list, context, comma);
174}
175
176} // namespace WTF
177
178using WTF::listDump;
179using WTF::listDumpInContext;
180using WTF::mapDump;
181using WTF::pointerListDump;
182using WTF::sortedListDump;
183using WTF::sortedMapDump;
184