This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [c++0x] unique_ptr.hpp implementation


Attached is tuple with EBO (and tuple_cat) along iwth unique_ptr that
uses it. I modified tuple_size to inherit from
std::integral_constant<std::size_t, sizeof...(_Elements)> as per
20.3.1.3 in N2369.
make_tuple also takes rvalue ref type and fowards to tuple ctor.

First issue is that I'm not 100% certain when forward is required (vs
move). I wouldn't mind some clarification on the matter.

Second issue... array types.
tuple<char[3]> t; tuple<char[3]> u = t;

This fails to compile citing "error: array used as initializer". Is
the correct solution to somehow collapse the array type to its native
pointer type ala remove_all_extents<T>::type* ? Or specialize
everything for arrays?

Chris

On 8/15/07, Howard Hinnant <hhinnant@apple.com> wrote:
> On Aug 14, 2007, at 5:56 PM, Chris Fairles wrote:
>
> > However, EBO only kicks in when theres only one instance of each empty
> > type int the tuple (i.e. sizeof(tuple<deleter, deleter>) == 2 when I'd
> > like it to be 1).
>
> I believe this is as it has to be.  There's a note in section 10,
> paragraph 5:
>
> > however, two subobjects that have the same class type and that
> > belong to the same most derived object must not be allocated at the
> > same address
>
> In practice this hasn't been much of a problem.  I haven't run across
> any use cases where I wanted two potentially zero-sized elements of
> the same type.
>
> -Howard
>
>
#ifndef UNIQUE_PTR_HPP_
#define UNIQUE_PTR_HPP_

#include <type_traits>
#include <utility>
#include <cassert>
#include <tuple>

template <class T> struct default_delete;
template <class T> struct default_delete<T[]>;
template <class T, size_t N> struct default_delete<T[N]>;

template <class T, class D = default_delete<T>> class unique_ptr;
template <class T, class D> class unique_ptr<T[], D>;
template <class T, class D, size_t N> class unique_ptr<T[N], D>;

template <class T> struct default_delete {
    default_delete() {}
    template <class U> default_delete(const default_delete<U>&) {}
    void operator()(T * ptr) const {
        static_assert(sizeof(T) > 0, "can't delete pointer to incomplete type");
        delete ptr;
    }
};

template <class T> struct default_delete<T[]> {
    void operator()(T * ptr) const {
        static_assert(sizeof(T) > 0, "can't delete pointer to incomplete type");
        delete [] ptr;
    }
};

template <class T, size_t N> struct default_delete<T[N]> {
    void operator()(T * ptr, size_t) const {
        static_assert(sizeof(T) > 0, "can't delete pointer to incomplete type");
        delete [] ptr;
    }
};

template <class T, class D> class unique_ptr {
    typedef unique_ptr<T,D> this_type;
    typedef std::tuple<T*,D> tuple_type;
    typedef tuple_type * this_type::* unspecified_bool_type;
    typedef T * this_type::* unspecified_pointer_type;
public:
    typedef T element_type;
    typedef T* pointer;
    typedef D deleter_type;
    
    // constructors
    unique_ptr() : _M_t(0,D()) {static_assert(!std::is_pointer<D>::value, "constructed with null function pointer deleter");}
    explicit unique_ptr(T* __p) : _M_t(__p,D()) {static_assert(!std::is_pointer<D>::value, "constructed with null function pointer deleter");}
    
    unique_ptr(T* __p, typename std::conditional<std::is_reference<D>::value, D, const D&>::type __d) : _M_t(__p,__d) {}
    unique_ptr(T* __p, typename std::remove_reference<D>::type && __d) : _M_t(std::move(__p),std::move(__d)) {
        static_assert(!std::is_reference<D>::value, "rvalue deleter bound to reference");
    }
    
    // move constructors
    unique_ptr(unique_ptr && u) : _M_t(u.release(), std::forward<D>(u.get_deleter())) {}
    template <class U, class E> unique_ptr(unique_ptr<U, E> && u) 
    : _M_t(u.release(), std::forward<D>(u.get_deleter())) {}
        
    // destructor
    ~unique_ptr() { reset(); }
    
    // assignment
    unique_ptr& operator=(unique_ptr&& u) { reset(u.release()); get_deleter() = std::move(u.get_deleter()); return *this; }
    template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) 
    { reset(u.release()); std::get<1>(_M_t) = std::move(u.get_deleter()); return *this; }
    unique_ptr& operator=(unspecified_pointer_type) { reset(); return *this;}
    
    // observers
    typename std::add_lvalue_reference<T>::type operator*() const { return *get(); } 
    T* operator->() const { assert(std::get<0>(_M_t) != 0); return get(); }
    T* get() const { return std::get<0>(_M_t); }
    bool operator! () const { return get() == 0; }
    D& get_deleter() { return std::get<1>(_M_t); }
    const D& get_deleter() const { return std::get<1>(_M_t); }    
    operator unspecified_bool_type () const { return get() == 0 ? 0 : &this_type::_M_t; }
    
    // modifiers
    T* release() { T* __p = std::get<0>(_M_t); std::get<0>(_M_t) = 0; return __p; }
    void reset(T* p = 0) { assert(p == 0 || p != std::get<0>(_M_t)); std::get<1>(_M_t)(std::get<0>(_M_t)); std::get<0>(_M_t) = p; }
    
    void swap(unique_ptr && __u) {
        _M_t.swap(__u._M_t);
    }
private:
    tuple_type _M_t;
       
    // disable copy from lvalue
    unique_ptr(const unique_ptr&);
    template <class U, class E> unique_ptr(const unique_ptr<U, E>&);
    unique_ptr& operator=(const unique_ptr&);
    template <class U, class E> unique_ptr& operator=(const unique_ptr<U, E>&);
};

template <class T, class D> class unique_ptr<T[], D> {
    typedef unique_ptr<T,D> this_type;
    typedef std::tuple<T*,D> tuple_type;
    typedef tuple_type * this_type::* unspecified_bool_type;
    typedef T * this_type::* unspecified_pointer_type;
public:
    typedef T element_type;
    typedef T* pointer;
    typedef D deleter_type;
    
    // constructors
    unique_ptr() : _M_t(0,D()) {static_assert(!std::is_pointer<D>::value, "constructed with null function pointer deleter");}
    explicit unique_ptr(T* __p) : _M_t(__p,D()) {static_assert(!std::is_pointer<D>::value, "constructed with null function pointer deleter");}
    
    unique_ptr(T* __p, typename std::conditional<std::is_reference<D>::value, D, const D&>::type __d) : _M_t(__p,__d) {}
    unique_ptr(T* __p, typename std::remove_reference<D>::type && __d) : _M_t(std::move(__p),std::move(__d)) {
        static_assert(!std::is_reference<D>::value, "rvalue deleter bound to reference");
    }
    
    // move constructors
    unique_ptr(unique_ptr && u) : _M_t(u.release(), std::forward<D>(u.get_deleter())) {}
        
    // destructor
    ~unique_ptr() { reset(); }
    
    // assignment
    unique_ptr& operator=(unique_ptr&& u) { reset(u.release()); get_deleter() = std::move(u.get_deleter()); return *this; }
    unique_ptr& operator=(unspecified_pointer_type) { reset(); return *this;}
    
    // observers
    typename std::add_lvalue_reference<T>::type operator[](size_t i) const { return get()[i]; }   
    T* get() const { return std::get<0>(_M_t); }
    bool operator! () const { return get() == 0; }
    D& get_deleter() { return std::get<1>(_M_t); }
    const D& get_deleter() const { return std::get<1>(_M_t); }    
    operator unspecified_bool_type () const { return get() == 0 ? 0 : &this_type::_M_t; }
    
    // modifiers
    T* release() { T* __p = std::get<0>(_M_t); std::get<0>(_M_t) = 0; return __p; }
    void reset(T* p = 0) { assert(p == 0 || p != std::get<0>(_M_t)); std::get<1>(_M_t)(std::get<0>(_M_t)); std::get<0>(_M_t) = p; }
    
    void swap(unique_ptr && __u) {
        _M_t.swap(__u._M_t);
    }
private:
    tuple_type _M_t;
       
    // disable copy from lvalue
    unique_ptr(const unique_ptr&);
    unique_ptr& operator=(const unique_ptr&);
    
    //disable construction from convertible pointer types (N2315 - 20.6.5.3.1) 
    template <class U> unique_ptr(U p,
        typename std::conditional<std::is_reference<D>::value, D, const D&>::type d,
        typename std::enable_if<std::is_convertible<U, pointer>::value>::type* = 0);
    
    template <class U> unique_ptr(U p, typename std::remove_reference<D>::type&& d,
        typename std::enable_if<std::is_convertible<U, pointer>::value>::type* = 0);
    
    template <class U> explicit unique_ptr(U,
        typename std::enable_if<std::is_convertible<U, pointer>::value>::type* = 0);
    
    //disable reset with convertible pointer types (N2315 - 20.6.5.3.3) 
    template <class U>
    typename std::enable_if<std::is_convertible<U,pointer>::value>::type reset(U);
};

template <class T, class D, size_t N> class unique_ptr<T[N], D> {
    typedef unique_ptr<T,D> this_type;
    typedef std::tuple<T*,D> tuple_type;
    typedef tuple_type * this_type::* unspecified_bool_type;
    typedef T * this_type::* unspecified_pointer_type;
public:
    typedef T element_type;
    typedef T* pointer;
    typedef D deleter_type;
    static const size_t size = N;
    
    // constructors
    unique_ptr() : _M_t(0,D()) {static_assert(!std::is_pointer<D>::value, "constructed with null function pointer deleter");}
    explicit unique_ptr(T* __p) : _M_t(__p,D()) {static_assert(!std::is_pointer<D>::value, "constructed with null function pointer deleter");}
    
    unique_ptr(T* __p, typename std::conditional<std::is_reference<D>::value, D, const D&>::type __d) : _M_t(__p,__d) {}
    unique_ptr(T* __p, typename std::remove_reference<D>::type && __d) : _M_t(std::move(__p),std::move(__d)) {
        static_assert(!std::is_reference<D>::value, "rvalue deleter bound to reference");
    }
    
    // move constructors
    unique_ptr(unique_ptr && u) : _M_t(u.release(), std::forward<D>(u.get_deleter())) {}
        
    // destructor
    ~unique_ptr() { reset(); }
    
    // assignment
    unique_ptr& operator=(unique_ptr&& u) { reset(u.release()); std::get<1>(_M_t) = std::move(u.d_); return *this; }
    unique_ptr& operator=(unspecified_pointer_type) { reset(); return *this;}
    
    // observers
    typename std::add_lvalue_reference<T>::type operator[](size_t i) const { return std::get<0>(_M_t)[i]; }   
    T* get() const { return std::get<0>(_M_t); }
    bool operator! () const { return get() == 0; }
    D& get_deleter() { return std::get<1>(_M_t); }
    const D& get_deleter() const { return std::get<1>(_M_t); }    
    operator unspecified_bool_type () const { return std::get<0>(_M_t) == 0 ? 0 : &this_type::_M_t; }
    
    // modifiers
    T* release() { T* __p = std::get<0>(_M_t); std::get<0>(_M_t) = 0; return __p; }
    void reset(T* p = 0) { assert(p == 0 || p != std::get<0>(_M_t)); std::get<1>(_M_t)(get(), N); std::get<0>(_M_t) = p; }
    
    void swap(unique_ptr && __u) {
        _M_t.swap(__u._M_t);
    }
private:
    tuple_type _M_t;
       
    // disable copy from lvalue
    unique_ptr(const unique_ptr&);
    unique_ptr& operator=(const unique_ptr&);
    
    //disable construction from convertible pointer types (N2315 - 20.6.5.3.1) 
    template <class U> unique_ptr(U p,
        typename std::conditional<std::is_reference<D>::value, D, const D&>::type d,
        typename std::enable_if<std::is_convertible<U, pointer>::value>::type* = 0);
    
    template <class U> unique_ptr(U p, typename std::remove_reference<D>::type&& d,
        typename std::enable_if<std::is_convertible<U, pointer>::value>::type* = 0);
    
    template <class U> explicit unique_ptr(U,
        typename std::enable_if<std::is_convertible<U, pointer>::value>::type* = 0);
    
    //disable reset with convertible pointer types (N2315 - 20.6.5.3.3) 
    template <class U>
    typename std::enable_if<std::is_convertible<U,pointer>::value>::type reset(U);
};

template<class T, class D> 
inline void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) {x.swap(y);}

template<class T, class D> 
inline void swap(unique_ptr<T, D>&& x, unique_ptr<T, D>& y){x.swap(y);}

template<class T, class D> 
inline void swap(unique_ptr<T, D>& x, unique_ptr<T, D>&& y){x.swap(y);}

template<class T1, class D1, class T2, class D2>
inline bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y)
{ return x.get() == y.get(); }

template<class T1, class D1, class T2, class D2>
inline bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y)
{ return x.get() != y.get(); }

template<class T1, class D1, class T2, class D2>
inline bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y)
{ return x.get() < y.get(); }

template<class T1, class D1, class T2, class D2>
inline bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y)
{ return x.get() <= y.get(); }

template<class T1, class D1, class T2, class D2>
inline bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y)
{ return x.get() > y.get(); }

template<class T1, class D1, class T2, class D2>
inline bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y)
{ return x.get() >= y.get(); }

#endif

// class template tuple -*- C++ -*-

// Copyright (C) 2007 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library 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 this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 51 Franklin Street, 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.

/** @file tr1_impl/tuple
 *  This is an internal header file, included by other library headers.
 *  You should not attempt to use it directly.
*/

// Chris Jefferson <chris@bubblescope.net>
// Variadic Templates support by Douglas Gregor <doug.gregor@gmail.com>
// EBCO and tuple_cat added by Chris Fairles <chris.fairles@gmail.com>

namespace std
{
_GLIBCXX_BEGIN_NAMESPACE_TR1
    
    template<typename _Tp>
    class reference_wrapper;
    
    template<typename _Tp>
    class add_lvalue_reference;

    template<typename _Tp>
    class add_const;

namespace {
    template <typename _Tp>
    struct __add_const_lvalue_reference {
        typedef typename add_lvalue_reference<typename add_const<_Tp>::type>::type type;
    };
 
    template <typename _Tp>
    struct __add_const_lvalue_reference<_Tp[]> {
        typedef _Tp* type;
    };

    // Helper which adds a reference to a type when given a reference_wrapper
    template<typename _Tp>
    struct __strip_reference_wrapper
    { typedef _Tp __type; };

    template<typename _Tp>
    struct __strip_reference_wrapper<reference_wrapper<_Tp> >
    { typedef _Tp& __type; };

    template<typename _Tp>
    struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
    { typedef _Tp& __type; };
}

    /**
     * @if maint
     * Stores actual tuple element. If the type is empty, simply inherit it.
     * @endif
     */
    template<int _Idx, typename _Head, bool _IsEmpty>
    struct _Head_base;    
    
    template<int _Idx, typename _Head>
    struct _Head_base<_Idx, _Head, true> : public _Head {
        _Head_base() : _Head() {}
        _Head_base(_Head const& __h) : _Head(__h) {}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
        _Head_base(typename remove_reference<_Head>::type && __h) : _Head(forward<_Head>(__h)) {}
#endif
        _Head & _M_head() { return static_cast<_Head&>(*this); }
        _Head const& _M_head() const { return static_cast<const _Head&>(*this); }        
    };
    
    template<int _Idx, typename _Head>
    struct _Head_base<_Idx, _Head, false> {
    private:
        //typedef typename conditional<is_array<_HeadType>::value, typename remove_all_extents<_HeadType>::type*,_HeadType>::type _Head;
    public:
        _Head_base() : _M_head_impl() {}
        _Head_base(const _Head & __h) : _M_head_impl(__h) {}
        _Head_base(_Head_base const& __h) : _M_head_impl(__h._M_head_impl) {}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
        _Head_base(_Head && __h) : _M_head_impl(forward<_Head>(__h)) {  }
#endif
        _Head & _M_head() { return _M_head_impl; }
        _Head const& _M_head() const { return _M_head_impl; }        
        _Head _M_head_impl; 
    };
    
    
  /**
   * @if maint
   * Contains the actual implementation of the @c tuple template, stored
   * as a recursive inheritance hierarchy from the first element (most
   * derived class) to the last (least derived class). The @c Idx
   * parameter gives the 0-based index of the element stored at this
   * point in the hierarchy; we use it to implement a constant-time
   * get() operation.
   * @endif
   */
    template<int _Idx, typename... _Elements>
    struct _Tuple_impl; 

  /**
   * @if maint
   * Zero-element tuple implementation. This is the basis case for the 
   * inheritance recursion.
   * @endif maint
   */
    template<int _Idx>
    struct _Tuple_impl<_Idx>  { };
  /**
   * @if maint
   * Recursive tuple implementation. Here we store the @c Head element
   * and derive from a @c Tuple_impl containing the remaining elements
   * (which contains the @c Tail).
   * @endif
   */
    template<int _Idx, typename _Head, typename... _Tail>
    struct _Tuple_impl<_Idx, _Head, _Tail...>
    : public _Tuple_impl<_Idx + 1, _Tail...>, 
        private _Head_base<_Idx, _Head, is_empty<_Head>::value>
    {
    private:
        typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
        typedef _Head_base<_Idx, _Head, is_empty<_Head>::value> _Base;
    public:
        _Inherited&       _M_tail()       { return *this; }
        const _Inherited& _M_tail() const { return *this; }
      
        _Head & _M_head() { return _Base::_M_head(); }
        const _Head& _M_head() const { return _Base::_M_head(); }      
      
        _Tuple_impl() : _Inherited(), _Base() {}
       
        explicit 
        _Tuple_impl(typename __add_const_lvalue_reference<_Head>::type __head,
            typename __add_const_lvalue_reference<_Tail>::type... __tail)
        : _Inherited(__tail...), _Base(__head) { }

#ifdef __GXX_EXPERIMENTAL_CXX0X__		  
        template<typename _UHead, typename... _UTail>
        explicit _Tuple_impl(typename remove_reference<_UHead>::type && __head,
            typename remove_reference<_UTail>::type &&...  __tail)
        : _Inherited(move(__tail)...), _Base(forward<_Head>(__head)) { }
        
        template<typename... _UElements>
        _Tuple_impl( _Tuple_impl<_Idx, _UElements...>&& __in)
        : _Inherited(move(__in._M_tail())), _Base(forward<_Head>(__in._M_head())) { }
        
        _Tuple_impl(_Tuple_impl && __in)
        : _Inherited(move(__in._M_tail())), _Base(forward<_Head>(__in._M_head())) { }
        
        template<typename... _UElements>
        _Tuple_impl& operator=(_Tuple_impl<_Idx, _UElements...>&& __in) {
            _M_head() = move(__in._M_head());
            _M_tail() = move(__in._M_tail());
            return *this;
        }      
        
        _Tuple_impl& operator=(_Tuple_impl && __in) {
            _M_head() = move(__in._M_head());
            _M_tail() = move(__in._M_tail());
            return *this;
        }
#endif
          
        template<typename... _UElements>
        _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
        : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
      
        _Tuple_impl(const _Tuple_impl& __in)
        : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
            
        template<typename... _UElements>
        _Tuple_impl& operator=(const _Tuple_impl<_Idx, _UElements...>& __in) {
            _M_head() = __in._M_head();
            _M_tail() = __in._M_tail();
            return *this;
        }
        
        _Tuple_impl& operator=(const _Tuple_impl& __in) {
            _M_head() = __in._M_head();
            _M_tail() = __in._M_tail();
            return *this;
        }      
    };

    template<typename... _Elements> 
    class tuple : public _Tuple_impl<0, _Elements...>
    {
        typedef _Tuple_impl<0, _Elements...> _Inherited;

    public:
        tuple() : _Inherited() {}

        explicit
        tuple(_Elements const&... __elements)
        : _Inherited(__elements...) { }
      
        template<typename... _UElements>
        tuple(const tuple<_UElements...>& __in)
        : _Inherited(__in) { }
        
        
        tuple(const tuple& __in)
        : _Inherited(__in) { }
      
      
        template<typename... _UElements>
        tuple& operator=(const tuple<_UElements...>& __in) {
            static_cast<_Inherited&>(*this) = __in;
            return *this;
        }     
        
        
        tuple& operator=(const tuple& __in) {
            static_cast<_Inherited&>(*this) = __in;
            return *this;
        }
        
#ifdef __GXX_EXPERIMENTAL_CXX0X__
 
        //causes ICE
//        template<typename... _UElements>
//        explicit  tuple(_UElements &&... __in)
//        : _Inherited(forward<_UElements>(__in)...) {static_assert(sizeof...(_Elements)==sizeof...(_UElements),"");}

        tuple(tuple && __in)
        : _Inherited(move(__in)) { }

        template<typename... _UElements>
        tuple(tuple<_UElements...>&& __in)
        : _Inherited(move(__in)) { }

        tuple& operator=(tuple&& __in) {
            static_cast<_Inherited&>(*this) = move(__in);
            return *this;
        }

        template<typename... _UElements>
        tuple& operator=(tuple<_UElements...>&& __in) {
            static_cast<_Inherited&>(*this) = move(__in);
            return *this;
        }   
#endif
    };

    template<> class tuple<> { };

    // 2-element tuple, with construction and assignment from a pair.
    template<typename _T1, typename _T2>
    class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
    {
        typedef _Tuple_impl<0, _T1, _T2> _Inherited;

    public:
        tuple() : _Inherited() { }

        explicit
        tuple(typename __add_const_lvalue_reference<_T1>::type __a1,
              typename __add_const_lvalue_reference<_T2>::type __a2)
        : _Inherited(__a1, __a2) { }

        template<typename _U1, typename _U2>
        tuple(const tuple<_U1, _U2>& __in)
        : _Inherited(__in) { }
        
        tuple(const tuple& __in)
        : _Inherited(__in) { }
      
        template<typename _U1, typename _U2>
        tuple(const pair<_U1, _U2>& __in)
        : _Inherited(_Tuple_impl<0, 
		     typename __add_const_lvalue_reference<_U1>::type,
		     typename __add_const_lvalue_reference<_U2>::type>(__in.first, 
						      __in.second))
        { }
  
        template<typename _U1, typename _U2>
        tuple& operator=(const tuple<_U1, _U2>& __in) {
            static_cast<_Inherited&>(*this) = __in;
            return *this;
        }
       
        tuple& operator=(const tuple& __in) {
            static_cast<_Inherited&>(*this) = __in;
            return *this;
        }
      
        template<typename _U1, typename _U2>
        tuple& operator=(const pair<_U1, _U2>& __in) {
            this->_M_head() = __in.first;
            this->_M_tail()._M_head() = __in.second;
            return *this;
        }

#ifdef __GXX_EXPERIMENTAL_CXX0X__
        template<typename _U1, typename _U2>
        tuple(tuple<_U1, _U2>&& __in)
        : _Inherited(move(__in)) { }

        tuple(tuple && __in)
        : _Inherited(move(__in)) { }
        
        template<typename _U1, typename _U2>
        tuple& operator=(tuple<_U1, _U2>&& __in) {
            static_cast<_Inherited&>(*this) = move(__in);
            return *this;
        }
        tuple& operator=(tuple&& __in) {
            static_cast<_Inherited&>(*this) = move(__in);
            return *this;
        }
#endif
    };

  
    /// Gives the type of the ith element of a given tuple type.
    template<int __i, typename _Tp>
    struct tuple_element;

  /**
   * @if maint
   * Recursive case for tuple_element: strip off the first element in
   * the tuple and retrieve the (i-1)th element of the remaining tuple.
   * @endif
   */
    template<int __i, typename _Head, typename... _Tail>
    struct tuple_element<__i, tuple<_Head, _Tail...> >
    : tuple_element<__i - 1, tuple<_Tail...> > { };

  /**
   * @if maint
   * Basis case for tuple_element: The first element is the one we're seeking.
   * @endif
   */
    template<typename _Head, typename... _Tail>
    struct tuple_element<0, tuple<_Head, _Tail...> >
    { typedef _Head type; };

    /// Finds the size of a given tuple type.
    template<typename _Tp>
    struct tuple_size;

    /// @brief class tuple_size
    template <typename... _Elements>
    struct tuple_size<tuple<_Elements...> > 
    : public integral_constant<size_t, sizeof...(_Elements)>
    {};
    
    template<int __i, typename _Head, typename... _Tail>
    inline typename add_lvalue_reference<_Head>::type
    __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
    { return __t._M_head(); }

    template<int __i, typename _Head, typename... _Tail>
    inline typename __add_const_lvalue_reference<_Head>::type
    __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
    { return __t._M_head(); }

  // Return a reference (const reference) to the ith element of a tuple.
  // Any const or non-const ref elements are returned with their original type.
    template<int __i, typename... _Elements>
    inline typename add_lvalue_reference<
                      typename tuple_element<__i, tuple<_Elements...> >::type
                    >::type
    get(tuple<_Elements...>& __t)
    { return __get_helper<__i>(__t); }

    template<int __i, typename... _Elements>
    inline typename __add_const_lvalue_reference<
                      typename tuple_element<__i, tuple<_Elements...> >::type                      
                    >::type
    get(const tuple<_Elements...>& __t)
    { return __get_helper<__i>(__t); }

    // This class helps construct the various comparison operations on tuples
    template<int __check_equal_size, int __i, int __j, typename _Tp, typename _Up>
    struct __tuple_compare;

    template<int __i, int __j, typename _Tp, typename _Up>
    struct __tuple_compare<0, __i, __j, _Tp, _Up>
    {
      static bool __eq(const _Tp& __t, const _Up& __u)
      {
          return (get<__i>(__t) == get<__i>(__u) &&
              __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u));
      }
     
      static bool __less(const _Tp& __t, const _Up& __u)
      {
          return ((get<__i>(__t) < get<__i>(__u))
              || (!(get<__i>(__u) < get<__i>(__t)) &&
                  __tuple_compare<0, __i+1, __j, _Tp, _Up>::__less(__t, __u)));
      }
    };

  template<int __i, typename _Tp, typename _Up>
    struct __tuple_compare<0, __i, __i, _Tp, _Up>
    {
      static bool __eq(const _Tp&, const _Up&)
      { return true; }
     
      static bool __less(const _Tp&, const _Up&)
      { return false; }
    };

  template<typename... _TElements, typename... _UElements>
    bool
    operator==(const tuple<_TElements...>& __t,
	       const tuple<_UElements...>& __u)
    {
      typedef tuple<_TElements...> _Tp;
      typedef tuple<_UElements...> _Up;
      return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Tp>::value,
	      0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
    }

  template<typename... _TElements, typename... _UElements>
    bool
    operator<(const tuple<_TElements...>& __t,
	      const tuple<_UElements...>& __u)
    {
      typedef tuple<_TElements...> _Tp;
      typedef tuple<_UElements...> _Up;
      return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Tp>::value,
	      0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
    }

  template<typename... _TElements, typename... _UElements>
    bool
    operator!=(const tuple<_TElements...>& __t,
	       const tuple<_UElements...>& __u)
    { return !(__t == __u); }

  template<typename... _TElements, typename... _UElements>
    bool
    operator>(const tuple<_TElements...>& __t,
	      const tuple<_UElements...>& __u)
    { return __u < __t; }

  template<typename... _TElements, typename... _UElements>
    bool
    operator<=(const tuple<_TElements...>& __t,
	       const tuple<_UElements...>& __u)
    { return !(__u < __t); }

  template<typename... _TElements, typename... _UElements>
    bool
    operator>=(const tuple<_TElements...>& __t,
	       const tuple<_UElements...>& __u)
    { return !(__t < __u); } 

#ifdef __GXX_EXPERIMENTAL_CXX0X__
  template<typename... _Elements>
    inline tuple<typename __strip_reference_wrapper<_Elements>::__type...>
    make_tuple(_Elements &&... __args)
    {
      typedef tuple<typename __strip_reference_wrapper<_Elements>::__type...>
        __result_type;
      return __result_type(forward<_Elements>(__args)...);
    }
#else
  template<typename... _Elements>
    inline tuple<typename __strip_reference_wrapper<_Elements>::__type...>
    make_tuple(_Elements... __args)
    {
      typedef tuple<typename __strip_reference_wrapper<_Elements>::__type...>
        __result_type;
      return __result_type(__args...);
    }
#endif
  template<typename... _Elements>
    inline tuple<_Elements&...>
    tie(_Elements&... __args)
    {
      return tuple<_Elements&...>(__args...);
    }

  // A class (and instance) which can be used in 'tie' when an element
  // of a tuple is not required
  struct _Swallow_assign
  {
    template<class _Tp>
      _Swallow_assign&
      operator=(const _Tp&)
      { return *this; }
  };

  // TODO: Put this in some kind of shared file.
  namespace
  {
    _Swallow_assign ignore __attribute__((unused));
  } // anonymous namespace
  
    template<int...> struct __index_holder {};    

    template<int __i, typename _IdxHolder, typename... _Elements>
    struct __index_holder_impl;

    template<int __i, int... _Indexes, typename _IdxHolder, typename... _Elements>
    struct __index_holder_impl<__i, __index_holder<_Indexes...>, _IdxHolder, _Elements...> {
    typedef typename __index_holder_impl<__i + 1,
                                       __index_holder<_Indexes..., __i>,
                                       _Elements...>::type type;
    };
    
    template<int __i, int... _Indexes>
    struct __index_holder_impl<__i, __index_holder<_Indexes...> > {
       typedef __index_holder<_Indexes...> type;
    };

    template<typename... _Elements>
    struct __make_index_holder : __index_holder_impl<0, __index_holder<>, _Elements...> { };
    
    template <class... _TElements, int... _TIdx, class... _UElements, int... _UIdx> 
    tuple<_TElements..., _UElements...> 
    __tuple_cat_helper(tuple<_TElements...> const& __t, __index_holder<_TIdx...> const& i, 
                       tuple<_UElements...> const& __u, __index_holder<_UIdx...> const& j) {
        return tuple<_TElements..., _UElements...>(get<_TIdx>(__t)..., get<_UIdx>(__u)...);
    }

#ifdef __GXX_EXPERIMENTAL_CXX0X__
 
    template <class... _TElements, int... _TIdx, class... _UElements, int... _UIdx> 
    tuple<_TElements..., _UElements...> 
    __tuple_cat_helper(tuple<_TElements...>     && __t, __index_holder<_TIdx...> const& i, 
                       tuple<_UElements...> const& __u, __index_holder<_UIdx...> const& j) {
        return tuple<_TElements..., _UElements...>(move(get<_TIdx>(__t))..., get<_UIdx>(__u)...);
    }
    
    template <class... _TElements, int... _TIdx, class... _UElements, int... _UIdx> 
    tuple<_TElements..., _UElements...> 
    __tuple_cat_helper(tuple<_TElements...> const& __t, __index_holder<_TIdx...> const& i, 
                       tuple<_UElements...>     && __u, __index_holder<_UIdx...> const& j) {
        return tuple<_TElements..., _UElements...>(get<_TIdx>(__t)..., move(get<_UIdx>(__u))...);
    }
    
    template <class... _TElements, int... _TIdx, class... _UElements, int... _UIdx> 
    tuple<_TElements..., _UElements...> 
    __tuple_cat_helper(tuple<_TElements...> && __t, __index_holder<_TIdx...> const& i, 
                       tuple<_UElements...> && __u, __index_holder<_UIdx...> const& j) {
        return tuple<_TElements..., _UElements...>(move(get<_TIdx>(__t))..., move(get<_UIdx>(__u))...);
    }
#endif
    
    template <class... _TElements, class... _UElements>
    tuple<_TElements..., _UElements...> tuple_cat(const tuple<_TElements...>& __t, const tuple<_UElements...>& __u) {
      return __tuple_cat_helper( __t,typename __make_index_holder<_TElements...>::type(),
          __u, typename __make_index_holder<_UElements...>::type() );
    }

#ifdef __GXX_EXPERIMENTAL_CXX0X__
  
    template <class... _TElements, class... _UElements>
    tuple<_TElements..., _UElements...> tuple_cat(tuple<_TElements...>&& __t, const tuple<_UElements...>& __u) {
        return __tuple_cat_helper( move(__t), typename __make_index_holder<_TElements...>::type(),
            __u, typename __make_index_holder<_UElements...>::type() );
    }
    
    template <class... _TElements, class... _UElements>
    tuple<_TElements..., _UElements...> tuple_cat(const tuple<_TElements...>& __t, tuple<_UElements...>&& __u) {
        return __tuple_cat_helper(__t, typename __make_index_holder<_TElements...>::type(),
            move(__u), typename __make_index_holder<_UElements...>::type() );
  } 
    
    template <class... _TElements, class... _UElements>
    tuple<_TElements..., _UElements...> tuple_cat(tuple<_TElements...>&& __t, tuple<_UElements...>&& __u) {
        return __tuple_cat_helper( move(__t), typename __make_index_holder<_TElements...>::type(),
            move(__u), typename __make_index_holder<_UElements...>::type() );
    }
#endif

_GLIBCXX_END_NAMESPACE_TR1
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]