tuple.h.svn-base

来自「ffshow源码」· SVN-BASE 代码 · 共 604 行 · 第 1/2 页

SVN-BASE
604
字号
//  tuple.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://lambda.cs.utu.fi // ----------------------------------------------------------------- #ifndef TUPLE_HPP#define TUPLE_HPP// ---------------------------------------------------#define LL_TLAST T10#define LL_ULAST U10#define LL_REPEAT0(X) X, X, X, X, X, X, X, X#define LL_REPEAT1(B, A)\ B##2 A, B##3 A, B##4 A, B##5 A, B##6 A, B##7 A, B##8 A, B##9 A#define LL_REPEAT2(B, M, A) B##2 M##2 A, B##3 M##3 A, B##4 M##4 A, \B##5 M##5 A, B##6 M##6 A, B##7 M##7 A, B##8 M##8 A, B##9 M##9 A#define LL_REPEAT3(B, M1, M2, A) B##2 M1##2 M2##2 A, B##3 M1##3 M2##3 A,\B##4 M1##4 M2##4 A, B##5 M1##5 M2##5 A, B##6 M1##6 M2##6 A, \B##7 M1##7 M2##7 A,B##8 M1##8 M2##8 A, B##9 M1##9 M2##9 A// some compile time assertion templates ------------------------namespace std {namespace tuple_internal {// -- generate error template, referencing to non-existing members of this // template is used to produce compilation erros intentionallytemplate<class T>class generate_error;// -- ct_assert<cond>::check leads to a compile time error if cond = falsetemplate <bool Cond> struct ct_assert {  enum { n = Cond ? 1 : -1 };  static char check[n];};template <bool Cond>char ct_assert<Cond>::check[n];} //namespace tuple_internal// ----------------------------------------------------// type conversion classes -------------------------------//   plain_to_c_reference - converts only the plain type to reference to const//                          identity mapping with all other types//   plain_to_reference - see above but no consttemplate<class T>struct convert {  typedef const T& plain_to_c_reference;  typedef T& plain_to_reference;};template<class T>struct convert <const T> {  typedef const T& plain_to_c_reference;  typedef const T& plain_to_reference;};template<class T>struct convert <T&> {  typedef T& plain_to_c_reference;  typedef T& plain_to_reference;};template<class T>struct convert <const T&> {  typedef const T& plain_to_c_reference;  typedef const T& plain_to_reference;};template <class T> struct remove_const { typedef T type; };template <class T> struct remove_const<const T> { typedef T type; };// -- if construct ------------------------------------------------// Proposed by Krzysztof Czarnecki and Ulrich Eiseneckertemplate <bool If, class Then, class Else> struct IF { typedef Then RET; };template <class Then, class Else> struct IF<false, Then, Else> {  typedef Else RET;};// -----------------------------------------------------------// reference wrappers --------------------------------------------------// const_reference_wrapper is used in various functions to  specify // that a tuple element should be stored as const referencetemplate<class T> class  const_reference_wrapper;template <class T> inline const const_reference_wrapper<T> cref(const T& t);template<class T>class  const_reference_wrapper {   const T&  x;   explicit const_reference_wrapper(const T& t) : x(t) {} public:  operator const T&() const { return x; }  friend const const_reference_wrapper cref<>(const T& t); };template <class T>inline const const_reference_wrapper<T> cref(const T& t) {   return const_reference_wrapper<T>(t);}// reference_wrapper is used to specify that a tuple element should be // stored as non-const reference// A disguise for non-const references. // Can only be created via the ref function, which accepts only // non-const references. The conversion operator converts the constenss awaytemplate<class T> class reference_wrapper;template<class T> inline const reference_wrapper<T> ref(T& t);template<class T> inline const reference_wrapper<T> ref(const T& t);template<class T>class reference_wrapper {   T& x;   explicit reference_wrapper(T& t) : x(t) {} public:  operator T&() const { return x; }  friend const reference_wrapper ref<>(T& t);  friend const reference_wrapper ref<>(const T& t);};template<class T> inline const reference_wrapper<T> ref(T& t) { return reference_wrapper<T>(t); }template <class T>inline const reference_wrapper<T> ref(const T& t) {   return tuple_internal::generate_error<T>::const_reference_not_allowed;  }// -- type conversion templates -------------------------------------// T1 is the default type, specialisations for // reference_wrapper, const_reference_wrapper and  array types.// references to functions require special handling as well// do not use directly this template directly// ------------------------------------------------------------------------// first some helperstemplate<class T> struct is_function_reference {  static const bool test = false;};template<class Ret> struct is_function_reference<Ret (...)> {  static const bool test = true;};template<class T1, class T2 = T1> struct general_type_conversion {  typedef typename IF<    is_function_reference<typename remove_const<T1>::type >::test,                      T1&,                      T2>::RET type;};template<class T, int n, class Any> struct general_type_conversion<T[n], Any> {  typedef const T (&type)[n];};template<class T, int n, class Any> struct general_type_conversion<const T[n], Any> {  typedef const T (&type)[n];};template<class T, class Any> struct general_type_conversion<const_reference_wrapper<T>, Any> {  typedef const T& type;};template<class T, class Any> struct general_type_conversion<const const_reference_wrapper<T>, Any >{  typedef const T& type;};template<class T, class Any> struct general_type_conversion<reference_wrapper<T>, Any >{  typedef T& type;};template<class T, class Any> struct general_type_conversion<const reference_wrapper<T>, Any >{  typedef T& type;};// ---------------------------------------------------------------------------// Use these conversion templates instead of general_type_conversion // These must be instantiated with plain or const plain types// from template<class T> foo(const T& t) : convert_to_xxx<const T>::type// from template<class T> foo(T& t) : convert_to_xxx<T>::type// The default is plain type -------------------------// const T -> const T, // T -> T, // T[n] -> const T (&) [n]// references -> compile_time_error// reference_wrapper<T> -> T&// const reference_wrapper<T> -> const T&template<class T>struct convert_to_plain_by_default {  typedef typename general_type_conversion<                     T,                     T                   >::type type; };template<class T>struct convert_to_plain_by_default<T&>; // error// -- nil --------------------------------------------------------struct nil {};// a helper function to provide a const nil type temporaryinline const nil cnil() { return nil(); }// - cons forward declaration -----------------------------------------------template <class HT, class TT>struct cons;// - tuple forward declaration -----------------------------------------------template <class T1, LL_REPEAT1(class T, = nil), class LL_TLAST = nil> class tuple; // tuple_length forward declarationtemplate<class T> struct tuple_length;// tuple default argument wrappers ---------------------------------------// Works for const reference and plain types, intentionally not for references// tuple default argument wrapper ----------------------------------------template <class T>struct tuple_default_arg_wrap {  static T f() { return T(); }};template <class T>struct tuple_default_arg_wrap<T&> {  static T& f() {     return tuple_internal::generate_error<T>::no_default_values_for_reference_types;  }};// -----------------------------------------------------------------------// - cons getters --------------------------------------------------------// called: tuple_element<3>::get(aTuple)// -----------------------------------------------------------------------template< int N >struct tuple_element {  template<class RET, class HT, class TT >  inline static RET get(const cons<HT, TT>& t)  {    return tuple_element<N-1>::template get<RET>(t.tail);  }  template<class RET, class HT, class TT >  inline static RET get(cons<HT, TT>& t)  {    return tuple_element<N-1>::template get<RET>(t.tail);  }};template<>struct tuple_element<1> {  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;  }};// -cons type accessors ----------------------------------------

⌨️ 快捷键说明

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