int main(int argc, char** argv) { // Create some ASN.1 data that can be parsed by this code sample. char asn[] = "Date-std ::= { year 1998, month 1, day 2, season \"winter\" }"; // Setup an input stream, based on the sample ASN.1. CNcbiIstrstream iss(asn); unique_ptr<CObjectIStream> in(CObjectIStream::Open(eSerial_AsnText, iss)); //////////////////////////////////////////////////// // Create a hook for the 'year' class member of Date-std objects. // The year class member was aribtrarily chosen to illustrate the // use of hooks - many other entities would work equally well. // Get data structures that model the type information for Date-std // objects and their 'year' class members. // The type information will be used to recognize and forward 'year' // class members of Date-std objects found in the stream to the hook. CObjectTypeInfo stdInfo = CType<CDate_std>(); CObjectTypeInfoMI memberInfo = stdInfo.FindMember("year"); // Set a local hook for Date-std 'year' class members. This involves // creating an instance of the hook class and passing that hook to the // "set hook" method, which registers the hook to be called when a hooked // type is encountered in the stream. memberInfo.SetLocalReadHook(*in, new CReadCMHook_Date_std__year()); // Read from the input stream, storing data in the object. At this point, // the hook is in place so simply reading from the input stream will // cause the hook to be triggered whenever the 'year' class member is // encountered. in->Read(stdInfo); return 0; }
CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info, const string& id, CSkipClassMemberHook& hook, CObjectIStream* stream) : m_Hook(&hook), m_HookMode(eHook_Skip), m_HookType(eHook_Member), m_Id(id) { m_Stream.m_IStream = stream; CObjectTypeInfoMI member = info.FindMember(id); if ( stream ) { member.SetLocalSkipHook(*stream, &hook); } }
CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info, const string& id, CCopyClassMemberHook& hook, CObjectStreamCopier* stream) : m_Hook(&hook), m_HookMode(eHook_Copy), m_HookType(eHook_Member), m_Id(id) { m_Stream.m_Copier = stream; CObjectTypeInfoMI member = info.FindMember(id); if ( stream ) { member.SetLocalCopyHook(*stream, &hook); } else { member.SetGlobalCopyHook(&hook); } }
CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info, const string& id, CWriteClassMemberHook& hook, CObjectOStream* stream) : m_Hook(&hook), m_HookMode(eHook_Write), m_HookType(eHook_Member), m_Id(id) { m_Stream.m_OStream = stream; CObjectTypeInfoMI member = info.FindMember(id); if ( stream ) { member.SetLocalWriteHook(*stream, &hook); } else { member.SetGlobalWriteHook(&hook); } }
int main(int argc, char** argv) { auto_ptr<CObjectIStream> in(CObjectIStream::Open(eSerial_AsnText, "if")); auto_ptr<CObjectOStream> out(CObjectOStream::Open(eSerial_AsnText, "of")); CObjectTypeInfo typeInfo = CType<CAuth_list>(); CObjectTypeInfoMI memberInfo = typeInfo.FindMember("names"); CObjectTypeInfoVI variantInfo = (*memberInfo).GetPointedType() .FindVariant("std"); variantInfo.SetLocalWriteHook(*out, new CDemoHook); (*CObjectTypeInfo(CType<CAuth_list>()).FindMember("names")) .GetPointedType() .FindVariant("std") .SetLocalWriteHook(*out, new CDemoHook); CCit_art article; *in >> article; *out << article; return 0; }
void CObjectHookGuardBase::ResetHook(const CObjectTypeInfo& info) { switch (m_HookType) { case eHook_Object: switch (m_HookMode) { case eHook_Read: if ( m_Stream.m_IStream ) { info.ResetLocalReadHook(*m_Stream.m_IStream); } else { info.ResetGlobalReadHook(); } break; case eHook_Write: if ( m_Stream.m_OStream ) { info.ResetLocalWriteHook(*m_Stream.m_OStream); } else { info.ResetGlobalWriteHook(); } break; case eHook_Skip: if ( m_Stream.m_IStream ) { info.ResetLocalSkipHook(*m_Stream.m_IStream); } break; case eHook_Copy: if ( m_Stream.m_Copier ) { info.ResetLocalCopyHook(*m_Stream.m_Copier); } else { info.ResetGlobalCopyHook(); } break; default: break; } break; case eHook_Member: { CObjectTypeInfoMI member = info.FindMember(m_Id); switch (m_HookMode) { case eHook_Read: if ( m_Stream.m_IStream ) { member.ResetLocalReadHook(*m_Stream.m_IStream); } else { member.ResetGlobalReadHook(); } break; case eHook_Write: if ( m_Stream.m_OStream ) { member.ResetLocalWriteHook(*m_Stream.m_OStream); } else { member.ResetGlobalWriteHook(); } break; case eHook_Skip: if ( m_Stream.m_IStream ) { member.ResetLocalSkipHook(*m_Stream.m_IStream); } break; case eHook_Copy: if ( m_Stream.m_Copier ) { member.ResetLocalCopyHook(*m_Stream.m_Copier); } else { member.ResetGlobalCopyHook(); } break; default: break; } break; } case eHook_Variant: { CObjectTypeInfoVI variant = info.FindVariant(m_Id); switch (m_HookMode) { case eHook_Read: if ( m_Stream.m_IStream ) { variant.ResetLocalReadHook(*m_Stream.m_IStream); } else { variant.ResetGlobalReadHook(); } break; case eHook_Write: if ( m_Stream.m_OStream ) { variant.ResetLocalWriteHook(*m_Stream.m_OStream); } else { variant.ResetGlobalWriteHook(); } break; case eHook_Skip: if ( m_Stream.m_IStream ) { variant.ResetLocalSkipHook(*m_Stream.m_IStream); } break; case eHook_Copy: if ( m_Stream.m_Copier ) { variant.ResetLocalCopyHook(*m_Stream.m_Copier); } else { variant.ResetGlobalCopyHook(); } break; default: break; } break; } case eHook_Element: case eHook_Null: default: break; } m_HookMode = eHook_None; m_HookType = eHook_Null; }