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

生物技术

开发平台:

C/C++

  1. /*
  2.  * ===========================================================================
  3.  * PRODUCTION $Log: serial.cpp,v $
  4.  * PRODUCTION Revision 1000.1  2004/06/01 19:41:40  gouriano
  5.  * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.27
  6.  * PRODUCTION
  7.  * ===========================================================================
  8.  */
  9. /*  $Id: serial.cpp,v 1000.1 2004/06/01 19:41:40 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. *   Serialization classes.
  38. *
  39. * ---------------------------------------------------------------------------
  40. * $Log: serial.cpp,v $
  41. * Revision 1000.1  2004/06/01 19:41:40  gouriano
  42. * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.27
  43. *
  44. * Revision 1.27  2004/05/17 21:03:03  gorelenk
  45. * Added include of PCH ncbi_pch.hpp
  46. *
  47. * Revision 1.26  2003/08/14 20:03:58  vasilche
  48. * Avoid memory reallocation when reading over preallocated object.
  49. * Simplified CContainerTypeInfo iterators interface.
  50. *
  51. * Revision 1.25  2003/04/10 20:13:39  vakatov
  52. * Rollback the "uninitialized member" verification -- it still needs to
  53. * be worked upon...
  54. *
  55. * Revision 1.23  2003/03/06 21:48:41  grichenk
  56. * Removed type-info cleanup code
  57. *
  58. * Revision 1.22  2002/11/14 21:01:11  gouriano
  59. * modified AddMember to use CClassTypeInfoBase
  60. *
  61. * Revision 1.21  2002/11/05 17:45:10  grichenk
  62. * Reset object before reading
  63. *
  64. * Revision 1.20  2002/10/25 15:05:44  vasilche
  65. * Moved more code to libxcser library.
  66. *
  67. * Revision 1.19  2002/09/19 20:05:44  vasilche
  68. * Safe initialization of static mutexes
  69. *
  70. * Revision 1.18  2002/09/05 21:22:51  vasilche
  71. * Added mutex for module names set
  72. *
  73. * Revision 1.17  2001/08/24 13:48:02  grichenk
  74. * Prevented some memory leaks
  75. *
  76. * Revision 1.16  2000/11/07 17:25:41  vasilche
  77. * Fixed encoding of XML:
  78. *     removed unnecessary apostrophes in OCTET STRING
  79. *     removed unnecessary content in NULL
  80. * Added module names to CTypeInfo and CEnumeratedTypeValues
  81. *
  82. * Revision 1.15  2000/10/13 20:22:56  vasilche
  83. * Fixed warnings on 64 bit compilers.
  84. * Fixed missing typename in templates.
  85. *
  86. * Revision 1.14  2000/09/26 17:38:23  vasilche
  87. * Fixed incomplete choiceptr implementation.
  88. * Removed temporary comments.
  89. *
  90. * Revision 1.13  2000/09/18 20:00:25  vasilche
  91. * Separated CVariantInfo and CMemberInfo.
  92. * Implemented copy hooks.
  93. * All hooks now are stored in CTypeInfo/CMemberInfo/CVariantInfo.
  94. * Most type specific functions now are implemented via function pointers instead of virtual functions.
  95. *
  96. * Revision 1.12  2000/09/01 13:16:20  vasilche
  97. * Implemented class/container/choice iterators.
  98. * Implemented CObjectStreamCopier for copying data without loading into memory.
  99. *
  100. * Revision 1.11  2000/08/15 19:44:51  vasilche
  101. * Added Read/Write hooks:
  102. * CReadObjectHook/CWriteObjectHook for objects of specified type.
  103. * CReadClassMemberHook/CWriteClassMemberHook for specified members.
  104. * CReadChoiceVariantHook/CWriteChoiceVariant for specified choice variants.
  105. * CReadContainerElementHook/CWriteContainerElementsHook for containers.
  106. *
  107. * Revision 1.10  2000/07/11 20:36:19  vasilche
  108. * File included in all generated headers made lighter.
  109. * Nonnecessary code moved to serialimpl.hpp.
  110. *
  111. * Revision 1.9  2000/07/03 18:42:47  vasilche
  112. * Added interface to typeinfo via CObjectInfo and CConstObjectInfo.
  113. * Reduced header dependency.
  114. *
  115. * Revision 1.8  2000/06/16 16:31:22  vasilche
  116. * Changed implementation of choices and classes info to allow use of the same classes in generated and user written classes.
  117. *
  118. * Revision 1.7  2000/02/01 21:47:23  vasilche
  119. * Added CGeneratedChoiceTypeInfo for generated choice classes.
  120. * Added buffering to CObjectIStreamAsn.
  121. * Removed CMemberInfo subclasses.
  122. * Added support for DEFAULT/OPTIONAL members.
  123. *
  124. * Revision 1.6  1999/12/28 21:04:25  vasilche
  125. * Removed three more implicit virtual destructors.
  126. *
  127. * Revision 1.5  1999/12/17 19:05:04  vasilche
  128. * Simplified generation of GetTypeInfo methods.
  129. *
  130. * Revision 1.4  1999/11/22 21:04:41  vasilche
  131. * Cleaned main interface headers. Now generated files should include serial/serialimpl.hpp and user code should include serial/serial.hpp which became might lighter.
  132. *
  133. * Revision 1.3  1999/06/04 20:51:49  vasilche
  134. * First compilable version of serialization.
  135. *
  136. * Revision 1.2  1999/05/19 19:56:57  vasilche
  137. * Commit just in case.
  138. *
  139. * Revision 1.1  1999/03/25 19:12:04  vasilche
  140. * Beginning of serialization library.
  141. *
  142. * ===========================================================================
  143. */
  144. #include <ncbi_pch.hpp>
  145. #include <corelib/ncbistd.hpp>
  146. #include <corelib/ncbimtx.hpp>
  147. #include <serial/serial.hpp>
  148. #include <serial/serialimpl.hpp>
  149. #include <serial/serialbase.hpp>
  150. #include <serial/ptrinfo.hpp>
  151. #include <serial/classinfo.hpp>
  152. #include <serial/choice.hpp>
  153. #include <serial/objostr.hpp>
  154. #include <serial/objistr.hpp>
  155. #include <serial/memberlist.hpp>
  156. #include <corelib/ncbi_safe_static.hpp>
  157. BEGIN_NCBI_SCOPE
  158. TTypeInfo CPointerTypeInfoGetTypeInfo(TTypeInfo type)
  159. {
  160.     return CPointerTypeInfo::GetTypeInfo(type);
  161. }
  162. void Write(CObjectOStream& out, TConstObjectPtr object, const CTypeRef& type)
  163. {
  164.     out.Write(object, type.Get());
  165. }
  166. void Read(CObjectIStream& in, TObjectPtr object, const CTypeRef& type)
  167. {
  168.     //type.Get()->SetDefault(object);
  169.     in.Read(object, type.Get());
  170. }
  171. DEFINE_STATIC_FAST_MUTEX(s_ModuleNameMutex);
  172. static const string& GetModuleName(const char* moduleName)
  173. {
  174.     CFastMutexGuard GUARD(s_ModuleNameMutex);
  175.     static CSafeStaticPtr< set<string> > s_ModuleNames;
  176.     return *s_ModuleNames.Get().insert(moduleName).first;
  177. }
  178. void SetModuleName(CTypeInfo* info, const char* moduleName)
  179. {
  180.     info->SetModuleName(GetModuleName(moduleName));
  181. }
  182. void SetModuleName(CEnumeratedTypeValues* info, const char* moduleName)
  183. {
  184.     info->SetModuleName(GetModuleName(moduleName));
  185. }
  186. // add member functions
  187. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  188.                        const void* member,
  189.                        const CTypeRef& r)
  190. {
  191.     return info->AddMember(name, member, r);
  192. }
  193. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  194.                        const void* member,
  195.                        TTypeInfo t)
  196. {
  197.     return AddMember(info, name, member, CTypeRef(t));
  198. }
  199. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  200.                        const void* member,
  201.                        TTypeInfoGetter f)
  202. {
  203.     return AddMember(info, name, member, CTypeRef(f));
  204. }
  205. // two arguments:
  206. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  207.                        const void* member,
  208.                        TTypeInfoGetter1 f1,
  209.                        const CTypeRef& r)
  210. {
  211.     return AddMember(info, name, member, CTypeRef(f1, r));
  212. }
  213. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  214.                        const void* member,
  215.                        TTypeInfoGetter1 f1,
  216.                        TTypeInfo t)
  217. {
  218.     return AddMember(info, name, member, f1, CTypeRef(t));
  219. }
  220. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  221.                        const void* member,
  222.                        TTypeInfoGetter1 f1,
  223.                        TTypeInfoGetter f)
  224. {
  225.     return AddMember(info, name, member, f1, CTypeRef(f));
  226. }
  227. // three arguments:
  228. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  229.                        const void* member,
  230.                        TTypeInfoGetter1 f2,
  231.                        TTypeInfoGetter1 f1,
  232.                        const CTypeRef& r)
  233. {
  234.     return AddMember(info, name, member, f2, CTypeRef(f1, r));
  235. }
  236. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  237.                        const void* member,
  238.                        TTypeInfoGetter1 f2,
  239.                        TTypeInfoGetter1 f1,
  240.                        TTypeInfo t)
  241. {
  242.     return AddMember(info, name, member, f2, f1, CTypeRef(t));
  243. }
  244. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  245.                        const void* member,
  246.                        TTypeInfoGetter1 f2,
  247.                        TTypeInfoGetter1 f1,
  248.                        TTypeInfoGetter f)
  249. {
  250.     return AddMember(info, name, member, f2, f1, CTypeRef(f));
  251. }
  252. // four arguments:
  253. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  254.                        const void* member,
  255.                        TTypeInfoGetter1 f3,
  256.                        TTypeInfoGetter1 f2,
  257.                        TTypeInfoGetter1 f1,
  258.                        const CTypeRef& r)
  259. {
  260.     return AddMember(info, name, member, f3, f2, CTypeRef(f1, r));
  261. }
  262. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  263.                        const void* member,
  264.                        TTypeInfoGetter1 f3,
  265.                        TTypeInfoGetter1 f2,
  266.                        TTypeInfoGetter1 f1,
  267.                        TTypeInfo t)
  268. {
  269.     return AddMember(info, name, member, f3, f2, f1, CTypeRef(t));
  270. }
  271. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  272.                        const void* member,
  273.                        TTypeInfoGetter1 f3,
  274.                        TTypeInfoGetter1 f2,
  275.                        TTypeInfoGetter1 f1,
  276.                        TTypeInfoGetter f)
  277. {
  278.     return AddMember(info, name, member, f3, f2, f1, CTypeRef(f));
  279. }
  280. // five arguments:
  281. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  282.                        const void* member,
  283.                        TTypeInfoGetter1 f4,
  284.                        TTypeInfoGetter1 f3,
  285.                        TTypeInfoGetter1 f2,
  286.                        TTypeInfoGetter1 f1,
  287.                        const CTypeRef& r)
  288. {
  289.     return AddMember(info, name, member, f4, f3, f2, CTypeRef(f1, r));
  290. }
  291. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  292.                        const void* member,
  293.                        TTypeInfoGetter1 f4,
  294.                        TTypeInfoGetter1 f3,
  295.                        TTypeInfoGetter1 f2,
  296.                        TTypeInfoGetter1 f1,
  297.                        TTypeInfo t)
  298. {
  299.     return AddMember(info, name, member, f4, f3, f2, f1, CTypeRef(t));
  300. }
  301. CMemberInfo* AddMember(CClassTypeInfoBase* info, const char* name,
  302.                        const void* member,
  303.                        TTypeInfoGetter1 f4,
  304.                        TTypeInfoGetter1 f3,
  305.                        TTypeInfoGetter1 f2,
  306.                        TTypeInfoGetter1 f1,
  307.                        TTypeInfoGetter f)
  308. {
  309.     return AddMember(info, name, member, f4, f3, f2, f1, CTypeRef(f));
  310. }
  311. // add variant functions
  312. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  313.                        const void* member,
  314.                        const CTypeRef& r)
  315. {
  316.     return info->AddVariant(name, member, r);
  317. }
  318. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  319.                        const void* member,
  320.                        TTypeInfo t)
  321. {
  322.     return AddVariant(info, name, member, CTypeRef(t));
  323. }
  324. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  325.                        const void* member,
  326.                        TTypeInfoGetter f)
  327. {
  328.     return AddVariant(info, name, member, CTypeRef(f));
  329. }
  330. // two arguments:
  331. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  332.                        const void* member,
  333.                        TTypeInfoGetter1 f1,
  334.                        const CTypeRef& r)
  335. {
  336.     return AddVariant(info, name, member, CTypeRef(f1, r));
  337. }
  338. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  339.                        const void* member,
  340.                        TTypeInfoGetter1 f1,
  341.                        TTypeInfo t)
  342. {
  343.     return AddVariant(info, name, member, f1, CTypeRef(t));
  344. }
  345. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  346.                        const void* member,
  347.                        TTypeInfoGetter1 f1,
  348.                        TTypeInfoGetter f)
  349. {
  350.     return AddVariant(info, name, member, f1, CTypeRef(f));
  351. }
  352. // three arguments:
  353. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  354.                        const void* member,
  355.                        TTypeInfoGetter1 f2,
  356.                        TTypeInfoGetter1 f1,
  357.                        const CTypeRef& r)
  358. {
  359.     return AddVariant(info, name, member, f2, CTypeRef(f1, r));
  360. }
  361. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  362.                        const void* member,
  363.                        TTypeInfoGetter1 f2,
  364.                        TTypeInfoGetter1 f1,
  365.                        TTypeInfo t)
  366. {
  367.     return AddVariant(info, name, member, f2, f1, CTypeRef(t));
  368. }
  369. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  370.                        const void* member,
  371.                        TTypeInfoGetter1 f2,
  372.                        TTypeInfoGetter1 f1,
  373.                        TTypeInfoGetter f)
  374. {
  375.     return AddVariant(info, name, member, f2, f1, CTypeRef(f));
  376. }
  377. // four arguments:
  378. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  379.                        const void* member,
  380.                        TTypeInfoGetter1 f3,
  381.                        TTypeInfoGetter1 f2,
  382.                        TTypeInfoGetter1 f1,
  383.                        const CTypeRef& r)
  384. {
  385.     return AddVariant(info, name, member, f3, f2, CTypeRef(f1, r));
  386. }
  387. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  388.                        const void* member,
  389.                        TTypeInfoGetter1 f3,
  390.                        TTypeInfoGetter1 f2,
  391.                        TTypeInfoGetter1 f1,
  392.                        TTypeInfo t)
  393. {
  394.     return AddVariant(info, name, member, f3, f2, f1, CTypeRef(t));
  395. }
  396. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  397.                        const void* member,
  398.                        TTypeInfoGetter1 f3,
  399.                        TTypeInfoGetter1 f2,
  400.                        TTypeInfoGetter1 f1,
  401.                        TTypeInfoGetter f)
  402. {
  403.     return AddVariant(info, name, member, f3, f2, f1, CTypeRef(f));
  404. }
  405. // five arguments:
  406. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  407.                        const void* member,
  408.                        TTypeInfoGetter1 f4,
  409.                        TTypeInfoGetter1 f3,
  410.                        TTypeInfoGetter1 f2,
  411.                        TTypeInfoGetter1 f1,
  412.                        const CTypeRef& r)
  413. {
  414.     return AddVariant(info, name, member, f4, f3, f2, CTypeRef(f1, r));
  415. }
  416. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  417.                        const void* member,
  418.                        TTypeInfoGetter1 f4,
  419.                        TTypeInfoGetter1 f3,
  420.                        TTypeInfoGetter1 f2,
  421.                        TTypeInfoGetter1 f1,
  422.                        TTypeInfo t)
  423. {
  424.     return AddVariant(info, name, member, f4, f3, f2, f1, CTypeRef(t));
  425. }
  426. CVariantInfo* AddVariant(CChoiceTypeInfo* info, const char* name,
  427.                        const void* member,
  428.                        TTypeInfoGetter1 f4,
  429.                        TTypeInfoGetter1 f3,
  430.                        TTypeInfoGetter1 f2,
  431.                        TTypeInfoGetter1 f1,
  432.                        TTypeInfoGetter f)
  433. {
  434.     return AddVariant(info, name, member, f4, f3, f2, f1, CTypeRef(f));
  435. }
  436. CChoiceTypeInfo*
  437. CClassInfoHelperBase::CreateChoiceInfo(const char* name, size_t size,
  438.                                        const void* nonCObject,
  439.                                        TCreateFunction createFunc,
  440.                                        const type_info& ti,
  441.                                        TWhichFunction whichFunc,
  442.                                        TSelectFunction selectFunc,
  443.                                        TResetFunction resetFunc)
  444. {
  445.     return new CChoiceTypeInfo(size, name, nonCObject, createFunc,
  446.                                ti, whichFunc, selectFunc, resetFunc);
  447. }
  448. CChoiceTypeInfo*
  449. CClassInfoHelperBase::CreateChoiceInfo(const char* name, size_t size,
  450.                                        const CObject* cObject,
  451.                                        TCreateFunction createFunc,
  452.                                        const type_info& ti,
  453.                                        TWhichFunction whichFunc,
  454.                                        TSelectFunction selectFunc,
  455.                                        TResetFunction resetFunc)
  456. {
  457.     return new CChoiceTypeInfo(size, name, cObject, createFunc,
  458.                                ti, whichFunc, selectFunc, resetFunc);
  459. }
  460. CClassTypeInfo*
  461. CClassInfoHelperBase::CreateClassInfo(const char* name, size_t size,
  462.                                       const void* nonCObject,
  463.                                       TCreateFunction createFunc,
  464.                                       const type_info& id,
  465.                                       TGetTypeIdFunction idFunc)
  466. {
  467.     return new CClassTypeInfo(size, name, nonCObject, createFunc, id, idFunc);
  468. }
  469. CClassTypeInfo*
  470. CClassInfoHelperBase::CreateClassInfo(const char* name, size_t size,
  471.                                       const CObject* cObject,
  472.                                       TCreateFunction createFunc,
  473.                                       const type_info& id,
  474.                                       TGetTypeIdFunction idFunc)
  475. {
  476.     return new CClassTypeInfo(size, name, cObject, createFunc, id, idFunc);
  477. }
  478. void SetPreWrite(CClassTypeInfo* info, TPreWriteFunction func)
  479. {
  480.     info->SetPreWriteFunction(func);
  481. }
  482. void SetPostRead(CClassTypeInfo* info, TPostReadFunction func)
  483. {
  484.     info->SetPostReadFunction(func);
  485. }
  486. void SetPreWrite(CChoiceTypeInfo* info, TPreWriteFunction func)
  487. {
  488.     info->SetPreWriteFunction(func);
  489. }
  490. void SetPostRead(CChoiceTypeInfo* info, TPostReadFunction func)
  491. {
  492.     info->SetPostReadFunction(func);
  493. }
  494. // Functions preventing memory leaks due to undestroyed type info objects
  495. void RegisterEnumTypeValuesObject(CEnumeratedTypeValues* /*object*/)
  496. {
  497. /*
  498.     typedef AutoPtr<CEnumeratedTypeValues> TEnumTypePtr;
  499.     typedef list<TEnumTypePtr>             TEnumTypeList;
  500.     static CSafeStaticPtr<TEnumTypeList> s_EnumTypeList;
  501.     TEnumTypePtr ap(object);
  502.     s_EnumTypeList.Get().push_back(ap);
  503. */
  504. }
  505. void RegisterTypeInfoObject(CTypeInfo* /*object*/)
  506. {
  507. /*
  508.     typedef AutoPtr<CTypeInfo> TTypeInfoPtr;
  509.     typedef list<TTypeInfoPtr> TTypeInfoList;
  510.     static CSafeStaticPtr<TTypeInfoList> s_TypeInfoList;
  511.     TTypeInfoPtr ap(object);
  512.     s_TypeInfoList.Get().push_back(ap);
  513. */
  514. }
  515. END_NCBI_SCOPE