Thread: [Cppunit-cvs] cppunit2/include/cpptl functor.h,NONE,1.1 functor.py,NONE,1.1
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-02-25 20:54:08
|
Update of /cvsroot/cppunit/cppunit2/include/cpptl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8256 Added Files: functor.h functor.py Log Message: * simpler implementation of functors. * complete rewrote of the generator (much easier to maintain) --- NEW FILE: functor.h --- // This script is generated by the python script functor.py // Do not edit. #ifndef CPPTL_FUNCTOR_H_INCLUDED # define CPPTL_FUNCTOR_H_INCLUDED # include <cpptl/config.h> namespace CppTL { namespace Impl { class FunctorBase { public: virtual ~FunctorBase() { } virtual FunctorBase *clone() const = 0; }; class FunctorBase0 : public FunctorBase { public: virtual void operator()( ) const = 0; }; template< class Functor > class GenericFunctor0 : public FunctorBase0 { public: typedef GenericFunctor0< Functor > SelfType; GenericFunctor0( const Functor &functor ) : functor_( functor ) { } void operator()( ) const { functor_( ); } FunctorBase *clone() const { return new SelfType( *this ); } private: Functor functor_; }; template< class Holder, class Object > class MemberFunctor0 : public FunctorBase0 { public: typedef MemberFunctor0< Holder, Object > SelfType; typedef void (Object::*MemberFn)( ); MemberFunctor0( const Holder &holder, MemberFn member ) : holder_( holder ) , member_( member ) { } void operator()( ) const { Object &object = *holder_; (object.*member_)( ); } FunctorBase *clone() const { return new SelfType( *this ); } private: Holder holder_; MemberFn member_; }; template< class Return > class FunctorBase0R : public FunctorBase { public: virtual Return operator()( ) const = 0; }; template< class Functor, class Return > class GenericFunctor0R : public FunctorBase0R< Return > { public: typedef GenericFunctor0R< Functor, Return > SelfType; GenericFunctor0R( const Functor &functor ) : functor_( functor ) { } Return operator()( ) const { return functor_( ); } FunctorBase *clone() const { return new SelfType( *this ); } private: Functor functor_; }; template< class Holder, class Object, class Return > class MemberFunctor0R : public FunctorBase0R< Return > { public: typedef MemberFunctor0R< Holder, Object, Return > SelfType; typedef Return (Object::*MemberFn)( ); MemberFunctor0R( const Holder &holder, MemberFn member ) : holder_( holder ) , member_( member ) { } Return operator()( ) const { Object &object = *holder_; return (object.*member_)( ); } FunctorBase *clone() const { return new SelfType( *this ); } private: Holder holder_; MemberFn member_; }; template< class Arg1 > class FunctorBase1 : public FunctorBase { public: virtual void operator()( Arg1 a1 ) const = 0; }; template< class Functor, class Arg1 > class GenericFunctor1 : public FunctorBase1< Arg1 > { public: typedef GenericFunctor1< Functor, Arg1 > SelfType; GenericFunctor1( const Functor &functor ) : functor_( functor ) { } void operator()( Arg1 a1 ) const { functor_( a1 ); } FunctorBase *clone() const { return new SelfType( *this ); } private: Functor functor_; }; template< class Holder, class Object, class Arg1 > class MemberFunctor1 : public FunctorBase1< Arg1 > { public: typedef MemberFunctor1< Holder, Object, Arg1 > SelfType; typedef void (Object::*MemberFn)( Arg1 a1 ); MemberFunctor1( const Holder &holder, MemberFn member ) : holder_( holder ) , member_( member ) { } void operator()( Arg1 a1 ) const { Object &object = *holder_; (object.*member_)( a1 ); } FunctorBase *clone() const { return new SelfType( *this ); } private: Holder holder_; MemberFn member_; }; template< class Return, class Arg1 > class FunctorBase1R : public FunctorBase { public: virtual Return operator()( Arg1 a1 ) const = 0; }; template< class Functor, class Return, class Arg1 > class GenericFunctor1R : public FunctorBase1R< Return, Arg1 > { public: typedef GenericFunctor1R< Functor, Return, Arg1 > SelfType; GenericFunctor1R( const Functor &functor ) : functor_( functor ) { } Return operator()( Arg1 a1 ) const { return functor_( a1 ); } FunctorBase *clone() const { return new SelfType( *this ); } private: Functor functor_; }; template< class Holder, class Object, class Return, class Arg1 > class MemberFunctor1R : public FunctorBase1R< Return, Arg1 > { public: typedef MemberFunctor1R< Holder, Object, Return, Arg1 > SelfType; typedef Return (Object::*MemberFn)( Arg1 a1 ); MemberFunctor1R( const Holder &holder, MemberFn member ) : holder_( holder ) , member_( member ) { } Return operator()( Arg1 a1 ) const { Object &object = *holder_; return (object.*member_)( a1 ); } FunctorBase *clone() const { return new SelfType( *this ); } private: Holder holder_; MemberFn member_; }; } // namespace Impl class FunctorCommon { public: ~FunctorCommon() { delete impl_; } bool empty() const { return impl_ == 0; } operator bool() const { return impl_ != 0; } bool operator !() const { return impl_ == 0; } protected: FunctorCommon() : impl_( 0 ) { } FunctorCommon( Impl::FunctorBase *impl ) : impl_( impl ) { } FunctorCommon( const FunctorCommon &other ) : impl_( other.impl_ ? other.impl_->clone() : 0 ) { } void swap( FunctorCommon &other ) { Impl::FunctorBase *temp = impl_; impl_ = other.impl_; other.impl_ = temp; } Impl::FunctorBase *impl_; private: void operator =( const FunctorCommon &other ); }; class Functor0 : public FunctorCommon { public: typedef Functor0 SelfType; typedef Impl::FunctorBase0 FunctorImplType; typedef void result_type; Functor0() { } explicit Functor0( FunctorImplType *impl ) : FunctorCommon( impl ) { } Functor0( const SelfType &other ) : FunctorCommon( other ) { } SelfType &operator=( const SelfType &other ) { SelfType temp( other ); swap( temp ); return *this; } result_type operator()( ) const { FunctorImplType &impl = *( static_cast<FunctorImplType *>(impl_) ); return impl( ); } void swap( SelfType &other ) { FunctorCommon::swap( other ); } }; template< class Return > class Functor0R : public FunctorCommon { public: typedef Functor0R< Return > SelfType; typedef Impl::FunctorBase0R< Return > FunctorImplType; typedef Return result_type; Functor0R() { } explicit Functor0R( FunctorImplType *impl ) : FunctorCommon( impl ) { } Functor0R( const SelfType &other ) : FunctorCommon( other ) { } SelfType &operator=( const SelfType &other ) { SelfType temp( other ); swap( temp ); return *this; } result_type operator()( ) const { FunctorImplType &impl = *( static_cast<FunctorImplType *>(impl_) ); return impl( ); } void swap( SelfType &other ) { FunctorCommon::swap( other ); } }; template< class Arg1 > class Functor1 : public FunctorCommon { public: typedef Functor1< Arg1 > SelfType; typedef Impl::FunctorBase1< Arg1 > FunctorImplType; typedef void result_type; typedef Arg1 arg1_type; typedef Arg1 first_argument_type; Functor1() { } explicit Functor1( FunctorImplType *impl ) : FunctorCommon( impl ) { } Functor1( const SelfType &other ) : FunctorCommon( other ) { } SelfType &operator=( const SelfType &other ) { SelfType temp( other ); swap( temp ); return *this; } result_type operator()( Arg1 a1 ) const { FunctorImplType &impl = *( static_cast<FunctorImplType *>(impl_) ); return impl( a1 ); } void swap( SelfType &other ) { FunctorCommon::swap( other ); } }; template< class Return, class Arg1 > class Functor1R : public FunctorCommon { public: typedef Functor1R< Return, Arg1 > SelfType; typedef Impl::FunctorBase1R< Return, Arg1 > FunctorImplType; typedef Return result_type; typedef Arg1 arg1_type; typedef Arg1 first_argument_type; Functor1R() { } explicit Functor1R( FunctorImplType *impl ) : FunctorCommon( impl ) { } Functor1R( const SelfType &other ) : FunctorCommon( other ) { } SelfType &operator=( const SelfType &other ) { SelfType temp( other ); swap( temp ); return *this; } result_type operator()( Arg1 a1 ) const { FunctorImplType &impl = *( static_cast<FunctorImplType *>(impl_) ); return impl( a1 ); } void swap( SelfType &other ) { FunctorCommon::swap( other ); } }; // Functor0 inline Functor0 cfn0( void (*function)( ) ) { typedef void (*Functor)(); return Functor0( new Impl::GenericFunctor0< Functor >( function ) ); } template< class Holder, class Object > inline Functor0 memfn0( const Holder &holder, void (Object::*member)( ) ) { return Functor0( new Impl::MemberFunctor0< Holder, Object >( holder, member ) ); } template<class Functor> inline Functor0 fn0( Functor functor ) { return Functor0( new Impl::GenericFunctor0< Functor >( functor ) ); } // Functor0R template< class Return > inline Functor0R< Return > cfn0r( Return (*function)( ) ) { typedef Return (*Functor)(); return Functor0R< Return >( new Impl::GenericFunctor0R< Functor, Return >( function ) ); } template< class Holder, class Object, class Return > inline Functor0R< Return > memfn0r( const Holder &holder, Return (Object::*member)( ) ) { return Functor0R< Return >( new Impl::MemberFunctor0R< Holder, Object, Return >( holder, member ) ); } template<class Functor> inline Functor0R< CPPTL_TYPENAME Functor::result_type > fn0r( Functor functor ) { typedef CPPTL_TYPENAME Functor::result_type Return; return Functor0R< Return >( new Impl::GenericFunctor0R< Functor, Return >( functor ) ); } // Functor1 template< class Arg1 > inline Functor1< Arg1 > cfn1( void (*function)( Arg1 a1 ) ) { typedef void (*Functor)(Arg1); return Functor1< Arg1 >( new Impl::GenericFunctor1< Functor, Arg1 >( function ) ); } template< class Holder, class Object, class Arg1 > inline Functor1< Arg1 > memfn1( const Holder &holder, void (Object::*member)( Arg1 a1 ) ) { return Functor1< Arg1 >( new Impl::MemberFunctor1< Holder, Object, Arg1 >( holder, member ) ); } template<class Functor> inline Functor1< CPPTL_TYPENAME Functor::first_argument_type > fn1( Functor functor ) { typedef CPPTL_TYPENAME Functor::first_argument_type Arg1; return Functor1< Arg1 >( new Impl::GenericFunctor1< Functor, Arg1 >( functor ) ); } // Functor1R template< class Return, class Arg1 > inline Functor1R< Return, Arg1 > cfn1r( Return (*function)( Arg1 a1 ) ) { typedef Return (*Functor)(Arg1); return Functor1R< Return, Arg1 >( new Impl::GenericFunctor1R< Functor, Return, Arg1 >( function ) ); } template< class Holder, class Object, class Return, class Arg1 > inline Functor1R< Return, Arg1 > memfn1r( const Holder &holder, Return (Object::*member)( Arg1 a1 ) ) { return Functor1R< Return, Arg1 >( new Impl::MemberFunctor1R< Holder, Object, Return, Arg1 >( holder, member ) ); } template<class Functor> inline Functor1R< CPPTL_TYPENAME Functor::result_type ,CPPTL_TYPENAME Functor::first_argument_type > fn1r( Functor functor ) { typedef CPPTL_TYPENAME Functor::result_type Return; typedef CPPTL_TYPENAME Functor::first_argument_type Arg1; return Functor1R< Return, Arg1 >( new Impl::GenericFunctor1R< Functor, Return, Arg1 >( functor ) ); } } // namespace CppTL #endif // CPPTL_FUNCTOR_H_INCLUDED --- NEW FILE: functor.py --- # script to generate cpptl/functor.h header =\ """// This script is generated by the python script functor.py // Do not edit. #ifndef CPPTL_FUNCTOR_H_INCLUDED # define CPPTL_FUNCTOR_H_INCLUDED # include <cpptl/config.h> namespace CppTL { namespace Impl { class FunctorBase { public: virtual ~FunctorBase() { } virtual FunctorBase *clone() const = 0; }; %(functors_impl)s } // namespace Impl class FunctorCommon { public: ~FunctorCommon() { delete impl_; } bool empty() const { return impl_ == 0; } operator bool() const { return impl_ != 0; } bool operator !() const { return impl_ == 0; } protected: FunctorCommon() : impl_( 0 ) { } FunctorCommon( Impl::FunctorBase *impl ) : impl_( impl ) { } FunctorCommon( const FunctorCommon &other ) : impl_( other.impl_ ? other.impl_->clone() : 0 ) { } void swap( FunctorCommon &other ) { Impl::FunctorBase *temp = impl_; impl_ = other.impl_; other.impl_ = temp; } Impl::FunctorBase *impl_; private: void operator =( const FunctorCommon &other ); }; %(functors)s %(functor_generators)s } // namespace CppTL #endif // CPPTL_FUNCTOR_H_INCLUDED """ functor_base =\ """ %(base_template_decl)s class %(impl_base)s : public FunctorBase { public: virtual %(return_type)s operator()( %(fn_parameters)s ) const = 0; }; """ # base_template_decl : template<class ReturnType, class Arg1Type> # impl_base : Functor1BaseR # return_type : ReturnType # fn_parameters: Arg1 a1 generic_functor = \ """ %(generic_functor_template_decl)s class %(generic_functor)s : public %(impl_base_instantiation)s { public: typedef %(generic_functor_instantiation)s SelfType; %(generic_functor)s( const Functor &functor ) : functor_( functor ) { } %(return_type)s operator()( %(fn_parameters)s ) const { %(return_keyword)sfunctor_( %(fn_call)s ); } FunctorBase *clone() const { return new SelfType( *this ); } private: Functor functor_; }; """ # generic_functor_template_decl : template<class Functor, class ReturnType, class Arg1Type> # generic_functor : GenericFunctor1R # impl_base_instantiation : Functor1BaseR<ReturnType,Arg1Type> # generic_functor_instantiation : GenericFunctor1R<Functor,ReturnType,Arg1Type> # return_keyword : return # fn_call : a1 member_functor = \ """ %(member_functor_template_decl)s class %(member_functor)s : public %(impl_base_instantiation)s { public: typedef %(member_functor_instantiation)s SelfType; typedef %(return_type)s (Object::*MemberFn)( %(fn_parameters)s ); %(member_functor)s( const Holder &holder, MemberFn member ) : holder_( holder ) , member_( member ) { } %(return_type)s operator()( %(fn_parameters)s ) const { Object &object = *holder_; %(return_keyword)s(object.*member_)( %(fn_call)s ); } FunctorBase *clone() const { return new SelfType( *this ); } private: Holder holder_; MemberFn member_; }; """ # member_functor_template_decl : template<class HolderType, class ObjectType, class ReturnType, class Arg1Type> # member_functor : MemberFunctor1R # member_functor_instantiation : MemberFunctor1R<HolderType,ObjectType,ReturnType,Arg1Type> functor = \ """%(functor_template_decl)s class %(functor)s : public FunctorCommon { public: typedef %(functor_instantiation)s SelfType; typedef Impl::%(impl_base_instantiation)s FunctorImplType; typedef %(return_type)s result_type; %(argument_types_typedef)s %(functor)s() { } explicit %(functor)s( FunctorImplType *impl ) : FunctorCommon( impl ) { } %(functor)s( const SelfType &other ) : FunctorCommon( other ) { } SelfType &operator=( const SelfType &other ) { SelfType temp( other ); swap( temp ); return *this; } result_type operator()( %(fn_parameters)s ) const { FunctorImplType &impl = *( static_cast<FunctorImplType *>(impl_) ); return impl( %(fn_call)s ); } void swap( SelfType &other ) { FunctorCommon::swap( other ); } }; """ # functor_template_decl : template<class ReturnType, class Arg1Type> # functor : Functor1R # functor_instantiation : Functor1R<ReturnType,Arg1Type> # argument_types_typedef : # typedef Arg1Type first_argument_type; # typedef Arg1Type arg1_type; generator = \ """// %(functor)s %(functor_template_decl)s inline %(functor_instantiation)s %(cfn)s( %(return_type)s (*function)( %(fn_parameters)s ) ) { typedef %(return_type)s (*Functor)(%(fn_types)s); return %(functor_instantiation)s( new Impl::%(generic_functor_instantiation)s( function ) ); } %(member_functor_template_decl)s inline %(functor_instantiation)s %(memfn)s( const Holder &holder, %(return_type)s (Object::*member)( %(fn_parameters)s ) ) { return %(functor_instantiation)s( new Impl::%(member_functor_instantiation)s( holder, member ) ); } template<class Functor> inline %(functor)s%(functor_deduced_types)s %(fn)s( Functor functor ) { %(functor_deduced_type_alias)s return %(functor_instantiation)s( new Impl::%(generic_functor_instantiation)s( functor ) ); } """ # cfn : cfn1r # fn_types : Arg1 # memfn : memfn1r # fn : fnr1 # functor_deduced_types : # CPPTL_TYPENAME Functor::result_type # ,CPPTL_TYPENAME Functor::first_argument_type # functor_deduced_type_alias : # typedef CPPTL_TYPENAME Functor::result_type Return; # typedef CPPTL_TYPENAME Functor::first_argument_type Arg1; class GenerationParameters(object): def __init__( self, count, is_void ): self.count = count self.is_void = is_void self.return_type = (is_void and 'void') or 'Return' self.fn_parameters = ', '.join( [ 'Arg%d a%d' % (n,n) for n in xrange(1,count+1) ] ) self.fn_call = ', '.join( [ 'a%d' % n for n in xrange(1,count+1) ] ) base_template_parameters = [] return_suffix = (not is_void and 'R') or '' self.impl_base = 'FunctorBase%d%s' % (count,return_suffix) self.return_keyword = (not is_void and 'return ') or '' if not is_void: base_template_parameters.append( 'Return' ) base_template_parameters.extend( [ 'Arg%d' % n for n in xrange(1,count+1) ] ) self.base_template_decl = self.makeTemplateDecl( base_template_parameters ) self.impl_base_instantiation = self.impl_base + self.makeTemplateInstantiation( base_template_parameters ) generic_functor_template_parameters = ['Functor'] + base_template_parameters self.generic_functor_template_decl = self.makeTemplateDecl( generic_functor_template_parameters ) self.generic_functor = 'GenericFunctor%d%s' % (count,return_suffix) self.generic_functor_instantiation = self.generic_functor + \ self.makeTemplateInstantiation(generic_functor_template_parameters ) self.member_functor = 'MemberFunctor%d%s' % (count,return_suffix) member_functor_template_parameters = ['Holder','Object'] + base_template_parameters self.member_functor_template_decl = self.makeTemplateDecl( member_functor_template_parameters ) self.member_functor_instantiation = self.member_functor + \ self.makeTemplateInstantiation(member_functor_template_parameters ) self.functor = 'Functor%d%s' % (count,return_suffix) self.functor_template_decl = self.makeTemplateDecl( base_template_parameters ) self.functor_instantiation = self.functor + self.makeTemplateInstantiation( base_template_parameters ) self.argument_types_typedef = '' for n in xrange(1,count+1): self.argument_types_typedef += self.makeArgumentTypedef( n ) self.cfn = 'cfn%d%s' % (count,return_suffix.lower()) self.memfn = 'memfn%d%s' % (count,return_suffix.lower()) self.fn = 'fn%d%s' % (count,return_suffix.lower()) self.fn_types = ','.join( [ 'Arg%d' % n for n in xrange(1,count+1) ] ) functor_deduced_types = [] if not is_void: functor_deduced_types.append( 'CPPTL_TYPENAME Functor::result_type' ) for n in xrange(1,count+1): functor_deduced_types.append( 'CPPTL_TYPENAME Functor::%s' % self.getFunctorArgTypeName(n) ) if len(functor_deduced_types): self.functor_deduced_types = '< ' + '\n ,'.join( functor_deduced_types ) + ' >' else: self.functor_deduced_types = '' functor_deduced_type_alias = [] if not is_void: functor_deduced_type_alias.append(' typedef CPPTL_TYPENAME Functor::result_type Return;') for n in xrange(1,count+1): functor_deduced_type_alias.append( ' typedef CPPTL_TYPENAME Functor::%s Arg%d;' % (self.getFunctorArgTypeName(n),n) ) self.functor_deduced_type_alias = '\n'.join( functor_deduced_type_alias ) def __getitem__( self, key ): return getattr( self, key ) def makeTemplateDecl( self, parameters ): if not parameters: return '' return 'template< class ' + ', class '.join( parameters ) + ' >' def makeTemplateInstantiation( self, parameters ): if not parameters: return '' return '< ' + ', '.join( parameters ) + ' >' def getFunctorArgTypeName( self, n ): names = [ 'arg%d_type' % n ] if n == 1: names.append( 'first_argument_type' ) elif n == 2: names.append( 'second_argument_type' ) return names[-1] def makeArgumentTypedef( self, n ): names = [ 'arg%d_type' % n ] if n == 1: names.append( 'first_argument_type' ) elif n == 2: names.append( 'second_argument_type' ) return '\n'.join( [ ' typedef Arg%d %s;' % (n, name) for name in names ] ) + '\n' functors_impl = '' functors = '' functor_generators = '' for argument_count in xrange(0,2): for is_void in (True,False): parameters = GenerationParameters( argument_count, is_void ) functors_impl += functor_base % parameters functors_impl += generic_functor % parameters functors_impl += member_functor % parameters functors += functor % parameters functor_generators += generator % parameters final_header = header % locals() out = file( 'functor.h', 'wt') out.write( final_header ) out.close() |