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

📄 classstr.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/* * =========================================================================== * PRODUCTION $Log: classstr.cpp,v $ * PRODUCTION Revision 1000.3  2004/06/01 19:42:34  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.63 * PRODUCTION * =========================================================================== *//*  $Id: classstr.cpp,v 1000.3 2004/06/01 19:42:34 gouriano Exp $* ===========================================================================**                            PUBLIC DOMAIN NOTICE*               National Center for Biotechnology Information**  This software/database is a "United States Government Work" under the*  terms of the United States Copyright Act.  It was written as part of*  the author's official duties as a United States Government employee and*  thus cannot be copyrighted.  This software/database is freely available*  to the public for use. The National Library of Medicine and the U.S.*  Government have not placed any restriction on its use or reproduction.**  Although all reasonable efforts have been taken to ensure the accuracy*  and reliability of the software and data, the NLM and the U.S.*  Government do not and cannot warrant the performance or results that*  may be obtained by using this software or data. The NLM and the U.S.*  Government disclaim all warranties, express or implied, including*  warranties of performance, merchantability or fitness for any particular*  purpose.**  Please cite the author in any work or product based on this material.** ===========================================================================** Author: Eugene Vasilchenko** File Description:*   Type info for class generation: includes, used classes, C code etc.*/#include <ncbi_pch.hpp>#include <serial/datatool/exceptions.hpp>#include <serial/datatool/type.hpp>#include <serial/datatool/blocktype.hpp>#include <serial/datatool/classstr.hpp>#include <serial/datatool/stdstr.hpp>#include <serial/datatool/code.hpp>#include <serial/datatool/srcutil.hpp>#include <serial/datatool/comments.hpp>BEGIN_NCBI_SCOPE#define SET_PREFIX "m_set_State"#define DELAY_PREFIX "m_delay_"CClassTypeStrings::CClassTypeStrings(const string& externalName,                                     const string& className)    : m_IsObject(true), m_HaveUserClass(true), m_HaveTypeInfo(true),      m_ExternalName(externalName), m_ClassName(className){}CClassTypeStrings::~CClassTypeStrings(void){}CTypeStrings::EKind CClassTypeStrings::GetKind(void) const{    return m_IsObject? eKindObject: eKindClass;}bool CClassTypeStrings::x_IsNullType(TMembers::const_iterator i) const{    return i->haveFlag ?        (dynamic_cast<CNullTypeStrings*>(i->type.get()) != 0) : false;}bool CClassTypeStrings::x_IsNullWithAttlist(TMembers::const_iterator i) const{    if (i->ref && i->dataType) {        const CDataType* resolved = i->dataType->Resolve();        if (resolved && resolved != i->dataType) {            CClassTypeStrings* typeStr = resolved->GetTypeStr();            if (typeStr) {                ITERATE ( TMembers, ir, typeStr->m_Members ) {                    if (ir->simple) {                        return x_IsNullType(ir);                    }                }            }        }    }    return false;}void CClassTypeStrings::AddMember(const string& name,                                  const AutoPtr<CTypeStrings>& type,                                  const string& pointerType,                                  bool optional,                                  const string& defaultValue,                                  bool delayed, int tag,                                  bool noPrefix, bool attlist, bool noTag,                                  bool simple,const CDataType* dataType,                                  bool nonempty){    m_Members.push_back(SMemberInfo(name, type,                                    pointerType,                                    optional, defaultValue,                                    delayed, tag, noPrefix,attlist,noTag,                                    simple,dataType,nonempty));}CClassTypeStrings::SMemberInfo::SMemberInfo(const string& name,                                            const AutoPtr<CTypeStrings>& t,                                            const string& pType,                                            bool opt, const string& defValue,                                            bool del, int tag, bool noPrefx,                                            bool attlst, bool noTg, bool simpl,                                            const CDataType* dataTp, bool nEmpty)    : externalName(name), cName(Identifier(name)),      mName("m_"+cName), tName('T'+cName),      type(t), ptrType(pType),      optional(opt), delayed(del), memberTag(tag),      defaultValue(defValue), noPrefix(noPrefx), attlist(attlst), noTag(noTg),      simple(simpl),dataType(dataTp),nonEmpty(nEmpty){    if ( cName.empty() ) {        mName = "m_data";        tName = "Tdata";    }    bool haveDefault = !defaultValue.empty();//    if ( optional && !haveDefault ) {//    }    // true [optional] CObject type should be implemented as CRef    if ( ptrType.empty() ) {        if ( type->GetKind() == eKindObject )            ptrType = "Ref";        else            ptrType = "false";    }    if ( ptrType == "Ref" ) {        ref = true;    }    else if ( /*ptrType.empty()  ||*/ ptrType == "false" ) {        ref = false;    }    else {        _ASSERT("Unknown reference type: "+ref);    }        if ( ref ) {        valueName = "(*"+mName+")";        haveFlag = false;    }    else {        valueName = mName;        haveFlag = /*optional &&*/ type->NeedSetFlag();    }    if ( haveDefault ) // cannot detect DEFAULT value        haveFlag = true;        canBeNull = ref && optional && !haveFlag;}string CClassTypeStrings::GetCType(const CNamespace& /*ns*/) const{    return GetClassNameDT();}string CClassTypeStrings::GetPrefixedCType(const CNamespace& ns,                                           const string& methodPrefix) const{    string s;    if (!HaveUserClass()) {        s += methodPrefix;    }    return s + GetCType(ns);}string CClassTypeStrings::GetRef(const CNamespace& /*ns*/) const{    return "CLASS, ("+GetClassNameDT()+')';}string CClassTypeStrings::NewInstance(const string& init) const{    return "new "+GetCType(CNamespace::KEmptyNamespace)+"("+init+')';}string CClassTypeStrings::GetResetCode(const string& var) const{    return var+".Reset();\n";}void CClassTypeStrings::SetParentClass(const string& className,                                       const CNamespace& ns,                                       const string& fileName){    m_ParentClassName = className;    m_ParentClassNamespace = ns;    m_ParentClassFileName = fileName;}staticCNcbiOstream& DeclareConstructor(CNcbiOstream& out, const string className){    return out <<        "    // constructor\n"        "    "<<className<<"(void);\n";}staticCNcbiOstream& DeclareDestructor(CNcbiOstream& out, const string className,                                bool virt){    out <<        "    // destructor\n"        "    ";    if ( virt )        out << "virtual ";    return out << '~'<<className<<"(void);\n"        "\n";}void CClassTypeStrings::GenerateTypeCode(CClassContext& ctx) const{    bool haveUserClass = HaveUserClass();    string codeClassName = GetClassNameDT();    if ( haveUserClass )        codeClassName += "_Base";    CClassCode code(ctx, codeClassName);    if ( !m_ParentClassName.empty() ) {        code.SetParentClass(m_ParentClassName, m_ParentClassNamespace);        if ( !m_ParentClassFileName.empty() )            code.HPPIncludes().insert(m_ParentClassFileName);    }    else if ( GetKind() == eKindObject ) {        code.SetParentClass("CSerialObject", CNamespace::KNCBINamespace);    }    string methodPrefix = code.GetMethodPrefix();    DeclareConstructor(code.ClassPublic(), codeClassName);    DeclareDestructor(code.ClassPublic(), codeClassName, haveUserClass);    string ncbiNamespace =        code.GetNamespace().GetNamespaceRef(CNamespace::KNCBINamespace);    if (HaveTypeInfo()) {        code.ClassPublic() <<            "    // type info\n"            "    DECLARE_INTERNAL_TYPE_INFO();\n"            "\n";    }    GenerateClassCode(code,                      code.ClassPublic(),                      methodPrefix, haveUserClass, ctx.GetMethodPrefix());    // constructors/destructor code    code.Methods() <<        "// constructor\n"<<        methodPrefix<<codeClassName<<"(void)\n";    if ( code.HaveInitializers() ) {        code.Methods() <<            "    : ";        code.WriteInitializers(code.Methods());        code.Methods() << '\n';    }    code.Methods() <<        "{\n";    if (typeid(*this) == typeid(CClassTypeStrings)) {        code.Methods() <<            "    memset("SET_PREFIX",0,sizeof("SET_PREFIX"));\n";    }    code.Methods() <<        "}\n"        "\n";    code.Methods() <<        "// destructor\n"<<        methodPrefix<<"~"<<codeClassName<<"(void)\n"        "{\n";    code.WriteDestructionCode(code.Methods());    code.Methods() <<        "}\n"        "\n";}void CClassTypeStrings::GenerateClassCode(CClassCode& code,                                          CNcbiOstream& setters,                                          const string& methodPrefix,                                          bool haveUserClass,                                          const string& classPrefix) const{    bool delayed = false;    bool generateDoNotDeleteThisObject = false;    bool wrapperClass = SizeIsOne(m_Members) &&        m_Members.front().cName.empty();    // generate member methods    {        ITERATE ( TMembers, i, m_Members ) {            if ( i->ref ) {                i->type->GeneratePointerTypeCode(code);            }            else {                i->type->GenerateTypeCode(code);                if ( i->type->GetKind() == eKindObject )                    generateDoNotDeleteThisObject = true;            }            if ( i->delayed )                delayed = true;        }    }    // check if the class is Attlist    bool isAttlist = false;    if (!m_Members.empty()) {        TMembers::const_iterator i = m_Members.begin();        if (i->dataType) {            const CDataType* t2 = i->dataType->GetParentType();            if (t2) {                const CDataMember* d = t2->GetDataMember();                if (d) {                    isAttlist = d->Attlist();                }            }        }    }    if ( GetKind() != eKindObject )        generateDoNotDeleteThisObject = false;    if ( delayed )        code.HPPIncludes().insert("serial/delaybuf");    // generate member types    {        code.ClassPublic() <<            "    // types\n";        ITERATE ( TMembers, i, m_Members ) {            string cType = i->type->GetCType(code.GetNamespace());

⌨️ 快捷键说明

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