⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classtype_tmpl.hpp

📁 一个实现C++反射机制的类库
💻 HPP
📖 第 1 页 / 共 4 页
字号:
        }

        virtual void invoke1(const PtrHolder &ptrHolder, RetValHolder &rvh,
                             const int n, Arguments &args) const {
            const SmartPtrHolder_tmpl<Obj_TP> &smartPtrHolder = 
                dynamic_cast<const SmartPtrHolder_tmpl<Obj_TP> &>(ptrHolder);
            Obj_TP &obj = *(smartPtrHolder.ptr);
            invoke2(&obj, rvh, n, args);
        }        

        virtual void invoke2(Obj_TP *op, RetValHolder &rvh,
                             const int n, Arguments &args) const = 0;

        // This declaration is just to avoid warning messages about virtual
        // function hiding.
        virtual BoundMemberFunction_body *bind(const ObjHolder &oh) const = 0;
        virtual BoundMemberFunction_body *bind(Obj_TP *) const = 0;

        void registerMember(dataMember_map_t *fm, dataMember_vector_t *fv, 
                            memberFunction_map_t *mm, memberFunction_vector_t *mv) {
            mm->insert(typename memberFunction_map_t::value_type(name(), this));
            mv->push_back(MemberFunction(this));
        }
};

template <typename Obj_TP>
inline
MemberFunction_body_tmpl1<Obj_TP>::MemberFunction_body_tmpl1(const std::string &name)
    : MemberFunction_body(name) {}


/*-----------------------------------------------------------------------------\
|------------------------------------------------------------------------------|
|                                                                              |
|   MemberFunction_body_tmpl2                                                  |
|                                                                              |
|------------------------------------------------------------------------------|
\------------------------------------------------------------------------------/

    MemberFunction body class.  This one is templated on both the object type and the
    return type.
-                                                                             */

template <typename Obj_TP, typename Ret_TP>
class MemberFunction_body_tmpl2 : public MemberFunction_body_tmpl1<Obj_TP> {

    /*--------------------------------------------------------------------------

        Types and Constants                                                   */

        typedef std::vector<const Type *> argument_vector_t;

    /*--------------------------------------------------------------------------

        Constructors, Initters, Assignments, and Destructors                  */

    public:

        MemberFunction_body_tmpl2(const std::string &member_name);

	private:
	    
	    // Disallow these unless specifically allowed.
	    MemberFunction_body_tmpl2(const MemberFunction_body_tmpl2 &);
	    MemberFunction_body_tmpl2 &operator=(const MemberFunction_body_tmpl2 &);

    /*--------------------------------------------------------------------------

        Accessors and Convertors                                              */

    public:

    /*--------------------------------------------------------------------------

        Methods                                                               */

    public:

        virtual const argument_vector_t &getArguments() const { return m_argument_vector; }

        virtual void invoke2(Obj_TP *op, RetValHolder &rvh,
                             const int n, Arguments &args) const = 0;
        virtual typename Ret_TP::type invoke3(Obj_TP *op, const int n, Arguments &args) const = 0;

        // The first version is for creating a BoundMemberFunction from a 
        // MemberFunction.  The second is for creating a BoundMemberFunction 
        // from a BoundClass.
        virtual BoundMemberFunction_body *bind(const ObjHolder &oh) const {
            // Convert the object holder.
            ObjHolder_tmpl<Obj_TP> oh_t;
            using namespace std;
            oh.castTo(&oh_t);
            assert(oh_t.ptr != NULL);
            // XXX - Memleak.
            return new BoundMemberFunction_body_tmpl2<Obj_TP, Ret_TP>(oh_t.ptr, this);
        }

        virtual BoundMemberFunction_body *bind(Obj_TP *o) const {
            return new BoundMemberFunction_body_tmpl2<Obj_TP, Ret_TP>(o, this);
        }

    protected:
        void registerArgument(const Type *type) {
            m_argument_vector.push_back(type);
        }

    /*--------------------------------------------------------------------------

        Data                                                                  */

    private:

        argument_vector_t m_argument_vector; // Map for argument list
};


template <typename Obj_TP, typename Ret_TP>
MemberFunction_body_tmpl2<Obj_TP, Ret_TP>::MemberFunction_body_tmpl2(const std::string &member_name)
 : MemberFunction_body_tmpl1<Obj_TP>(member_name) {}


/*-----------------------------------------------------------------------------\
|------------------------------------------------------------------------------|
|                                                                              |
|   MemberFunction_body_tmpl3                                                  |
|                                                                              |
|------------------------------------------------------------------------------|
\------------------------------------------------------------------------------/

    Handle return type issues.

-                                                                             */

template <typename Obj_TP, typename Ret_TP>
class MemberFunction_body_tmpl3 : public MemberFunction_body_tmpl2<Obj_TP, Ret_TP> {

    /*--------------------------------------------------------------------------

        Constructors, Initters, Assignments, and Destructors                  */

    public:

        MemberFunction_body_tmpl3(const std::string &member_name)
            : MemberFunction_body_tmpl2<Obj_TP, Ret_TP>(member_name) {}
	
    /*--------------------------------------------------------------------------

        Methods                                                               */

    public:

        virtual void invoke2(Obj_TP *op, RetValHolder &rvh,
                             const int n, Arguments &args) const {
            // Downcast the retval holder.
            RetValHolder_tmpl<typename Ret_TP::type> *rvh_t
                = dynamic_cast<RetValHolder_tmpl<typename Ret_TP::type> *>(&rvh);
            assert(rvh_t != NULL);
            rvh_t->ref() = invoke3(op, n, args);
        }
};
template <typename Obj_TP>
class MemberFunction_body_tmpl3<Obj_TP, FundamentalType_tmpl<void> > 
    : public MemberFunction_body_tmpl2<Obj_TP, FundamentalType_tmpl<void> > {

    /*--------------------------------------------------------------------------

        Constructors, Initters, Assignments, and Destructors                  */

    public:

        MemberFunction_body_tmpl3(const std::string &member_name)
            : MemberFunction_body_tmpl2<Obj_TP, FundamentalType_tmpl<void> >(member_name) {}
	
    /*--------------------------------------------------------------------------

        Methods                                                               */

    public:

        virtual void invoke2(Obj_TP *op, RetValHolder &rvh,
                             const int n, Arguments &args) const {
            invoke3(op, n, args);
        }
};


/*-----------------------------------------------------------------------------\
|------------------------------------------------------------------------------|
|                                                                              |
|   MemberList_base                                                            |
|                                                                              |
|------------------------------------------------------------------------------|
\------------------------------------------------------------------------------/

    <Type comments>

-                                                                             */

template <typename Obj_TP>
class MemberList_base : public virtual ClassType_body {

    /*--------------------------------------------------------------------------

        Types and Constants                                                   */

    public:

        typedef std::map<std::string, const DataMember_body_tmpl1<Obj_TP> *>
            dataMember_map_t;
        typedef std::map<std::string, const MemberFunction_body_tmpl1<Obj_TP> *>
            memberFunction_map_t;

    /*--------------------------------------------------------------------------

        Constructors, Initters, Assignments, and Destructors                  */

    protected:

        MemberList_base() {}
        virtual ~MemberList_base() {}

	private:
	    
	    // Disallow these unless specifically allowed.
	    MemberList_base(const MemberList_base &);
	    MemberList_base &operator=(const MemberList_base &); 

    /*--------------------------------------------------------------------------

        Data                                                                  */
        
    protected:

        // Map of string names to DataMember_body objects.
        dataMember_map_t m_dataMember_map;
        // Map of string names to MemberFunction_body objects.
        memberFunction_map_t m_memberFunction_map;
};

/*-----------------------------------------------------------------------------\
|------------------------------------------------------------------------------|
|                                                                              |
|   MemberList_name_base                                                       |
|                                                                              |
|------------------------------------------------------------------------------|
\------------------------------------------------------------------------------/

    <Type comments>

-                                                                             */

template <typename Obj_TP, const char *Name_TP>
class MemberList_name_base : public MemberList_base<Obj_TP> {

    /*--------------------------------------------------------------------------

        Constructors, Initters, Assignments, and Destructors                  */

    protected:

        MemberList_name_base() {}
        virtual ~MemberList_name_base() {}

	private:
	    
	    // Disallow these unless specifically allowed.
	    MemberList_name_base(const MemberList_name_base &);
	    MemberList_name_base &operator=(const MemberList_name_base &); 

    /*--------------------------------------------------------------------------

        Static Data                                                           */

    public:

        // "tp" means stands for "template parameter".
        static const char *const tp_name;
};

/*-----------------------------------------------------------------------------\
|------------------------------------------------------------------------------|
|                                                                              |
|   DataMemberDcl                                                              |
|                                                                              |
|------------------------------------------------------------------------------|
\------------------------------------------------------------------------------/

    <Type comments>

-                                                                             */

template <typename Obj_TP, typename MemType_TP,
 typename MemType_TP::type (Obj_TP::*MemP), const char *Name>
class DataMemberDcl : public DataMember_body_tmpl2<Obj_TP, MemType_TP> {

    public:

        DataMemberDcl() : DataMember_body_tmpl2<Obj_TP, MemType_TP>(Name) {}

        virtual typename MemType_TP::type *ptr(Obj_TP *op) const {
            return &(op->*MemP);
        }
};


/*-----------------------------------------------------------------------------\
|------------------------------------------------------------------------------|

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -