From e292da29bfeb2faad71a568e9cbb706affd5f330 Mon Sep 17 00:00:00 2001 From: Alkis Evlogimenos Date: Wed, 5 Nov 2003 05:58:26 +0000 Subject: [PATCH] Add std::pair tier. This is a much simplified version of boost::tie 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 | 48 ++++++++++++++++++++++++++++++++++++ include/llvm/ADT/STLExtras.h | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/include/Support/STLExtras.h b/include/Support/STLExtras.h index bc84f32037a..28c46e3d99a 100644 --- a/include/Support/STLExtras.h +++ b/include/Support/STLExtras.h @@ -19,6 +19,7 @@ #include #include "Support/iterator" +#include "boost/type_traits/transform_traits.hpp" //===----------------------------------------------------------------------===// // Extra additions to @@ -230,4 +231,51 @@ template 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 +//===----------------------------------------------------------------------===// + +// 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 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 + struct tier { + typedef typename boost::add_reference::type first_type; + typedef typename boost::add_reference::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& p) { + first = p.first; + second = p.second; + return *this; + } + }; +} + +template +inline tier tie(T1& f, T2& s) { + return tier(f, s); +} + #endif diff --git a/include/llvm/ADT/STLExtras.h b/include/llvm/ADT/STLExtras.h index bc84f32037a..28c46e3d99a 100644 --- a/include/llvm/ADT/STLExtras.h +++ b/include/llvm/ADT/STLExtras.h @@ -19,6 +19,7 @@ #include #include "Support/iterator" +#include "boost/type_traits/transform_traits.hpp" //===----------------------------------------------------------------------===// // Extra additions to @@ -230,4 +231,51 @@ template 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 +//===----------------------------------------------------------------------===// + +// 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 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 + struct tier { + typedef typename boost::add_reference::type first_type; + typedef typename boost::add_reference::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& p) { + first = p.first; + second = p.second; + return *this; + } + }; +} + +template +inline tier tie(T1& f, T2& s) { + return tier(f, s); +} + #endif