Thread: [Cppunit-cvs] cppunit2/include/cpptl functor.h,1.1,1.2 functor.py,1.1,1.2
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-02-26 11:41:07
|
Update of /cvsroot/cppunit/cppunit2/include/cpptl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11910/include/cpptl Modified Files: functor.h functor.py Log Message: * simpler implementation of functors. * complete rewrote of the generator (much easier to maintain) Index: functor.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/functor.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** functor.h 25 Feb 2005 20:53:56 -0000 1.1 --- functor.h 26 Feb 2005 11:40:58 -0000 1.2 *************** *** 8,11 **** --- 8,46 ---- namespace CppTL { + // Partially borrowed from the standard + // See: + // N1453: http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1453.html + template<class T> + class ReferenceWrapper + { + public: + explicit ReferenceWrapper( T &t ) : t_( &t ) + { + } + + operator T&() const + { + return *t_; + } + + private: + T *t_; + }; + + template<class T> + inline ReferenceWrapper<T> + ref( T &t ) + { + return ReferenceWrapper<T>( t ); + } + + template<class T> + inline ReferenceWrapper<const T> + cref( const T& t ) + { + return ReferenceWrapper<const T>( t ); + } + + namespace Impl { *************** *** 84,87 **** --- 119,146 ---- + template< class Functor, class BindArg > + class Bind0 + { + public: + typedef void result_type; + + + Bind0( Functor functor, BindArg arg ) + : functor_( functor ) + , arg_( arg ) + { + } + + result_type operator()( ) const + { + functor_( arg_ ); + } + private: + Functor functor_; + BindArg arg_; + }; + + + template< class Return > class FunctorBase0R : public FunctorBase *************** *** 148,151 **** --- 207,234 ---- + template< class Functor, class Return, class BindArg > + class Bind0R + { + public: + typedef Return result_type; + + + Bind0R( Functor functor, BindArg arg ) + : functor_( functor ) + , arg_( arg ) + { + } + + result_type operator()( ) const + { + return functor_( arg_ ); + } + private: + Functor functor_; + BindArg arg_; + }; + + + template< class Arg1 > class FunctorBase1 : public FunctorBase *************** *** 212,215 **** --- 295,324 ---- + template< class Functor, class Arg1, class BindArg > + class Bind1 + { + public: + typedef void result_type; + typedef Arg1 arg1_type; + typedef Arg1 first_argument_type; + + + Bind1( Functor functor, BindArg arg ) + : functor_( functor ) + , arg_( arg ) + { + } + + result_type operator()( Arg1 a1 ) const + { + functor_( a1, arg_ ); + } + private: + Functor functor_; + BindArg arg_; + }; + + + template< class Return, class Arg1 > class FunctorBase1R : public FunctorBase *************** *** 276,279 **** --- 385,598 ---- + template< class Functor, class Return, class Arg1, class BindArg > + class Bind1R + { + public: + typedef Return result_type; + typedef Arg1 arg1_type; + typedef Arg1 first_argument_type; + + + Bind1R( Functor functor, BindArg arg ) + : functor_( functor ) + , arg_( arg ) + { + } + + result_type operator()( Arg1 a1 ) const + { + return functor_( a1, arg_ ); + } + private: + Functor functor_; + BindArg arg_; + }; + + + + template< class Arg1, class Arg2 > + class FunctorBase2 : public FunctorBase + { + public: + virtual void operator()( Arg1 a1, Arg2 a2 ) const = 0; + }; + + + template< class Functor, class Arg1, class Arg2 > + class GenericFunctor2 : public FunctorBase2< Arg1, Arg2 > + { + public: + typedef GenericFunctor2< Functor, Arg1, Arg2 > SelfType; + + GenericFunctor2( const Functor &functor ) + : functor_( functor ) + { + } + + void operator()( Arg1 a1, Arg2 a2 ) const + { + functor_( a1, a2 ); + } + + FunctorBase *clone() const + { + return new SelfType( *this ); + } + + private: + Functor functor_; + }; + + + template< class Holder, class Object, class Arg1, class Arg2 > + class MemberFunctor2 : public FunctorBase2< Arg1, Arg2 > + { + public: + typedef MemberFunctor2< Holder, Object, Arg1, Arg2 > SelfType; + typedef void (Object::*MemberFn)( Arg1 a1, Arg2 a2 ); + + MemberFunctor2( const Holder &holder, MemberFn member ) + : holder_( holder ) + , member_( member ) + { + } + + void operator()( Arg1 a1, Arg2 a2 ) const + { + Object &object = *holder_; + (object.*member_)( a1, a2 ); + } + + FunctorBase *clone() const + { + return new SelfType( *this ); + } + + private: + Holder holder_; + MemberFn member_; + }; + + + template< class Functor, class Arg1, class Arg2, class BindArg > + class Bind2 + { + public: + typedef void result_type; + typedef Arg1 arg1_type; + typedef Arg1 first_argument_type; + typedef Arg2 arg2_type; + typedef Arg2 second_argument_type; + + + Bind2( Functor functor, BindArg arg ) + : functor_( functor ) + , arg_( arg ) + { + } + + result_type operator()( Arg1 a1, Arg2 a2 ) const + { + functor_( a1, a2, arg_ ); + } + private: + Functor functor_; + BindArg arg_; + }; + + + + template< class Return, class Arg1, class Arg2 > + class FunctorBase2R : public FunctorBase + { + public: + virtual Return operator()( Arg1 a1, Arg2 a2 ) const = 0; + }; + + + template< class Functor, class Return, class Arg1, class Arg2 > + class GenericFunctor2R : public FunctorBase2R< Return, Arg1, Arg2 > + { + public: + typedef GenericFunctor2R< Functor, Return, Arg1, Arg2 > SelfType; + + GenericFunctor2R( const Functor &functor ) + : functor_( functor ) + { + } + + Return operator()( Arg1 a1, Arg2 a2 ) const + { + return functor_( a1, a2 ); + } + + FunctorBase *clone() const + { + return new SelfType( *this ); + } + + private: + Functor functor_; + }; + + + template< class Holder, class Object, class Return, class Arg1, class Arg2 > + class MemberFunctor2R : public FunctorBase2R< Return, Arg1, Arg2 > + { + public: + typedef MemberFunctor2R< Holder, Object, Return, Arg1, Arg2 > SelfType; + typedef Return (Object::*MemberFn)( Arg1 a1, Arg2 a2 ); + + MemberFunctor2R( const Holder &holder, MemberFn member ) + : holder_( holder ) + , member_( member ) + { + } + + Return operator()( Arg1 a1, Arg2 a2 ) const + { + Object &object = *holder_; + return (object.*member_)( a1, a2 ); + } + + FunctorBase *clone() const + { + return new SelfType( *this ); + } + + private: + Holder holder_; + MemberFn member_; + }; + + + template< class Functor, class Return, class Arg1, class Arg2, class BindArg > + class Bind2R + { + public: + typedef Return result_type; + typedef Arg1 arg1_type; + typedef Arg1 first_argument_type; + typedef Arg2 arg2_type; + typedef Arg2 second_argument_type; + + + Bind2R( Functor functor, BindArg arg ) + : functor_( functor ) + , arg_( arg ) + { + } + + result_type operator()( Arg1 a1, Arg2 a2 ) const + { + return functor_( a1, a2, arg_ ); + } + private: + Functor functor_; + BindArg arg_; + }; + + + *************** *** 510,513 **** --- 829,926 ---- + template< class Arg1, class Arg2 > + class Functor2 : public FunctorCommon + { + public: + typedef Functor2< Arg1, Arg2 > SelfType; + typedef Impl::FunctorBase2< Arg1, Arg2 > FunctorImplType; + typedef void result_type; + typedef Arg1 arg1_type; + typedef Arg1 first_argument_type; + typedef Arg2 arg2_type; + typedef Arg2 second_argument_type; + + + Functor2() + { + } + + explicit Functor2( FunctorImplType *impl ) + : FunctorCommon( impl ) + { + } + + Functor2( const SelfType &other ) + : FunctorCommon( other ) + { + } + + SelfType &operator=( const SelfType &other ) + { + SelfType temp( other ); + swap( temp ); + return *this; + } + + result_type operator()( Arg1 a1, Arg2 a2 ) const + { + FunctorImplType &impl = *( static_cast<FunctorImplType *>(impl_) ); + return impl( a1, a2 ); + } + + void swap( SelfType &other ) + { + FunctorCommon::swap( other ); + } + }; + + + template< class Return, class Arg1, class Arg2 > + class Functor2R : public FunctorCommon + { + public: + typedef Functor2R< Return, Arg1, Arg2 > SelfType; + typedef Impl::FunctorBase2R< Return, Arg1, Arg2 > FunctorImplType; + typedef Return result_type; + typedef Arg1 arg1_type; + typedef Arg1 first_argument_type; + typedef Arg2 arg2_type; + typedef Arg2 second_argument_type; + + + Functor2R() + { + } + + explicit Functor2R( FunctorImplType *impl ) + : FunctorCommon( impl ) + { + } + + Functor2R( const SelfType &other ) + : FunctorCommon( other ) + { + } + + SelfType &operator=( const SelfType &other ) + { + SelfType temp( other ); + swap( temp ); + return *this; + } + + result_type operator()( Arg1 a1, Arg2 a2 ) const + { + FunctorImplType &impl = *( static_cast<FunctorImplType *>(impl_) ); + return impl( a1, a2 ); + } + + void swap( SelfType &other ) + { + FunctorCommon::swap( other ); + } + }; + + *************** *** 536,540 **** } ! // Functor0R --- 949,960 ---- } ! template<class Functor, class BindArg> ! inline Impl::Bind0<Functor ! ,BindArg > ! bind1( Functor functor, BindArg arg ) ! { ! return Impl::Bind0<Functor ! ,BindArg >( functor, arg ); ! } // Functor0R *************** *** 562,566 **** } ! // Functor1 --- 982,995 ---- } ! template<class Functor, class BindArg> ! inline Impl::Bind0R<Functor ! ,CPPTL_TYPENAME Functor::result_type ! ,BindArg > ! bind1r( Functor functor, BindArg arg ) ! { ! return Impl::Bind0R<Functor ! ,CPPTL_TYPENAME Functor::result_type ! ,BindArg >( functor, arg ); ! } // Functor1 *************** *** 588,592 **** } ! // Functor1R --- 1017,1030 ---- } ! template<class Functor, class BindArg> ! inline Impl::Bind1<Functor ! ,CPPTL_TYPENAME Functor::first_argument_type ! ,BindArg > ! bind2( Functor functor, BindArg arg ) ! { ! return Impl::Bind1<Functor ! ,CPPTL_TYPENAME Functor::first_argument_type ! ,BindArg >( functor, arg ); ! } // Functor1R *************** *** 616,620 **** --- 1054,1151 ---- } + template<class Functor, class BindArg> + inline Impl::Bind1R<Functor + ,CPPTL_TYPENAME Functor::result_type + ,CPPTL_TYPENAME Functor::first_argument_type + ,BindArg > + bind2r( Functor functor, BindArg arg ) + { + return Impl::Bind1R<Functor + ,CPPTL_TYPENAME Functor::result_type + ,CPPTL_TYPENAME Functor::first_argument_type + ,BindArg >( functor, arg ); + } + + // Functor2 + + template< class Arg1, class Arg2 > + inline Functor2< Arg1, Arg2 > cfn2( void (*function)( Arg1 a1, Arg2 a2 ) ) + { + typedef void (*Functor)(Arg1,Arg2); + return Functor2< Arg1, Arg2 >( new Impl::GenericFunctor2< Functor, Arg1, Arg2 >( function ) ); + } + + template< class Holder, class Object, class Arg1, class Arg2 > + inline Functor2< Arg1, Arg2 > + memfn2( const Holder &holder, void (Object::*member)( Arg1 a1, Arg2 a2 ) ) + { + return Functor2< Arg1, Arg2 >( new Impl::MemberFunctor2< Holder, Object, Arg1, Arg2 >( holder, member ) ); + } + + template<class Functor> + inline Functor2< CPPTL_TYPENAME Functor::first_argument_type + ,CPPTL_TYPENAME Functor::second_argument_type > + fn2( Functor functor ) + { + typedef CPPTL_TYPENAME Functor::first_argument_type Arg1; + typedef CPPTL_TYPENAME Functor::second_argument_type Arg2; + return Functor2< Arg1, Arg2 >( new Impl::GenericFunctor2< Functor, Arg1, Arg2 >( functor ) ); + } + + template<class Functor, class BindArg> + inline Impl::Bind2<Functor + ,CPPTL_TYPENAME Functor::first_argument_type + ,CPPTL_TYPENAME Functor::second_argument_type + ,BindArg > + bind3( Functor functor, BindArg arg ) + { + return Impl::Bind2<Functor + ,CPPTL_TYPENAME Functor::first_argument_type + ,CPPTL_TYPENAME Functor::second_argument_type + ,BindArg >( functor, arg ); + } + + // Functor2R + + template< class Return, class Arg1, class Arg2 > + inline Functor2R< Return, Arg1, Arg2 > cfn2r( Return (*function)( Arg1 a1, Arg2 a2 ) ) + { + typedef Return (*Functor)(Arg1,Arg2); + return Functor2R< Return, Arg1, Arg2 >( new Impl::GenericFunctor2R< Functor, Return, Arg1, Arg2 >( function ) ); + } + template< class Holder, class Object, class Return, class Arg1, class Arg2 > + inline Functor2R< Return, Arg1, Arg2 > + memfn2r( const Holder &holder, Return (Object::*member)( Arg1 a1, Arg2 a2 ) ) + { + return Functor2R< Return, Arg1, Arg2 >( new Impl::MemberFunctor2R< Holder, Object, Return, Arg1, Arg2 >( holder, member ) ); + } + + template<class Functor> + inline Functor2R< CPPTL_TYPENAME Functor::result_type + ,CPPTL_TYPENAME Functor::first_argument_type + ,CPPTL_TYPENAME Functor::second_argument_type > + fn2r( Functor functor ) + { + typedef CPPTL_TYPENAME Functor::result_type Return; + typedef CPPTL_TYPENAME Functor::first_argument_type Arg1; + typedef CPPTL_TYPENAME Functor::second_argument_type Arg2; + return Functor2R< Return, Arg1, Arg2 >( new Impl::GenericFunctor2R< Functor, Return, Arg1, Arg2 >( functor ) ); + } + + template<class Functor, class BindArg> + inline Impl::Bind2R<Functor + ,CPPTL_TYPENAME Functor::result_type + ,CPPTL_TYPENAME Functor::first_argument_type + ,CPPTL_TYPENAME Functor::second_argument_type + ,BindArg > + bind3r( Functor functor, BindArg arg ) + { + return Impl::Bind2R<Functor + ,CPPTL_TYPENAME Functor::result_type + ,CPPTL_TYPENAME Functor::first_argument_type + ,CPPTL_TYPENAME Functor::second_argument_type + ,BindArg >( functor, arg ); + } Index: functor.py =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/functor.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** functor.py 25 Feb 2005 20:53:57 -0000 1.1 --- functor.py 26 Feb 2005 11:40:58 -0000 1.2 *************** *** 1,4 **** --- 1,6 ---- # script to generate cpptl/functor.h + MAX_ARGUMENT_COUNT = 2 + header =\ """// This script is generated by the python script functor.py *************** *** 11,14 **** --- 13,51 ---- namespace CppTL { + // Partially borrowed from the standard + // See: + // N1453: http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1453.html + template<class T> + class ReferenceWrapper + { + public: + explicit ReferenceWrapper( T &t ) : t_( &t ) + { + } + + operator T&() const + { + return *t_; + } + + private: + T *t_; + }; + + template<class T> + inline ReferenceWrapper<T> + ref( T &t ) + { + return ReferenceWrapper<T>( t ); + } + + template<class T> + inline ReferenceWrapper<const T> + cref( const T& t ) + { + return ReferenceWrapper<const T>( t ); + } + + namespace Impl { *************** *** 180,183 **** --- 217,249 ---- # member_functor_instantiation : MemberFunctor1R<HolderType,ObjectType,ReturnType,Arg1Type> + bind_functor = \ + """ %(bind_functor_template_decl)s + class %(bind_functor)s + { + public: + typedef %(return_type)s result_type; + %(argument_types_typedef)s + + %(bind_functor)s( Functor functor, BindArg arg ) + : functor_( functor ) + , arg_( arg ) + { + } + + result_type operator()( %(fn_parameters)s ) const + { + %(return_keyword)sfunctor_( %(bind_fn_call)s ); + } + private: + Functor functor_; + BindArg arg_; + }; + + + + """ + # bind_functor : BindR1 + # bind_functor_template_decl : template<class Functor,class ReturnType, class Arg1, class BindArg> + # bind_fn_call: a1, arg_ functor = \ *************** *** 259,263 **** } ! """ --- 325,334 ---- } ! template<class Functor, class BindArg> ! inline Impl::%(bind_functor)s%(bind_functor_deduced_types)s ! %(bind)s( Functor functor, BindArg arg ) ! { ! return Impl::%(bind_functor)s%(bind_functor_deduced_types)s( functor, arg ); ! } """ *************** *** 265,269 **** # fn_types : Arg1 # memfn : memfn1r ! # fn : fnr1 # functor_deduced_types : # CPPTL_TYPENAME Functor::result_type --- 336,340 ---- # fn_types : Arg1 # memfn : memfn1r ! # fn : fn1r # functor_deduced_types : # CPPTL_TYPENAME Functor::result_type *************** *** 272,275 **** --- 343,351 ---- # typedef CPPTL_TYPENAME Functor::result_type Return; # typedef CPPTL_TYPENAME Functor::first_argument_type Arg1; + # bind : bind1r + # bind_functor_deduced_types : + # CPPTL_TYPENAME Functor::result_type + # ,CPPTL_TYPENAME Functor::first_argument_type + # ,BindArg *************** *** 304,307 **** --- 380,387 ---- self.functor_template_decl = self.makeTemplateDecl( base_template_parameters ) self.functor_instantiation = self.functor + self.makeTemplateInstantiation( base_template_parameters ) + self.bind_functor = 'Bind%d%s' % (count,return_suffix) + bind_functor_template_parameters = ['Functor'] + base_template_parameters + ['BindArg'] + self.bind_functor_template_decl = self.makeTemplateDecl( bind_functor_template_parameters ) + self.bind_fn_call = ', '.join( [ 'a%d' % n for n in xrange(1,count+1) ] + ['arg_'] ) self.argument_types_typedef = '' for n in xrange(1,count+1): *************** *** 310,313 **** --- 390,394 ---- self.memfn = 'memfn%d%s' % (count,return_suffix.lower()) self.fn = 'fn%d%s' % (count,return_suffix.lower()) + self.bind = 'bind%d%s' % (count+1,return_suffix.lower()) self.fn_types = ','.join( [ 'Arg%d' % n for n in xrange(1,count+1) ] ) functor_deduced_types = [] *************** *** 327,330 **** --- 408,413 ---- (self.getFunctorArgTypeName(n),n) ) self.functor_deduced_type_alias = '\n'.join( functor_deduced_type_alias ) + bind_functor_deduced_types = ['Functor'] + functor_deduced_types + ['BindArg'] + self.bind_functor_deduced_types = '<' + '\n ,'.join( bind_functor_deduced_types ) + ' >' def __getitem__( self, key ): *************** *** 361,365 **** functor_generators = '' ! for argument_count in xrange(0,2): for is_void in (True,False): parameters = GenerationParameters( argument_count, is_void ) --- 444,448 ---- functor_generators = '' ! for argument_count in xrange(0,MAX_ARGUMENT_COUNT+1): for is_void in (True,False): parameters = GenerationParameters( argument_count, is_void ) *************** *** 367,370 **** --- 450,454 ---- functors_impl += generic_functor % parameters functors_impl += member_functor % parameters + functors_impl += bind_functor % parameters functors += functor % parameters functor_generators += generator % parameters |