📄 choicestr.cpp
字号:
switch ( i->memberType ) { case ePointerMember: havePointers = true; i->type->GeneratePointerTypeCode(code); break; case eObjectPointerMember: if (i->attlist) { haveAttlist = true; } else { haveObjectPointer = true; } i->type->GeneratePointerTypeCode(code); break; case eSimpleMember: haveSimple = true; i->type->GenerateTypeCode(code); break; case eStringMember: haveString = true; i->type->GenerateTypeCode(code); break; } if ( i->delayed ) delayed = true; } } if ( delayed ) code.HPPIncludes().insert("serial/delaybuf"); bool haveUnion = havePointers || haveSimple || (haveString && haveObjectPointer); if ( haveString && haveUnion ) { // convert string member to pointer member havePointers = true; } string stdNamespace = code.GetNamespace().GetNamespaceRef(CNamespace::KSTDNamespace); string ncbiNamespace = code.GetNamespace().GetNamespaceRef(CNamespace::KNCBINamespace); if ( HaveAssignment() ) { code.ClassPublic() << " /// Copy constructor.\n" " "<<codeClassName<<"(const "<<codeClassName<<"& src);\n\n" " /// Assignment operator\n" " "<<codeClassName<<"& operator=(const "<<codeClassName<<"& src);\n\n\n"; } else { code.ClassPrivate() << " // copy constructor and assignment operator\n" " "<<codeClassName<<"(const "<<codeClassName<<"& );\n" " "<<codeClassName<<"& operator=(const "<<codeClassName<<"& );\n"; } // generated choice enum { string cName(STATE_NOT_SET); size_t currlen, maxlen = cName.size() + 2; ITERATE(TVariants, i, m_Variants) { if (!i->attlist) { maxlen = max(maxlen,i->cName.size()); } } code.ClassPublic() << "\n /// Choice variants.\n" " enum "STATE_ENUM" {\n" " "STATE_NOT_SET" = "<<kEmptyChoice <<"," ; for (currlen = strlen(STATE_NOT_SET)+2; currlen < maxlen; ++currlen) { code.ClassPublic() << " "; } code.ClassPublic() <<" ///< No variant selected\n" ; TMemberIndex currIndex = kEmptyChoice; bool needIni = false; for (TVariants::const_iterator i= m_Variants.begin(); i != m_Variants.end();) { ++currIndex; if (!i->attlist) { cName = i->cName; code.ClassPublic() << " "STATE_PREFIX<<cName; if (needIni) { code.ClassPublic() << " = "<<currIndex; needIni = false; } ++i; if (i != m_Variants.end()) { code.ClassPublic() << ","; } else { code.ClassPublic() << " "; } for (currlen = cName.size(); currlen < maxlen; ++currlen) { code.ClassPublic() << " "; } code.ClassPublic() << " ///< Variant "<<cName<<" is selected."; code.ClassPublic() << "\n"; } else { ++i; needIni = true; } } code.ClassPublic() << " };\n"; code.ClassPublic() << " /// Maximum+1 value of the choice variant enumerator.\n"; code.ClassPublic() << " enum E_ChoiceStopper {\n" " e_MaxChoice = " << currIndex+1 << " ///< == "STATE_PREFIX << m_Variants.rbegin()->cName << "+1\n" " };\n" "\n"; } code.ClassPublic() << " /// Reset the selection (set it to "STATE_NOT_SET").\n" " "; if ( HaveUserClass() ) code.ClassPublic() << "virtual "; code.ClassPublic() << "void Reset(void);\n" "\n"; // generate choice methods code.ClassPublic() << " /// Which variant is currently selected.\n"; if (CClassCode::GetDoxygenComments()) { code.ClassPublic() << " ///\n" " /// @return\n" " /// Choice state enumerator.\n"; } code.ClassPublic() << " "STATE_ENUM" Which(void) const;\n\n" " /// Verify selection, throw exception if it differs from the expected.\n"; if (CClassCode::GetDoxygenComments()) { code.ClassPublic() << " ///\n" " /// @param index\n" " /// Expected selection.\n"; } code.ClassPublic() << " void CheckSelected("STATE_ENUM" index) const;\n\n" " /// Throw \'InvalidSelection\' exception.\n"; if (CClassCode::GetDoxygenComments()) { code.ClassPublic() << " ///\n" " /// @param index\n" " /// Expected selection.\n"; } code.ClassPublic() << " void ThrowInvalidSelection("STATE_ENUM" index) const;\n\n" " /// Retrieve selection name (for diagnostic purposes).\n"; if (CClassCode::GetDoxygenComments()) { code.ClassPublic() << " ///\n" " /// @param index\n" " /// One of possible selection states.\n" " /// @return\n" " /// Name string.\n"; } code.ClassPublic() << " static "<<stdNamespace<<"string SelectionName("STATE_ENUM" index);\n" "\n"; setters << " /// Select the requested variant if needed.\n"; if (CClassCode::GetDoxygenComments()) { setters << " ///\n" " /// @param index\n" " /// New selection state.\n" " /// @param reset\n" " /// Flag that defines the resetting of the variant data. The data will\n" " /// be reset if either the current selection differs from the new one,\n" " /// or the flag is set to eDoResetVariant.\n"; } setters << " void Select("STATE_ENUM" index, "<<ncbiNamespace<<"EResetVariant reset = "<<ncbiNamespace<<"eDoResetVariant);\n"; if ( delayed ) { setters << " /// Select the requested variant using delay buffer (for internal use).\n"; if (CClassCode::GetDoxygenComments()) { setters << " ///\n" " /// @param index\n" " /// New selection state.\n"; } setters << " void SelectDelayBuffer("STATE_ENUM" index);\n"; } setters << "\n"; CNcbiOstream& methods = code.Methods(); CNcbiOstream& inlineMethods = code.InlineMethods(); inlineMethods << "inline\n"<< methodPrefix<<STATE_ENUM" "<<methodPrefix<<"Which(void) const\n" "{\n" " return "STATE_MEMBER";\n" "}\n" "\n" "inline\n" "void "<<methodPrefix<<"CheckSelected("STATE_ENUM" index) const\n" "{\n" " if ( "STATE_MEMBER" != index )\n" " ThrowInvalidSelection(index);\n" "}\n" "\n" "inline\n" "void "<<methodPrefix<<"Select("STATE_ENUM" index, NCBI_NS_NCBI::EResetVariant reset)\n" "{\n" " if ( reset == NCBI_NS_NCBI::eDoResetVariant || "STATE_MEMBER" != index ) {\n" " if ( "STATE_MEMBER" != "STATE_NOT_SET" )\n" " Reset();\n" " DoSelect(index);\n" " }\n" "}\n" "\n"; if ( delayed ) { inlineMethods << "inline\n" "void "<<methodPrefix<<"SelectDelayBuffer("STATE_ENUM" index)\n" "{\n" " if ( "STATE_MEMBER" != "STATE_NOT_SET" || "DELAY_MEMBER".GetIndex() != (index - 1))\n" " NCBI_THROW(ncbi::CSerialException,eIllegalCall, \"illegal call\");\n" " "STATE_MEMBER" = index;\n" "}\n" "\n"; } if ( HaveAssignment() ) { inlineMethods << "inline\n"<< methodPrefix<<codeClassName<<"(const "<<codeClassName<<"& src)\n" "{\n" " DoAssign(src);\n" "}\n" "\n" "inline\n"<< methodPrefix<<codeClassName<<"& "<<methodPrefix<<"operator=(const "<<codeClassName<<"& src)\n" "{\n" " if ( this != &src ) {\n" " Reset();\n" " DoAssign(src);\n" " }\n" " return *this;\n" "}\n" "\n"; } // generate choice state code.ClassPrivate() << " // choice state\n" " "STATE_ENUM" "STATE_MEMBER";\n" " // helper methods\n" " void DoSelect("STATE_ENUM" index);\n"; if ( HaveAssignment() ) { code.ClassPrivate() << " void DoAssign(const "<<codeClassName<<"& src);\n"; } code.ClassPrivate() << "\n"; // generate initialization code code.AddInitializer(STATE_MEMBER, STATE_NOT_SET); if (haveAttlist) { ITERATE ( TVariants, i, m_Variants ) { if (i->attlist) { string member("m_"); member += i->cName; string init("new C_"); init += i->cName; init += "()"; code.AddInitializer(member, init); } } } // generate destruction code code.AddDestructionCode("if ( "STATE_MEMBER" != "STATE_NOT_SET" )\n" " Reset();"); // generate Reset method { methods << "void "<<methodPrefix<<"Reset(void)\n" "{\n"; if (haveAttlist) { ITERATE ( TVariants, i, m_Variants ) { if (i->attlist) { methods << " Reset" << i->cName << "();\n"; } } } if ( haveObjectPointer || havePointers || haveString ) { if ( delayed ) { methods << " if ( "DELAY_MEMBER" )\n" " "DELAY_MEMBER".Forget();\n" " else\n"; } methods << " switch ( "STATE_MEMBER" ) {\n"; // generate destruction code for pointers ITERATE ( TVariants, i, m_Variants ) { if (i->attlist) { continue; } if ( i->memberType == ePointerMember ) { methods << " case "STATE_PREFIX<<i->cName<<":\n"; WriteTabbed(methods, i->type->GetDestructionCode("*m_"+i->cName), " "); methods << " delete m_"<<i->cName<<";\n" " break;\n"; } } if ( haveString ) { // generate destruction code for string ITERATE ( TVariants, i, m_Variants ) { if (i->attlist) { continue; } if ( i->memberType == eStringMember ) { methods << " case "STATE_PREFIX<<i->cName<<":\n"; } } if ( haveUnion ) { // string is pointer inside union methods << " delete "STRING_MEMBER";\n"; } else { methods << " "STRING_MEMBER".erase();\n"; } methods << " break;\n"; } if ( haveObjectPointer ) { // generate destruction code for pointers to CObject ITERATE ( TVariants, i, m_Variants ) { if (i->attlist) { continue; } if ( i->memberType == eObjectPointerMember ) { methods << " case "STATE_PREFIX<<i->cName<<":\n"; } } methods << " "OBJECT_MEMBER"->RemoveReference();\n" " break;\n"; } methods << " default:\n" " break;\n" " }\n"; } methods << " "STATE_MEMBER" = "STATE_NOT_SET";\n" "}\n" "\n"; } // generate Assign method if ( HaveAssignment() ) { methods << "void "<<methodPrefix<<"DoAssign(const "<<codeClassName<<"& src)\n" "{\n"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -