⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tuple_basic.hpp

📁 著名的Parser库Spirit在VC6上的Port
💻 HPP
📖 第 1 页 / 共 2 页
字号:
//  tuple_basic.hpp -----------------------------------------------------

// Copyright (C) 1999, 2000 Jaakko J鋜vi (jaakko.jarvi@cs.utu.fi)
//
// Permission to copy, use, sell and distribute this software is granted
// provided this copyright notice appears in all copies. 
// Permission to modify the code and to distribute modified code is granted
// provided this copyright notice appears in all copies, and a notice 
// that the code was modified is included with the copyright notice.
//
// This software is provided "as is" without express or implied warranty, 
// and with no claim as to its suitability for any purpose.

// For more information, see http://www.boost.org

// Outside help:
// This and that, Gary Powell.
// Fixed return types for get_head/get_tail 
// ( and other bugs ) per suggestion of Jens Maurer
// simplified element type accessors + bug fix  (Jeremy Siek)
// Several changes/additions according to suggestions by Doug Gregor, 
// William Kempf, Vesa Karvonen, John Max Skaller, Ed Brey, Beman Davis,
// David Abrahams.

// Revision history:
// 2001 10 22 John Maddock
//      Fixes for Borland C++
// 2001 08 30 David Abrahams
//      Added default constructor for cons<>.
// ----------------------------------------------------------------- 

#ifndef BOOST_TUPLE_BASIC_HPP
#define BOOST_TUPLE_BASIC_HPP


#include <utility> // needed for the assignment from pair to tuple

#include "boost/type_traits/cv_traits.hpp"
							    
namespace boost {
namespace tuples {

// -- null_type --------------------------------------------------------
struct null_type {};

// a helper function to provide a const null_type type temporary
namespace detail {
  inline const null_type cnull_type() { return null_type(); }
} // end detail

// - cons forward declaration -----------------------------------------------
template <class HT, class TT> struct cons; 


// - tuple forward declaration -----------------------------------------------
template <
  class T0 = null_type, class T1 = null_type, class T2 = null_type, 
  class T3 = null_type, class T4 = null_type, class T5 = null_type, 
  class T6 = null_type, class T7 = null_type, class T8 = null_type, 
  class T9 = null_type>
class tuple; 

// tuple_length forward declaration
template<class T> struct length;



namespace detail {

// -- generate error template, referencing to non-existing members of this 
// template is used to produce compilation errors intentionally
template<class T>
class generate_error;

// tuple default argument wrappers ---------------------------------------
// Work for non-reference types, intentionally not for references
template <class T>
struct default_arg {

// Non-class temporaries cannot have qualifiers.
// To prevent f to return for example const int, we remove cv-qualifiers
// from all temporaries. 
     static typename boost::remove_cv<T>::type f() { return T(); }
};

// This is just to produce a more informative error message
// The code would fail in any case
template<class T, int N>
struct default_arg<T[N]> {
  static T* f() { 
    return generate_error<T[N]>::arrays_are_not_valid_tuple_elements; }
};
   
template <class T>
struct default_arg<T&> {
  static T& f() { 
#ifndef __sgi
    return generate_error<T>::no_default_values_for_reference_types;
#else
    // MIPSpro instantiates functions even when it should not, so
    // this technique can not be used for error checking.
    // The simple workaround is to just not have this error checking
    // with MIPSpro.
    static T x;
    return x;
#endif
  }
};

// - cons getters --------------------------------------------------------
// called: get_class<N>::get<RETURN_TYPE>(aTuple)

template< int N >
struct get_class {
  template<class RET, class HT, class TT >
  inline static RET get(const cons<HT, TT>& t)
  {
    return get_class<N-1>::template get<RET>(t.tail);
  }
  template<class RET, class HT, class TT >
  inline static RET get(cons<HT, TT>& t)
  {
    return get_class<N-1>::template get<RET>(t.tail);
  }
};

template<>
struct get_class<0> {
  template<class RET, class HT, class TT> 
  inline static RET get(const cons<HT, TT>& t)
  {
    return t.head;
  }
  template<class RET, class HT, class TT> 
  inline static RET get(cons<HT, TT>& t)
  {
    return t.head;
  }
};

} // end of namespace detail


// -cons type accessors ----------------------------------------
// typename tuples::element<N,T>::type gets the type of the 
// Nth element ot T, first element is at index 0
// -------------------------------------------------------

template<int N, class T>
struct element
{
private:
  typedef typename T::tail_type Next;
public:
  typedef typename element<N-1, Next>::type type;
};
template<class T>
struct element<0,T>
{
  typedef typename T::head_type type;
};

// -get function templates -----------------------------------------------
// Usage: get<N>(aTuple)

// -- some traits classes for get functions

// access traits lifted from detail namespace to be part of the interface,
// (Joel de Guzman's suggestion). Rationale: get functions are part of the
// interface, so should the way to express their return types be.

template <class T> struct access_traits {
  typedef const T& const_type;
  typedef T& non_const_type;

  typedef const typename boost::remove_cv<T>::type& parameter_type;
// used as the tuple constructors parameter types
// Rationale: non-reference tuple element types can be cv-qualified.
// It should be possible to initialize such types with temporaries,
// and when binding temporaries to references, the reference must
// be non-volatile and const. 8.5.3. (5)
};

template <class T> struct access_traits<T&> {

  typedef T& const_type;
  typedef T& non_const_type;
   
  typedef T& parameter_type;   
};


// get function for non-const cons-lists, returns a reference to the element

template<int N, class HT, class TT>
inline typename access_traits<
                  typename element<N, cons<HT, TT> >::type
                >::non_const_type
get(cons<HT, TT>& c) { 
  return detail::get_class<N>::template 
         get<
           typename access_traits<
             typename element<N, cons<HT, TT> >::type
           >::non_const_type>(c); 
} 

// get function for const cons-lists, returns a const reference to
// the element. If the element is a reference, returns the reference
// as such (that is, can return a non-const reference)
template<int N, class HT, class TT>
inline typename access_traits<
                  typename element<N, cons<HT, TT> >::type
                >::const_type
get(const cons<HT, TT>& c) { 
  return detail::get_class<N>::template 
         get<
           typename access_traits<
             typename element<N, cons<HT, TT> >::type
         >::const_type>(c);
} 




// -- the cons template  --------------------------------------------------

template <class HT, class TT>
struct cons {

  typedef HT head_type;
  typedef TT tail_type;

  head_type head;
  tail_type tail;

  typename access_traits<head_type>::non_const_type 
  get_head() { return head; }

  typename access_traits<tail_type>::non_const_type 
  get_tail() { return tail; }  

  typename access_traits<head_type>::const_type 
  get_head() const { return head; }
  
  typename access_traits<tail_type>::const_type 
  get_tail() const { return tail; }  

  cons() : head(detail::default_arg<HT>::f()), tail() {}
  // the argument for head is not strictly needed, but it prevents 
  // array type elements. This is good, since array type elements 
  // cannot be supported properly in any case (no assignment, 
  // copy works only if the tails are exactly the same type, ...)
  
  cons(typename access_traits<head_type>::parameter_type h,
       const tail_type& t)
    : head (h), tail(t) {}  

  template <class T1, class T2, class T3, class T4, class T5, 
            class T6, class T7, class T8, class T9, class T10>
  cons( T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, 
        T6& t6, T7& t7, T8& t8, T9& t9, T10& t10 ) 
    : head (t1), 
      tail (t2, t3, t4, t5, t6, t7, t8, t9, t10, detail::cnull_type())
      {}

  template <class HT2, class TT2>
  cons( const cons<HT2, TT2>& u ) : head(u.head), tail(u.tail) {}

  template <class HT2, class TT2>
  cons& operator=( const cons<HT2, TT2>& u ) { 
    head=u.head; tail=u.tail; return *this; 
  }

  // must define assignment operator explicitly, implicit version is 
  // illformed if HT is a reference (12.8. (12))
  cons& operator=(const cons& u) { 
    head = u.head; tail = u.tail;  return *this; 
  }

  template <class T1, class T2>
  cons& operator=( const std::pair<T1, T2>& u ) { 
    BOOST_STATIC_ASSERT(length<cons>::value == 2); // check length = 2
    head = u.first; tail.head = u.second; return *this;
  }

  // get member functions (non-const and const)
  template <int N>
  typename access_traits<
             typename element<N, cons<HT, TT> >::type
           >::non_const_type
  get() {
    return boost::tuples::get<N>(*this); // delegate to non-member get
  }

  template <int N>
  typename access_traits<
             typename element<N, cons<HT, TT> >::type
           >::const_type
  get() const {
    return boost::tuples::get<N>(*this); // delegate to non-member get
  }
};

template <class HT>
struct cons<HT, null_type> {

  typedef HT head_type;
  typedef null_type tail_type;

  head_type head;
 
  typename access_traits<head_type>::non_const_type 
  get_head() { return head; }
  
  null_type get_tail() { return null_type(); }  

  typename access_traits<head_type>::const_type 
  get_head() const { return head; }
  
  const null_type get_tail() const { return null_type(); }  

  cons() : head(detail::default_arg<HT>::f()) {}

  cons(typename access_traits<head_type>::parameter_type h,
       const null_type& = null_type())
    : head (h) {}  

  template<class T1>
  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) {}

  template <class HT2>
  cons( const cons<HT2, null_type>& u ) : head(u.head) {}
  
  template <class HT2>
  cons& operator=(const cons<HT2, null_type>& u ) 
  { head = u.head; return *this; }

  // must define assignment operator explicitely, implicit version 
  // is illformed if HT is a reference
  cons& operator=(const cons& u) { head = u.head; return *this; }

  template <int N>
  typename access_traits<
             typename element<N, cons>::type
            >::non_const_type
  get() {
    return boost::tuples::get<N>(*this);
  }

  template <int N>
  typename access_traits<
             typename element<N, cons>::type
           >::const_type
  get() const {
    return boost::tuples::get<N>(*this);
  }

};

// templates for finding out the length of the tuple -------------------

template<class T>
struct length  {
  BOOST_STATIC_CONSTANT(int, value = 1 + length<typename T::tail_type>::value);
};

template<>
struct length<null_type> {
  BOOST_STATIC_CONSTANT(int, value = 0);
};


namespace detail {

// Tuple to cons mapper --------------------------------------------------
template <class T0, class T1, class T2, class T3, class T4, 
          class T5, class T6, class T7, class T8, class T9>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -