/*************************************************************************** * ext/indirect.hpp * * Fri Mar 4 17:41:23 2005 * Copyright 2005 Matthias Kaeppler * Email matthias@digitalraid.com ****************************************************************************/ /* * This program 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 of the License, or * (at your option) any later version. * * This program 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 Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include namespace ext { // This class template is an adaptor for a binary function, which will be invoked // on two dereferenced pointers. This is useful, if you need to process elements of // a collection of pointers, by invoking the operation on the pointees, e.g. sorting // a list of pointers by a predicate which applies to the pointees. // Both function pointers and function objects may be passed to the constructor. template< typename Operation > class indirecter_binary { typedef typename boost::binary_traits::first_argument_type arg_type_1st; typedef typename boost::binary_traits::second_argument_type arg_type_2nd; typedef typename boost::binary_traits::param_type param_type; typedef typename boost::binary_traits::result_type result_type; typedef typename boost::binary_traits::function_type function_type; typedef typename boost::add_const::type>::type* first_argument_type; typedef typename boost::add_const::type>::type* second_argument_type; function_type op_; public: explicit indirecter_binary( param_type op ): op_(op) {} result_type operator() (first_argument_type ptr1, second_argument_type ptr2) const { return op_(*ptr1, *ptr2); } }; // Convenience function template. // Constructs an object of type indirecter_binary while binding the operation to it. template< typename Operation > inline indirecter_binary< Operation > indirect_binary( Operation op ) { return indirecter_binary< Operation >( op ); } // This class template is an adaptor for an unary function, which will be invoked // on a dereferenced pointer. This is useful, if you need to process elements of // a collection of pointers, by invoking the operation on the pointees, e.g. sorting // a list of pointers by a predicate which applies to the pointees. // Both function pointers and function objects may be passed to the constructor. template< typename Operation > class indirecter_unary { typedef typename boost::unary_traits::argument_type arg_type; typedef typename boost::unary_traits::param_type param_type; typedef typename boost::unary_traits::result_type result_type; typedef typename boost::unary_traits::function_type function_type; typedef typename boost::add_const::type>::type* argument_type; function_type op_; public: explicit indirecter_unary( param_type op ): op_(op) {} result_type operator() (argument_type ptr) const { return op_(*ptr); } }; // Convenience function template. // Constructs an object of type indirecter_unary while binding the operation to it. template< typename Operation > inline indirecter_unary< Operation > indirect_unary( Operation op ) { return indirecter_unary< Operation >( op ); } } // EOF