From a40c0bf6a889f3343244565a19353f6d1aa4152b Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Thu, 19 Apr 2012 12:28:12 +0000 Subject: [PATCH 1/3] A branch to extend the CGAL::cpp0x::get function template in --- .../doc_tex/STL_Extension_ref/tuple.tex | 3 +- STL_Extension/include/CGAL/tuple.h | 52 +++++++++++++++++-- .../test/STL_Extension/test_stl_extension.cpp | 21 ++++++-- 3 files changed, 66 insertions(+), 10 deletions(-) diff --git a/STL_Extension/doc_tex/STL_Extension_ref/tuple.tex b/STL_Extension/doc_tex/STL_Extension_ref/tuple.tex index b366e999da4..8689874bea1 100644 --- a/STL_Extension/doc_tex/STL_Extension_ref/tuple.tex +++ b/STL_Extension/doc_tex/STL_Extension_ref/tuple.tex @@ -33,7 +33,8 @@ is taken from Boost. Some free functions part of the standard interface of \ccc{tuple} are also brought in namespace \ccc{CGAL::cpp0x} with using declarations, these are \ccc{make_tuple}, -\ccc{get}, \ccc{tie}. +\ccc{get}, \ccc{tie}. Like in C++0x, the \ccc{get} function template is +specialized so that it can take \ccc{std::pair} as argument. Two standard helper classes are also provided for convenience (\ccc{tuple_size} and \ccc{tuple_element}). \end{ccRefClass} diff --git a/STL_Extension/include/CGAL/tuple.h b/STL_Extension/include/CGAL/tuple.h index 65699b3c76e..c1ab51bde52 100644 --- a/STL_Extension/include/CGAL/tuple.h +++ b/STL_Extension/include/CGAL/tuple.h @@ -27,12 +27,14 @@ #ifndef CGAL_CFG_NO_CPP0X_TUPLE # include -#elif !defined CGAL_CFG_NO_TR1_TUPLE -# include -#else -# include -# include #endif +#ifndef CGAL_CFG_NO_TR1_TUPLE +# include +#endif + +#include +#include +#include namespace CGAL { @@ -68,6 +70,46 @@ struct tuple_element: public boost::tuples::element{}; #endif + +#if defined(CGAL_CFG_NO_CPP0X_TUPLE) // if not C++11 tuple + +//////////////////////////////////////////////////////////// +// // +// Allow CGAL::cpp0x::get(std::pair), if N==0 or N==1. // +// // +// That is already in C++11, but not in TR1 or in Boost // +// // +//////////////////////////////////////////////////////////// +template +struct pair_get; + +template +struct pair_get<0, T1, T2> { + static T1& get(std::pair& pair) { return pair.first; } + static const T1& get(const std::pair& pair) { return pair.first; } +}; // end specialization struct pair_get<0, T2, T2> + +template +struct pair_get<1, T1, T2> { + static T2& get(std::pair& pair) { return pair.second; } + static const T2& get(const std::pair& pair) { return pair.second; } +}; // end specialization struct pair_get<0, T2, T2> + +template +inline typename boost::tuples::element >::type& +get(std::pair& pair) { + return pair_get::get(pair); +} + +template +inline const typename boost::tuples::element >::type& +get(const std::pair& pair) { + return pair_get::get(pair); +} + +#endif // end if not C++11 tuple + + } // cpp0x #ifndef CGAL_CFG_NO_CPP0X_VARIADIC_TEMPLATES diff --git a/STL_Extension/test/STL_Extension/test_stl_extension.cpp b/STL_Extension/test/STL_Extension/test_stl_extension.cpp index 142fbd51689..75e8bbbb05e 100644 --- a/STL_Extension/test/STL_Extension/test_stl_extension.cpp +++ b/STL_Extension/test/STL_Extension/test_stl_extension.cpp @@ -8110,10 +8110,7 @@ void test_tuple(){ T1 t1=CGAL::cpp0x::make_tuple(1,2); T1 t1_2=CGAL::cpp0x::make_tuple(1,2); - int i1=-1,i2=-1; - CGAL::cpp0x::tie(i1,i2)=t1; - CGAL_assertion( CGAL::cpp0x::get<0>(t1)==i1 ); - CGAL_assertion( CGAL::cpp0x::get<1>(t1)==i2 ); + CGAL_assertion(t1==t1_2); // test the equality operator // T2 t2 = T2(); @@ -8122,6 +8119,22 @@ void test_tuple(){ // Do not test equality between default initialized tuples, because // GNU/g++ version 4.1.2 does not default-initialize correctly // std::tr1::tuple. + + // Test CGAL::cpp0x::tie + int i1=-1,i2=-1; + CGAL::cpp0x::tie(i1,i2)=t1; + CGAL_assertion( CGAL::cpp0x::get<0>(t1)==i1 ); + CGAL_assertion( CGAL::cpp0x::get<1>(t1)==i2 ); + + // Test CGAL::cpp0x::get for a pair + double d = 1; + std::pair pair(-3, &d); + const std::pair const_pair(2, &d); + + assert(CGAL::cpp0x::get<0>(pair) == -3); + assert(CGAL::cpp0x::get<1>(pair) == &d); + assert(CGAL::cpp0x::get<0>(const_pair) == 2); + assert(CGAL::cpp0x::get<1>(const_pair) == &d); } void test_prev_next() From 66ec3ee6b8e5f3e06a952f5288ececf0977a7159 Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Fri, 20 Apr 2012 10:30:10 +0000 Subject: [PATCH 2/3] I was wrong: get(std::pair) was already in TR1. Strange that it is not in Boost. --- STL_Extension/include/CGAL/tuple.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/STL_Extension/include/CGAL/tuple.h b/STL_Extension/include/CGAL/tuple.h index c1ab51bde52..8088806b939 100644 --- a/STL_Extension/include/CGAL/tuple.h +++ b/STL_Extension/include/CGAL/tuple.h @@ -71,13 +71,14 @@ struct tuple_element: public boost::tuples::element{}; #endif -#if defined(CGAL_CFG_NO_CPP0X_TUPLE) // if not C++11 tuple +#if defined(CGAL_CFG_NO_CPP0X_TUPLE) && defined(CGAL_CFG_NO_TR1_TUPLE) +// If not TR1 or C++11 tuple, we need to add get(std::pair). //////////////////////////////////////////////////////////// // // // Allow CGAL::cpp0x::get(std::pair), if N==0 or N==1. // // // -// That is already in C++11, but not in TR1 or in Boost // +// That is already in TR1 and C++11, but not in Boost. // // // //////////////////////////////////////////////////////////// template From 4c186e9417da06202cad8d1c77f8ae0ef95c9865 Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Fri, 20 Apr 2012 10:34:06 +0000 Subject: [PATCH 3/3] Oops: std::tr1::get(std::pair) is in --- STL_Extension/include/CGAL/tuple.h | 1 + 1 file changed, 1 insertion(+) diff --git a/STL_Extension/include/CGAL/tuple.h b/STL_Extension/include/CGAL/tuple.h index 8088806b939..698df419e55 100644 --- a/STL_Extension/include/CGAL/tuple.h +++ b/STL_Extension/include/CGAL/tuple.h @@ -30,6 +30,7 @@ #endif #ifndef CGAL_CFG_NO_TR1_TUPLE # include +# include #endif #include