| 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 | |
| 32 | namespace WTF { |
| 33 | |
| 34 | template<typename T> |
| 35 | class ListDump { |
| 36 | public: |
| 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 | |
| 49 | private: |
| 50 | const T& m_list; |
| 51 | CommaPrinter m_comma; |
| 52 | }; |
| 53 | |
| 54 | template<typename T> |
| 55 | class PointerListDump { |
| 56 | public: |
| 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 | |
| 69 | private: |
| 70 | const T& m_list; |
| 71 | CommaPrinter m_comma; |
| 72 | }; |
| 73 | |
| 74 | template<typename T> |
| 75 | class MapDump { |
| 76 | public: |
| 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 | |
| 90 | private: |
| 91 | const T& m_map; |
| 92 | const char* m_arrow; |
| 93 | CommaPrinter m_comma; |
| 94 | }; |
| 95 | |
| 96 | template<typename T> |
| 97 | ListDump<T> listDump(const T& list, const char* comma = ", " ) |
| 98 | { |
| 99 | return ListDump<T>(list, comma); |
| 100 | } |
| 101 | |
| 102 | template<typename T> |
| 103 | PointerListDump<T> pointerListDump(const T& list, const char* comma = ", " ) |
| 104 | { |
| 105 | return PointerListDump<T>(list, comma); |
| 106 | } |
| 107 | |
| 108 | template<typename T, typename Comparator> |
| 109 | CString 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 | |
| 121 | template<typename T> |
| 122 | CString sortedListDump(const T& list, const char* comma = ", " ) |
| 123 | { |
| 124 | return sortedListDump(list, std::less<typename T::ValueType>(), comma); |
| 125 | } |
| 126 | |
| 127 | template<typename T> |
| 128 | MapDump<T> mapDump(const T& map, const char* arrow = "=>" , const char* comma = ", " ) |
| 129 | { |
| 130 | return MapDump<T>(map, arrow, comma); |
| 131 | } |
| 132 | |
| 133 | template<typename T, typename Comparator> |
| 134 | CString 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 | |
| 147 | template<typename T, typename U> |
| 148 | class ListDumpInContext { |
| 149 | public: |
| 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 | |
| 163 | private: |
| 164 | const T& m_list; |
| 165 | U* m_context; |
| 166 | CommaPrinter m_comma; |
| 167 | }; |
| 168 | |
| 169 | template<typename T, typename U> |
| 170 | ListDumpInContext<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 | |
| 178 | using WTF::listDump; |
| 179 | using WTF::listDumpInContext; |
| 180 | using WTF::mapDump; |
| 181 | using WTF::pointerListDump; |
| 182 | using WTF::sortedListDump; |
| 183 | using WTF::sortedMapDump; |
| 184 | |