OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [gcc-4.5.1/] [gcc-4.5.1-or32-1.0rc1/] [gcc/] [testsuite/] [g++.dg/] [cpp0x/] [variadic-bind.C] - Diff between revs 301 and 338

Only display areas with differences | Details | Blame | View Log

Rev 301 Rev 338
// { dg-options "-std=gnu++0x" }
// { dg-options "-std=gnu++0x" }
// { dg-do "run" }
// { dg-do "run" }
// A basic implementation of TR1's bind using variadic teplates
// A basic implementation of TR1's bind using variadic teplates
// Contributed by Douglas Gregor 
// Contributed by Douglas Gregor 
#include 
#include 
// Trivial reference_wrapper
// Trivial reference_wrapper
template
template
struct reference_wrapper
struct reference_wrapper
{
{
  reference_wrapper(T& x) : ptr(&x) { }
  reference_wrapper(T& x) : ptr(&x) { }
  operator T&() const { return *ptr; }
  operator T&() const { return *ptr; }
  T& get() const { return *ptr; }
  T& get() const { return *ptr; }
  T* ptr;
  T* ptr;
};
};
template reference_wrapper ref(T& x) { return x; }
template reference_wrapper ref(T& x) { return x; }
template reference_wrapper cref(const T& x) { return x; }
template reference_wrapper cref(const T& x) { return x; }
// Simple type-traits we'll need
// Simple type-traits we'll need
template
template
struct add_reference
struct add_reference
{
{
  typedef T& type;
  typedef T& type;
};
};
template
template
struct add_reference
struct add_reference
{
{
  typedef T& type;
  typedef T& type;
};
};
template
template
struct is_same
struct is_same
{
{
  static const bool value = false;
  static const bool value = false;
};
};
template
template
struct is_same
struct is_same
{
{
  static const bool value = true;
  static const bool value = true;
};
};
// For creating the constructor parameters of tuple<>
// For creating the constructor parameters of tuple<>
template
template
struct add_const_reference
struct add_const_reference
{
{
  typedef const T& type;
  typedef const T& type;
};
};
template
template
struct add_const_reference
struct add_const_reference
{
{
  typedef T& type;
  typedef T& type;
};
};
// 6.1.3 Class template tuple: Needed for bind() implementation
// 6.1.3 Class template tuple: Needed for bind() implementation
template
template
class tuple;
class tuple;
template<> class tuple<> { };
template<> class tuple<> { };
template
template
class tuple
class tuple
  : private tuple
  : private tuple
{
{
  typedef tuple inherited;
  typedef tuple inherited;
 public:
 public:
  tuple() { }
  tuple() { }
  // implicit copy-constructor is okay
  // implicit copy-constructor is okay
  tuple(typename add_const_reference::type v,
  tuple(typename add_const_reference::type v,
        typename add_const_reference::type... vtail)
        typename add_const_reference::type... vtail)
    : m_head(v), inherited(vtail...) { }
    : m_head(v), inherited(vtail...) { }
  template
  template
  tuple(const tuple& other)
  tuple(const tuple& other)
    : m_head(other.head()), inherited(other.tail()) { }
    : m_head(other.head()), inherited(other.tail()) { }
  template
  template
  tuple& operator=(const tuple& other)
  tuple& operator=(const tuple& other)
  {
  {
    m_head = other.head();
    m_head = other.head();
    tail() = other.tail();
    tail() = other.tail();
    return *this;
    return *this;
  }
  }
  typename add_reference::type       head()       { return m_head; }
  typename add_reference::type       head()       { return m_head; }
  typename add_reference::type head() const { return m_head; }
  typename add_reference::type head() const { return m_head; }
  inherited&                               tail()       { return *this; }
  inherited&                               tail()       { return *this; }
  const inherited&                         tail() const { return *this; }
  const inherited&                         tail() const { return *this; }
 protected:
 protected:
  Head m_head;
  Head m_head;
};
};
template
template
struct make_tuple_result
struct make_tuple_result
{
{
  typedef T type;
  typedef T type;
};
};
template
template
struct make_tuple_result >
struct make_tuple_result >
{
{
  typedef T& type;
  typedef T& type;
};
};
// 6.1.3.2 Tuple creation functions
// 6.1.3.2 Tuple creation functions
struct ignore_t {
struct ignore_t {
  template ignore_t& operator=(const T&) { return *this; }
  template ignore_t& operator=(const T&) { return *this; }
} ignore;
} ignore;
template
template
tuple::type...>
tuple::type...>
make_tuple(const Values&... values)
make_tuple(const Values&... values)
{
{
  return tuple::type...>(values...);
  return tuple::type...>(values...);
}
}
template
template
tuple tie(Values&... values)
tuple tie(Values&... values)
{
{
  return tuple(values...);
  return tuple(values...);
}
}
// 6.1.3.3 Tuple helper classes
// 6.1.3.3 Tuple helper classes
template
template
struct tuple_size;
struct tuple_size;
template<>
template<>
struct tuple_size >
struct tuple_size >
{
{
  static const __SIZE_TYPE__ value = 0;
  static const __SIZE_TYPE__ value = 0;
};
};
template
template
struct tuple_size >
struct tuple_size >
{
{
  static const __SIZE_TYPE__ value = 1 + tuple_size >::value;
  static const __SIZE_TYPE__ value = 1 + tuple_size >::value;
};
};
template
template
struct tuple_element;
struct tuple_element;
template
template
struct tuple_element >
struct tuple_element >
{
{
  typedef typename tuple_element >::type type;
  typedef typename tuple_element >::type type;
};
};
template
template
struct tuple_element<0, tuple >
struct tuple_element<0, tuple >
{
{
  typedef Head type;
  typedef Head type;
};
};
// 6.1.3.4 Element access
// 6.1.3.4 Element access
template
template
class get_impl;
class get_impl;
template
template
class get_impl >
class get_impl >
{
{
  typedef typename tuple_element >::type Element;
  typedef typename tuple_element >::type Element;
  typedef typename add_reference::type RJ;
  typedef typename add_reference::type RJ;
  typedef typename add_const_reference::type PJ;
  typedef typename add_const_reference::type PJ;
  typedef get_impl > Next;
  typedef get_impl > Next;
 public:
 public:
  static RJ get(tuple& t)
  static RJ get(tuple& t)
  { return Next::get(t.tail()); }
  { return Next::get(t.tail()); }
  static PJ get(const tuple& t)
  static PJ get(const tuple& t)
  { return Next::get(t.tail()); }
  { return Next::get(t.tail()); }
};
};
template
template
class get_impl<0, tuple >
class get_impl<0, tuple >
{
{
  typedef typename add_reference::type RJ;
  typedef typename add_reference::type RJ;
  typedef typename add_const_reference::type PJ;
  typedef typename add_const_reference::type PJ;
 public:
 public:
  static RJ get(tuple& t)       { return t.head(); }
  static RJ get(tuple& t)       { return t.head(); }
  static PJ get(const tuple& t) { return t.head(); }
  static PJ get(const tuple& t) { return t.head(); }
};
};
template
template
typename add_reference<
typename add_reference<
           typename tuple_element >::type
           typename tuple_element >::type
         >::type
         >::type
get(tuple& t)
get(tuple& t)
{
{
  return get_impl >::get(t);
  return get_impl >::get(t);
}
}
template
template
typename add_const_reference<
typename add_const_reference<
           typename tuple_element >::type
           typename tuple_element >::type
         >::type
         >::type
get(const tuple& t)
get(const tuple& t)
{
{
  return get_impl >::get(t);
  return get_impl >::get(t);
}
}
// 6.1.3.5 Relational operators
// 6.1.3.5 Relational operators
inline bool operator==(const tuple<>&, const tuple<>&) { return true; }
inline bool operator==(const tuple<>&, const tuple<>&) { return true; }
template
template
bool operator==(const tuple& t, const tuple& u)
bool operator==(const tuple& t, const tuple& u)
{
{
  return t.head() == u.head() && t.tail() == u.tail();
  return t.head() == u.head() && t.tail() == u.tail();
}
}
template
template
bool operator!=(const tuple& t, const tuple& u)
bool operator!=(const tuple& t, const tuple& u)
{
{
  return !(t == u);
  return !(t == u);
}
}
inline bool operator<(const tuple<>&, const tuple<>&) { return false; }
inline bool operator<(const tuple<>&, const tuple<>&) { return false; }
template
template
bool operator<(const tuple& t, const tuple& u)
bool operator<(const tuple& t, const tuple& u)
{
{
  return (t.head() < u.head() ||
  return (t.head() < u.head() ||
          (!(t.head() < u.head()) && t.tail() < u.tail()));
          (!(t.head() < u.head()) && t.tail() < u.tail()));
}
}
template
template
bool operator>(const tuple& t, const tuple& u)
bool operator>(const tuple& t, const tuple& u)
{
{
  return u < t;
  return u < t;
}
}
template
template
bool operator<=(const tuple& t, const tuple& u)
bool operator<=(const tuple& t, const tuple& u)
{
{
  return !(u < t);
  return !(u < t);
}
}
template
template
bool operator>=(const tuple& t, const tuple& u)
bool operator>=(const tuple& t, const tuple& u)
{
{
  return !(t < u);
  return !(t < u);
}
}
// enable_if, the breakfast of champions
// enable_if, the breakfast of champions
template
template
struct enable_if {
struct enable_if {
  typedef Type type;
  typedef Type type;
};
};
template
template
struct enable_if { };
struct enable_if { };
// 3.6 Function object binders
// 3.6 Function object binders
// 3.6.1 Class template is_bind_expression
// 3.6.1 Class template is_bind_expression
template
template
struct is_bind_expression {
struct is_bind_expression {
  static const bool value = false;
  static const bool value = false;
};
};
// 3.6.2 Class template is_placeholder
// 3.6.2 Class template is_placeholder
template
template
struct is_placeholder {
struct is_placeholder {
  static const int value = 0;
  static const int value = 0;
};
};
// 3.6.3 Function template bind
// 3.6.3 Function template bind
template struct placeholder {} ;
template struct placeholder {} ;
template struct int_c { };
template struct int_c { };
// A tuple of integer values
// A tuple of integer values
template struct int_tuple {};
template struct int_tuple {};
// make_indexes_impl is a helper for make_indexes
// make_indexes_impl is a helper for make_indexes
template
template
struct make_indexes_impl;
struct make_indexes_impl;
template
template
struct make_indexes_impl, T, Types...>
struct make_indexes_impl, T, Types...>
{
{
  typedef typename make_indexes_impl
  typedef typename make_indexes_impl
                                     int_tuple,
                                     int_tuple,
                                     Types...>::type type;
                                     Types...>::type type;
};
};
template
template
struct make_indexes_impl > {
struct make_indexes_impl > {
  typedef int_tuple type;
  typedef int_tuple type;
};
};
// make_indexes takes a variable-length number of N types and
// make_indexes takes a variable-length number of N types and
// generates an int_tuple that contains <0, 1, 2, ..., N-1>. These can
// generates an int_tuple that contains <0, 1, 2, ..., N-1>. These can
// be used as indexes for tuple's get or tuple_element operation.
// be used as indexes for tuple's get or tuple_element operation.
template
template
struct make_indexes : make_indexes_impl<0, int_tuple<>, Types...> { };
struct make_indexes : make_indexes_impl<0, int_tuple<>, Types...> { };
// Get the Ith tuple element, but only if I is in bounds.
// Get the Ith tuple element, but only if I is in bounds.
template
template
struct safe_tuple_element{ };
struct safe_tuple_element{ };
template
template
struct safe_tuple_element,
struct safe_tuple_element,
         typename enable_if<(I >= 0 &&
         typename enable_if<(I >= 0 &&
                             I < tuple_size >::value)
                             I < tuple_size >::value)
                            >::type>
                            >::type>
{
{
  typedef typename tuple_element >::type type;
  typedef typename tuple_element >::type type;
};
};
// mu maps a bound argument to an actual argument, given a tuple of
// mu maps a bound argument to an actual argument, given a tuple of
// the arguments passed to the function object returned by bind().
// the arguments passed to the function object returned by bind().
// Return the stored reference from reference_wrapper
// Return the stored reference from reference_wrapper
template
template
inline T& mu(reference_wrapper& bound_arg, const tuple&)
inline T& mu(reference_wrapper& bound_arg, const tuple&)
{
{
  return bound_arg.get();
  return bound_arg.get();
}
}
// Unwrap a tuple into separate arguments and forward to the function
// Unwrap a tuple into separate arguments and forward to the function
// object f.
// object f.
template
template
inline typename F::result_type
inline typename F::result_type
unwrap_and_forward(F& f, int_tuple, const tuple& args)
unwrap_and_forward(F& f, int_tuple, const tuple& args)
{
{
  return f(get(args)...);
  return f(get(args)...);
}
}
// Evaluate the inner bind expression
// Evaluate the inner bind expression
template
template
inline typename enable_if::value,
inline typename enable_if::value,
                          typename Bound::result_type>::type
                          typename Bound::result_type>::type
mu(Bound& bound_arg, const tuple& args)
mu(Bound& bound_arg, const tuple& args)
{
{
  typedef typename make_indexes::type Indexes;
  typedef typename make_indexes::type Indexes;
  return unwrap_and_forward(bound_arg, Indexes(), args);
  return unwrap_and_forward(bound_arg, Indexes(), args);
}
}
// Retrieve the Ith argument from args
// Retrieve the Ith argument from args
template
template
inline typename safe_tuple_element::value - 1,
inline typename safe_tuple_element::value - 1,
                                   tuple >::type
                                   tuple >::type
mu(Bound& bound_arg, const tuple& args)
mu(Bound& bound_arg, const tuple& args)
{
{
  return get::value-1>(args);
  return get::value-1>(args);
}
}
// Return the stored value.
// Return the stored value.
template
template
struct is_reference_wrapper {
struct is_reference_wrapper {
  static const bool value = false;
  static const bool value = false;
};
};
template
template
struct is_reference_wrapper > {
struct is_reference_wrapper > {
  static const bool value = true;
  static const bool value = true;
};
};
template
template
inline typename enable_if<(!is_bind_expression::value
inline typename enable_if<(!is_bind_expression::value
                           && !is_placeholder::value
                           && !is_placeholder::value
                           && !is_reference_wrapper::value),
                           && !is_reference_wrapper::value),
                          Bound&>::type
                          Bound&>::type
mu(Bound& bound_arg, const tuple&)
mu(Bound& bound_arg, const tuple&)
{
{
  return bound_arg;
  return bound_arg;
}
}
//
//
template
template
typename F::result_type
typename F::result_type
apply_functor(F& f, tuple& bound_args, int_tuple,
apply_functor(F& f, tuple& bound_args, int_tuple,
              const tuple& args)
              const tuple& args)
{
{
  return f(mu(get(bound_args), args)...);
  return f(mu(get(bound_args), args)...);
}
}
template
template
class bound_functor
class bound_functor
{
{
  typedef typename make_indexes::type indexes;
  typedef typename make_indexes::type indexes;
 public:
 public:
  typedef typename F::result_type result_type;
  typedef typename F::result_type result_type;
  explicit bound_functor(const F& f, const BoundArgs&... bound_args)
  explicit bound_functor(const F& f, const BoundArgs&... bound_args)
    : f(f), bound_args(bound_args...) { }
    : f(f), bound_args(bound_args...) { }
  template
  template
  typename F::result_type operator()(Args&... args) {
  typename F::result_type operator()(Args&... args) {
    return apply_functor(f, bound_args, indexes(), tie(args...));
    return apply_functor(f, bound_args, indexes(), tie(args...));
  }
  }
 private:
 private:
  F f;
  F f;
  tuple bound_args;
  tuple bound_args;
};
};
template
template
struct is_bind_expression > {
struct is_bind_expression > {
  static const bool value = true;
  static const bool value = true;
};
};
template
template
inline bound_functor
inline bound_functor
bind(const F& f, const BoundArgs&... bound_args)
bind(const F& f, const BoundArgs&... bound_args)
{
{
  return bound_functor(f, bound_args...);
  return bound_functor(f, bound_args...);
}
}
// 3.6.4 Placeholders
// 3.6.4 Placeholders
template
template
struct is_placeholder > {
struct is_placeholder > {
  static const int value = I;
  static const int value = I;
};
};
placeholder<1> _1;
placeholder<1> _1;
placeholder<2> _2;
placeholder<2> _2;
placeholder<3> _3;
placeholder<3> _3;
placeholder<4> _4;
placeholder<4> _4;
placeholder<5> _5;
placeholder<5> _5;
placeholder<6> _6;
placeholder<6> _6;
placeholder<7> _7;
placeholder<7> _7;
placeholder<8> _8;
placeholder<8> _8;
placeholder<9> _9;
placeholder<9> _9;
// Test code
// Test code
template
template
struct plus {
struct plus {
  typedef T result_type;
  typedef T result_type;
  T operator()(T x, T y) { return x + y; }
  T operator()(T x, T y) { return x + y; }
};
};
template
template
struct multiplies {
struct multiplies {
  typedef T result_type;
  typedef T result_type;
  T operator()(T x, T y) { return x * y; }
  T operator()(T x, T y) { return x * y; }
};
};
template
template
struct negate {
struct negate {
  typedef T result_type;
  typedef T result_type;
  T operator()(T x) { return -x; }
  T operator()(T x) { return -x; }
};
};
int main()
int main()
{
{
  int seventeen = 17;
  int seventeen = 17;
  int forty_two = 42;
  int forty_two = 42;
  assert(bind(plus(), _1, _2)(seventeen, forty_two) == 59);
  assert(bind(plus(), _1, _2)(seventeen, forty_two) == 59);
  assert(bind(plus(), _1, _1)(seventeen, forty_two) == 34);
  assert(bind(plus(), _1, _1)(seventeen, forty_two) == 34);
  assert(bind(plus(), _2, _1)(seventeen, forty_two) == 59);
  assert(bind(plus(), _2, _1)(seventeen, forty_two) == 59);
  assert(bind(plus(), 5, _1)(seventeen, forty_two) == 22);
  assert(bind(plus(), 5, _1)(seventeen, forty_two) == 22);
  assert(bind(plus(), ref(seventeen), _2)(seventeen, forty_two) == 59);
  assert(bind(plus(), ref(seventeen), _2)(seventeen, forty_two) == 59);
  assert(bind(plus(), bind(multiplies(), 3, _1), _2)(seventeen, forty_two)
  assert(bind(plus(), bind(multiplies(), 3, _1), _2)(seventeen, forty_two)
         == 93);
         == 93);
  return 0;
  return 0;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.