Use a STL helper template 'pair_value_iterator', by both DeclReferenceMap and Selecto...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 29 Jul 2009 23:41:33 +0000 (23:41 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 29 Jul 2009 23:41:33 +0000 (23:41 +0000)
llvm-svn: 77545

clang/include/clang/Index/DeclReferenceMap.h
clang/include/clang/Index/STLExtras.h [new file with mode: 0644]
clang/include/clang/Index/SelectorMap.h

index d1e2c09..1ed6436 100644 (file)
@@ -16,6 +16,7 @@
 #define LLVM_CLANG_INDEX_DECLREFERENCEMAP_H
 
 #include "clang/Index/ASTLocation.h"
+#include "clang/Index/STLExtras.h"
 #include <map>
 
 namespace clang {
@@ -32,43 +33,7 @@ public:
   explicit DeclReferenceMap(ASTContext &Ctx);
   
   typedef std::multimap<NamedDecl*, ASTLocation> MapTy;
-
-  class astlocation_iterator {
-    MapTy::iterator I;
-
-    astlocation_iterator(MapTy::iterator i) : I(i) { }
-    friend class DeclReferenceMap;
-
-  public:
-    typedef ASTLocation  value_type;
-    typedef ASTLocation& reference;
-    typedef ASTLocation* pointer;
-    typedef MapTy::iterator::iterator_category iterator_category;
-    typedef MapTy::iterator::difference_type   difference_type;
-
-    astlocation_iterator() { }
-
-    reference operator*() const { return I->second; }
-    pointer operator->() const { return &I->second; }
-
-    astlocation_iterator& operator++() {
-      ++I;
-      return *this;
-    }
-
-    astlocation_iterator operator++(int) {
-      astlocation_iterator tmp(*this);
-      ++(*this);
-      return tmp;
-    }
-
-    friend bool operator==(astlocation_iterator L, astlocation_iterator R) { 
-      return L.I == R.I;
-    }
-    friend bool operator!=(astlocation_iterator L, astlocation_iterator R) { 
-      return L.I != R.I;
-    }
-  };
+  typedef pair_value_iterator<MapTy::iterator> astlocation_iterator;
 
   astlocation_iterator refs_begin(NamedDecl *D) const;
   astlocation_iterator refs_end(NamedDecl *D) const;
diff --git a/clang/include/clang/Index/STLExtras.h b/clang/include/clang/Index/STLExtras.h
new file mode 100644 (file)
index 0000000..a970720
--- /dev/null
@@ -0,0 +1,63 @@
+//===--- STLExtras.h - Helper STL related templates -------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  Helper templates for using with the STL.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_INDEX_STLEXTRAS_H
+#define LLVM_CLANG_INDEX_STLEXTRAS_H
+
+namespace clang {
+
+namespace idx {
+
+/// \brief Wraps an iterator whose value_type is a pair, and provides
+/// pair's second object as the value.
+template <typename iter_type>
+class pair_value_iterator {
+  iter_type I;
+
+public:
+  typedef typename iter_type::value_type::second_type value_type;
+  typedef value_type& reference;
+  typedef value_type* pointer;
+  typedef typename iter_type::iterator_category iterator_category;
+  typedef typename iter_type::difference_type   difference_type;
+
+  pair_value_iterator() { }
+  pair_value_iterator(iter_type i) : I(i) { }
+
+  reference operator*() const { return I->second; }
+  pointer operator->() const { return &I->second; }
+
+  pair_value_iterator& operator++() {
+    ++I;
+    return *this;
+  }
+
+  pair_value_iterator operator++(int) {
+    pair_value_iterator tmp(*this);
+    ++(*this);
+    return tmp;
+  }
+
+  friend bool operator==(pair_value_iterator L, pair_value_iterator R) { 
+    return L.I == R.I;
+  }
+  friend bool operator!=(pair_value_iterator L, pair_value_iterator R) { 
+    return L.I != R.I;
+  }
+};
+
+} // end idx namespace
+  
+} // end clang namespace
+
+#endif
index 4cd10cc..0fb6afb 100644 (file)
@@ -16,6 +16,7 @@
 #define LLVM_CLANG_INDEX_SELECTORMAP_H
 
 #include "clang/Index/ASTLocation.h"
+#include "clang/Index/STLExtras.h"
 #include "clang/Basic/IdentifierTable.h"
 #include <map>
 
@@ -29,53 +30,14 @@ namespace idx {
 ///
 /// References are mapped and retrieved using the canonical decls.
 class SelectorMap {
-
-  template <typename iter_type>
-  class wrap_pair_iterator {
-    iter_type I;
-
-    wrap_pair_iterator(iter_type i) : I(i) { }
-    friend class SelectorMap;
-
-  public:
-    typedef typename iter_type::value_type::second_type value_type;
-    typedef value_type& reference;
-    typedef value_type* pointer;
-    typedef typename iter_type::iterator_category iterator_category;
-    typedef typename iter_type::difference_type   difference_type;
-
-    wrap_pair_iterator() { }
-
-    reference operator*() const { return I->second; }
-    pointer operator->() const { return &I->second; }
-
-    wrap_pair_iterator& operator++() {
-      ++I;
-      return *this;
-    }
-
-    wrap_pair_iterator operator++(int) {
-      wrap_pair_iterator tmp(*this);
-      ++(*this);
-      return tmp;
-    }
-
-    friend bool operator==(wrap_pair_iterator L, wrap_pair_iterator R) { 
-      return L.I == R.I;
-    }
-    friend bool operator!=(wrap_pair_iterator L, wrap_pair_iterator R) { 
-      return L.I != R.I;
-    }
-  };
-
 public:
   explicit SelectorMap(ASTContext &Ctx);
   
   typedef std::multimap<Selector, ObjCMethodDecl *> SelMethMapTy;
   typedef std::multimap<Selector, ASTLocation> SelRefMapTy;
 
-  typedef wrap_pair_iterator<SelMethMapTy::iterator> method_iterator;
-  typedef wrap_pair_iterator<SelRefMapTy::iterator> astlocation_iterator;
+  typedef pair_value_iterator<SelMethMapTy::iterator> method_iterator;
+  typedef pair_value_iterator<SelRefMapTy::iterator> astlocation_iterator;
 
   method_iterator methods_begin(Selector Sel) const;
   method_iterator methods_end(Selector Sel) const;