1/* 2 * Copyright (C) 2013 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#ifndef ListDump_h 27#define ListDump_h 28 29#include "CommaPrinter.h" 30#include "PrintStream.h" 31#include "StringPrintStream.h" 32 33namespace WTF { 34 35template<typename T> 36class ListDump { 37public: 38 ListDump(const T& list, const char* comma) 39 : m_list(list) 40 , m_comma(comma) 41 { 42 } 43 44 void dump(PrintStream& out) const 45 { 46 for (typename T::const_iterator iter = m_list.begin(); iter != m_list.end(); ++iter) 47 out.print(m_comma, *iter); 48 } 49 50private: 51 const T& m_list; 52 CommaPrinter m_comma; 53}; 54 55template<typename T> 56class MapDump { 57public: 58 MapDump(const T& map, const char* arrow, const char* comma) 59 : m_map(map) 60 , m_arrow(arrow) 61 , m_comma(comma) 62 { 63 } 64 65 void dump(PrintStream& out) const 66 { 67 for (typename T::const_iterator iter = m_map.begin(); iter != m_map.end(); ++iter) 68 out.print(m_comma, iter->key, m_arrow, iter->value); 69 } 70 71private: 72 const T& m_map; 73 const char* m_arrow; 74 CommaPrinter m_comma; 75}; 76 77template<typename T> 78ListDump<T> listDump(const T& list, const char* comma = ", ") 79{ 80 return ListDump<T>(list, comma); 81} 82 83template<typename T, typename Comparator> 84CString sortedListDump(const T& list, const Comparator& comparator, const char* comma = ", ") 85{ 86 Vector<typename T::ValueType> myList; 87 myList.appendRange(list.begin(), list.end()); 88 std::sort(myList.begin(), myList.end(), comparator); 89 StringPrintStream out; 90 CommaPrinter commaPrinter(comma); 91 for (unsigned i = 0; i < myList.size(); ++i) 92 out.print(commaPrinter, myList[i]); 93 return out.toCString(); 94} 95 96template<typename T> 97CString sortedListDump(const T& list, const char* comma = ", ") 98{ 99 return sortedListDump(list, std::less<typename T::ValueType>(), comma); 100} 101 102template<typename T> 103MapDump<T> mapDump(const T& map, const char* arrow = "=>", const char* comma = ", ") 104{ 105 return MapDump<T>(map, arrow, comma); 106} 107 108template<typename T, typename Comparator> 109CString sortedMapDump(const T& map, const Comparator& comparator, const char* arrow = "=>", const char* comma = ", ") 110{ 111 Vector<typename T::KeyType> keys; 112 for (typename T::const_iterator iter = map.begin(); iter != map.end(); ++iter) 113 keys.append(iter->key); 114 std::sort(keys.begin(), keys.end(), comparator); 115 StringPrintStream out; 116 CommaPrinter commaPrinter(comma); 117 for (unsigned i = 0; i < keys.size(); ++i) 118 out.print(commaPrinter, keys[i], arrow, map.get(keys[i])); 119 return out.toCString(); 120} 121 122} // namespace WTF 123 124using WTF::listDump; 125using WTF::sortedListDump; 126using WTF::mapDump; 127using WTF::sortedMapDump; 128 129#endif // ListDump_h 130 131