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#if defined(__cplusplus)
13
14#include <string.h>
15
16#include <map>
17#include <ostream>
18#include <vector>
19
20//----------------------------------------------------------------------
21// C string less than compare function object
22//----------------------------------------------------------------------
23struct CStringCompareFunctionObject
24{
25    bool operator() (const char* s1, const char* s2) const
26    {
27        return strcmp(s1, s2) < 0;
28    }
29};
30
31
32//----------------------------------------------------------------------
33// C string equality function object (binary predicate).
34//----------------------------------------------------------------------
35struct CStringEqualBinaryPredicate
36{
37    bool operator()(const char* s1, const char* s2) const
38    {
39        return strcmp(s1, s2) == 0;
40    }
41};
42
43
44//----------------------------------------------------------------------
45// Templated type for finding an entry in a std::map<F,S> whose value
46// is equal to something
47//----------------------------------------------------------------------
48template <class F, class S>
49class ValueEquals
50{
51private:
52    S second_value;
53
54public:
55    ValueEquals (const S& val) : second_value(val)
56    {}
57    // Compare the second item
58    bool operator() (std::pair<const F, S> elem)
59    {
60        return elem.second == second_value;
61    }
62};
63
64template <class T>
65inline void PrintAllCollectionElements (std::ostream &s, const T& coll, const char* header_cstr=NULL, const char* separator_cstr=" ")
66{
67    typename T::const_iterator pos;
68
69    if (header_cstr)
70        s << header_cstr;
71    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
72        s << *pos << separator_cstr;
73    }
74    s << std::endl;
75}
76
77// The function object below can be used to delete a STL container that
78// contains C++ object pointers.
79//
80// Usage: std::for_each(vector.begin(), vector.end(), for_each_delete());
81
82struct for_each_cplusplus_delete
83{
84   template <typename T>
85   void operator()(T *ptr){ delete ptr;}
86};
87
88typedef std::vector<std::string> STLStringArray;
89typedef std::vector<const char *> CStringArray;
90
91
92
93#endif  // #if defined(__cplusplus)
94#endif  // liblldb_STLUtils_h_
95