여러 소스에서 가져올 수있는 객체 (예 : 소스 1과 소스 2)의 직렬화를 구성하려고하는데 스키마 처리가 모두 엉망입니다. 그래서 나는 부드러운 직렬화를 다루려고 노력하고있다.다중 소스 및 다른 스키마에서 객체를 직렬화하는 매끄러운 방법?
void CSerFoo::Serialize(CArchive& ar)
{
try
{
//ar.Flush();
SerializeEx(ar); // For the Serialization of the Good Ones, from the point now on
}
catch(...) // If the Object cannot be Serialized using the above Method try the Next way to decode
{
try
{
//ar.Flush();
SpecialSerialize1(ar); // For the Serialization of the Objects from the Source 1, actually code from the Source 1
}
catch(...) // If the Object cannot be Serialized using the above Method try the Last way to decode
{
try
{
//ar.Flush();
SpecialSerialize2(ar); // For the Serialization of the Objects from the Source 2, actually code from the Source 2
}
catch(...)
{
// No way
}
}
}
}
편집 1 :
이 소스에서 직렬화 코드 1
// From Source 1
IMPLEMENT_SERIAL(CSerFoo, CObject, VERSIONABLE_SCHEMA | 3)
void CSerFoo::Serialize(CArchive& ar) // This will be the Consolidated CSerFoo's SpecialSerialize1
{
UINT uiSchema = ar.GetObjectSchema();
if (ar.IsStoring())
{
ar << m_sName;
ar << m_sDesc;
ar << m_String1;
ar << m_fValue1;
ar << m_fValue2;
ar << m_iValue1;
ar << m_iValue2;
ar << m_String2;
ar << m_String3;
}
else
{
ar >> m_sName;
ar >> m_sDesc;
ar >> m_String1;
if(uischema > 0)// Added in VERSION_SCHEMA 1
{
ar >> m_fValue1;
ar >> m_fValue2;
}
if(uischema > 1) // Added in VERSION_SCHEMA 2
{
ar >> m_iValue1;
ar >> m_iValue2;
ar >> m_String4;
}
if(uischema > 2) // Added in VERSION_SCHEMA 3
{
ar >> m_String2;
ar >> m_String3;
}
}
}
이것은 직렬화 코드 소스에서 지금이
// From Source 2
IMPLEMENT_SERIAL(CSerFoo, CObject, VERSIONABLE_SCHEMA | 3)
void CSerFoo::Serialize(CArchive& ar) // This will be the Consolidated CSerFoo's SpecialSerialize2
{
UINT uiSchema = ar.GetObjectSchema();
if (ar.IsStoring())
{
ar << m_sName;
ar << m_sDesc;
ar << m_String1;
ar << m_fValue1;
ar << m_fValue2;
ar << m_iValue1;
ar << m_iValue2;
ar << m_iValue3;
ar << m_String2;
ar << m_String3;
}
else
{
ar >> m_sName;
ar >> m_sDesc;
ar >> m_String1;
if(uischema > 0)// Added in VERSION_SCHEMA 1
{
ar >> m_fValue1;
}
if(uischema > 1) // Added in VERSION_SCHEMA 2
{
ar >> m_fValue2;
ar >> m_iValue1;
ar >> m_iValue2;
}
if(uischema > 2) // Added in VERSION_SCHEMA 3
{
ar >> m_iValue3;
ar >> m_String2;
ar >> m_String3;
}
}
// m_String4 is not there in the Source 2
}
통합 CSerFoo
(에서 지금) 양쪽 출처에서 모든 들판을 가지고있다. 그리고 거기에서 우리는 ne wer CSerFoo의 동일한 객체에 대한 직렬화. 우리는 다른 수업을 만드는 것에 타협 할 수 없습니다.
문제는 내가 직면 한 문제입니다. 직렬화의 후속 호출에서 커서 (CArchive :: m_lpBufCur)가 이동되어 다음 번에 Serialize가 실패합니다.
이 방법이 있습니까?
나는 무엇이 있습니까?
미리 감사드립니다.
SerializeEx 및 SpecialSerializeX의 기능을 알지 못하면이 문제에 관해 말할 내용이 없습니다. –
이것은 모두 int, float 및 string을 순차적으로 직렬화 한 것입니다. 시퀀스 및 필드 수는 각 serialization에서 다를 수 있으며 문제가 무엇인지 알 수 있습니다. –
버전 관리가 가능한 스키마를 사용할 때 아무런 문제가 없으며 버전 관리가 가능한 스키마가 사용자가 개발 한 문제의 표준 솔루션입니다. 간단한 소개는 [CArchive :: GetObjectSchema] (https://msdn.microsoft.com/en-us/library/wkbc2za4.aspx)와 [TN002 : Persistent Object Data Format] (https : // msdn .microsoft.com/en-us/library/32wxt301.aspx)를 참조하십시오. 어쨌든 질문에 따라 대답 할 수 없습니다. 우리가 공통 기반을 가질 수 있도록 [mcve]를 제공하십시오. – IInspectable