📄 choice.cpp
字号:
CChoiceTypeInfo::CChoiceTypeInfo(size_t size, const string& name, const CObject* cObject, TTypeCreate createFunc, const type_info& ti, TWhichFunction whichFunc, TSelectFunction selectFunc, TResetFunction resetFunc) : CParent(eTypeFamilyChoice, size, name, cObject, createFunc, ti), m_WhichFunction(whichFunc), m_ResetFunction(resetFunc), m_SelectFunction(selectFunc){ InitChoiceTypeInfoFunctions();}void CChoiceTypeInfo::InitChoiceTypeInfoFunctions(void){ SetReadFunction(&TFunc::ReadChoiceDefault); SetWriteFunction(&TFunc::WriteChoiceDefault); SetCopyFunction(&TFunc::CopyChoiceDefault); SetSkipFunction(&TFunc::SkipChoiceDefault); m_SelectDelayFunction = 0;}CVariantInfo* CChoiceTypeInfo::AddVariant(const char* memberId, const void* memberPtr, const CTypeRef& memberType){ CVariantInfo* variantInfo = new CVariantInfo(this, memberId, TPointerOffsetType(memberPtr), memberType); GetItems().AddItem(variantInfo); return variantInfo;}CVariantInfo* CChoiceTypeInfo::AddVariant(const CMemberId& memberId, const void* memberPtr, const CTypeRef& memberType){ CVariantInfo* variantInfo = new CVariantInfo(this, memberId, TPointerOffsetType(memberPtr), memberType); GetItems().AddItem(variantInfo); return variantInfo;}bool CChoiceTypeInfo::IsDefault(TConstObjectPtr object) const{ return GetIndex(object) == kEmptyChoice;}static inlineTObjectPtr GetMember(const CMemberInfo* memberInfo, TObjectPtr object){ if ( memberInfo->CanBeDelayed() ) memberInfo->GetDelayBuffer(object).Update(); return memberInfo->GetItemPtr(object);}static inlineTConstObjectPtr GetMember(const CMemberInfo* memberInfo, TConstObjectPtr object){ if ( memberInfo->CanBeDelayed() ) const_cast<CDelayBuffer&>(memberInfo->GetDelayBuffer(object)).Update(); return memberInfo->GetItemPtr(object);}bool CChoiceTypeInfo::Equals(TConstObjectPtr object1, TConstObjectPtr object2, ESerialRecursionMode how) const{ // User defined comparison if ( IsCObject() ) { const CSerialUserOp* op1 = dynamic_cast<const CSerialUserOp*> (static_cast<const CObject*>(object1)); const CSerialUserOp* op2 = dynamic_cast<const CSerialUserOp*> (static_cast<const CObject*>(object2)); if ( op1 && op2 ) { if ( !op1->UserOp_Equals(*op2) ) return false; } } TMemberIndex index; index = GetVariants().FirstIndex(); const CVariantInfo* variantInfo = GetVariantInfo(index); if (variantInfo->GetId().IsAttlist()) { const CMemberInfo* info = dynamic_cast<const CMemberInfo*>(GetVariants().GetItemInfo(index)); if ( !info->GetTypeInfo()->Equals(GetMember(info, object1), GetMember(info, object2), how) ) { return false; } } // Default comparison index = GetIndex(object1); if ( index != GetIndex(object2) ) return false; if ( index == kEmptyChoice ) return true; return GetVariantInfo(index)->GetTypeInfo()->Equals(GetData(object1, index), GetData(object2, index), how);}void CChoiceTypeInfo::SetDefault(TObjectPtr dst) const{ ResetIndex(dst);}void CChoiceTypeInfo::Assign(TObjectPtr dst, TConstObjectPtr src, ESerialRecursionMode how) const{ TMemberIndex index; index = GetVariants().FirstIndex(); const CVariantInfo* variantInfo = GetVariantInfo(index); if (variantInfo->GetId().IsAttlist()) { const CMemberInfo* info = dynamic_cast<const CMemberInfo*>(GetVariants().GetItemInfo(index)); info->GetTypeInfo()->Assign(GetMember(info, dst), GetMember(info, src),how); } index = GetIndex(src); if ( index == kEmptyChoice ) ResetIndex(dst); else { _ASSERT(index >= GetVariants().FirstIndex() && index <= GetVariants().LastIndex()); SetIndex(dst, index); GetVariantInfo(index)->GetTypeInfo()->Assign(GetData(dst, index), GetData(src, index), how); } // User defined assignment if ( IsCObject() ) { const CSerialUserOp* opsrc = dynamic_cast<const CSerialUserOp*> (static_cast<const CObject*>(src)); CSerialUserOp* opdst = dynamic_cast<CSerialUserOp*> (static_cast<CObject*>(dst)); if ( opdst && opsrc ) { opdst->UserOp_Assign(*opsrc); } }}void CChoiceTypeInfo::SetSelectDelayFunction(TSelectDelayFunction func){ _ASSERT(m_SelectDelayFunction == 0); _ASSERT(func != 0); m_SelectDelayFunction = func;}void CChoiceTypeInfo::SetDelayIndex(TObjectPtr objectPtr, TMemberIndex index) const{ m_SelectDelayFunction(this, objectPtr, index);}void CChoiceTypeInfoFunctions::ReadChoiceDefault(CObjectIStream& in, TTypeInfo objectType, TObjectPtr objectPtr){ const CChoiceTypeInfo* choiceType = CTypeConverter<CChoiceTypeInfo>::SafeCast(objectType); BEGIN_OBJECT_FRAME_OF2(in, eFrameChoice, choiceType); in.BeginChoice(choiceType); BEGIN_OBJECT_FRAME_OF(in, eFrameChoiceVariant); TMemberIndex index = in.BeginChoiceVariant(choiceType); if ( index == kInvalidMember ) in.ThrowError(in.fFormatError, "choice variant id expected"); const CVariantInfo* variantInfo = choiceType->GetVariantInfo(index); if (variantInfo->GetId().IsAttlist()) { const CMemberInfo* memberInfo = dynamic_cast<const CMemberInfo*>( choiceType->GetVariants().GetItemInfo(index)); memberInfo->ReadMember(in,objectPtr); index = in.BeginChoiceVariant(choiceType); if ( index == kInvalidMember ) in.ThrowError(in.fFormatError, "choice variant id expected"); variantInfo = choiceType->GetVariantInfo(index); } in.SetTopMemberId(variantInfo->GetId()); variantInfo->ReadVariant(in, objectPtr); in.EndChoiceVariant(); END_OBJECT_FRAME_OF(in); in.EndChoice(); END_OBJECT_FRAME_OF(in);}void CChoiceTypeInfoFunctions::WriteChoiceDefault(CObjectOStream& out, TTypeInfo objectType, TConstObjectPtr objectPtr){ const CChoiceTypeInfo* choiceType = CTypeConverter<CChoiceTypeInfo>::SafeCast(objectType); BEGIN_OBJECT_FRAME_OF2(out, eFrameChoice, choiceType); out.BeginChoice(choiceType); TMemberIndex index = choiceType->GetVariants().FirstIndex(); const CVariantInfo* variantInfo = choiceType->GetVariantInfo(index); if (variantInfo->GetId().IsAttlist()) { const CMemberInfo* memberInfo = dynamic_cast<const CMemberInfo*>( choiceType->GetVariants().GetItemInfo(index)); memberInfo->WriteMember(out,objectPtr); } index = choiceType->GetIndex(objectPtr); if ( index == kInvalidMember ) out.ThrowError(out.fIllegalCall, "cannot write empty choice"); variantInfo = choiceType->GetVariantInfo(index); BEGIN_OBJECT_FRAME_OF2(out, eFrameChoiceVariant, variantInfo->GetId()); out.BeginChoiceVariant(choiceType, variantInfo->GetId()); variantInfo->WriteVariant(out, objectPtr); out.EndChoiceVariant(); END_OBJECT_FRAME_OF(out); out.EndChoice(); END_OBJECT_FRAME_OF(out);}void CChoiceTypeInfoFunctions::CopyChoiceDefault(CObjectStreamCopier& copier, TTypeInfo objectType){ copier.CopyChoice(CTypeConverter<CChoiceTypeInfo>::SafeCast(objectType));}void CChoiceTypeInfoFunctions::SkipChoiceDefault(CObjectIStream& in, TTypeInfo objectType){ const CChoiceTypeInfo* choiceType = CTypeConverter<CChoiceTypeInfo>::SafeCast(objectType); BEGIN_OBJECT_FRAME_OF2(in, eFrameChoice, choiceType); in.BeginChoice(choiceType); BEGIN_OBJECT_FRAME_OF(in, eFrameChoiceVariant); TMemberIndex index = in.BeginChoiceVariant(choiceType); if ( index == kInvalidMember ) in.ThrowError(in.fFormatError,"choice variant id expected"); const CVariantInfo* variantInfo = choiceType->GetVariantInfo(index); if (variantInfo->GetId().IsAttlist()) { const CMemberInfo* memberInfo = dynamic_cast<const CMemberInfo*>( choiceType->GetVariants().GetItemInfo(index)); memberInfo->SkipMember(in); index = in.BeginChoiceVariant(choiceType); if ( index == kInvalidMember ) in.ThrowError(in.fFormatError,"choice variant id expected"); variantInfo = choiceType->GetVariantInfo(index); } in.SetTopMemberId(variantInfo->GetId()); variantInfo->SkipVariant(in); in.EndChoiceVariant(); END_OBJECT_FRAME_OF(in); in.EndChoice(); END_OBJECT_FRAME_OF(in);}END_NCBI_SCOPE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -