[Cppunit-cvs] cppunit2/include/cpptl forwards.h,1.3,1.4 reflection.h,1.1,1.2 reflection.inl,1.1,1.2
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-03-03 20:49:08
|
Update of /cvsroot/cppunit/cppunit2/include/cpptl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9744/include/cpptl Modified Files: forwards.h reflection.h reflection.inl Log Message: * moved reflection implementation details in Impl namespace * added reflection support for attribut Index: reflection.inl =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/reflection.inl,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** reflection.inl 3 Mar 2005 08:14:23 -0000 1.1 --- reflection.inl 3 Mar 2005 20:48:54 -0000 1.2 *************** *** 2,6 **** # define CPPTL_REFLECTION_INL_INCLUDED ! namespace CppTL { namespace Reflect { // ////////////////////////////////////////////////////////////////// --- 2,6 ---- # define CPPTL_REFLECTION_INL_INCLUDED ! namespace CppTL { // ////////////////////////////////////////////////////////////////// *************** *** 66,70 **** void Class::addMethod( const ConstString &name, ! const Invokable &invokable ) { MethodPtr method( new Method( name, invokable ) ); --- 66,70 ---- void Class::addMethod( const ConstString &name, ! const Impl::Invokable &invokable ) { MethodPtr method( new Method( name, invokable ) ); *************** *** 72,75 **** --- 72,95 ---- } + inline + const Attribut * + Class::findAttribut( const ConstString &name ) const + { + AttributsByName::const_iterator it = attributsByName_.find( name ); + if ( it == attributsByName_.end() ) + return 0; + return it->second.get(); + } + + + inline + void + Class::addAttribut( const ConstString &name, + const Impl::AttributAccessorPtr &accessor ) + { + AttributPtr attribut( new Attribut( name, accessor ) ); + attributsByName_[name] = attribut; + } + // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// *************** *** 80,84 **** inline Method::Method( const ConstString &name, ! const Invokable &invokable ) : name_( name ) , invokable_( invokable ) --- 100,104 ---- inline Method::Method( const ConstString &name, ! const Impl::Invokable &invokable ) : name_( name ) , invokable_( invokable ) *************** *** 88,98 **** inline ! void Method::invoke( const Any &object, ! const ArgValues &args, ! Any &result ) const { ! MethodCall call( *this, object, args, result ); invokable_.invoke( call ); } --- 108,119 ---- inline ! Any Method::invoke( const Any &object, ! const MethodParameters &args ) const { ! Any result; ! Impl::MethodCall call( *this, object, args, result ); invokable_.invoke( call ); + return result; } *************** *** 137,161 **** } // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! // Class Invokable // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// inline ! Invokable::Invokable( const InvokableBasePtr &invokable ) ! : invokable_( invokable ) ! , returnType_( typeId( Type<void>() ) ) { } inline void ! Invokable::invoke( MethodCall &call ) const { ! invokable_->invoke( call ); } ! } } // namespace CppTL { namespace Reflect { --- 158,234 ---- } + // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! // Class Attribut // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// inline ! Attribut::Attribut( const ConstString &name, ! const Impl::AttributAccessorPtr &accessor ) ! : name_( name ) ! , accessor_( accessor ) { } + + inline + ConstString + Attribut::getName() const + { + return name_; + } + + + inline + TypeId + Attribut::getType() const + { + return accessor_->getType(); + } + + inline void ! Attribut::set( const Any &object, ! const Any &value ) const { ! return accessor_->set( object, value ); } ! ! inline ! Any ! Attribut::get( const Any &object ) const ! { ! return accessor_->get( object ); ! } ! ! ! // ////////////////////////////////////////////////////////////////// ! // ////////////////////////////////////////////////////////////////// ! // Class Invokable ! // ////////////////////////////////////////////////////////////////// ! // ////////////////////////////////////////////////////////////////// ! ! namespace Impl { ! ! inline ! Invokable::Invokable( const InvokableBasePtr &invokable ) ! : invokable_( invokable ) ! , returnType_( typeId( Type<void>() ) ) ! { ! } ! ! inline ! void ! Invokable::invoke( MethodCall &call ) const ! { ! invokable_->invoke( call ); ! } ! ! } // namespace Impl ! } // namespace CppTL Index: reflection.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/reflection.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** reflection.h 3 Mar 2005 08:14:23 -0000 1.1 --- reflection.h 3 Mar 2005 20:48:54 -0000 1.2 *************** *** 11,169 **** ! namespace CppTL { namespace Reflect { ! // Forwards ! class Class; ! class Invokable; ! class InvokableBase; ! class Method; ! struct MethodCall; ! typedef SharedPtr<InvokableBase> InvokableBasePtr; ! typedef std::vector<Any> ArgValues; ! struct MethodCall ! { ! MethodCall( const Method &method, ! const Any &holder, ! const ArgValues &args, ! Any &result ) ! : method_( method ) ! , holder_( holder ) ! , args_( args ) ! , result_( result ) { ! } ! const Method &method_; ! const Any &holder_; ! const ArgValues &args_; ! Any &result_; ! }; ! class Invokable ! { ! public: ! Invokable( const InvokableBasePtr &invokable ); ! virtual ~Invokable() { ! } ! void invoke( MethodCall &call ) const; ! TypeId returnType_; ! std::vector<TypeId> argTypes_; ! InvokableBasePtr invokable_; ! }; ! class InvokableBase ! { ! public: ! virtual ~InvokableBase() { ! } ! virtual void invoke( MethodCall &call ) const = 0; ! }; ! template<class Object> ! class Invokable0 : public InvokableBase ! { ! public: ! typedef void (Object::*Member)(); ! Invokable0( Member member ) ! : member_( member ) { ! } ! void invoke( MethodCall &call ) const { ! Object &object = *any_cast( call.holder_, Type<Object *>() ); ! (object.*member_)(); ! } ! private: ! Member member_; ! }; ! template<class Object ! ,class Arg1> ! class Invokable1 : public InvokableBase ! { ! public: ! typedef void (Object::*Member)( Arg1 a1 ); ! Invokable1( Member member ) ! : member_( member ) { } ! void invoke( MethodCall &call ) const { ! Object &object = *any_cast( call.holder_, Type<Object *>() ); ! (object.*member_)( any_cast( call.args_.at(0), Type<Arg1>() ) ); } - private: - Member member_; - }; ! template<class Object ! ,class Arg1 ! ,class Arg2> ! class Invokable2: public InvokableBase ! { ! public: ! typedef void (Object::*Member)( Arg1 a1, Arg2 a2 ); ! Invokable2( Member member ) ! : member_( member ) { ! } ! void invoke( MethodCall &call ) const { ! Object &object = *any_cast( call.holder_, Type<Object *>() ); ! (object.*member_)( any_cast( call.args_.at(0), Type<Arg1>() ), ! any_cast( call.args_.at(1), Type<Arg2>() ) ); } ! private: ! Member member_; ! }; ! template<class Object> ! Invokable makeInvokable( void (Object::*member)() ) { ! Invokable invokable( InvokableBasePtr( new Invokable0<Object>( member ) ) ); ! return invokable; ! } ! template<class Object, class Arg1> ! Invokable makeInvokable( void (Object::*member)( Arg1 a1 ) ) ! { ! Invokable invokable( InvokableBasePtr( new Invokable1<Object,Arg1>( member ) ) ); ! invokable.argTypes_.push_back( typeId( Type<Arg1>() ) ); ! return invokable; ! } ! template<class Object, class Arg1, class Arg2> ! Invokable makeInvokable( void (Object::*member)( Arg1 a1, Arg2 a2 ) ) ! { ! Invokable invokable( InvokableBasePtr( new Invokable2<Object,Arg1,Arg2>( member ) ) ); ! invokable.argTypes_.push_back( typeId( Type<Arg1>() ) ); ! invokable.argTypes_.push_back( typeId( Type<Arg2>() ) ); ! return invokable; ! } --- 11,248 ---- ! namespace CppTL { ! typedef std::vector<Any> MethodParameters; ! namespace Impl { ! class AttributAccessor; ! class InvokableBase; ! typedef SharedPtr<AttributAccessor> AttributAccessorPtr; ! typedef SharedPtr<InvokableBase> InvokableBasePtr; ! ! class MethodCall { ! public: ! MethodCall( const Method &method, ! const Any &holder, ! const MethodParameters &args, ! Any &result ) ! : method_( method ) ! , holder_( holder ) ! , args_( args ) ! , result_( result ) ! { ! } ! const Method &method_; ! const Any &holder_; ! const MethodParameters &args_; ! Any &result_; ! }; ! class InvokableBase ! { ! public: ! virtual ~InvokableBase() ! { ! } ! virtual void invoke( MethodCall &call ) const = 0; ! }; ! ! ! template<class Object> ! class Invokable0 : public InvokableBase { ! public: ! typedef void (Object::*Member)(); ! Invokable0( Member member ) ! : member_( member ) ! { ! } ! void invoke( MethodCall &call ) const ! { ! Object &object = *any_cast( call.holder_, Type<Object *>() ); ! (object.*member_)(); ! } + private: + Member member_; + }; ! ! template<class Object ! ,class Arg1> ! class Invokable1 : public InvokableBase { ! public: ! typedef void (Object::*Member)( Arg1 a1 ); ! Invokable1( Member member ) ! : member_( member ) ! { ! } + void invoke( MethodCall &call ) const + { + Object &object = *any_cast( call.holder_, Type<Object *>() ); + (object.*member_)( any_cast( call.args_.at(0), Type<Arg1>() ) ); + } ! private: ! Member member_; ! }; ! ! template<class Object ! ,class Arg1 ! ,class Arg2> ! class Invokable2: public InvokableBase { ! public: ! typedef void (Object::*Member)( Arg1 a1, Arg2 a2 ); ! Invokable2( Member member ) ! : member_( member ) ! { ! } ! ! void invoke( MethodCall &call ) const ! { ! Object &object = *any_cast( call.holder_, Type<Object *>() ); ! (object.*member_)( any_cast( call.args_.at(0), Type<Arg1>() ), ! any_cast( call.args_.at(1), Type<Arg2>() ) ); ! } ! ! private: ! Member member_; ! }; ! ! ! class Invokable { ! public: ! Invokable( const Impl::InvokableBasePtr &invokable ); ! virtual ~Invokable() ! { ! } + void invoke( Impl::MethodCall &call ) const; ! TypeId returnType_; ! std::vector<TypeId> argTypes_; ! InvokableBasePtr invokable_; ! }; ! ! template<class Object> ! Invokable makeInvokable( void (Object::*member)() ) { + Invokable invokable( InvokableBasePtr( new Invokable0<Object>( member ) ) ); + return invokable; } ! ! template<class Object, class Arg1> ! Invokable makeInvokable( void (Object::*member)( Arg1 a1 ) ) { ! Invokable invokable( InvokableBasePtr( new Invokable1<Object,Arg1>( member ) ) ); ! invokable.argTypes_.push_back( typeId( Type<Arg1>() ) ); ! return invokable; } + template<class Object, class Arg1, class Arg2> + Invokable makeInvokable( void (Object::*member)( Arg1 a1, Arg2 a2 ) ) + { + Invokable invokable( InvokableBasePtr( new Invokable2<Object,Arg1,Arg2>( member ) ) ); + invokable.argTypes_.push_back( typeId( Type<Arg1>() ) ); + invokable.argTypes_.push_back( typeId( Type<Arg2>() ) ); + return invokable; + } ! class AttributAccessor ! { ! public: ! virtual ~AttributAccessor() ! { ! } ! virtual TypeId getType() const =0; ! ! virtual Any get( const Any &holder) const = 0; ! ! virtual void set( const Any &holder, ! const Any &value ) const = 0; ! }; ! ! template<class Object, class AttributType> ! class AttributAccessorImpl : public AttributAccessor { ! public: ! typedef AttributType Object::*Attribut; ! AttributAccessorImpl( Attribut attribut ) ! : attribut_( attribut ) ! { ! } ! ! TypeId getType() const ! { ! return typeId( Type<AttributType>() ); ! } ! ! Any get( const Any &holder ) const ! { ! Object &object = *any_cast( holder, Type<Object *>() ); ! return makeAny( object.*attribut_ ); ! } ! ! void set( const Any &holder, ! const Any &value ) const ! { ! Object &object = *any_cast( holder, Type<Object *>() ); ! object.*attribut_ = any_cast( value, Type<AttributType>() ); ! } ! ! private: ! Attribut attribut_; ! }; ! ! template<class Object, class TheAttributType> ! AttributAccessorPtr makeAccessor( TheAttributType Object::*attribut ) { ! return AttributAccessorPtr( ! new AttributAccessorImpl<Object ! ,TheAttributType>( attribut ) ); } ! } // namespace Impl ! class Attribut { ! public: ! Attribut( const ConstString &name, ! const Impl::AttributAccessorPtr &accessor ); + ConstString getName() const; ! TypeId getType() const; + void set( const Any &object, + const Any &value ) const; ! Any get( const Any &object ) const; + private: + Class *class_; + ConstString name_; + Impl::AttributAccessorPtr accessor_; + }; *************** *** 172,180 **** public: Method( const ConstString &name, ! const Invokable &invokable ); ! void invoke( const Any &object, ! const ArgValues &args, ! Any &result ) const; ConstString getName() const; --- 251,258 ---- public: Method( const ConstString &name, ! const Impl::Invokable &invokable ); ! Any invoke( const Any &object, ! const MethodParameters &args ) const; ConstString getName() const; *************** *** 191,210 **** Class *class_; ConstString name_; ! Invokable invokable_; }; - template<class Holder> - Any invokeWithArgValues( const Holder &object, - const Method &method, - const ArgValues &args ) - { - Any actualObject = makeAny( &*object ); - Any result; - method.invoke( actualObject, args, result ); - return result; - } - - class Class { --- 269,276 ---- Class *class_; ConstString name_; ! Impl::Invokable invokable_; }; class Class { *************** *** 220,230 **** void addMethod( const ConstString &name, ! const Invokable &invokable ); private: typedef SharedPtr<Class> ClassPtr; typedef SharedPtr<Method> MethodPtr; typedef std::map<ConstString,ClassPtr> ClassesByName; typedef std::map<ConstString,MethodPtr> MethodsByName; static ClassesByName ®istry(); --- 286,303 ---- void addMethod( const ConstString &name, ! const Impl::Invokable &invokable ); ! ! const Attribut *findAttribut( const ConstString &name ) const; ! ! void addAttribut( const ConstString &name, ! const Impl::AttributAccessorPtr &accessor ); private: typedef SharedPtr<Class> ClassPtr; typedef SharedPtr<Method> MethodPtr; + typedef SharedPtr<Attribut> AttributPtr; typedef std::map<ConstString,ClassPtr> ClassesByName; typedef std::map<ConstString,MethodPtr> MethodsByName; + typedef std::map<ConstString,AttributPtr> AttributsByName; static ClassesByName ®istry(); *************** *** 233,236 **** --- 306,310 ---- TypeId type ); + AttributsByName attributsByName_; MethodsByName methodsByName_; ConstString name_; *************** *** 240,247 **** template<class ClassType> ! class AutoRegisterClass { public: ! AutoRegisterClass() { ClassType::registerClassReflection(); --- 314,321 ---- template<class ClassType> ! class AutoRegisterClassReflection { public: ! AutoRegisterClassReflection() { ClassType::registerClassReflection(); *************** *** 250,258 **** ! # define CPPTL_REFLECT_CLASS_BEGIN( ClassType ) \ ! typedef ClassType _Reflection_SelfType; \ ! static void registerClassReflection() \ ! { \ ! ::CppTL::Reflect::Class &class_ = ::CppTL::Reflect::Class::create( #ClassType, \ ::CppTL::typeId( ::CppTL::Type<ClassType>() ) ); --- 324,332 ---- ! # define CPPTL_REFLECT_CLASS_BEGIN( ClassType ) \ ! typedef ClassType _Reflection_SelfType; \ ! static void registerClassReflection() \ ! { \ ! ::CppTL::Class &class_ = ::CppTL::Class::create( #ClassType, \ ::CppTL::typeId( ::CppTL::Type<ClassType>() ) ); *************** *** 260,277 **** # define CPPTL_REFLECT_METHOD( method ) \ { \ ! ::CppTL::Reflect::Invokable invokable = ::CppTL::Reflect::makeInvokable( \ &_Reflection_SelfType::method ); \ class_.addMethod( #method, invokable ); \ } # define CPPTL_REFLECT_CLASS_END() \ } # define CPPTL_REFLECT_REGISTER_CLASS( ClassType ) \ ! static ::CppTL::Reflect::AutoRegisterClass<ClassType> \ CPPUT_MAKE_UNIQUE_NAME(cpptlReflectRegisterClass); ! } } // namespace CppTL { namespace Reflect # include <cpptl/reflection.inl> --- 334,361 ---- # define CPPTL_REFLECT_METHOD( method ) \ { \ ! ::CppTL::Impl::Invokable invokable = ::CppTL::Impl::makeInvokable( \ &_Reflection_SelfType::method ); \ class_.addMethod( #method, invokable ); \ } + # define CPPTL_REFLECT_RENAMED_ATTRIBUT( attribut, name ) \ + { \ + ::CppTL::Impl::AttributAccessorPtr accessor = ::CppTL::Impl::makeAccessor( \ + &_Reflection_SelfType::attribut ); \ + class_.addAttribut( name, accessor ); \ + } + + # define CPPTL_REFLECT_ATTRIBUT( attribut ) \ + CPPTL_REFLECT_RENAMED_ATTRIBUT( attribut, #attribut ) + # define CPPTL_REFLECT_CLASS_END() \ } # define CPPTL_REFLECT_REGISTER_CLASS( ClassType ) \ ! static ::CppTL::AutoRegisterClassReflection<ClassType> \ CPPUT_MAKE_UNIQUE_NAME(cpptlReflectRegisterClass); ! } // namespace CppTL # include <cpptl/reflection.inl> Index: forwards.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/forwards.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** forwards.h 27 Feb 2005 14:38:25 -0000 1.3 --- forwards.h 3 Mar 2005 20:48:54 -0000 1.4 *************** *** 26,29 **** --- 26,35 ---- class Functor0R; + // reflection.h + class Attribut; + class Class; + class Method; + + } // namespace CppTL |