/* Copyright 2005-2011 Intel Corporation. All Rights Reserved. This file is part of Threading Building Blocks. Threading Building Blocks is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. Threading Building Blocks is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Threading Building Blocks; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA As a special exception, you may use this file as part of a free software library without restriction. Specifically, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other files to produce an executable, this file does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */ #ifndef __TBB_tuple_H #define __TBB_tuple_H #if !TBB_PREVIEW_TUPLE #error Set TBB_PREVIEW_TUPLE to include compat/tuple #endif #include #include "../tbb_stddef.h" namespace tbb { namespace interface5 { namespace internal { struct null_type { }; } using internal::null_type; // tuple forward declaration template class tuple; namespace internal { // const null_type temp inline const null_type cnull() { return null_type(); } // cons forward declaration template struct cons; // type of a component of the cons template struct component { typedef typename T::tail_type next; typedef typename component::type type; }; template struct component<0,T> { typedef typename T::head_type type; }; template<> struct component<0,null_type> { typedef null_type type; }; // const version of component template struct component { typedef typename T::tail_type next; typedef typename component::type type; }; template struct component<0, const T> { typedef const typename T::head_type type; }; // helper class for getting components of cons template< int N> struct get_helper { template inline static typename component >::type& get(cons& ti) { return get_helper::get(ti.tail); } }; template<> struct get_helper<0> { template inline static typename component<0, cons >::type& get(cons& ti) { return ti.head; } }; // traits adaptor template struct tuple_traits { typedef cons ::U > U; }; template <> struct tuple_traits { typedef cons U; }; template<> struct tuple_traits { typedef null_type U; }; // core cons defs template struct cons{ typedef HT head_type; typedef TT tail_type; HT head; TT tail; static const int length = 1 + tail_type::length; // default constructors explicit cons() : head(), tail() { } // non-default constructors cons(head_type& h, const tail_type& t) : head(h), tail(t) { } template cons(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9) : head(t0), tail(t1, t2, t3, t4, t5, t6, t7, t8, t9, cnull()) { } template cons(T0& t0, T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6, T7& t7, T8& t8, T9& t9) : head(t0), tail(t1, t2, t3, t4, t5, t6, t7, t8, t9, cnull()) { } template cons(const cons& other) : head(other.head), tail(other.tail) { } cons& operator=(const cons& other) { head = other.head; tail = other.tail; return *this; } friend bool operator==(const cons& me, const cons& other) { return me.head == other.head && me.tail == other.tail; } friend bool operator<(const cons& me, const cons& other) { return me.head < other.head || (!(other.head < me.head) && me.tail < other.tail); } friend bool operator>(const cons& me, const cons& other) { return other=(const cons& me, const cons& other) { return !(meother); } template friend bool operator==(const cons& me, const cons& other) { return me.head == other.head && me.tail == other.tail; } template friend bool operator<(const cons& me, const cons& other) { return me.head < other.head || (!(other.head < me.head) && me.tail < other.tail); } template friend bool operator>(const cons& me, const cons& other) { return other friend bool operator!=(const cons& me, const cons& other) { return !(me==other); } template friend bool operator>=(const cons& me, const cons& other) { return !(me friend bool operator<=(const cons& me, const cons& other) { return !(me>other); } }; // cons template struct cons { typedef HT head_type; typedef null_type tail_type; static const int length = 1; head_type head; // default constructor cons() : head() { /*std::cout << "default constructor 1\n";*/ } cons(const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&) : head() { /*std::cout << "default constructor 2\n";*/ } // non-default constructor template cons(T1& t1, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type& ) : head(t1) { /*std::cout << "non-default a1, t1== " << t1 << "\n";*/} cons(head_type& h, const null_type& = null_type() ) : head(h) { } cons(const head_type& t0, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&) : head(t0) { } // converting constructor template cons(HT1 h1, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&, const null_type&) : head(h1) { } // copy constructor template cons( const cons& other) : head(other.head) { } // assignment operator cons& operator=(const cons& other) { head = other.head; return *this; } friend bool operator==(const cons& me, const cons& other) { return me.head == other.head; } friend bool operator<(const cons& me, const cons& other) { return me.head < other.head; } friend bool operator>(const cons& me, const cons& other) { return otherother); } friend bool operator>=(const cons& me, const cons& other) {return !(me friend bool operator==(const cons& me, const cons& other) { return me.head == other.head; } template friend bool operator<(const cons& me, const cons& other) { return me.head < other.head; } template friend bool operator>(const cons& me, const cons& other) { return other friend bool operator!=(const cons& me, const cons& other) { return !(me==other); } template friend bool operator<=(const cons& me, const cons& other) { return !(me>other); } template friend bool operator>=(const cons& me, const cons& other) { return !(me struct cons { typedef null_type tail_type; static const int length = 0; }; // wrapper for default constructor template inline const T wrap_dcons(T*) { return T(); } } // namespace internal // tuple definition template class tuple : public internal::tuple_traits::U { // friends template friend class tuple_size; template friend struct tuple_element; // stl components typedef tuple value_type; typedef value_type *pointer; typedef const value_type *const_pointer; typedef value_type &reference; typedef const value_type &const_reference; typedef size_t size_type; typedef typename internal::tuple_traits::U my_cons; public: tuple(const T0& t0=internal::wrap_dcons((T0*)NULL), const T1& t1=internal::wrap_dcons((T1*)NULL), const T2& t2=internal::wrap_dcons((T2*)NULL), const T3& t3=internal::wrap_dcons((T3*)NULL), const T4& t4=internal::wrap_dcons((T4*)NULL), const T5& t5=internal::wrap_dcons((T5*)NULL), const T6& t6=internal::wrap_dcons((T6*)NULL), const T7& t7=internal::wrap_dcons((T7*)NULL), const T8& t8=internal::wrap_dcons((T8*)NULL), const T9& t9=internal::wrap_dcons((T9*)NULL) ) : internal::tuple_traits::U(t0,t1,t2,t3,t4,t5,t6,t7,t8,t9) { } template struct internal_tuple_element { typedef typename internal::component::type type; }; template typename internal_tuple_element::type& get() { return internal::get_helper::get(*this); } template tuple& operator=(const internal::cons& other) { my_cons::operator=(other); return *this; } template tuple& operator=(const std::pair& other) { // __TBB_ASSERT(tuple_size::value == 2, "Invalid size for pair to tuple assignment"); this->head = other.first; this->tail.head = other.second; return *this; } friend bool operator==(const tuple& me, const tuple& other) {return static_cast(me)==(other);} friend bool operator<(const tuple& me, const tuple& other) {return static_cast(me)<(other);} friend bool operator>(const tuple& me, const tuple& other) {return static_cast(me)>(other);} friend bool operator!=(const tuple& me, const tuple& other) {return static_cast(me)!=(other);} friend bool operator>=(const tuple& me, const tuple& other) {return static_cast(me)>=(other);} friend bool operator<=(const tuple& me, const tuple& other) {return static_cast(me)<=(other);} template friend bool operator==(const tuple& me, const tuple& other) { return static_cast(me)==(other); } template friend bool operator<(const tuple& me, const tuple& other) { return static_cast(me)<(other); } template friend bool operator>(const tuple& me, const tuple& other) { return static_cast(me)>(other); } template friend bool operator!=(const tuple& me, const tuple& other) { return static_cast(me)!=(other); } template friend bool operator>=(const tuple& me, const tuple& other) { return static_cast(me)>=(other); } template friend bool operator<=(const tuple& me, const tuple& other) { return static_cast(me)<=(other); } }; // tuple // empty tuple template<> class tuple : public null_type { typedef null_type inherited; }; // helper classes template < class T> class tuple_size { public: static const size_t value = 1 + tuple_size::value; }; template <> class tuple_size > { public: static const size_t value = 0; }; template <> class tuple_size { public: static const size_t value = 0; }; template struct tuple_element { typedef typename internal::component::type type; }; template inline static typename tuple_element::type& get(T &t) { return t.get(); } template inline static typename tuple_element::type const& get(T const &t) { return const_cast::type const &> (const_cast(t).get()); } } // interface5 } // tbb #if TBB_IMPLEMENT_CPP0X namespace std { using tbb::interface5::tuple; using tbb::interface5::tuple_size; using tbb::interface5::tuple_element; using tbb::interface5::get; } #endif #endif /* __TBB_tuple_H */