From 5bae1b9ad1bab043521ebd5faa6d267dfd8de71c Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 20 Mar 2002 12:52:40 +0000 Subject: [PATCH] Data member pointers support added. [SVN r13232] --- bind_test.cpp | 6 ++-- include/boost/bind.hpp | 11 ++++++ include/boost/mem_fn.hpp | 75 +++++++++++++++++++++++++++++++++++++++- mem_fn_derived_test.cpp | 4 +-- mem_fn_test.cpp | 4 +-- 5 files changed, 92 insertions(+), 8 deletions(-) diff --git a/bind_test.cpp b/bind_test.cpp index 81b5db4..cd5807d 100644 --- a/bind_test.cpp +++ b/bind_test.cpp @@ -8,7 +8,7 @@ // // bind_test.cpp - monolithic test for bind.hpp // -// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. // Copyright (c) 2001 David Abrahams // // Permission to copy, use, modify, sell and distribute this software @@ -372,7 +372,7 @@ void member_function_test() bind(&X::g8, x, 1, 2, 3, 4, 5, 6, 7, 8)(); bind(&X::g8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8)(); - BOOST_TEST( x.hash == 23558 ); + BOOST_TEST( bind(&X::hash, _1)(x) == 23558 ); } void member_function_void_test() @@ -462,7 +462,7 @@ void member_function_void_test() bind(&V::g8, v, 1, 2, 3, 4, 5, 6, 7, 8)(); bind(&V::g8, ref(v), 1, 2, 3, 4, 5, 6, 7, 8)(); - BOOST_TEST( v.hash == 23558 ); + BOOST_TEST( bind(&V::hash, _1)(v) == 23558 ); } void nested_bind_test() diff --git a/include/boost/bind.hpp b/include/boost/bind.hpp index f59c9ca..ddbabbd 100644 --- a/include/boost/bind.hpp +++ b/include/boost/bind.hpp @@ -1355,6 +1355,17 @@ template + _bi::bind_t< R, _mfi::dm, typename _bi::list_av_1::type > + BOOST_BIND(R T::*f, A1 a1) +{ + typedef _mfi::dm F; + typedef typename _bi::list_av_1::type list_type; + return _bi::bind_t(F(f), list_type(a1)); +} + } // namespace boost #ifndef BOOST_BIND_NO_PLACEHOLDERS diff --git a/include/boost/mem_fn.hpp b/include/boost/mem_fn.hpp index 3148992..f59fff4 100644 --- a/include/boost/mem_fn.hpp +++ b/include/boost/mem_fn.hpp @@ -8,7 +8,7 @@ // // mem_fn.hpp - a generalization of std::mem_fun[_ref] // -// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. // Copyright (c) 2001 David Abrahams // // Permission to copy, use, modify, sell and distribute this software @@ -187,6 +187,79 @@ namespace _mfi #endif +// data member support + +namespace _mfi +{ + +template class dm +{ +public: + + typedef R const & result_type; + typedef T const * argument_type; + +private: + + typedef R (T::*F); + F f_; + + template R const & call(U & u, T const *) const + { + return (u.*f_); + } + + template R & call(U & u, T *) const + { + return (u.*f_); + } + + template R const & call(U & u, void const *) const + { + return (get_pointer(u)->*f_); + } + +public: + + explicit dm(F f): f_(f) {} + + R & operator()(T * p) const + { + return (p->*f_); + } + + R const & operator()(T const * p) const + { + return (p->*f_); + } + + template R const & operator()(U & u) const + { + return call(u, &u); + } + +#if !defined(BOOST_MSVC) || (BOOST_MSVC > 1300) + + R & operator()(T & t) const + { + return (t.*f_); + } + +#endif + + R const & operator()(T const & t) const + { + return (t.*f_); + } +}; + +} // namespace _mfi + +template _mfi::dm mem_fn(R T::*f) +{ + return _mfi::dm(f); +} + } // namespace boost #endif // #ifndef BOOST_MEM_FN_HPP_INCLUDED diff --git a/mem_fn_derived_test.cpp b/mem_fn_derived_test.cpp index c689ac2..e09d38b 100644 --- a/mem_fn_derived_test.cpp +++ b/mem_fn_derived_test.cpp @@ -8,7 +8,7 @@ // // mem_fn_derived_test.cpp - tests mem_fn.hpp with derived objects // -// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. // // Permission to copy, use, modify, sell and distribute this software // is granted provided this copyright notice appears in all copies. @@ -183,5 +183,5 @@ int main() mem_fn(&X::g8)(pcx, 1, 2, 3, 4, 5, 6, 7, 8); mem_fn(&X::g8)(sp, 1, 2, 3, 4, 5, 6, 7, 8); - return detect_errors(x.hash == 17610 && sp->hash == 2155); + return detect_errors(mem_fn(&X::hash)(x) == 17610 && mem_fn(&X::hash)(sp) == 2155); } diff --git a/mem_fn_test.cpp b/mem_fn_test.cpp index 63756f2..20321c9 100644 --- a/mem_fn_test.cpp +++ b/mem_fn_test.cpp @@ -8,7 +8,7 @@ // // mem_fn_test.cpp - a test for mem_fn.hpp // -// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. // // Permission to copy, use, modify, sell and distribute this software // is granted provided this copyright notice appears in all copies. @@ -179,5 +179,5 @@ int main() mem_fn(&X::g8)(pcx, 1, 2, 3, 4, 5, 6, 7, 8); mem_fn(&X::g8)(sp, 1, 2, 3, 4, 5, 6, 7, 8); - return detect_errors(x.hash == 17610 && sp->hash == 2155); + return detect_errors(mem_fn(&X::hash)(x) == 17610 && mem_fn(&X::hash)(sp) == 2155); }