📄 choicestr.cpp
字号:
" "STATE_ENUM" index = src.Which();\n" " switch ( index ) {\n"; ITERATE ( TVariants, i, m_Variants ) { switch ( i->memberType ) { case eSimpleMember: methods << " case "STATE_PREFIX<<i->cName<<":\n" " m_"<<i->cName<<" = src.m_"<<i->cName<<";\n" " break;\n"; break; case ePointerMember: methods << " case "STATE_PREFIX<<i->cName<<":\n" " m_"<<i->cName<<" = new T"<<i->cName<<"(*src.m_"<<i->cName<<");\n" " break;\n"; break; case eStringMember: _ASSERT(haveString); // will be handled specially break; case eObjectPointerMember: // will be handled specially _ASSERT(haveObjectPointer); break; } } if ( haveString ) { // generate copy code for string ITERATE ( TVariants, i, m_Variants ) { if ( i->memberType == eStringMember ) { methods << " case "STATE_PREFIX<<i->cName<<":\n"; } } if ( haveUnion ) { // string is pointer methods << " "STRING_MEMBER" = src."STRING_MEMBER";\n"; } else { methods << " "STRING_MEMBER" = new "STRING_TYPE_FULL"(*src."STRING_MEMBER");\n"; } methods << " break;\n"; } if ( haveObjectPointer ) { // generate copy code for string ITERATE ( TVariants, i, m_Variants ) { if ( i->memberType == eObjectPointerMember ) { methods << " case "STATE_PREFIX<<i->cName<<":\n"; } } methods << " ("OBJECT_MEMBER" = src."OBJECT_MEMBER")->AddReference();\n" " break;\n"; } methods << " default:\n" " break;\n" " }\n" " "STATE_MEMBER" = index;\n" "}\n" "\n"; } // generate Select method { methods << "void "<<methodPrefix<<"DoSelect("STATE_ENUM" index)\n" "{\n"; if ( haveUnion || haveObjectPointer ) { methods << " switch ( index ) {\n"; ITERATE ( TVariants, i, m_Variants ) { if (i->attlist) { continue; } switch ( i->memberType ) { case eSimpleMember: if (!x_IsNullType(i)) { string init = i->type->GetInitializer(); methods << " case "STATE_PREFIX<<i->cName<<":\n" " m_"<<i->cName<<" = "<<init<<";\n" " break;\n"; } break; case ePointerMember: methods << " case "STATE_PREFIX<<i->cName<<":\n" " m_"<<i->cName<<" = "<<i->type->NewInstance(NcbiEmptyString)<<";\n" " break;\n"; break; case eObjectPointerMember: methods << " case "STATE_PREFIX<<i->cName<<":\n" " ("OBJECT_MEMBER" = "<<i->type->NewInstance(NcbiEmptyString)<<")->AddReference();\n" " break;\n"; break; case eStringMember: // will be handled specially break; } } if ( haveString ) { ITERATE ( TVariants, i, m_Variants ) { if ( i->memberType == eStringMember ) { methods << " case "STATE_PREFIX<<i->cName<<":\n"; } } methods << " "STRING_MEMBER" = new "STRING_TYPE_FULL";\n" " break;\n"; } methods << " default:\n" " break;\n" " }\n"; } methods << " "STATE_MEMBER" = index;\n" "}\n" "\n"; } // generate choice variants names code.ClassPrivate() << " static const char* const sm_SelectionNames[];\n"; { methods << "const char* const "<<methodPrefix<<"sm_SelectionNames[] = {\n" " \"not set\""; ITERATE ( TVariants, i, m_Variants ) { methods << ",\n" " \""<<i->externalName<<"\""; if (i->attlist) { methods << " /* place holder */"; } } methods << "\n" "};\n" "\n" "NCBI_NS_STD::string "<<methodPrefix<<"SelectionName("STATE_ENUM" index)\n" "{\n" " return NCBI_NS_NCBI::CInvalidChoiceSelection::GetName(index, sm_SelectionNames, sizeof(sm_SelectionNames)/sizeof(sm_SelectionNames[0]));\n" "}\n" "\n" "void "<<methodPrefix<<"ThrowInvalidSelection("STATE_ENUM" index) const\n" "{\n" " throw NCBI_NS_NCBI::CInvalidChoiceSelection(__FILE__,__LINE__,m_choice, index, sm_SelectionNames, sizeof(sm_SelectionNames)/sizeof(sm_SelectionNames[0]));\n" "}\n" "\n"; } // generate variant types { code.ClassPublic() << " // types\n"; ITERATE ( TVariants, i, m_Variants ) { string cType = i->type->GetCType(code.GetNamespace()); if (!x_IsNullType(i)) { code.ClassPublic() << " typedef "<<cType<<" T"<<i->cName<<";\n"; } } code.ClassPublic() << "\n"; } // generate variant getters & setters { code.ClassPublic() << " // getters\n"; setters << " // setters\n\n"; ITERATE ( TVariants, i, m_Variants ) { string cType = i->type->GetCType(code.GetNamespace()); string tType = "T" + i->cName; string rType = i->type->GetPrefixedCType(code.GetNamespace(),methodPrefix); bool isNull = x_IsNullType(i); bool isNullWithAtt = x_IsNullWithAttlist(i); if (!CClassCode::GetDoxygenComments()) { if (!isNull) { code.ClassPublic() << " // typedef "<< cType <<" "<<tType<<"\n"; } else { code.ClassPublic() << "\n"; } } if (i->attlist) { if (CClassCode::GetDoxygenComments()) { code.ClassPublic() << " /// Reset the attribute list.\n"; } code.ClassPublic() << " void Reset"<<i->cName<<"(void);\n"; } else { if (CClassCode::GetDoxygenComments()) { code.ClassPublic() << "\n" " /// Check if variant "<<i->cName<<" is selected.\n" " ///\n"; if (!isNull) { code.ClassPublic() << " /// "<<i->cName<<" type is defined as \'typedef "<< cType <<" "<<tType<<"\'.\n"; } code.ClassPublic() << " /// @return\n" " /// - true, if the variant is selected.\n" " /// - false, otherwise.\n"; } code.ClassPublic() << " bool Is"<<i->cName<<"(void) const;\n"; } if (i->dataType && i->dataType->IsPrimitive()) { if (CClassCode::GetDoxygenComments()) { code.ClassPublic() << "\n" " /// Get the variant data.\n" " ///\n" " /// @return\n" " /// Copy of the variant data.\n"; } code.ClassPublic() << " "<<tType<<" Get"<<i->cName<<"(void) const;\n" "\n"; } else { if (!isNull) { if (CClassCode::GetDoxygenComments()) { if (i->attlist) { code.ClassPublic() << "\n" " /// Get the attribute list data.\n"; } else { code.ClassPublic() << "\n" " /// Get the variant data.\n"; } code.ClassPublic() << " ///\n" " /// @return\n" " /// Reference to the data.\n"; } code.ClassPublic() << " const "<<tType<<"& Get"<<i->cName<<"(void) const;\n"; } } if (isNull) { if (CClassCode::GetDoxygenComments()) { setters << "\n" " /// Select the variant.\n"; } setters << " void Set"<<i->cName<<"(void);\n"; } else { if (CClassCode::GetDoxygenComments()) { if (i->attlist) { setters << "\n" " /// Set the attribute list data.\n" " ///\n" " /// @return\n" " /// Reference to the data.\n"; } else { setters << "\n" " /// Select the variant.\n" " ///\n" " /// @return\n" " /// Reference to the variant data.\n"; } } setters << " "<<tType<<"& Set"<<i->cName<<"(void);\n"; } if ( i->type->CanBeCopied() ) { if (i->attlist) { if (CClassCode::GetDoxygenComments()) { setters << "\n" " /// Set the attribute list data.\n" " ///\n" " /// @param value\n" " /// Reference to data.\n"; } setters << " void Set"<<i->cName<<"("<<tType<<"& value);\n"; } else { if (!isNull) { if (CClassCode::GetDoxygenComments()) { setters << "\n" " /// Select the variant and set its data.\n" " ///\n" " /// @param value\n" " /// Reference to variant data.\n"; } setters << " void Set"<<i->cName<<"(const "<<tType<<"& value);\n"; } } } if ( i->memberType == eObjectPointerMember && !isNullWithAtt) { if (CClassCode::GetDoxygenComments()) { if (i->attlist) { setters << "\n" " /// Set the attribute list data.\n"; } else { setters << " /// Select the variant and set its data.\n"; } setters << " ///\n" " /// @param value\n" " /// Reference to the data.\n"; } setters << " void Set"<<i->cName<<"("<<tType<<"& value);\n"; } string memberRef; string constMemberRef; bool inl = true; switch ( i->memberType ) { case eSimpleMember: memberRef = constMemberRef = "m_"+i->cName; break; case ePointerMember: memberRef = constMemberRef = "*m_"+i->cName; break; case eStringMember: memberRef = STRING_MEMBER; if ( haveUnion ) { // string is pointer memberRef = '*'+memberRef; } constMemberRef = memberRef; break; case eObjectPointerMember: memberRef = "*static_cast<T"+i->cName+"*>("OBJECT_MEMBER")"; constMemberRef = "*static_cast<const T"+i->cName+"*>("OBJECT_MEMBER")"; inl = false; break; } if ( i->delayed ) inl = false; if (i->attlist) { code.MethodStart(inl) << "void "<<methodPrefix<<"Reset"<<i->cName<<"(void)\n" "{\n" " (*m_" <<i->cName<< ").Reset();\n" "}\n" "\n"; if (i->dataType && i->dataType->IsPrimitive()) { code.MethodStart(inl) << rType; } else { code.MethodStart(inl) << "const "<<rType<<"&"; } code.Methods(inl) << " "<<methodPrefix<<"Get"<<i->cName<<"(void) const\n" "{\n"; code.Methods(inl) << " return (*m_"<<i->cName<<");\n" "}\n" "\n"; code.MethodStart(inl) << rType<<"& "<<methodPrefix<<"Set"<<i->cName<<"(void)\n" "{\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -