Add namespace qualifier
[oota-llvm.git] / include / Support / STLExtras.h
index 8196aebbdf31ab90b5ca234652f3ab6dd3da280e..06a15734a7b0149963c88b77ba8a29993a99fb0e 100644 (file)
@@ -1,4 +1,11 @@
-//===-- STLExtras.h - Useful functions when working with the STL -*- C++ -*--=//
+//===- STLExtras.h - Useful functions when working with the STL -*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file contains some templates that are useful if you are working with the
 // STL at all.
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_SUPPORT_STL_EXTRAS_H
-#define LLVM_SUPPORT_STL_EXTRAS_H
+#ifndef SUPPORT_STLEXTRAS_H
+#define SUPPORT_STLEXTRAS_H
 
 #include <functional>
+#include "Support/iterator"
+#include "boost/type_traits/transform_traits.hpp"
+
+namespace llvm {
 
 //===----------------------------------------------------------------------===//
 //     Extra additions to <functional>
@@ -75,7 +86,9 @@ public:
   typedef typename std::iterator_traits<RootIt>::difference_type
           difference_type;
   typedef typename UnaryFunc::result_type value_type;
-  typedef typename UnaryFunc::result_type *pointer;
+
+  typedef void pointer;
+  //typedef typename UnaryFunc::result_type *pointer;
   typedef void reference;        // Can't modify value returned by fn
 
   typedef RootIt iterator_type;
@@ -220,4 +233,53 @@ template <class InIt, class OutIt, class Functor>
 inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
   return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);
 }
+
+
+//===----------------------------------------------------------------------===//
+//     Extra additions to <utility>
+//===----------------------------------------------------------------------===//
+
+// tie - this function ties two objects and returns a temporary object
+// that is assignable from a std::pair. This can be used to make code
+// more readable when using values returned from functions bundled in
+// a std::pair. Since an example is worth 1000 words:
+//
+// typedef std::map<int, int> Int2IntMap;
+// 
+// Int2IntMap myMap;
+// Int2IntMap::iterator where;
+// bool inserted;
+// tie(where, inserted) = myMap.insert(std::make_pair(123,456));
+//
+// if (inserted)
+//   // do stuff
+// else
+//   // do other stuff
+
+namespace
+{
+  template <typename T1, typename T2>
+  struct tier {
+    typedef typename boost::add_reference<T1>::type first_type;
+    typedef typename boost::add_reference<T2>::type second_type;
+
+    first_type first;
+    second_type second;
+
+    tier(first_type f, second_type s) : first(f), second(s) { }
+    tier& operator=(const std::pair<T1, T2>& p) {
+      first = p.first;
+      second = p.second;
+      return *this;
+    }
+  };
+}
+
+template <typename T1, typename T2>
+inline tier<T1, T2> tie(T1& f, T2& s) {
+  return tier<T1, T2>(f, s);
+}
+
+} // End llvm namespace
+
 #endif