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

📄 classtype_tmpl.hpp

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

        virtual const DataMember_body_tmpl1<Obj_TP> *
        getDataMember(const std::string &n) const;

        virtual const MemberFunction_body_tmpl1<Obj_TP> *
        getMemberFunction(const std::string &n) const;

        virtual void newInstance(ObjHolder *) const;
        virtual PtrHolder_smptr_t createInstance() const;
        virtual PtrHolder *createInstancePtr() const;

        virtual bool derivesFrom(const ClassType_body &) const;
};


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

    <Type comments>

-                                                                             */

template <typename Obj_TP>
class ClassType_tmpl : public ClassType {

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

        Types and Constants                                                   */

    public:

        typedef Obj_TP type;

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

        Constructors, Initters, Assignments, and Destructors                  */

    public:

        // Construct the base object with the pointer to the static
        // Type body.
        ClassType_tmpl()
            : ClassType(s_class_body == NULL
                ? (s_class_body = new ClassType_body_tmpl<typename TypeOp<Obj_TP>::BareT>())
                : s_class_body) {}

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

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

        Static Data                                                           */

    public:

        static ClassType_body_tmpl<typename TypeOp<Obj_TP>::BareT> *s_class_body;
};


/******************************************************************************
**************************** Template Definitions *****************************
******************************************************************************/



/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
|                                                                              |
|   ObjHolder_tmpl<T>                                                          |
|                                                                              |
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
\- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

template <typename T>
void
ObjHolder_tmpl<T>::castTo(ObjHolder *oh) const {

    assert(oh != NULL);
    assert(ptr != NULL);

    typedef typename BaseList_base<T>::map_t map_t;

    if (typeid(*this) == typeid(*oh)) {

        IXCR_DCDCL(ObjHolder_tmpl<T> *, oh_t, oh); 
        oh_t->ptr = ptr;

    } else {

        map_t &map(ClassType_tmpl<T>::s_class_body->m_base_info_map);

        // g++ 3.2.2 requires typename here.
        typename map_t::const_iterator it = map.find(&oh->typeInfo);
        if (it != map.end()) {
            const BaseInfo_base<T> *c = (*it).second;
            c->upCast(ptr, oh);
        } else {
            oh->castFrom(*this);
        }
    }
}

template <typename T>
void
ObjHolder_tmpl<T>::castFrom(const ObjHolder &oh) {

    typedef typename BaseList_base<T>::map_t map_t;

    if (typeid(*this) == typeid(oh)) {

        IXCR_DCDCL(const ObjHolder_tmpl<T> &, oh_t, oh); 
        ptr = oh_t.ptr;

    } else {

        map_t &map(ClassType_tmpl<T>::s_class_body->m_base_info_map);

        // g++ 3.2.2 requires typename here.
        typename map_t::const_iterator it = map.find(&oh.typeInfo);
        if (it != map.end()) {
            const BaseInfo_base<T> *c = (*it).second;
            ptr = c->downCast(oh);
        }
    }
}


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

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

    Constructors, Initters, Assignments, and Destructors                      */

template <typename Obj_TP>
inline
DataMember_body_tmpl1<Obj_TP>::DataMember_body_tmpl1(const std::string &name,
 const std::string &type_name)
 : DataMember_body(name, type_name) {
}


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

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

    Constructors, Initters, Assignments, and Destructors                      */

template <typename Obj_TP, typename Val_T>
DataMember_body_tmpl2<Obj_TP, Val_T>::DataMember_body_tmpl2(const std::string &member_name)
 : DataMember_body_tmpl1<Obj_TP>(member_name, Val_T().name()) {}

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

    Methods                                                                   */

template <typename Obj_TP, typename Val_T>
BoundDataMember_body *
DataMember_body_tmpl2<Obj_TP, Val_T>::bind(Obj_TP *o) const {
    return new BoundDataMember_body_tmpl2<Obj_TP, Val_T>(o, this);
}

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

template <typename Obj_TP, const char *Name_TP>
const char *const 
MemberList_name_base<Obj_TP, Name_TP>::tp_name = Name_TP;

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

template <typename Obj_TP>
const DataMember_body_tmpl1<Obj_TP> *
ClassType_body_tmpl<Obj_TP>::getDataMember(const std::string &n) const {
    typename Members<Obj_TP>::dataMember_map_t::const_iterator it = this->m_dataMember_map.find(n);
    if (it == this->m_dataMember_map.end()) {
        Throw(NoSuchDataMemberException(this->tp_name, n));
    }
    //assert(it != m_dataMember_map.end());
    return (*it).second;
}

template <typename Obj_TP>
const MemberFunction_body_tmpl1<Obj_TP> *
ClassType_body_tmpl<Obj_TP>::getMemberFunction(const std::string &n) const {
    typename Members<Obj_TP>::memberFunction_map_t::const_iterator it = this->m_memberFunction_map.find(n);
    if (it == this->m_memberFunction_map.end()) {
        Throw(NoSuchMemberFunctionException(this->tp_name, n));
    }
    //assert(it != m_memberFunction_map.end());
    return (*it).second;
}

template <typename Obj_TP>
void
ClassType_body_tmpl<Obj_TP>::newInstance(ObjHolder *ph) const {
    // Create a obj holder of the actual derived type.
    const ObjHolder_tmpl<Obj_TP> tph(new Obj_TP());
    // Cast the actual derived type to the desired ObjHolder type.
    tph.castTo(ph);
}

/**
 * Return a SmartPtrHolder of the actual type. The SmartPtrHolder is also wrapped
 * with a PtrHolder_smptr_t in order to ensure automatic destruction.
 */
template <typename Obj_TP>
PtrHolder_smptr_t 
ClassType_body_tmpl<Obj_TP>::createInstance() const {
    return PtrHolder_smptr_t(new SmartPtrHolder_tmpl<Obj_TP>(new Obj_TP));
}

/**
 * Return a SmartPtrHolder of the actual type.
 */
template <typename Obj_TP>
PtrHolder *
ClassType_body_tmpl<Obj_TP>::createInstancePtr() const {
    return new SmartPtrHolder_tmpl<Obj_TP>(new Obj_TP);
}

template <typename Obj_TP>
bool
ClassType_body_tmpl<Obj_TP>::derivesFrom(const ClassType_body &ct) const {

    if (typeid(Obj_TP) == ct.m_type_info) {
        return true;
    } else {
        typedef typename BaseList_base<Obj_TP>::map_t map_t;
        typename map_t::const_iterator it = this->m_base_info_map.find(&ct.m_type_info);
        return it != this->m_base_info_map.end();
    }

    return false;
}



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

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

    Static Data                                                               */

template <typename Obj_TP>
ClassType_body_tmpl<typename TypeOp<Obj_TP>::BareT> *
ClassType_tmpl<Obj_TP>::s_class_body;



static const char ClassType_tmpl_hpp_id[] =
"$Id: ClassType_tmpl.hpp,v 1.1 2007/10/25 14:05:24 tdevadit Exp $";



} // namespace



#endif



/* Local Variables: */
/* c-basic-offset: 4 */
/* indent-tabs-mode: nil */
/* End: */
/* vim: set filetype=cpp tabstop=8 shiftwidth=4 softtabstop=4 expandtab: */

⌨️ 快捷键说明

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