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

生物技术

开发平台:

C/C++

  1. /*
  2.  * ===========================================================================
  3.  * PRODUCTION $Log: objhook.hpp,v $
  4.  * PRODUCTION Revision 1000.1  2004/06/01 19:38:50  gouriano
  5.  * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.15
  6.  * PRODUCTION
  7.  * ===========================================================================
  8.  */
  9. #ifndef OBJHOOK__HPP
  10. #define OBJHOOK__HPP
  11. /*  $Id: objhook.hpp,v 1000.1 2004/06/01 19:38:50 gouriano Exp $
  12. * ===========================================================================
  13. *
  14. *                            PUBLIC DOMAIN NOTICE
  15. *               National Center for Biotechnology Information
  16. *
  17. *  This software/database is a "United States Government Work" under the
  18. *  terms of the United States Copyright Act.  It was written as part of
  19. *  the author's official duties as a United States Government employee and
  20. *  thus cannot be copyrighted.  This software/database is freely available
  21. *  to the public for use. The National Library of Medicine and the U.S.
  22. *  Government have not placed any restriction on its use or reproduction.
  23. *
  24. *  Although all reasonable efforts have been taken to ensure the accuracy
  25. *  and reliability of the software and data, the NLM and the U.S.
  26. *  Government do not and cannot warrant the performance or results that
  27. *  may be obtained by using this software or data. The NLM and the U.S.
  28. *  Government disclaim all warranties, express or implied, including
  29. *  warranties of performance, merchantability or fitness for any particular
  30. *  purpose.
  31. *
  32. *  Please cite the author in any work or product based on this material.
  33. *
  34. * ===========================================================================
  35. *
  36. * Author: Eugene Vasilchenko
  37. *
  38. * File Description:
  39. *   !!! PUT YOUR DESCRIPTION HERE !!!
  40. */
  41. #include <corelib/ncbistd.hpp>
  42. #include <corelib/ncbiobj.hpp>
  43. #include <serial/serialdef.hpp>
  44. #include <serial/objecttype.hpp>
  45. #include <serial/objstack.hpp>
  46. #include <serial/objectiter.hpp>
  47. /** @addtogroup HookSupport
  48.  *
  49.  * @{
  50.  */
  51. BEGIN_NCBI_SCOPE
  52. class CObjectIStream;
  53. class CObjectOStream;
  54. class CObjectStreamCopier;
  55. class CObjectInfo;
  56. class CConstObjectInfo;
  57. class CObjectTypeInfo;
  58. class NCBI_XSERIAL_EXPORT CReadObjectHook : public CObject
  59. {
  60. public:
  61.     virtual ~CReadObjectHook(void);
  62.     
  63.     virtual void ReadObject(CObjectIStream& in,
  64.                             const CObjectInfo& object) = 0;
  65.     // Default actions
  66.     void DefaultRead(CObjectIStream& in,
  67.                      const CObjectInfo& object);
  68.     void DefaultSkip(CObjectIStream& in,
  69.                      const CObjectInfo& object);
  70. };
  71. class NCBI_XSERIAL_EXPORT CReadClassMemberHook : public CObject
  72. {
  73. public:
  74.     virtual ~CReadClassMemberHook(void);
  75.     virtual void ReadClassMember(CObjectIStream& in,
  76.                                  const CObjectInfoMI& member) = 0;
  77.     virtual void ReadMissingClassMember(CObjectIStream& in,
  78.                                         const CObjectInfoMI& member);
  79.     void DefaultRead(CObjectIStream& in,
  80.                      const CObjectInfoMI& object);
  81.     void DefaultSkip(CObjectIStream& in,
  82.                      const CObjectInfoMI& object);
  83.     void ResetMember(const CObjectInfoMI& object,
  84.                      CObjectInfoMI::EEraseFlag flag =
  85.                          CObjectInfoMI::eErase_Optional);
  86. };
  87. class NCBI_XSERIAL_EXPORT CReadChoiceVariantHook : public CObject
  88. {
  89. public:
  90.     virtual ~CReadChoiceVariantHook(void);
  91.     virtual void ReadChoiceVariant(CObjectIStream& in,
  92.                                    const CObjectInfoCV& variant) = 0;
  93.     void DefaultRead(CObjectIStream& in,
  94.                      const CObjectInfoCV& object);
  95.     // No default skip method - can not skip variants
  96. };
  97. class NCBI_XSERIAL_EXPORT CReadContainerElementHook : public CObject
  98. {
  99. public:
  100.     virtual ~CReadContainerElementHook(void);
  101.     virtual void ReadContainerElement(CObjectIStream& in,
  102.                                       const CObjectInfo& container) = 0;
  103. };
  104. class NCBI_XSERIAL_EXPORT CWriteObjectHook : public CObject
  105. {
  106. public:
  107.     virtual ~CWriteObjectHook(void);
  108.     
  109.     virtual void WriteObject(CObjectOStream& out,
  110.                              const CConstObjectInfo& object) = 0;
  111.     void DefaultWrite(CObjectOStream& out,
  112.                       const CConstObjectInfo& object);
  113. };
  114. class NCBI_XSERIAL_EXPORT CWriteClassMemberHook : public CObject
  115. {
  116. public:
  117.     virtual ~CWriteClassMemberHook(void);
  118.     
  119.     virtual void WriteClassMember(CObjectOStream& out,
  120.                                   const CConstObjectInfoMI& member) = 0;
  121.     void DefaultWrite(CObjectOStream& out,
  122.                       const CConstObjectInfoMI& member);
  123. };
  124. class NCBI_XSERIAL_EXPORT CWriteChoiceVariantHook : public CObject
  125. {
  126. public:
  127.     virtual ~CWriteChoiceVariantHook(void);
  128.     virtual void WriteChoiceVariant(CObjectOStream& out,
  129.                                     const CConstObjectInfoCV& variant) = 0;
  130.     void DefaultWrite(CObjectOStream& out,
  131.                       const CConstObjectInfoCV& variant);
  132. };
  133. class NCBI_XSERIAL_EXPORT CSkipObjectHook : public CObject
  134. {
  135. public:
  136.     virtual ~CSkipObjectHook(void);
  137.     
  138.     virtual void SkipObject(CObjectIStream& stream,
  139.                             const CObjectTypeInfo& type) = 0;
  140. //    void DefaultSkip(CObjectIStream& stream,
  141. //                     const CObjectTypeInfo& type);
  142. };
  143. class NCBI_XSERIAL_EXPORT CSkipClassMemberHook : public CObject
  144. {
  145. public:
  146.     virtual ~CSkipClassMemberHook(void);
  147.     
  148.     virtual void SkipClassMember(CObjectIStream& stream,
  149.                                  const CObjectTypeInfoMI& member) = 0;
  150.     virtual void SkipMissingClassMember(CObjectIStream& stream,
  151.                                         const CObjectTypeInfoMI& member);
  152. //    void DefaultSkip(CObjectIStream& stream,
  153. //                     const CObjectTypeInfoMI& member);
  154. };
  155. class NCBI_XSERIAL_EXPORT CSkipChoiceVariantHook : public CObject
  156. {
  157. public:
  158.     virtual ~CSkipChoiceVariantHook(void);
  159.     virtual void SkipChoiceVariant(CObjectIStream& stream,
  160.                                    const CObjectTypeInfoCV& variant) = 0;
  161. //    void DefaultSkip(CObjectIStream& stream,
  162. //                     const CObjectTypeInfoCV& variant);
  163. };
  164. class NCBI_XSERIAL_EXPORT CCopyObjectHook : public CObject
  165. {
  166. public:
  167.     virtual ~CCopyObjectHook(void);
  168.     
  169.     virtual void CopyObject(CObjectStreamCopier& copier,
  170.                             const CObjectTypeInfo& type) = 0;
  171.     void DefaultCopy(CObjectStreamCopier& copier,
  172.                      const CObjectTypeInfo& type);
  173. };
  174. class NCBI_XSERIAL_EXPORT CCopyClassMemberHook : public CObject
  175. {
  176. public:
  177.     virtual ~CCopyClassMemberHook(void);
  178.     
  179.     virtual void CopyClassMember(CObjectStreamCopier& copier,
  180.                                  const CObjectTypeInfoMI& member) = 0;
  181.     virtual void CopyMissingClassMember(CObjectStreamCopier& copier,
  182.                                         const CObjectTypeInfoMI& member);
  183.     void DefaultCopy(CObjectStreamCopier& copier,
  184.                      const CObjectTypeInfoMI& member);
  185. };
  186. class NCBI_XSERIAL_EXPORT CCopyChoiceVariantHook : public CObject
  187. {
  188. public:
  189.     virtual ~CCopyChoiceVariantHook(void);
  190.     virtual void CopyChoiceVariant(CObjectStreamCopier& copier,
  191.                                    const CObjectTypeInfoCV& variant) = 0;
  192.     void DefaultCopy(CObjectStreamCopier& copier,
  193.                      const CObjectTypeInfoCV& variant);
  194. };
  195. enum EDefaultHookAction {
  196.     eDefault_Normal,        // read or write data
  197.     eDefault_Skip           // skip data
  198. };
  199. class NCBI_XSERIAL_EXPORT CObjectHookGuardBase
  200. {
  201. protected:
  202.     // object read hook
  203.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  204.                          CReadObjectHook& hook,
  205.                          CObjectIStream* stream = 0);
  206.     // object write hook
  207.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  208.                          CWriteObjectHook& hook,
  209.                          CObjectOStream* stream = 0);
  210.     // object skip hook
  211.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  212.                          CSkipObjectHook& hook,
  213.                          CObjectIStream* stream = 0);
  214.     // object copy hook
  215.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  216.                          CCopyObjectHook& hook,
  217.                          CObjectStreamCopier* stream = 0);
  218.     // member read hook
  219.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  220.                          const string& id,
  221.                          CReadClassMemberHook& hook,
  222.                          CObjectIStream* stream = 0);
  223.     // member write hook
  224.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  225.                          const string& id,
  226.                          CWriteClassMemberHook& hook,
  227.                          CObjectOStream* stream = 0);
  228.     // member skip hook
  229.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  230.                          const string& id,
  231.                          CSkipClassMemberHook& hook,
  232.                          CObjectIStream* stream = 0);
  233.     // member copy hook
  234.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  235.                          const string& id,
  236.                          CCopyClassMemberHook& hook,
  237.                          CObjectStreamCopier* stream = 0);
  238.     // choice variant read hook
  239.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  240.                          const string& id,
  241.                          CReadChoiceVariantHook& hook,
  242.                          CObjectIStream* stream = 0);
  243.     // choice variant write hook
  244.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  245.                          const string& id,
  246.                          CWriteChoiceVariantHook& hook,
  247.                          CObjectOStream* stream = 0);
  248.     // choice variant skip hook
  249.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  250.                          const string& id,
  251.                          CSkipChoiceVariantHook& hook,
  252.                          CObjectIStream* stream = 0);
  253.     // choice variant copy hook
  254.     CObjectHookGuardBase(const CObjectTypeInfo& info,
  255.                          const string& id,
  256.                          CCopyChoiceVariantHook& hook,
  257.                          CObjectStreamCopier* stream = 0);
  258.     ~CObjectHookGuardBase(void);
  259.     void ResetHook(const CObjectTypeInfo& info);
  260. private:
  261.     CObjectHookGuardBase(const CObjectHookGuardBase&);
  262.     const CObjectHookGuardBase& operator=(const CObjectHookGuardBase&);
  263.     enum EHookMode {
  264.         eHook_None,
  265.         eHook_Read,
  266.         eHook_Write,
  267.         eHook_Skip,
  268.         eHook_Copy
  269.     };
  270.     enum EHookType {
  271.         eHook_Null,         // object hook
  272.         eHook_Object,       // object hook
  273.         eHook_Member,       // class member hook
  274.         eHook_Variant,      // choice variant hook
  275.         eHook_Element       // container element hook
  276.     };
  277.     union {
  278.         CObjectIStream*      m_IStream;
  279.         CObjectOStream*      m_OStream;
  280.         CObjectStreamCopier* m_Copier;
  281.     } m_Stream;
  282.     CRef<CObject> m_Hook;
  283.     EHookMode m_HookMode;
  284.     EHookType m_HookType;
  285.     string m_Id;            // member or variant id
  286. };
  287. template <class T>
  288. class CObjectHookGuard : CObjectHookGuardBase
  289. {
  290.     typedef CObjectHookGuardBase CParent;
  291. public:
  292.     // object read hook
  293.     CObjectHookGuard(CReadObjectHook& hook,
  294.                      CObjectIStream* stream = 0)
  295.         : CParent(CType<T>(), hook, stream)
  296.         {
  297.         }
  298.     // object write hook
  299.     CObjectHookGuard(CWriteObjectHook& hook,
  300.                      CObjectOStream* stream = 0)
  301.         : CParent(CType<T>(), hook, stream)
  302.         {
  303.         }
  304.     // object skip hook
  305.     CObjectHookGuard(CSkipObjectHook& hook,
  306.                      CObjectIStream* stream = 0)
  307.         : CParent(CType<T>(), hook, stream)
  308.         {
  309.         }
  310.     // object copy hook
  311.     CObjectHookGuard(CCopyObjectHook& hook,
  312.                      CObjectStreamCopier* stream = 0)
  313.         : CParent(CType<T>(), hook, stream)
  314.         {
  315.         }
  316.     // member read hook
  317.     CObjectHookGuard(const string& id,
  318.                      CReadClassMemberHook& hook,
  319.                      CObjectIStream* stream = 0)
  320.         : CParent(CType<T>(), id, hook, stream)
  321.         {
  322.         }
  323.     // member write hook
  324.     CObjectHookGuard(const string& id,
  325.                      CWriteClassMemberHook& hook,
  326.                      CObjectOStream* stream = 0)
  327.         : CParent(CType<T>(), id, hook, stream)
  328.         {
  329.         }
  330.     // member skip hook
  331.     CObjectHookGuard(const string& id,
  332.                      CSkipClassMemberHook& hook,
  333.                      CObjectIStream* stream = 0)
  334.         : CParent(CType<T>(), id, hook, stream)
  335.         {
  336.         }
  337.     // member copy hook
  338.     CObjectHookGuard(const string& id,
  339.                      CCopyClassMemberHook& hook,
  340.                      CObjectStreamCopier* stream = 0)
  341.         : CParent(CType<T>(), id, hook, stream)
  342.         {
  343.         }
  344.     // choice variant read hook
  345.     CObjectHookGuard(const string& id,
  346.                      CReadChoiceVariantHook& hook,
  347.                      CObjectIStream* stream = 0)
  348.         : CParent(CType<T>(), id, hook, stream)
  349.         {
  350.         }
  351.     // choice variant write hook
  352.     CObjectHookGuard(const string& id,
  353.                      CWriteChoiceVariantHook& hook,
  354.                      CObjectOStream* stream = 0)
  355.         : CParent(CType<T>(), id, hook, stream)
  356.         {
  357.         }
  358.     // choice variant skip hook
  359.     CObjectHookGuard(const string& id,
  360.                      CSkipChoiceVariantHook& hook,
  361.                      CObjectIStream* stream = 0)
  362.         : CParent(CType<T>(), id, hook, stream)
  363.         {
  364.         }
  365.     // choice variant copy hook
  366.     CObjectHookGuard(const string& id,
  367.                      CCopyChoiceVariantHook& hook,
  368.                      CObjectStreamCopier* stream = 0)
  369.         : CParent(CType<T>(), id, hook, stream)
  370.         {
  371.         }
  372.     ~CObjectHookGuard(void)
  373.         {
  374.             CParent::ResetHook(CType<T>());
  375.         }
  376. };
  377. /* @} */
  378. //#include <serial/objhook.inl>
  379. END_NCBI_SCOPE
  380. #endif  /* OBJHOOK__HPP */
  381. /* ---------------------------------------------------------------------------
  382. * $Log: objhook.hpp,v $
  383. * Revision 1000.1  2004/06/01 19:38:50  gouriano
  384. * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.15
  385. *
  386. * Revision 1.15  2004/04/30 13:28:40  gouriano
  387. * Remove obsolete function declarations
  388. *
  389. * Revision 1.14  2003/08/11 15:25:50  grichenk
  390. * Added possibility to reset an object member from
  391. * a read hook (including non-optional members).
  392. *
  393. * Revision 1.13  2003/07/29 18:47:46  vasilche
  394. * Fixed thread safeness of object stream hooks.
  395. *
  396. * Revision 1.12  2003/04/15 16:18:18  siyan
  397. * Added doxygen support
  398. *
  399. * Revision 1.11  2002/12/23 18:38:51  dicuccio
  400. * Added WIn32 export specifier: NCBI_XSERIAL_EXPORT.
  401. * Moved all CVS logs to the end.
  402. *
  403. * Revision 1.10  2002/09/19 14:00:37  grichenk
  404. * Implemented CObjectHookGuard for write and copy hooks
  405. * Added DefaultRead/Write/Copy methods to base hook classes
  406. *
  407. * Revision 1.8  2002/09/09 18:44:53  grichenk
  408. * Fixed CObjectHookGuard methods for GCC
  409. *
  410. * Revision 1.7  2002/09/09 18:20:18  grichenk
  411. * Fixed streamcludes (to declare Type<>)
  412. *
  413. * Revision 1.6  2002/09/09 18:13:59  grichenk
  414. * Added CObjectHookGuard class.
  415. * Added methods to be used by hooks for data
  416. * reading and skipping.
  417. *
  418. * Revision 1.5  2000/11/01 20:35:27  vasilche
  419. * Removed ECanDelete enum and related constructors.
  420. *
  421. * Revision 1.4  2000/10/03 17:22:34  vasilche
  422. * Reduced header dependency.
  423. * Reduced size of debug libraries on WorkShop by 3 times.
  424. * Fixed tag allocation for parent classes.
  425. * Fixed CObject allocation/deallocation stream streams.
  426. * Moved streamstantiation of several templates stream separate source file.
  427. *
  428. * Revision 1.3  2000/09/26 17:38:07  vasilche
  429. * Fixed streamcomplete choiceptr implementation.
  430. * Removed temporary comments.
  431. *
  432. * Revision 1.2  2000/09/18 20:00:04  vasilche
  433. * Separated CVariantInfo and CMemberInfo.
  434. * Implemented copy hooks.
  435. * All hooks now are stored stream CTypeInfo/CMemberInfo/CVariantInfo.
  436. * Most type specific functions now are implemented via function pointers instead of virtual functions.
  437. *
  438. * Revision 1.1  2000/08/15 19:44:39  vasilche
  439. * Added Read/Write hooks:
  440. * CReadObjectHook/CWriteObjectHook for objects of specified type.
  441. * CReadClassMemberHook/CWriteClassMemberHook for specified members.
  442. * CReadChoiceVariantHook/CWriteChoiceVariant for specified choice variants.
  443. * CReadContainerElementHook/CWriteContainerElementsHook for containers.
  444. *
  445. * ===========================================================================
  446. */