1 //===-- STLUtils.h ----------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_STLUtils_h_ 11 #define liblldb_STLUtils_h_ 12 13 #include <string.h> 14 15 #include <map> 16 #include <ostream> 17 #include <vector> 18 19 20 //---------------------------------------------------------------------- 21 // C string less than compare function object 22 //---------------------------------------------------------------------- 23 struct CStringCompareFunctionObject { operatorCStringCompareFunctionObject24 bool operator()(const char *s1, const char *s2) const { 25 return strcmp(s1, s2) < 0; 26 } 27 }; 28 29 //---------------------------------------------------------------------- 30 // C string equality function object (binary predicate). 31 //---------------------------------------------------------------------- 32 struct CStringEqualBinaryPredicate { operatorCStringEqualBinaryPredicate33 bool operator()(const char *s1, const char *s2) const { 34 return strcmp(s1, s2) == 0; 35 } 36 }; 37 38 //---------------------------------------------------------------------- 39 // Templated type for finding an entry in a std::map<F,S> whose value is equal 40 // to something 41 //---------------------------------------------------------------------- 42 template <class F, class S> class ValueEquals { 43 public: ValueEquals(const S & val)44 ValueEquals(const S &val) : second_value(val) {} 45 46 // Compare the second item operator()47 bool operator()(std::pair<const F, S> elem) { 48 return elem.second == second_value; 49 } 50 51 private: 52 S second_value; 53 }; 54 55 template <class T> 56 inline void PrintAllCollectionElements(std::ostream &s, const T &coll, 57 const char *header_cstr = nullptr, 58 const char *separator_cstr = " ") { 59 typename T::const_iterator pos; 60 61 if (header_cstr) 62 s << header_cstr; 63 for (pos = coll.begin(); pos != coll.end(); ++pos) { 64 s << *pos << separator_cstr; 65 } 66 s << std::endl; 67 } 68 69 // The function object below can be used to delete a STL container that 70 // contains C++ object pointers. 71 // 72 // Usage: std::for_each(vector.begin(), vector.end(), for_each_delete()); 73 74 struct for_each_cplusplus_delete { operatorfor_each_cplusplus_delete75 template <typename T> void operator()(T *ptr) { delete ptr; } 76 }; 77 78 typedef std::vector<std::string> STLStringArray; 79 typedef std::vector<const char *> CStringArray; 80 81 #endif // liblldb_STLUtils_h_ 82