Add std::pair tier. This is a much simplified version of boost::tie
authorAlkis Evlogimenos <alkis@evlogimenos.com>
Wed, 5 Nov 2003 05:58:26 +0000 (05:58 +0000)
committerAlkis Evlogimenos <alkis@evlogimenos.com>
Wed, 5 Nov 2003 05:58:26 +0000 (05:58 +0000)
that works only for std::pair.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9723 91177308-0d34-0410-b5e6-96231b3b80d8

include/Support/STLExtras.h
include/llvm/ADT/STLExtras.h

index bc84f32037a7a354e24342b934954e4ddaf0a6ca..28c46e3d99a0e3fd4749fa22849f82bf67acb1f2 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <functional>
 #include "Support/iterator"
+#include "boost/type_traits/transform_traits.hpp"
 
 //===----------------------------------------------------------------------===//
 //     Extra additions to <functional>
@@ -230,4 +231,51 @@ 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);
+}
+
 #endif
index bc84f32037a7a354e24342b934954e4ddaf0a6ca..28c46e3d99a0e3fd4749fa22849f82bf67acb1f2 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <functional>
 #include "Support/iterator"
+#include "boost/type_traits/transform_traits.hpp"
 
 //===----------------------------------------------------------------------===//
 //     Extra additions to <functional>
@@ -230,4 +231,51 @@ 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);
+}
+
 #endif