TR1 mem_fn and function support
From-SVN: r95486
This commit is contained in:
parent
8005da9977
commit
0179f2c632
@ -1,3 +1,31 @@
|
||||
2005-02-23 Douglas Gregor <doug.gregor@gmail.com>
|
||||
|
||||
* include/tr1/functional (function): New class template.
|
||||
(mem_fn): New function template.
|
||||
Implementations of TR1 function and mem_fn facilities.
|
||||
* include/tr1/functional_iterate.h: Implementations of TR1
|
||||
function and mem_fn facilities.
|
||||
* testsuite/tr1/3_function_objects/function/1.cc: New
|
||||
test of std::tr1::function.
|
||||
* testsuite/tr1/3_function_objects/function/2.cc: New
|
||||
test of std::tr1::function.
|
||||
* testsuite/tr1/3_function_objects/function/3.cc: New
|
||||
test of std::tr1::function.
|
||||
* testsuite/tr1/3_function_objects/function/4.cc: New
|
||||
test of std::tr1::function.
|
||||
* testsuite/tr1/3_function_objects/function/5.cc: New
|
||||
test of std::tr1::function.
|
||||
* testsuite/tr1/3_function_objects/function/6.cc: New
|
||||
test of std::tr1::function.
|
||||
* testsuite/tr1/3_function_objects/function/7.cc: New
|
||||
test of std::tr1::function.
|
||||
* testsuite/tr1/3_function_objects/function/8.cc: New
|
||||
test of std::tr1::function.
|
||||
* testsuite/tr1/3_function_objects/function/9.cc: New
|
||||
test of std::tr1::function.
|
||||
* testsuite/tr1/3_function_objects/mem_fn.cc: New test of
|
||||
std::tr1::mem_fn.
|
||||
|
||||
2005-02-23 Paolo Carlini <pcarlini@suse.de>
|
||||
|
||||
* include/tr1/type_traits: Implement is_convertible.
|
||||
|
@ -229,6 +229,7 @@ tr1_builddir = ./tr1
|
||||
tr1_headers = \
|
||||
${tr1_srcdir}/array \
|
||||
${tr1_srcdir}/functional \
|
||||
${tr1_srcdir}/functional_iterate.h \
|
||||
${tr1_srcdir}/memory \
|
||||
${tr1_srcdir}/tuple \
|
||||
${tr1_srcdir}/utility \
|
||||
|
@ -445,6 +445,7 @@ tr1_builddir = ./tr1
|
||||
tr1_headers = \
|
||||
${tr1_srcdir}/array \
|
||||
${tr1_srcdir}/functional \
|
||||
${tr1_srcdir}/functional_iterate.h \
|
||||
${tr1_srcdir}/memory \
|
||||
${tr1_srcdir}/tuple \
|
||||
${tr1_srcdir}/utility \
|
||||
|
File diff suppressed because it is too large
Load Diff
700
libstdc++-v3/include/tr1/functional_iterate.h
Normal file
700
libstdc++-v3/include/tr1/functional_iterate.h
Normal file
@ -0,0 +1,700 @@
|
||||
// TR1 functional -*- C++ -*-
|
||||
|
||||
// Copyright (C) 2005 Free Software Foundation, Inc.
|
||||
// Writtten by Douglas Gregor <dgregor@cs.indiana.edu>
|
||||
//
|
||||
// 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
// 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 function_iterate.h
|
||||
* This is an internal header file, included by other library headers.
|
||||
* You should not attempt to use it directly.
|
||||
*/
|
||||
|
||||
#if _GLIBCXX_NUM_ARGS > 0
|
||||
template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_TEMPLATE_PARAMS_SHIFTED>
|
||||
class _Mem_fn<_Res (_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED)>
|
||||
#if _GLIBCXX_NUM_ARGS == 1
|
||||
: public unary_function<_Class*, _Res>
|
||||
#elif _GLIBCXX_NUM_ARGS == 2
|
||||
: public binary_function<_Class*, _T1, _Res>
|
||||
#endif
|
||||
{
|
||||
typedef _Res (_Class::*_Functor)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED);
|
||||
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
_M_call(_Tp& __object, const volatile _Class * _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
_M_call(_Tp& __ptr, const volatile void * _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return ((*__ptr).*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
public:
|
||||
typedef _Res result_type;
|
||||
|
||||
explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
|
||||
|
||||
// Handle objects
|
||||
_Res
|
||||
operator()(_Class& __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
// Handle pointers
|
||||
_Res
|
||||
operator()(_Class* __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object->*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
// Handle smart pointers, references and pointers to derived
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
operator()(_Tp& __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{
|
||||
return _M_call(__object, &__object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_ARGS_SHIFTED);
|
||||
}
|
||||
|
||||
private:
|
||||
_Functor __pmf;
|
||||
};
|
||||
|
||||
template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_TEMPLATE_PARAMS_SHIFTED>
|
||||
class _Mem_fn<_Res (_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) const>
|
||||
#if _GLIBCXX_NUM_ARGS == 1
|
||||
: public unary_function<const _Class*, _Res>
|
||||
#elif _GLIBCXX_NUM_ARGS == 2
|
||||
: public binary_function<const _Class*, _T1, _Res>
|
||||
#endif
|
||||
{
|
||||
typedef _Res (_Class::*_Functor)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) const;
|
||||
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
_M_call(_Tp& __object, const volatile _Class * _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
_M_call(_Tp& __ptr, const volatile void * _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return ((*__ptr).*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
public:
|
||||
typedef _Res result_type;
|
||||
|
||||
explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
|
||||
|
||||
// Handle objects
|
||||
_Res
|
||||
operator()(const _Class& __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
// Handle pointers
|
||||
_Res
|
||||
operator()(const _Class* __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object->*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
// Handle smart pointers, references and pointers to derived
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
operator()(_Tp& __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{
|
||||
return _M_call(__object, &__object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_ARGS_SHIFTED);
|
||||
}
|
||||
|
||||
private:
|
||||
_Functor __pmf;
|
||||
};
|
||||
|
||||
template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_TEMPLATE_PARAMS_SHIFTED>
|
||||
class _Mem_fn<_Res (_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) volatile>
|
||||
#if _GLIBCXX_NUM_ARGS == 1
|
||||
: public unary_function<volatile _Class*, _Res>
|
||||
#elif _GLIBCXX_NUM_ARGS == 2
|
||||
: public binary_function<volatile _Class*, _T1, _Res>
|
||||
#endif
|
||||
{
|
||||
typedef _Res (_Class::*_Functor)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) volatile;
|
||||
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
_M_call(_Tp& __object, const volatile _Class * _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
_M_call(_Tp& __ptr, const volatile void * _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return ((*__ptr).*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
public:
|
||||
typedef _Res result_type;
|
||||
|
||||
explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
|
||||
|
||||
// Handle objects
|
||||
_Res
|
||||
operator()(volatile _Class& __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
// Handle pointers
|
||||
_Res
|
||||
operator()(volatile _Class* __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object->*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
// Handle smart pointers, references and pointers to derived
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
operator()(_Tp& __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{
|
||||
return _M_call(__object, &__object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_ARGS_SHIFTED);
|
||||
}
|
||||
private:
|
||||
_Functor __pmf;
|
||||
};
|
||||
|
||||
template<typename _Res, typename _Class _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_TEMPLATE_PARAMS_SHIFTED>
|
||||
class _Mem_fn<_Res (_Class::*)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED) const volatile>
|
||||
#if _GLIBCXX_NUM_ARGS == 1
|
||||
: public unary_function<const volatile _Class*, _Res>
|
||||
#elif _GLIBCXX_NUM_ARGS == 2
|
||||
: public binary_function<const volatile _Class*, _T1, _Res>
|
||||
#endif
|
||||
{
|
||||
typedef _Res (_Class::*_Functor)(_GLIBCXX_TEMPLATE_ARGS_SHIFTED)
|
||||
const volatile;
|
||||
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
_M_call(_Tp& __object, const volatile _Class * _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
_M_call(_Tp& __ptr, const volatile void * _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return ((*__ptr).*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
public:
|
||||
typedef _Res result_type;
|
||||
|
||||
explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
|
||||
|
||||
// Handle objects
|
||||
_Res
|
||||
operator()(const volatile _Class& __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object.*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
// Handle pointers
|
||||
_Res
|
||||
operator()(const volatile _Class* __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{ return (__object->*__pmf)(_GLIBCXX_ARGS_SHIFTED); }
|
||||
|
||||
// Handle smart pointers, references and pointers to derived
|
||||
template<typename _Tp>
|
||||
_Res
|
||||
operator()(_Tp& __object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_PARAMS_SHIFTED) const
|
||||
{
|
||||
return _M_call(__object, &__object _GLIBCXX_COMMA_SHIFTED
|
||||
_GLIBCXX_ARGS_SHIFTED);
|
||||
}
|
||||
|
||||
private:
|
||||
_Functor __pmf;
|
||||
};
|
||||
#endif
|
||||
|
||||
template<typename _Signature, typename _Functor> class _Function_handler;
|
||||
|
||||
template<typename _Res, typename _Functor _GLIBCXX_COMMA
|
||||
_GLIBCXX_TEMPLATE_PARAMS>
|
||||
class _Function_handler<_Res(_GLIBCXX_TEMPLATE_ARGS), _Functor>
|
||||
: public _Function_base::_Base_manager<_Functor>
|
||||
{
|
||||
typedef _Function_base::_Base_manager<_Functor> _Base;
|
||||
|
||||
public:
|
||||
static _Res
|
||||
_M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS)
|
||||
{
|
||||
return (*_Base::_M_get_pointer(__functor))(_GLIBCXX_ARGS);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS>
|
||||
class _Function_handler<void(_GLIBCXX_TEMPLATE_ARGS), _Functor>
|
||||
: public _Function_base::_Base_manager<_Functor>
|
||||
{
|
||||
typedef _Function_base::_Base_manager<_Functor> _Base;
|
||||
|
||||
public:
|
||||
static void
|
||||
_M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS)
|
||||
{
|
||||
(*_Base::_M_get_pointer(__functor))(_GLIBCXX_ARGS);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename _Res, typename _Functor _GLIBCXX_COMMA
|
||||
_GLIBCXX_TEMPLATE_PARAMS>
|
||||
class _Function_handler<_Res(_GLIBCXX_TEMPLATE_ARGS),
|
||||
reference_wrapper<_Functor> >
|
||||
: public _Function_base::_Ref_manager<_Functor>
|
||||
{
|
||||
typedef _Function_base::_Ref_manager<_Functor> _Base;
|
||||
|
||||
public:
|
||||
static _Res
|
||||
_M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS)
|
||||
{
|
||||
return __callable_functor(**_Base::_M_get_pointer(__functor))
|
||||
(_GLIBCXX_ARGS);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename _Functor _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS>
|
||||
class _Function_handler<void(_GLIBCXX_TEMPLATE_ARGS),
|
||||
reference_wrapper<_Functor> >
|
||||
: public _Function_base::_Ref_manager<_Functor>
|
||||
{
|
||||
typedef _Function_base::_Ref_manager<_Functor> _Base;
|
||||
|
||||
public:
|
||||
static void
|
||||
_M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS)
|
||||
{
|
||||
__callable_functor(**_Base::_M_get_pointer(__functor))(_GLIBCXX_ARGS);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename _Class, typename _Member, typename _Res
|
||||
_GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS>
|
||||
class _Function_handler<_Res(_GLIBCXX_TEMPLATE_ARGS), _Member _Class::*>
|
||||
: public _Function_handler<void(_GLIBCXX_TEMPLATE_ARGS), _Member _Class::*>
|
||||
{
|
||||
typedef _Function_handler<void(_GLIBCXX_TEMPLATE_ARGS), _Member _Class::*>
|
||||
_Base;
|
||||
|
||||
public:
|
||||
static _Res
|
||||
_M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS)
|
||||
{
|
||||
return std::tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)
|
||||
(_GLIBCXX_ARGS);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename _Class, typename _Member
|
||||
_GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS>
|
||||
class _Function_handler<void(_GLIBCXX_TEMPLATE_ARGS), _Member _Class::*>
|
||||
: public _Function_base::_Base_manager<
|
||||
_Simple_type_wrapper< _Member _Class::* > >
|
||||
{
|
||||
typedef _Member _Class::* _Functor;
|
||||
typedef _Simple_type_wrapper< _Functor > _Wrapper;
|
||||
typedef _Function_base::_Base_manager<_Wrapper> _Base;
|
||||
|
||||
public:
|
||||
static bool
|
||||
_M_manager(_Any_data& __dest, const _Any_data& __source,
|
||||
_Manager_operation __op)
|
||||
{
|
||||
switch (__op) {
|
||||
case __get_type_info:
|
||||
__dest._M_access<const type_info*>() = &typeid(_Functor);
|
||||
break;
|
||||
|
||||
case __get_functor_ptr:
|
||||
__dest._M_access<_Functor*>() =
|
||||
&_Base::_M_get_pointer(__source)->__value;
|
||||
break;
|
||||
|
||||
default:
|
||||
_Base::_M_manager(__dest, __source, __op);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void
|
||||
_M_invoke(const _Any_data& __functor _GLIBCXX_COMMA _GLIBCXX_PARAMS)
|
||||
{
|
||||
std::tr1::mem_fn(_Base::_M_get_pointer(__functor)->__value)
|
||||
(_GLIBCXX_ARGS);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS>
|
||||
class function<_Res(_GLIBCXX_TEMPLATE_ARGS)>
|
||||
#if _GLIBCXX_NUM_ARGS == 1
|
||||
: public unary_function<_T1, _Res>, private _Function_base
|
||||
#elif _GLIBCXX_NUM_ARGS == 2
|
||||
: public binary_function<_T1, _T2, _Res>, private _Function_base
|
||||
#else
|
||||
: private _Function_base
|
||||
#endif
|
||||
{
|
||||
/**
|
||||
* @if maint
|
||||
* This class is used to implement the safe_bool idiom.
|
||||
* @endif
|
||||
*/
|
||||
struct _Hidden_type
|
||||
{
|
||||
_Hidden_type* _M_bool;
|
||||
};
|
||||
|
||||
/**
|
||||
* @if maint
|
||||
* This typedef is used to implement the safe_bool idiom.
|
||||
* @endif
|
||||
*/
|
||||
typedef _Hidden_type* _Hidden_type::* _Safe_bool;
|
||||
|
||||
typedef _Res _Signature_type(_GLIBCXX_TEMPLATE_ARGS);
|
||||
|
||||
struct _Useless {};
|
||||
|
||||
public:
|
||||
typedef _Res result_type;
|
||||
|
||||
// [3.7.2.1] construct/copy/destroy
|
||||
|
||||
/**
|
||||
* @brief Default construct creates an empty function call wrapper.
|
||||
* @post @c !(bool)*this
|
||||
*/
|
||||
function() : _Function_base() { }
|
||||
|
||||
/**
|
||||
* @brief Default construct creates an empty function call wrapper.
|
||||
* @post @c !(bool)*this
|
||||
*/
|
||||
function(_M_clear_type*) : _Function_base() { }
|
||||
|
||||
/**
|
||||
* @brief %Function copy constructor.
|
||||
* @param x A %function object with identical call signature.
|
||||
* @pre @c (bool)*this == (bool)x
|
||||
*
|
||||
* The newly-created %function contains a copy of the target of @a
|
||||
* x (if it has one).
|
||||
*/
|
||||
function(const function& __x);
|
||||
|
||||
/**
|
||||
* @brief Builds a %function that targets a copy of the incoming
|
||||
* function object.
|
||||
* @param f A %function object that is callable with parameters of
|
||||
* type @c T1, @c T2, ..., @c TN and returns a value convertible
|
||||
* to @c Res.
|
||||
*
|
||||
* The newly-created %function object will target a copy of @a
|
||||
* f. If @a f is @c reference_wrapper<F>, then this function
|
||||
* object will contain a reference to the function object @c
|
||||
* f.get(). If @a f is a NULL function pointer or NULL
|
||||
* pointer-to-member, the newly-created object will be empty.
|
||||
*
|
||||
* If @a f is a non-NULL function pointer or an object of type @c
|
||||
* reference_wrapper<F>, this function will not throw.
|
||||
*/
|
||||
template<typename _Functor>
|
||||
function(_Functor __f,
|
||||
typename __enable_if<_Useless,
|
||||
!is_integral<_Functor>::value>::__type
|
||||
= _Useless());
|
||||
|
||||
/**
|
||||
* @brief %Function assignment operator.
|
||||
* @param x A %function with identical call signature.
|
||||
* @post @c (bool)*this == (bool)x
|
||||
* @returns @c *this
|
||||
*
|
||||
* The target of @a x is copied to @c *this. If @a x has no
|
||||
* target, then @c *this will be empty.
|
||||
*
|
||||
* If @a x targets a function pointer or a reference to a function
|
||||
* object, then this operation will not throw an exception.
|
||||
*/
|
||||
function& operator=(const function& __x)
|
||||
{
|
||||
function(__x).swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief %Function assignment to zero.
|
||||
* @post @c !(bool)*this
|
||||
* @returns @c *this
|
||||
*
|
||||
* The target of @a *this is deallocated, leaving it empty.
|
||||
*/
|
||||
function& operator=(_M_clear_type*)
|
||||
{
|
||||
if (_M_manager) {
|
||||
_M_manager(_M_functor, _M_functor, __destroy_functor);
|
||||
_M_manager = 0;
|
||||
_M_invoker = 0;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief %Function assignment to a new target.
|
||||
* @param f A %function object that is callable with parameters of
|
||||
* type @c T1, @c T2, ..., @c TN and returns a value convertible
|
||||
* to @c Res.
|
||||
* @return @c *this
|
||||
*
|
||||
* This %function object wrapper will target a copy of @a
|
||||
* f. If @a f is @c reference_wrapper<F>, then this function
|
||||
* object will contain a reference to the function object @c
|
||||
* f.get(). If @a f is a NULL function pointer or NULL
|
||||
* pointer-to-member, @c this object will be empty.
|
||||
*
|
||||
* If @a f is a non-NULL function pointer or an object of type @c
|
||||
* reference_wrapper<F>, this function will not throw.
|
||||
*/
|
||||
template<typename _Functor>
|
||||
typename __enable_if<function&, !is_integral<_Functor>::value>::__type
|
||||
operator=(_Functor __f)
|
||||
{
|
||||
function(__f).swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// [3.7.2.2] function modifiers
|
||||
|
||||
/**
|
||||
* @brief Swap the targets of two %function objects.
|
||||
* @param f A %function with identical call signature.
|
||||
*
|
||||
* Swap the targets of @c this function object and @a f. This
|
||||
* function will not throw an exception.
|
||||
*/
|
||||
void swap(function& __x)
|
||||
{
|
||||
_Any_data __old_functor = _M_functor;
|
||||
_M_functor = __x._M_functor;
|
||||
__x._M_functor = __old_functor;
|
||||
_Manager_type __old_manager = _M_manager;
|
||||
_M_manager = __x._M_manager;
|
||||
__x._M_manager = __old_manager;
|
||||
_Invoker_type __old_invoker = _M_invoker;
|
||||
_M_invoker = __x._M_invoker;
|
||||
__x._M_invoker = __old_invoker;
|
||||
}
|
||||
|
||||
// [3.7.2.3] function capacity
|
||||
|
||||
/**
|
||||
* @brief Determine if the %function wrapper has a target.
|
||||
*
|
||||
* @return @c true when this %function object contains a target,
|
||||
* or @c false when it is empty.
|
||||
*
|
||||
* This function will not throw an exception.
|
||||
*/
|
||||
operator _Safe_bool() const
|
||||
{
|
||||
if (_M_empty())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return &_Hidden_type::_M_bool;
|
||||
}
|
||||
}
|
||||
|
||||
// [3.7.2.4] function invocation
|
||||
|
||||
/**
|
||||
* @brief Invokes the function targeted by @c *this.
|
||||
* @returns the result of the target.
|
||||
* @throws bad_function_call when @c !(bool)*this
|
||||
*
|
||||
* The function call operator invokes the target function object
|
||||
* stored by @c this.
|
||||
*/
|
||||
_Res operator()(_GLIBCXX_PARAMS) const;
|
||||
|
||||
// [3.7.2.5] function target access
|
||||
/**
|
||||
* @brief Determine the type of the target of this function object
|
||||
* wrapper.
|
||||
*
|
||||
* @returns the type identifier of the target function object, or
|
||||
* @c typeid(void) if @c !(bool)*this.
|
||||
*
|
||||
* This function will not throw an exception.
|
||||
*/
|
||||
const type_info& target_type() const;
|
||||
|
||||
/**
|
||||
* @brief Access the stored target function object.
|
||||
*
|
||||
* @return Returns a pointer to the stored target function object,
|
||||
* if @c typeid(Functor).equals(target_type()); otherwise, a NULL
|
||||
* pointer.
|
||||
*
|
||||
* This function will not throw an exception.
|
||||
*/
|
||||
template<typename _Functor> _Functor* target();
|
||||
|
||||
/**
|
||||
* @overload
|
||||
*/
|
||||
template<typename _Functor> const _Functor* target() const;
|
||||
|
||||
private:
|
||||
// [3.7.2.6] undefined operators
|
||||
template<typename _Function>
|
||||
void operator==(const function<_Function>&) const;
|
||||
template<typename _Function>
|
||||
void operator!=(const function<_Function>&) const;
|
||||
|
||||
typedef _Res (*_Invoker_type)(const _Any_data& _GLIBCXX_COMMA _GLIBCXX_PARAMS);
|
||||
_Invoker_type _M_invoker;
|
||||
};
|
||||
|
||||
template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS>
|
||||
function<_Res(_GLIBCXX_TEMPLATE_ARGS)>::function(const function& __x)
|
||||
: _Function_base()
|
||||
{
|
||||
if (__x) {
|
||||
_M_invoker = __x._M_invoker;
|
||||
_M_manager = __x._M_manager;
|
||||
__x._M_manager(_M_functor, __x._M_functor, __clone_functor);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS>
|
||||
template<typename _Functor>
|
||||
function<_Res(_GLIBCXX_TEMPLATE_ARGS)>
|
||||
::function(_Functor __f,
|
||||
typename __enable_if<_Useless,
|
||||
!is_integral<_Functor>::value>::__type)
|
||||
: _Function_base()
|
||||
{
|
||||
typedef _Function_handler<_Signature_type, _Functor> _My_handler;
|
||||
if (_My_handler::_M_not_empty_function(__f)) {
|
||||
_M_invoker = &_My_handler::_M_invoke;
|
||||
_M_manager = &_My_handler::_M_manager;
|
||||
_My_handler::_M_init_functor(_M_functor, __f);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS>
|
||||
_Res function<_Res(_GLIBCXX_TEMPLATE_ARGS)>::operator()(_GLIBCXX_PARAMS) const
|
||||
{
|
||||
if (_M_empty())
|
||||
{
|
||||
#if __EXCEPTIONS
|
||||
throw bad_function_call();
|
||||
#else
|
||||
std::abort();
|
||||
#endif
|
||||
}
|
||||
return _M_invoker(_M_functor _GLIBCXX_COMMA _GLIBCXX_ARGS);
|
||||
}
|
||||
|
||||
template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS>
|
||||
const type_info&
|
||||
function<_Res(_GLIBCXX_TEMPLATE_ARGS)>::target_type() const
|
||||
{
|
||||
if (_M_manager)
|
||||
{
|
||||
_Any_data __typeinfo_result;
|
||||
_M_manager(__typeinfo_result, _M_functor, __get_type_info);
|
||||
return *__typeinfo_result._M_access<const type_info*>();
|
||||
}
|
||||
else
|
||||
{
|
||||
return typeid(void);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS>
|
||||
template<typename _Functor>
|
||||
_Functor*
|
||||
function<_Res(_GLIBCXX_TEMPLATE_ARGS)>::target()
|
||||
{
|
||||
if (typeid(_Functor) == target_type() && _M_manager)
|
||||
{
|
||||
_Any_data __ptr;
|
||||
if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
|
||||
&& !is_const<_Functor>::value)
|
||||
return 0;
|
||||
else
|
||||
return __ptr._M_access<_Functor*>();
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename _Res _GLIBCXX_COMMA _GLIBCXX_TEMPLATE_PARAMS>
|
||||
template<typename _Functor>
|
||||
const _Functor*
|
||||
function<_Res(_GLIBCXX_TEMPLATE_ARGS)>::target() const
|
||||
{
|
||||
if (typeid(_Functor) == target_type() && _M_manager)
|
||||
{
|
||||
_Any_data __ptr;
|
||||
_M_manager(__ptr, _M_functor, __get_functor_ptr);
|
||||
return __ptr._M_access<const _Functor*>();
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -32,10 +32,10 @@
|
||||
#define _GLIBCXX_TESTSUITE_TR1_H
|
||||
|
||||
namespace __gnu_test
|
||||
{
|
||||
{
|
||||
// For tr1/type_traits.
|
||||
template<template<typename> class Category,
|
||||
typename Type>
|
||||
typename Type>
|
||||
bool
|
||||
test_category(bool value)
|
||||
{
|
||||
@ -52,7 +52,7 @@ namespace __gnu_test
|
||||
}
|
||||
|
||||
template<template<typename> class Property,
|
||||
typename Type>
|
||||
typename Type>
|
||||
bool
|
||||
test_property(typename Property<Type>::value_type value)
|
||||
{
|
||||
@ -63,7 +63,7 @@ namespace __gnu_test
|
||||
}
|
||||
|
||||
template<template<typename> class Property,
|
||||
typename Type>
|
||||
typename Type>
|
||||
bool
|
||||
test_copy_property(bool value)
|
||||
{
|
||||
@ -80,7 +80,7 @@ namespace __gnu_test
|
||||
}
|
||||
|
||||
template<template<typename> class Property,
|
||||
typename Type>
|
||||
typename Type>
|
||||
bool
|
||||
test_assign_property(bool value)
|
||||
{
|
||||
@ -97,7 +97,7 @@ namespace __gnu_test
|
||||
}
|
||||
|
||||
template<template<typename, typename> class Relationship,
|
||||
typename Type1, typename Type2>
|
||||
typename Type1, typename Type2>
|
||||
bool
|
||||
test_relationship(bool value)
|
||||
{
|
||||
@ -122,7 +122,68 @@ namespace __gnu_test
|
||||
|
||||
class AbstractClass
|
||||
{ virtual void rotate(int) = 0; };
|
||||
|
||||
|
||||
|
||||
int truncate_float(float x) { return (int)x; }
|
||||
long truncate_double(double x) { return (long)x; }
|
||||
|
||||
struct do_truncate_float_t
|
||||
{
|
||||
do_truncate_float_t()
|
||||
{
|
||||
++live_objects;
|
||||
}
|
||||
|
||||
do_truncate_float_t(const do_truncate_float_t&)
|
||||
{
|
||||
++live_objects;
|
||||
}
|
||||
|
||||
~do_truncate_float_t()
|
||||
{
|
||||
--live_objects;
|
||||
}
|
||||
|
||||
int operator()(float x) { return (int)x; }
|
||||
|
||||
static int live_objects;
|
||||
};
|
||||
|
||||
int do_truncate_float_t::live_objects = 0;
|
||||
|
||||
struct do_truncate_double_t
|
||||
{
|
||||
do_truncate_double_t()
|
||||
{
|
||||
++live_objects;
|
||||
}
|
||||
|
||||
do_truncate_double_t(const do_truncate_double_t&)
|
||||
{
|
||||
++live_objects;
|
||||
}
|
||||
|
||||
~do_truncate_double_t()
|
||||
{
|
||||
--live_objects;
|
||||
}
|
||||
|
||||
long operator()(double x) { return (long)x; }
|
||||
|
||||
static int live_objects;
|
||||
};
|
||||
|
||||
int do_truncate_double_t::live_objects = 0;
|
||||
|
||||
struct X
|
||||
{
|
||||
int bar;
|
||||
|
||||
int foo() { return 1; }
|
||||
int foo_c() const { return 2; }
|
||||
int foo_v() volatile { return 3; }
|
||||
int foo_cv() const volatile { return 4; }
|
||||
};
|
||||
}; // namespace __gnu_test
|
||||
|
||||
#endif // _GLIBCXX_TESTSUITE_TR1_H
|
||||
|
95
libstdc++-v3/testsuite/tr1/3_function_objects/function/1.cc
Normal file
95
libstdc++-v3/testsuite/tr1/3_function_objects/function/1.cc
Normal file
@ -0,0 +1,95 @@
|
||||
// 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
|
||||
//
|
||||
// Copyright (C) 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
// USA.
|
||||
|
||||
// 3.7.2 polymorphic function object wrapper
|
||||
#include <tr1/functional>
|
||||
#include <testsuite_hooks.h>
|
||||
#include <testsuite_tr1.h>
|
||||
|
||||
using namespace __gnu_test;
|
||||
|
||||
bool test __attribute__((unused)) = true;
|
||||
|
||||
// Operations on empty function<> objects
|
||||
void test01()
|
||||
{
|
||||
using std::tr1::function;
|
||||
using std::tr1::bad_function_call;
|
||||
|
||||
// Default-construction
|
||||
function<int(float)> f1;
|
||||
VERIFY( ((bool)f1 == false) );
|
||||
VERIFY( !f1 );
|
||||
VERIFY( f1 == 0 );
|
||||
VERIFY( 0 == f1 );
|
||||
VERIFY( !(f1 != 0) );
|
||||
VERIFY( !(0 != f1) );
|
||||
|
||||
// Copy-construction
|
||||
function<int(float)> f2(f1);
|
||||
VERIFY( !f2 );
|
||||
|
||||
// Construct with NULL pointer
|
||||
function<int(float)> f3(0);
|
||||
VERIFY( !f3 );
|
||||
|
||||
// Assignment
|
||||
f1 = f2;
|
||||
VERIFY( !f1);
|
||||
|
||||
// Assignment to NULL pointer
|
||||
f1 = 0;
|
||||
VERIFY( !f1 );
|
||||
|
||||
// Swap
|
||||
swap(f1, f2);
|
||||
VERIFY( !f1 );
|
||||
VERIFY( !f2 );
|
||||
|
||||
// Invocation should throw bad_function_call
|
||||
bool thrown = false;
|
||||
try
|
||||
{
|
||||
f1(3.14159f);
|
||||
VERIFY( false );
|
||||
}
|
||||
catch (bad_function_call)
|
||||
{
|
||||
thrown = true;
|
||||
}
|
||||
VERIFY( thrown );
|
||||
|
||||
// target_type returns typeid(void)
|
||||
VERIFY( f1.target_type() == typeid(void) );
|
||||
|
||||
// target() always returns a NULL pointer
|
||||
VERIFY( f1.target<int (*)(float)>() == 0);
|
||||
|
||||
// Check const version
|
||||
const function<int(float)>& f1c = f1;
|
||||
VERIFY( f1c.target<int (*)(float)>() == 0 );
|
||||
VERIFY( !f1c );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test01();
|
||||
return 0;
|
||||
}
|
78
libstdc++-v3/testsuite/tr1/3_function_objects/function/2.cc
Normal file
78
libstdc++-v3/testsuite/tr1/3_function_objects/function/2.cc
Normal file
@ -0,0 +1,78 @@
|
||||
// 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
|
||||
//
|
||||
// Copyright (C) 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
// USA.
|
||||
|
||||
// 3.7.2 polymorphic function object wrapper
|
||||
#include <tr1/functional>
|
||||
#include <testsuite_hooks.h>
|
||||
#include <testsuite_tr1.h>
|
||||
|
||||
using namespace __gnu_test;
|
||||
|
||||
bool test __attribute__((unused)) = true;
|
||||
|
||||
// Put function pointers into function<> wrappers
|
||||
void test02()
|
||||
{
|
||||
using std::tr1::function;
|
||||
|
||||
function<int(float)> f1(truncate_float);
|
||||
VERIFY( f1 );
|
||||
VERIFY( !!f1 );
|
||||
VERIFY( !(f1 == 0) );
|
||||
VERIFY( !(0 == f1) );
|
||||
VERIFY( f1 != 0 );
|
||||
VERIFY( 0 != f1 );
|
||||
|
||||
// Copy-construction
|
||||
function<int(float)> f2(f1);
|
||||
VERIFY( f2 );
|
||||
|
||||
// Invocation
|
||||
VERIFY( f1(3.1f) == 3 );
|
||||
VERIFY( f2(3.1f) == 3 );
|
||||
|
||||
// Assignment to zero
|
||||
f1 = 0;
|
||||
VERIFY( !f1 );
|
||||
|
||||
// Swap
|
||||
f1.swap(f2);
|
||||
VERIFY( f1 );
|
||||
VERIFY( !f2 );
|
||||
VERIFY( f1(3.1f) == 3 );
|
||||
|
||||
// Assignment from a function pointer
|
||||
f2 = truncate_float;
|
||||
VERIFY( f2(3.1f) == 3 );
|
||||
|
||||
// target_type and target() functions
|
||||
const function<int(float)>& f1c = f1;
|
||||
VERIFY( typeid(int(*)(float)) == f1.target_type() );
|
||||
VERIFY( f2.target<int(*)(float)>() != 0 );
|
||||
VERIFY( *f2.target<int(*)(float)>() == &truncate_float );
|
||||
VERIFY( f1c.target<int(*)(float)>() != 0 );
|
||||
VERIFY( *f1c.target<int(*)(float)>() == &truncate_float );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test02();
|
||||
return 0;
|
||||
}
|
78
libstdc++-v3/testsuite/tr1/3_function_objects/function/3.cc
Normal file
78
libstdc++-v3/testsuite/tr1/3_function_objects/function/3.cc
Normal file
@ -0,0 +1,78 @@
|
||||
// 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
|
||||
//
|
||||
// Copyright (C) 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
// USA.
|
||||
|
||||
// 3.7.2 polymorphic function object wrapper
|
||||
#include <tr1/functional>
|
||||
#include <testsuite_hooks.h>
|
||||
#include <testsuite_tr1.h>
|
||||
|
||||
using namespace __gnu_test;
|
||||
|
||||
bool test __attribute__((unused)) = true;
|
||||
|
||||
// Put nearly-matching function pointers into function<> wrappers
|
||||
void test03()
|
||||
{
|
||||
using std::tr1::function;
|
||||
|
||||
function<int(float)> f1(truncate_double);
|
||||
VERIFY( f1 );
|
||||
VERIFY( !!f1 );
|
||||
VERIFY( !(f1 == 0) );
|
||||
VERIFY( !(0 == f1) );
|
||||
VERIFY( f1 != 0 );
|
||||
VERIFY( 0 != f1 );
|
||||
|
||||
// Copy-construction
|
||||
function<int(float)> f2(f1);
|
||||
VERIFY( f2 );
|
||||
|
||||
// Invocation
|
||||
VERIFY( f1(3.1f) == 3 );
|
||||
VERIFY( f2(3.1f) == 3 );
|
||||
|
||||
// Assignment to zero
|
||||
f1 = 0;
|
||||
VERIFY( !f1 );
|
||||
|
||||
// Swap
|
||||
f1.swap(f2);
|
||||
VERIFY( f1 );
|
||||
VERIFY( !f2 );
|
||||
VERIFY( f1(3.1f) == 3 );
|
||||
|
||||
// Assignment from a function pointer
|
||||
f2 = truncate_double;
|
||||
VERIFY( f2(3.1f) == 3 );
|
||||
|
||||
// target_type and target() functions
|
||||
const function<int(float)>& f1c = f1;
|
||||
VERIFY( typeid(long(*)(double)) == f1.target_type() );
|
||||
VERIFY( f2.target<long(*)(double)>() != 0 );
|
||||
VERIFY( *f2.target<long(*)(double)>() == &truncate_double );
|
||||
VERIFY( f1c.target<long(*)(double)>() != 0 );
|
||||
VERIFY( *f1c.target<long(*)(double)>() == &truncate_double );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test03();
|
||||
return 0;
|
||||
}
|
82
libstdc++-v3/testsuite/tr1/3_function_objects/function/4.cc
Normal file
82
libstdc++-v3/testsuite/tr1/3_function_objects/function/4.cc
Normal file
@ -0,0 +1,82 @@
|
||||
// 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
|
||||
//
|
||||
// Copyright (C) 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
// USA.
|
||||
|
||||
// 3.7.2 polymorphic function object wrapper
|
||||
#include <tr1/functional>
|
||||
#include <testsuite_hooks.h>
|
||||
#include <testsuite_tr1.h>
|
||||
|
||||
using namespace __gnu_test;
|
||||
|
||||
bool test __attribute__((unused)) = true;
|
||||
|
||||
// Put function objects into function<> wrappers
|
||||
void test04()
|
||||
{
|
||||
using std::tr1::function;
|
||||
|
||||
do_truncate_float_t truncate_float;
|
||||
|
||||
function<int(float)> f1(truncate_float);
|
||||
VERIFY( f1 );
|
||||
VERIFY( !!f1 );
|
||||
VERIFY( !(f1 == 0) );
|
||||
VERIFY( !(0 == f1) );
|
||||
VERIFY( f1 != 0 );
|
||||
VERIFY( 0 != f1 );
|
||||
|
||||
// Copy-construction
|
||||
function<int(float)> f2(f1);
|
||||
VERIFY( f2 );
|
||||
|
||||
// Invocation
|
||||
VERIFY( f1(3.1f) == 3 );
|
||||
VERIFY( f2(3.1f) == 3 );
|
||||
|
||||
// Assignment to zero
|
||||
f1 = 0;
|
||||
VERIFY( !f1 );
|
||||
|
||||
// Swap
|
||||
f1.swap(f2);
|
||||
VERIFY( f1 );
|
||||
VERIFY( !f2 );
|
||||
VERIFY( f1(3.1f) == 3 );
|
||||
|
||||
// Assignment from a function pointer
|
||||
f2 = do_truncate_float_t();
|
||||
VERIFY( f2(3.1f) == 3 );
|
||||
|
||||
// target_type and target() functions
|
||||
const function<int(float)>& f1c = f1;
|
||||
VERIFY( typeid(do_truncate_float_t) == f1.target_type() );
|
||||
VERIFY( f2.target<do_truncate_float_t>() != 0 );
|
||||
VERIFY( f1c.target<do_truncate_float_t>() != 0 );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test04();
|
||||
|
||||
VERIFY( do_truncate_double_t::live_objects == 0 );
|
||||
VERIFY( do_truncate_float_t::live_objects == 0 );
|
||||
|
||||
return 0;
|
||||
}
|
107
libstdc++-v3/testsuite/tr1/3_function_objects/function/5.cc
Normal file
107
libstdc++-v3/testsuite/tr1/3_function_objects/function/5.cc
Normal file
@ -0,0 +1,107 @@
|
||||
// 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
|
||||
//
|
||||
// Copyright (C) 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
// USA.
|
||||
|
||||
// 3.7.2 polymorphic function object wrapper
|
||||
#include <tr1/functional>
|
||||
#include <testsuite_hooks.h>
|
||||
#include <testsuite_tr1.h>
|
||||
|
||||
using namespace __gnu_test;
|
||||
|
||||
bool test __attribute__((unused)) = true;
|
||||
|
||||
// Put member pointers into function<> wrappers
|
||||
void test05()
|
||||
{
|
||||
using std::tr1::function;
|
||||
|
||||
X x;
|
||||
x.bar = 17;
|
||||
|
||||
function<int(X&)> frm(&X::bar);
|
||||
VERIFY( frm );
|
||||
VERIFY( frm(x) == 17 );
|
||||
VERIFY( typeid(int X::*) == frm.target_type() );
|
||||
VERIFY( *frm.target<int X::*>() == &X::bar );
|
||||
|
||||
function<int(X&)> fr(&X::foo);
|
||||
VERIFY( fr );
|
||||
VERIFY( fr(x) == 1 );
|
||||
VERIFY( typeid(int (X::*)()) == fr.target_type() );
|
||||
VERIFY( *fr.target<int (X::*)()>() == &X::foo );
|
||||
|
||||
function<int(const X&)> frc(&X::foo_c);
|
||||
VERIFY( frc );
|
||||
VERIFY( frc(x) == 2 );
|
||||
VERIFY( typeid(int (X::*)() const) == frc.target_type() );
|
||||
VERIFY( *frc.target<int (X::*)() const >() == &X::foo_c );
|
||||
|
||||
function<int(volatile X&)> frv(&X::foo_v);
|
||||
VERIFY( frv );
|
||||
VERIFY( frv(x) == 3 );
|
||||
VERIFY( typeid(int (X::*)() volatile) == frv.target_type() );
|
||||
VERIFY( *frv.target<int (X::*)() volatile >() == &X::foo_v );
|
||||
VERIFY( frv.target<int (X::*)() const volatile>() == 0 );
|
||||
|
||||
function<int(const volatile X&)> frcv(&X::foo_cv);
|
||||
VERIFY( frcv );
|
||||
VERIFY( frcv(x) == 4 );
|
||||
VERIFY( typeid(int (X::*)() const volatile) == frcv.target_type() );
|
||||
VERIFY( *frcv.target<int (X::*)() const volatile >() == &X::foo_cv );
|
||||
VERIFY( frcv.target<int (X::*)() const>() == 0 );
|
||||
|
||||
function<int(X*)> grm(&X::bar);
|
||||
VERIFY( grm );
|
||||
VERIFY( grm(&x) == 17 );
|
||||
VERIFY( typeid(int X::*) == grm.target_type() );
|
||||
VERIFY( *grm.target<int X::*>() == &X::bar );
|
||||
|
||||
function<int(X*)> gr(&X::foo);
|
||||
VERIFY( gr );
|
||||
VERIFY( gr(&x) == 1 );
|
||||
VERIFY( typeid(int (X::*)()) == gr.target_type() );
|
||||
VERIFY( *gr.target<int (X::*)()>() == &X::foo );
|
||||
|
||||
function<int(const X*)> grc(&X::foo_c);
|
||||
VERIFY( grc );
|
||||
VERIFY( grc(&x) == 2 );
|
||||
VERIFY( typeid(int (X::*)() const) == grc.target_type() );
|
||||
VERIFY( *grc.target<int (X::*)() const >() == &X::foo_c );
|
||||
|
||||
function<int(volatile X*)> grv(&X::foo_v);
|
||||
VERIFY( grv );
|
||||
VERIFY( grv(&x) == 3 );
|
||||
VERIFY( typeid(int (X::*)() volatile) == grv.target_type() );
|
||||
VERIFY( *grv.target<int (X::*)() volatile >() == &X::foo_v );
|
||||
VERIFY( grv.target<int (X::*)() const volatile>() == 0 );
|
||||
|
||||
function<int(const volatile X*)> grcv(&X::foo_cv);
|
||||
VERIFY( grcv );
|
||||
VERIFY( grcv(&x) == 4 );
|
||||
VERIFY( typeid(int (X::*)() const volatile) == grcv.target_type() );
|
||||
VERIFY( *grcv.target<int (X::*)() const volatile >() == &X::foo_cv );
|
||||
VERIFY( grcv.target<int (X::*)() const>() == 0 );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test05();
|
||||
return 0;
|
||||
}
|
83
libstdc++-v3/testsuite/tr1/3_function_objects/function/6.cc
Normal file
83
libstdc++-v3/testsuite/tr1/3_function_objects/function/6.cc
Normal file
@ -0,0 +1,83 @@
|
||||
// 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
|
||||
//
|
||||
// Copyright (C) 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
// USA.
|
||||
|
||||
// 3.7.2 polymorphic function object wrapper
|
||||
#include <tr1/functional>
|
||||
#include <testsuite_hooks.h>
|
||||
#include <testsuite_tr1.h>
|
||||
|
||||
using namespace __gnu_test;
|
||||
|
||||
bool test __attribute__((unused)) = true;
|
||||
|
||||
struct secret {};
|
||||
|
||||
struct noncopyable_function_object_type
|
||||
{
|
||||
noncopyable_function_object_type(secret) {}
|
||||
|
||||
int operator()() const { return 42; }
|
||||
int operator()() { return 17; }
|
||||
|
||||
private:
|
||||
noncopyable_function_object_type();
|
||||
noncopyable_function_object_type(const noncopyable_function_object_type&);
|
||||
void operator=(const noncopyable_function_object_type&);
|
||||
};
|
||||
|
||||
// Put reference_wrappers into function<> wrappers
|
||||
void test06()
|
||||
{
|
||||
using std::tr1::function;
|
||||
using std::tr1::ref;
|
||||
using std::tr1::cref;
|
||||
|
||||
secret password;
|
||||
noncopyable_function_object_type x(password);
|
||||
|
||||
function<int()> f(ref(x));
|
||||
VERIFY( f );
|
||||
VERIFY( f() == 17 );
|
||||
VERIFY( f.target_type() == typeid(noncopyable_function_object_type) );
|
||||
VERIFY( f.target<noncopyable_function_object_type>() == &x );
|
||||
|
||||
function<int()> g = f;
|
||||
VERIFY( g );
|
||||
VERIFY( g() == 17 );
|
||||
VERIFY( g.target_type() == typeid(noncopyable_function_object_type) );
|
||||
VERIFY( g.target<noncopyable_function_object_type>() == &x );
|
||||
|
||||
function<int()> h = cref(x);
|
||||
VERIFY( h );
|
||||
VERIFY( h() == 42 );
|
||||
VERIFY( h.target_type() == typeid(noncopyable_function_object_type) );
|
||||
VERIFY( h.target<const noncopyable_function_object_type>() == &x );
|
||||
VERIFY( h.target<const noncopyable_function_object_type>() == &x );
|
||||
|
||||
const function<int()>& hc = h;
|
||||
VERIFY( h.target<noncopyable_function_object_type>() == 0 );
|
||||
VERIFY( hc.target<noncopyable_function_object_type>() == &x );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test06();
|
||||
return 0;
|
||||
}
|
82
libstdc++-v3/testsuite/tr1/3_function_objects/function/7.cc
Normal file
82
libstdc++-v3/testsuite/tr1/3_function_objects/function/7.cc
Normal file
@ -0,0 +1,82 @@
|
||||
// 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
|
||||
//
|
||||
// Copyright (C) 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
// USA.
|
||||
|
||||
// 3.7.2 polymorphic function object wrapper
|
||||
#include <tr1/functional>
|
||||
#include <testsuite_hooks.h>
|
||||
#include <testsuite_tr1.h>
|
||||
|
||||
using namespace __gnu_test;
|
||||
|
||||
bool test __attribute__((unused)) = true;
|
||||
|
||||
// Put reference_wrappers to function pointers into function<> wrappers
|
||||
void test07()
|
||||
{
|
||||
using std::tr1::function;
|
||||
using std::tr1::ref;
|
||||
using std::tr1::cref;
|
||||
|
||||
int (*fptr)(float) = truncate_float;
|
||||
|
||||
function<int(float)> f1(ref(fptr));
|
||||
VERIFY( f1 );
|
||||
VERIFY( !!f1 );
|
||||
VERIFY( !(f1 == 0) );
|
||||
VERIFY( !(0 == f1) );
|
||||
VERIFY( f1 != 0 );
|
||||
VERIFY( 0 != f1 );
|
||||
|
||||
// Invocation
|
||||
VERIFY( f1(3.1f) == 3 );
|
||||
|
||||
// target_type and target() functions
|
||||
const function<int(float)>& f1c = f1;
|
||||
VERIFY( typeid(int(*)(float)) == f1.target_type() );
|
||||
VERIFY( f1.target<int(*)(float)>() != 0 );
|
||||
VERIFY( f1.target<int(*)(float)>() == &fptr );
|
||||
VERIFY( f1c.target<int(*)(float)>() != 0 );
|
||||
VERIFY( f1c.target<int(*)(float)>() == &fptr );
|
||||
|
||||
function<int(float)> f2(cref(fptr));
|
||||
VERIFY( f2 );
|
||||
VERIFY( !!f2 );
|
||||
VERIFY( !(f2 == 0) );
|
||||
VERIFY( !(0 == f2) );
|
||||
VERIFY( f2 != 0 );
|
||||
VERIFY( 0 != f2 );
|
||||
|
||||
// Invocation
|
||||
VERIFY( f2(3.1f) == 3 );
|
||||
|
||||
// target_type and target() functions
|
||||
const function<int(float)>& f2c = f2;
|
||||
VERIFY( typeid(int(*)(float)) == f2.target_type() );
|
||||
VERIFY( f2.target<int(*)(float)>() == 0 );
|
||||
VERIFY( f2.target<int(* const)(float)>() == &fptr );
|
||||
VERIFY( f2c.target<int(*)(float)>() != 0 );
|
||||
VERIFY( f2c.target<int(*)(float)>() == &fptr );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test07();
|
||||
return 0;
|
||||
}
|
148
libstdc++-v3/testsuite/tr1/3_function_objects/function/8.cc
Normal file
148
libstdc++-v3/testsuite/tr1/3_function_objects/function/8.cc
Normal file
@ -0,0 +1,148 @@
|
||||
// 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
|
||||
//
|
||||
// Copyright (C) 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
// USA.
|
||||
|
||||
// 3.7.2 polymorphic function object wrapper
|
||||
#include <tr1/functional>
|
||||
#include <testsuite_hooks.h>
|
||||
#include <testsuite_tr1.h>
|
||||
|
||||
using namespace __gnu_test;
|
||||
|
||||
bool test __attribute__((unused)) = true;
|
||||
|
||||
// Put reference_wrappers to member pointers
|
||||
void test08()
|
||||
{
|
||||
using std::tr1::function;
|
||||
using std::tr1::ref;
|
||||
using std::tr1::cref;
|
||||
|
||||
int X::* X_bar = &X::bar;
|
||||
int (X::* X_foo)() = &X::foo;
|
||||
int (X::* X_foo_c)() const = &X::foo_c;
|
||||
int (X::* X_foo_v)() volatile = &X::foo_v;
|
||||
int (X::* X_foo_cv)() const volatile = &X::foo_cv;
|
||||
|
||||
X x;
|
||||
x.bar = 17;
|
||||
|
||||
function<int(X&)> frm(ref(X_bar));
|
||||
VERIFY( frm );
|
||||
VERIFY( frm(x) == 17 );
|
||||
VERIFY( typeid(int X::*) == frm.target_type() );
|
||||
VERIFY( frm.target<int X::*>() == &X_bar );
|
||||
|
||||
function<int(X&)> fr(ref(X_foo));
|
||||
VERIFY( fr );
|
||||
VERIFY( fr(x) == 1 );
|
||||
VERIFY( typeid(int (X::*)()) == fr.target_type() );
|
||||
VERIFY( fr.target<int (X::*)()>() == &X_foo );
|
||||
|
||||
function<int(const X&)> frc(ref(X_foo_c));
|
||||
VERIFY( frc );
|
||||
VERIFY( frc(x) == 2 );
|
||||
VERIFY( typeid(int (X::*)() const) == frc.target_type() );
|
||||
VERIFY( frc.target<int (X::*)() const >() == &X_foo_c );
|
||||
|
||||
function<int(volatile X&)> frv(ref(X_foo_v));
|
||||
VERIFY( frv );
|
||||
VERIFY( frv(x) == 3 );
|
||||
VERIFY( typeid(int (X::*)() volatile) == frv.target_type() );
|
||||
VERIFY( *frv.target<int (X::*)() volatile >() == X_foo_v );
|
||||
VERIFY( frv.target<int (X::*)() const volatile>() == 0 );
|
||||
|
||||
function<int(const volatile X&)> frcv(ref(X_foo_cv));
|
||||
VERIFY( frcv );
|
||||
VERIFY( frcv(x) == 4 );
|
||||
VERIFY( typeid(int (X::*)() const volatile) == frcv.target_type() );
|
||||
VERIFY( *frcv.target<int (X::*)() const volatile >() == X_foo_cv );
|
||||
VERIFY( frcv.target<int (X::*)() const>() == 0 );
|
||||
|
||||
function<int(X*)> grm(ref(X_bar));
|
||||
VERIFY( grm );
|
||||
VERIFY( grm(&x) == 17 );
|
||||
VERIFY( typeid(int X::*) == grm.target_type() );
|
||||
VERIFY( *grm.target<int X::*>() == X_bar );
|
||||
|
||||
function<int(X*)> gr(ref(X_foo));
|
||||
VERIFY( gr );
|
||||
VERIFY( gr(&x) == 1 );
|
||||
VERIFY( typeid(int (X::*)()) == gr.target_type() );
|
||||
VERIFY( *gr.target<int (X::*)()>() == X_foo );
|
||||
|
||||
function<int(const X*)> grc(ref(X_foo_c));
|
||||
VERIFY( grc );
|
||||
VERIFY( grc(&x) == 2 );
|
||||
VERIFY( typeid(int (X::*)() const) == grc.target_type() );
|
||||
VERIFY( *grc.target<int (X::*)() const >() == X_foo_c );
|
||||
|
||||
function<int(volatile X*)> grv(ref(X_foo_v));
|
||||
VERIFY( grv );
|
||||
VERIFY( grv(&x) == 3 );
|
||||
VERIFY( typeid(int (X::*)() volatile) == grv.target_type() );
|
||||
VERIFY( *grv.target<int (X::*)() volatile >() == X_foo_v );
|
||||
VERIFY( grv.target<int (X::*)() const volatile>() == 0 );
|
||||
|
||||
function<int(const volatile X*)> grcv(ref(X_foo_cv));
|
||||
VERIFY( grcv );
|
||||
VERIFY( grcv(&x) == 4 );
|
||||
VERIFY( typeid(int (X::*)() const volatile) == grcv.target_type() );
|
||||
VERIFY( *grcv.target<int (X::*)() const volatile >() == X_foo_cv );
|
||||
VERIFY( grcv.target<int (X::*)() const>() == 0 );
|
||||
|
||||
function<int(X&)> hrm(cref(X_bar));
|
||||
VERIFY( hrm );
|
||||
VERIFY( hrm(x) == 17 );
|
||||
VERIFY( typeid(int X::*) == hrm.target_type() );
|
||||
VERIFY( hrm.target<int X::*>() == 0 );
|
||||
VERIFY( hrm.target<int X::* const>() == &X_bar );
|
||||
|
||||
function<int(X&)> hr(cref(X_foo));
|
||||
VERIFY( hr );
|
||||
VERIFY( hr(x) == 1 );
|
||||
VERIFY( typeid(int (X::*)()) == hr.target_type() );
|
||||
VERIFY( hr.target<int (X::* const)()>() == &X_foo );
|
||||
|
||||
function<int(const X&)> hrc(cref(X_foo_c));
|
||||
VERIFY( hrc );
|
||||
VERIFY( hrc(x) == 2 );
|
||||
VERIFY( typeid(int (X::*)() const) == hrc.target_type() );
|
||||
VERIFY( hrc.target<int (X::* const)() const >() == &X_foo_c );
|
||||
|
||||
function<int(volatile X&)> hrv(cref(X_foo_v));
|
||||
VERIFY( hrv );
|
||||
VERIFY( hrv(x) == 3 );
|
||||
VERIFY( typeid(int (X::*)() volatile) == hrv.target_type() );
|
||||
VERIFY( hrv.target<int (X::* const)() volatile >() == &X_foo_v );
|
||||
VERIFY( hrv.target<int (X::* const)() const volatile>() == 0 );
|
||||
|
||||
function<int(const volatile X&)> hrcv(cref(X_foo_cv));
|
||||
VERIFY( hrcv );
|
||||
VERIFY( hrcv(x) == 4 );
|
||||
VERIFY( typeid(int (X::*)() const volatile) == hrcv.target_type() );
|
||||
VERIFY( hrcv.target<int (X::* const)() const volatile >() == &X_foo_cv );
|
||||
VERIFY( hrcv.target<int (X::* const)() const>() == 0 );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test08();
|
||||
return 0;
|
||||
}
|
55
libstdc++-v3/testsuite/tr1/3_function_objects/function/9.cc
Normal file
55
libstdc++-v3/testsuite/tr1/3_function_objects/function/9.cc
Normal file
@ -0,0 +1,55 @@
|
||||
// 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
|
||||
//
|
||||
// Copyright (C) 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
// USA.
|
||||
|
||||
// 3.7.2 polymorphic function object wrapper
|
||||
#include <tr1/functional>
|
||||
#include <testsuite_hooks.h>
|
||||
#include <testsuite_tr1.h>
|
||||
|
||||
using namespace __gnu_test;
|
||||
|
||||
bool test __attribute__((unused)) = true;
|
||||
|
||||
// Put function objects into a void-returning function<> wrapper
|
||||
void test09()
|
||||
{
|
||||
using std::tr1::function;
|
||||
using std::tr1::ref;
|
||||
using std::tr1::cref;
|
||||
|
||||
int (X::*X_foo_c)() const = &X::foo_c;
|
||||
function<void(X&)> f(&X::bar);
|
||||
f = &X::foo;
|
||||
f = ref(X_foo_c);
|
||||
f = cref(X_foo_c);
|
||||
|
||||
function<void(float)> g = &truncate_float;
|
||||
g = do_truncate_float_t();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test09();
|
||||
|
||||
VERIFY( do_truncate_double_t::live_objects == 0 );
|
||||
VERIFY( do_truncate_float_t::live_objects == 0 );
|
||||
|
||||
return 0;
|
||||
}
|
78
libstdc++-v3/testsuite/tr1/3_function_objects/mem_fn.cc
Normal file
78
libstdc++-v3/testsuite/tr1/3_function_objects/mem_fn.cc
Normal file
@ -0,0 +1,78 @@
|
||||
// 2005-01-26 Douglas Gregor <dgregor@cs.indiana.edu>
|
||||
//
|
||||
// Copyright (C) 2005 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
||||
// USA.
|
||||
|
||||
// 3.5 function template mem_fn
|
||||
#include <tr1/functional>
|
||||
#include <testsuite_hooks.h>
|
||||
#include <testsuite_tr1.h>
|
||||
|
||||
struct X { int bar; };
|
||||
|
||||
struct Y : X { };
|
||||
|
||||
template<typename T>
|
||||
struct dumb_ptr
|
||||
{
|
||||
dumb_ptr(T* p) : p(p) {}
|
||||
|
||||
T& operator*() const { return *p; }
|
||||
|
||||
private:
|
||||
T* p;
|
||||
};
|
||||
|
||||
// Test mem_fn with a data member
|
||||
void test01()
|
||||
{
|
||||
using std::tr1::mem_fn;
|
||||
|
||||
X x;
|
||||
Y y;
|
||||
const X& xc = x;
|
||||
const Y& yc = y;
|
||||
X* xp = &x;
|
||||
Y* yp =&y;
|
||||
const X* xpc = xp;
|
||||
const Y* ypc = yp;
|
||||
dumb_ptr<X> xd(xp);
|
||||
dumb_ptr<Y> yd(yp);
|
||||
const dumb_ptr<X>& xdc = xd;
|
||||
const dumb_ptr<Y>& ydc = yd;
|
||||
|
||||
int& bx = mem_fn(&X::bar)(x);
|
||||
const int& bxc = mem_fn(&X::bar)(xc);
|
||||
int& bxp = mem_fn(&X::bar)(xp);
|
||||
const int& bxpc = mem_fn(&X::bar)(xpc);
|
||||
const int& bxd = mem_fn(&X::bar)(xd);
|
||||
const int& bxdc = mem_fn(&X::bar)(xdc);
|
||||
|
||||
int& by = mem_fn(&X::bar)(y);
|
||||
const int& byc = mem_fn(&X::bar)(yc);
|
||||
int& byp = mem_fn(&X::bar)(yp);
|
||||
const int& bypc = mem_fn(&X::bar)(ypc);
|
||||
const int& byd = mem_fn(&X::bar)(yd);
|
||||
const int& bydc = mem_fn(&X::bar)(ydc);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test01();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user