objhook.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:17k
源码类别:

生物技术

开发平台:

C/C++

  1. /*
  2.  * ===========================================================================
  3.  * PRODUCTION $Log: objhook.cpp,v $
  4.  * PRODUCTION Revision 1000.1  2004/06/01 19:40:51  gouriano
  5.  * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.10
  6.  * PRODUCTION
  7.  * ===========================================================================
  8.  */
  9. /*  $Id: objhook.cpp,v 1000.1 2004/06/01 19:40:51 gouriano Exp $
  10. * ===========================================================================
  11. *
  12. *                            PUBLIC DOMAIN NOTICE
  13. *               National Center for Biotechnology Information
  14. *
  15. *  This software/database is a "United States Government Work" under the
  16. *  terms of the United States Copyright Act.  It was written as part of
  17. *  the author's official duties as a United States Government employee and
  18. *  thus cannot be copyrighted.  This software/database is freely available
  19. *  to the public for use. The National Library of Medicine and the U.S.
  20. *  Government have not placed any restriction on its use or reproduction.
  21. *
  22. *  Although all reasonable efforts have been taken to ensure the accuracy
  23. *  and reliability of the software and data, the NLM and the U.S.
  24. *  Government do not and cannot warrant the performance or results that
  25. *  may be obtained by using this software or data. The NLM and the U.S.
  26. *  Government disclaim all warranties, express or implied, including
  27. *  warranties of performance, merchantability or fitness for any particular
  28. *  purpose.
  29. *
  30. *  Please cite the author in any work or product based on this material.
  31. *
  32. * ===========================================================================
  33. *
  34. * Author: Eugene Vasilchenko
  35. *
  36. * File Description:
  37. *   !!! PUT YOUR DESCRIPTION HERE !!!
  38. *
  39. * ---------------------------------------------------------------------------
  40. * $Log: objhook.cpp,v $
  41. * Revision 1000.1  2004/06/01 19:40:51  gouriano
  42. * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.10
  43. *
  44. * Revision 1.10  2004/05/17 21:03:03  gorelenk
  45. * Added include of PCH ncbi_pch.hpp
  46. *
  47. * Revision 1.9  2003/08/11 15:25:52  grichenk
  48. * Added possibility to reset an object member from
  49. * a read hook (including non-optional members).
  50. *
  51. * Revision 1.8  2003/07/29 19:14:31  vasilche
  52. * Added missing methods.
  53. *
  54. * Revision 1.7  2003/07/29 18:47:47  vasilche
  55. * Fixed thread safeness of object stream hooks.
  56. *
  57. * Revision 1.6  2002/09/19 14:00:38  grichenk
  58. * Implemented CObjectHookGuard for write and copy hooks
  59. * Added DefaultRead/Write/Copy methods to base hook classes
  60. *
  61. * Revision 1.5  2001/05/17 15:07:08  lavr
  62. * Typos corrected
  63. *
  64. * Revision 1.4  2000/10/20 15:51:40  vasilche
  65. * Fixed data error processing.
  66. * Added interface for constructing container objects directly into output stream.
  67. * object.hpp, object.inl and object.cpp were split to
  68. * objectinfo.*, objecttype.*, objectiter.* and objectio.*.
  69. *
  70. * Revision 1.3  2000/09/26 17:38:21  vasilche
  71. * Fixed incomplete choiceptr implementation.
  72. * Removed temporary comments.
  73. *
  74. * Revision 1.2  2000/09/18 20:00:23  vasilche
  75. * Separated CVariantInfo and CMemberInfo.
  76. * Implemented copy hooks.
  77. * All hooks now are stored in CTypeInfo/CMemberInfo/CVariantInfo.
  78. * Most type specific functions now are implemented via function pointers instead of virtual functions.
  79. *
  80. * Revision 1.1  2000/08/15 19:44:48  vasilche
  81. * Added Read/Write hooks:
  82. * CReadObjectHook/CWriteObjectHook for objects of specified type.
  83. * CReadClassMemberHook/CWriteClassMemberHook for specified members.
  84. * CReadChoiceVariantHook/CWriteChoiceVariant for specified choice variants.
  85. * CReadContainerElementHook/CWriteContainerElementsHook for containers.
  86. *
  87. * ===========================================================================
  88. */
  89. #include <ncbi_pch.hpp>
  90. #include <corelib/ncbistd.hpp>
  91. #include <serial/objhook.hpp>
  92. #include <serial/objectinfo.hpp>
  93. #include <serial/objectiter.hpp>
  94. #include <serial/objistr.hpp>
  95. #include <serial/objostr.hpp>
  96. #include <serial/member.hpp>
  97. #include <serial/memberid.hpp>
  98. BEGIN_NCBI_SCOPE
  99. CReadObjectHook::~CReadObjectHook(void)
  100. {
  101. }
  102. CReadClassMemberHook::~CReadClassMemberHook(void)
  103. {
  104. }
  105. void CReadClassMemberHook::ReadMissingClassMember(CObjectIStream& in,
  106.                                                   const CObjectInfoMI& member)
  107. {
  108.     member.GetMemberInfo()->
  109.         DefaultReadMissingMember(in, member.GetClassObject().GetObjectPtr());
  110. }
  111. CReadChoiceVariantHook::~CReadChoiceVariantHook(void)
  112. {
  113. }
  114. CReadContainerElementHook::~CReadContainerElementHook(void)
  115. {
  116. }
  117. CWriteObjectHook::~CWriteObjectHook(void)
  118. {
  119. }
  120. CWriteClassMemberHook::~CWriteClassMemberHook(void)
  121. {
  122. }
  123. CWriteChoiceVariantHook::~CWriteChoiceVariantHook(void)
  124. {
  125. }
  126. CSkipObjectHook::~CSkipObjectHook(void)
  127. {
  128. }
  129. CSkipClassMemberHook::~CSkipClassMemberHook(void)
  130. {
  131. }
  132. void CSkipClassMemberHook::SkipMissingClassMember(CObjectIStream& stream,
  133.                                                   const CObjectTypeInfoMI& member)
  134. {
  135.     member.GetMemberInfo()->DefaultSkipMissingMember(stream);
  136. }
  137. CSkipChoiceVariantHook::~CSkipChoiceVariantHook(void)
  138. {
  139. }
  140. CCopyObjectHook::~CCopyObjectHook(void)
  141. {
  142. }
  143. CCopyClassMemberHook::~CCopyClassMemberHook(void)
  144. {
  145. }
  146. void CCopyClassMemberHook::CopyMissingClassMember(CObjectStreamCopier& copier,
  147.                                                   const CObjectTypeInfoMI& member)
  148. {
  149.     member.GetMemberInfo()->DefaultCopyMissingMember(copier);
  150. }
  151. CCopyChoiceVariantHook::~CCopyChoiceVariantHook(void)
  152. {
  153. }
  154. void CReadObjectHook::DefaultRead(CObjectIStream& in,
  155.                                   const CObjectInfo& object)
  156. {
  157.     object.GetTypeInfo()->DefaultReadData(in, object.GetObjectPtr());
  158. }
  159. void CReadObjectHook::DefaultSkip(CObjectIStream& in,
  160.                                   const CObjectInfo& object)
  161. {
  162.     object.GetTypeInfo()->DefaultSkipData(in);
  163. }
  164. void CReadClassMemberHook::DefaultRead(CObjectIStream& in,
  165.                                        const CObjectInfoMI& object)
  166. {
  167.     in.ReadClassMember(object);
  168. }
  169. void CReadClassMemberHook::ResetMember(const CObjectInfoMI& object,
  170.                                        CObjectInfoMI::EEraseFlag flag)
  171. {
  172.     const_cast<CObjectInfoMI&>(object).Erase(flag);
  173. }
  174. void CReadClassMemberHook::DefaultSkip(CObjectIStream& in,
  175.                                        const CObjectInfoMI& object)
  176. {
  177.     in.SkipObject(object.GetMember());
  178. }
  179. void CReadChoiceVariantHook::DefaultRead(CObjectIStream& in,
  180.                                          const CObjectInfoCV& object)
  181. {
  182.     in.ReadChoiceVariant(object);
  183. }
  184. void CWriteObjectHook::DefaultWrite(CObjectOStream& out,
  185.                                     const CConstObjectInfo& object)
  186. {
  187.     object.GetTypeInfo()->DefaultWriteData(out, object.GetObjectPtr());
  188. }
  189. void CWriteClassMemberHook::DefaultWrite(CObjectOStream& out,
  190.                                          const CConstObjectInfoMI& member)
  191. {
  192.     out.WriteClassMember(member);
  193. }
  194. void CWriteChoiceVariantHook::DefaultWrite(CObjectOStream& out,
  195.                                            const CConstObjectInfoCV& variant)
  196. {
  197.     out.WriteChoiceVariant(variant);
  198. }
  199. void CCopyObjectHook::DefaultCopy(CObjectStreamCopier& copier,
  200.                                   const CObjectTypeInfo& type)
  201. {
  202.     type.GetTypeInfo()->DefaultCopyData(copier);
  203. }
  204. void CCopyClassMemberHook::DefaultCopy(CObjectStreamCopier& copier,
  205.                                        const CObjectTypeInfoMI& member)
  206. {
  207.     member.GetMemberInfo()->DefaultCopyMember(copier);
  208. }
  209. void CCopyChoiceVariantHook::DefaultCopy(CObjectStreamCopier& copier,
  210.                                          const CObjectTypeInfoCV& variant)
  211. {
  212.     variant.GetVariantInfo()->DefaultCopyVariant(copier);
  213. }
  214. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  215.                                            CReadObjectHook& hook,
  216.                                            CObjectIStream* stream)
  217.     : m_Hook(&hook),
  218.       m_HookMode(eHook_Read),
  219.       m_HookType(eHook_Object)
  220. {
  221.     m_Stream.m_IStream = stream;
  222.     if ( stream ) {
  223.         info.SetLocalReadHook(*stream, &hook);
  224.     }
  225.     else {
  226.         info.SetGlobalReadHook(&hook);
  227.     }
  228. }
  229. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  230.                                            CWriteObjectHook& hook,
  231.                                            CObjectOStream* stream)
  232.     : m_Hook(&hook),
  233.       m_HookMode(eHook_Write),
  234.       m_HookType(eHook_Object)
  235. {
  236.     m_Stream.m_OStream = stream;
  237.     if ( stream ) {
  238.         info.SetLocalWriteHook(*stream, &hook);
  239.     }
  240.     else {
  241.         info.SetGlobalWriteHook(&hook);
  242.     }
  243. }
  244. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  245.                                            CSkipObjectHook& hook,
  246.                                            CObjectIStream* stream)
  247.     : m_Hook(&hook),
  248.       m_HookMode(eHook_Skip),
  249.       m_HookType(eHook_Object)
  250. {
  251.     m_Stream.m_IStream = stream;
  252.     if ( stream ) {
  253.         info.SetLocalSkipHook(*stream, &hook);
  254.     }
  255.     else {
  256.         info.SetGlobalSkipHook(&hook);
  257.     }
  258. }
  259. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  260.                                            CCopyObjectHook& hook,
  261.                                            CObjectStreamCopier* stream)
  262.     : m_Hook(&hook),
  263.       m_HookMode(eHook_Copy),
  264.       m_HookType(eHook_Object)
  265. {
  266.     m_Stream.m_Copier = stream;
  267.     if ( stream ) {
  268.         info.SetLocalCopyHook(*stream, &hook);
  269.     }
  270.     else {
  271.         info.SetGlobalCopyHook(&hook);
  272.     }
  273. }
  274. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  275.                                            const string& id,
  276.                                            CReadClassMemberHook& hook,
  277.                                            CObjectIStream* stream)
  278.     : m_Hook(&hook),
  279.       m_HookMode(eHook_Read),
  280.       m_HookType(eHook_Member),
  281.       m_Id(id)
  282. {
  283.     m_Stream.m_IStream = stream;
  284.     CObjectTypeInfoMI member = info.FindMember(id);
  285.     if ( stream ) {
  286.         member.SetLocalReadHook(*stream, &hook);
  287.     }
  288.     else {
  289.         member.SetGlobalReadHook(&hook);
  290.     }
  291. }
  292. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  293.                                            const string& id,
  294.                                            CWriteClassMemberHook& hook,
  295.                                            CObjectOStream* stream)
  296.     : m_Hook(&hook),
  297.       m_HookMode(eHook_Write),
  298.       m_HookType(eHook_Member),
  299.       m_Id(id)
  300. {
  301.     m_Stream.m_OStream = stream;
  302.     CObjectTypeInfoMI member = info.FindMember(id);
  303.     if ( stream ) {
  304.         member.SetLocalWriteHook(*stream, &hook);
  305.     }
  306.     else {
  307.         member.SetGlobalWriteHook(&hook);
  308.     }
  309. }
  310. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  311.                                            const string& id,
  312.                                            CSkipClassMemberHook& hook,
  313.                                            CObjectIStream* stream)
  314.     : m_Hook(&hook),
  315.       m_HookMode(eHook_Skip),
  316.       m_HookType(eHook_Member),
  317.       m_Id(id)
  318. {
  319.     m_Stream.m_IStream = stream;
  320.     CObjectTypeInfoMI member = info.FindMember(id);
  321.     if ( stream ) {
  322.         member.SetLocalSkipHook(*stream, &hook);
  323.     }
  324.     else {
  325.         member.SetGlobalSkipHook(&hook);
  326.     }
  327. }
  328. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  329.                                            const string& id,
  330.                                            CCopyClassMemberHook& hook,
  331.                                            CObjectStreamCopier* stream)
  332.     : m_Hook(&hook),
  333.       m_HookMode(eHook_Copy),
  334.       m_HookType(eHook_Member),
  335.       m_Id(id)
  336. {
  337.     m_Stream.m_Copier = stream;
  338.     CObjectTypeInfoMI member = info.FindMember(id);
  339.     if ( stream ) {
  340.         member.SetLocalCopyHook(*stream, &hook);
  341.     }
  342.     else {
  343.         member.SetGlobalCopyHook(&hook);
  344.     }
  345. }
  346. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  347.                                            const string& id,
  348.                                            CReadChoiceVariantHook& hook,
  349.                                            CObjectIStream* stream)
  350.     : m_Hook(&hook),
  351.       m_HookMode(eHook_Read),
  352.       m_HookType(eHook_Variant),
  353.       m_Id(id)
  354. {
  355.     m_Stream.m_IStream = stream;
  356.     CObjectTypeInfoVI variant = info.FindVariant(id);
  357.     if ( stream ) {
  358.         variant.SetLocalReadHook(*stream, &hook);
  359.     }
  360.     else {
  361.         variant.SetGlobalReadHook(&hook);
  362.     }
  363. }
  364. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  365.                                            const string& id,
  366.                                            CWriteChoiceVariantHook& hook,
  367.                                            CObjectOStream* stream)
  368.     : m_Hook(&hook),
  369.       m_HookMode(eHook_Write),
  370.       m_HookType(eHook_Variant),
  371.       m_Id(id)
  372. {
  373.     m_Stream.m_OStream = stream;
  374.     CObjectTypeInfoVI variant = info.FindVariant(id);
  375.     if ( stream ) {
  376.         variant.SetLocalWriteHook(*stream, &hook);
  377.     }
  378.     else {
  379.         variant.SetGlobalWriteHook(&hook);
  380.     }
  381. }
  382. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  383.                                            const string& id,
  384.                                            CSkipChoiceVariantHook& hook,
  385.                                            CObjectIStream* stream)
  386.     : m_Hook(&hook),
  387.       m_HookMode(eHook_Skip),
  388.       m_HookType(eHook_Variant),
  389.       m_Id(id)
  390. {
  391.     m_Stream.m_IStream = stream;
  392.     CObjectTypeInfoVI variant = info.FindVariant(id);
  393.     if ( stream ) {
  394.         variant.SetLocalSkipHook(*stream, &hook);
  395.     }
  396.     else {
  397.         variant.SetGlobalSkipHook(&hook);
  398.     }
  399. }
  400. CObjectHookGuardBase::CObjectHookGuardBase(const CObjectTypeInfo& info,
  401.                                            const string& id,
  402.                                            CCopyChoiceVariantHook& hook,
  403.                                            CObjectStreamCopier* stream)
  404.     : m_Hook(&hook),
  405.       m_HookMode(eHook_Copy),
  406.       m_HookType(eHook_Variant),
  407.       m_Id(id)
  408. {
  409.     m_Stream.m_Copier = stream;
  410.     CObjectTypeInfoVI variant = info.FindVariant(id);
  411.     if ( stream ) {
  412.         variant.SetLocalCopyHook(*stream, &hook);
  413.     }
  414.     else {
  415.         variant.SetGlobalCopyHook(&hook);
  416.     }
  417. }
  418. CObjectHookGuardBase::~CObjectHookGuardBase(void)
  419. {
  420.     _ASSERT(m_HookMode == eHook_None);
  421.     _ASSERT(m_HookType == eHook_Null);
  422. }
  423. void CObjectHookGuardBase::ResetHook(const CObjectTypeInfo& info)
  424. {
  425.     switch (m_HookType) {
  426.     case eHook_Object:
  427.         switch (m_HookMode) {
  428.         case eHook_Read:
  429.             if ( m_Stream.m_IStream ) {
  430.                 info.ResetLocalReadHook(*m_Stream.m_IStream);
  431.             }
  432.             else {
  433.                 info.ResetGlobalReadHook();
  434.             }
  435.             break;
  436.         case eHook_Write:
  437.             if ( m_Stream.m_OStream ) {
  438.                 info.ResetLocalWriteHook(*m_Stream.m_OStream);
  439.             }
  440.             else {
  441.                 info.ResetGlobalWriteHook();
  442.             }
  443.             break;
  444.         case eHook_Skip:
  445.             if ( m_Stream.m_IStream ) {
  446.                 info.ResetLocalSkipHook(*m_Stream.m_IStream);
  447.             }
  448.             else {
  449.                 info.ResetGlobalSkipHook();
  450.             }
  451.             break;
  452.         case eHook_Copy:
  453.             if ( m_Stream.m_Copier ) {
  454.                 info.ResetLocalCopyHook(*m_Stream.m_Copier);
  455.             }
  456.             else {
  457.                 info.ResetGlobalCopyHook();
  458.             }
  459.             break;
  460.         }
  461.         break;
  462.     case eHook_Member:
  463.     {
  464.         CObjectTypeInfoMI member = info.FindMember(m_Id);
  465.         switch (m_HookMode) {
  466.         case eHook_Read:
  467.             if ( m_Stream.m_IStream ) {
  468.                 member.ResetLocalReadHook(*m_Stream.m_IStream);
  469.             }
  470.             else {
  471.                 member.ResetGlobalReadHook();
  472.             }
  473.             break;
  474.         case eHook_Write:
  475.             if ( m_Stream.m_OStream ) {
  476.                 member.ResetLocalWriteHook(*m_Stream.m_OStream);
  477.             }
  478.             else {
  479.                 member.ResetGlobalWriteHook();
  480.             }
  481.             break;
  482.         case eHook_Skip:
  483.             if ( m_Stream.m_IStream ) {
  484.                 member.ResetLocalSkipHook(*m_Stream.m_IStream);
  485.             }
  486.             else {
  487.                 member.ResetGlobalSkipHook();
  488.             }
  489.             break;
  490.         case eHook_Copy:
  491.             if ( m_Stream.m_Copier ) {
  492.                 member.ResetLocalCopyHook(*m_Stream.m_Copier);
  493.             }
  494.             else {
  495.                 member.ResetGlobalCopyHook();
  496.             }
  497.             break;
  498.         }
  499.         break;
  500.     }
  501.     case eHook_Variant:
  502.     {
  503.         CObjectTypeInfoVI variant = info.FindVariant(m_Id);
  504.         switch (m_HookMode) {
  505.         case eHook_Read:
  506.             if ( m_Stream.m_IStream ) {
  507.                 variant.ResetLocalReadHook(*m_Stream.m_IStream);
  508.             }
  509.             else {
  510.                 variant.ResetGlobalReadHook();
  511.             }
  512.             break;
  513.         case eHook_Write:
  514.             if ( m_Stream.m_OStream ) {
  515.                 variant.ResetLocalWriteHook(*m_Stream.m_OStream);
  516.             }
  517.             else {
  518.                 variant.ResetGlobalWriteHook();
  519.             }
  520.             break;
  521.         case eHook_Skip:
  522.             if ( m_Stream.m_IStream ) {
  523.                 variant.ResetLocalSkipHook(*m_Stream.m_IStream);
  524.             }
  525.             else {
  526.                 variant.ResetGlobalSkipHook();
  527.             }
  528.             break;
  529.         case eHook_Copy:
  530.             if ( m_Stream.m_Copier ) {
  531.                 variant.ResetLocalCopyHook(*m_Stream.m_Copier);
  532.             }
  533.             else {
  534.                 variant.ResetGlobalCopyHook();
  535.             }
  536.             break;
  537.         }
  538.         break;
  539.     }
  540.     case eHook_Element:
  541.         break;
  542.     }
  543.     m_HookMode = eHook_None;
  544.     m_HookType = eHook_Null;
  545. }
  546. END_NCBI_SCOPE