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

生物技术

开发平台:

C/C++

  1. /*
  2.  * ===========================================================================
  3.  * PRODUCTION $Log: choicetype.cpp,v $
  4.  * PRODUCTION Revision 1000.1  2004/06/01 19:42:30  gouriano
  5.  * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.29
  6.  * PRODUCTION
  7.  * ===========================================================================
  8.  */
  9. /*  $Id: choicetype.cpp,v 1000.1 2004/06/01 19:42:30 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. *   Type description for CHOIE type
  38. *
  39. * ---------------------------------------------------------------------------
  40. * $Log: choicetype.cpp,v $
  41. * Revision 1000.1  2004/06/01 19:42:30  gouriano
  42. * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.29
  43. *
  44. * Revision 1.29  2004/05/17 21:03:13  gorelenk
  45. * Added include of PCH ncbi_pch.hpp
  46. *
  47. * Revision 1.28  2003/03/11 20:06:47  kuznets
  48. * iterate -> ITERATE
  49. *
  50. * Revision 1.27  2002/12/26 19:29:47  gouriano
  51. * corrected CreateTypeInfo method to handle XML attribute lists
  52. *
  53. * Revision 1.26  2002/11/19 19:48:28  gouriano
  54. * added support of XML attributes of choice variants
  55. *
  56. * Revision 1.25  2002/11/14 21:03:39  gouriano
  57. * added support of XML attribute lists
  58. *
  59. * Revision 1.24  2002/10/15 13:58:04  gouriano
  60. * use "noprefix" flag
  61. *
  62. * Revision 1.23  2001/12/03 14:51:29  juran
  63. * Heed warning.
  64. *
  65. * Revision 1.22  2001/06/11 14:35:02  grichenk
  66. * Added support for numeric tags in ASN.1 specifications and data streams.
  67. *
  68. * Revision 1.21  2000/11/20 17:26:32  vasilche
  69. * Fixed warnings on 64 bit platforms.
  70. * Updated names of config variables.
  71. *
  72. * Revision 1.20  2000/11/08 17:02:50  vasilche
  73. * Added generation of modular DTD files.
  74. *
  75. * Revision 1.19  2000/11/07 17:26:25  vasilche
  76. * Added module names to CTypeInfo and CEnumeratedTypeValues
  77. * Added possibility to set include directory for whole module
  78. *
  79. * Revision 1.18  2000/10/03 17:22:49  vasilche
  80. * Reduced header dependency.
  81. * Reduced size of debug libraries on WorkShop by 3 times.
  82. * Fixed tag allocation for parent classes.
  83. * Fixed CObject allocation/deallocation in streams.
  84. * Moved instantiation of several templates in separate source file.
  85. *
  86. * Revision 1.17  2000/09/26 17:38:25  vasilche
  87. * Fixed incomplete choiceptr implementation.
  88. * Removed temporary comments.
  89. *
  90. * Revision 1.16  2000/09/18 20:00:28  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.15  2000/08/25 15:59:20  vasilche
  97. * Renamed directory tool -> datatool.
  98. *
  99. * Revision 1.14  2000/08/15 19:45:28  vasilche
  100. * Added Read/Write hooks:
  101. * CReadObjectHook/CWriteObjectHook for objects of specified type.
  102. * CReadClassMemberHook/CWriteClassMemberHook for specified members.
  103. * CReadChoiceVariantHook/CWriteChoiceVariant for specified choice variants.
  104. * CReadContainerElementHook/CWriteContainerElementsHook for containers.
  105. *
  106. * Revision 1.13  2000/06/16 16:31:38  vasilche
  107. * Changed implementation of choices and classes info to allow use of the same classes in generated and user written classes.
  108. *
  109. * Revision 1.12  2000/05/24 20:09:28  vasilche
  110. * Implemented DTD generation.
  111. *
  112. * Revision 1.11  2000/05/03 14:38:17  vasilche
  113. * SERIAL: added support for delayed reading to generated classes.
  114. * DATATOOL: added code generation for delayed reading.
  115. *
  116. * Revision 1.10  2000/04/17 19:11:07  vasilche
  117. * Fixed failed assertion.
  118. * Removed redundant namespace specifications.
  119. *
  120. * Revision 1.9  2000/04/12 15:36:49  vasilche
  121. * Added -on <namespace> argument to datatool.
  122. * Removed unnecessary namespace specifications in generated files.
  123. *
  124. * Revision 1.8  2000/04/07 19:26:24  vasilche
  125. * Added namespace support to datatool.
  126. * By default with argument -oR datatool will generate objects in namespace
  127. * NCBI_NS_NCBI::objects (aka ncbi::objects).
  128. * Datatool's classes also moved to NCBI namespace.
  129. *
  130. * Revision 1.7  2000/03/07 14:06:30  vasilche
  131. * Added generation of reference counted objects.
  132. *
  133. * Revision 1.6  2000/02/01 21:47:55  vasilche
  134. * Added CGeneratedChoiceTypeInfo for generated choice classes.
  135. * Removed CMemberInfo subclasses.
  136. * Added support for DEFAULT/OPTIONAL members.
  137. * Changed class generation.
  138. * Moved datatool headers to include/internal/serial/tool.
  139. *
  140. * Revision 1.5  2000/01/05 19:43:59  vasilche
  141. * Fixed error messages when reading from ASN.1 binary file.
  142. * Fixed storing of integers with enumerated values in ASN.1 binary file.
  143. * Added TAG support to key/value of map.
  144. * Added support of NULL variant in CHOICE.
  145. *
  146. * Revision 1.4  1999/12/28 18:55:56  vasilche
  147. * Reduced size of compiled object files:
  148. * 1. avoid inline or implicit virtual methods (especially destructors).
  149. * 2. avoid std::string's methods usage in inline methods.
  150. * 3. avoid string literals ("xxx") in inline methods.
  151. *
  152. * Revision 1.3  1999/12/21 17:18:33  vasilche
  153. * Added CDelayedFostream class which rewrites file only if contents is changed.
  154. *
  155. * Revision 1.2  1999/12/17 19:05:18  vasilche
  156. * Simplified generation of GetTypeInfo methods.
  157. *
  158. * Revision 1.1  1999/12/03 21:42:11  vasilche
  159. * Fixed conflict of enums in choices.
  160. *
  161. * ===========================================================================
  162. */
  163. #include <ncbi_pch.hpp>
  164. #include <serial/datatool/choicetype.hpp>
  165. #include <serial/autoptrinfo.hpp>
  166. #include <serial/choice.hpp>
  167. #include <serial/datatool/value.hpp>
  168. #include <serial/datatool/choicestr.hpp>
  169. #include <serial/datatool/choiceptrstr.hpp>
  170. #include <serial/member.hpp>
  171. #include <typeinfo>
  172. BEGIN_NCBI_SCOPE
  173. struct CAnyTypeChoice {
  174.     AnyType data;
  175.     TMemberIndex index;
  176.     CAnyTypeChoice(void)
  177.         : index(kEmptyChoice)
  178.         {
  179.         }
  180. };
  181. static TObjectPtr CreateAnyTypeChoice(TTypeInfo /*typeInfo*/)
  182. {
  183.     return new CAnyTypeChoice();
  184. }
  185. static
  186. TMemberIndex GetIndexAnyTypeChoice(const CChoiceTypeInfo* /*choiceType*/,
  187.                                    TConstObjectPtr choicePtr)
  188. {
  189.     const CAnyTypeChoice* choice =
  190.         static_cast<const CAnyTypeChoice*>(choicePtr);
  191.     return choice->index;
  192. }
  193. static
  194. void SetIndexAnyTypeChoice(const CChoiceTypeInfo* /*choiceType*/,
  195.                            TObjectPtr choicePtr,
  196.                            TMemberIndex index)
  197. {
  198.     CAnyTypeChoice* choice = static_cast<CAnyTypeChoice*>(choicePtr);
  199.     choice->index = index;
  200. }
  201. static
  202. void ResetIndexAnyTypeChoice(const CChoiceTypeInfo* /*choiceType*/,
  203.                              TObjectPtr choicePtr)
  204. {
  205.     CAnyTypeChoice* choice = static_cast<CAnyTypeChoice*>(choicePtr);
  206.     choice->index = kEmptyChoice;
  207. }
  208. const char* CChoiceDataType::GetASNKeyword(void) const
  209. {
  210.     return "CHOICE";
  211. }
  212. void CChoiceDataType::FixTypeTree(void) const
  213. {
  214.     CParent::FixTypeTree();
  215.     ITERATE ( TMembers, m, GetMembers() ) {
  216.         (*m)->GetType()->SetInChoice(this);
  217.     }
  218. }
  219. const char* CChoiceDataType::XmlMemberSeparator(void) const
  220. {
  221.     return " |";
  222. }
  223. bool CChoiceDataType::CheckValue(const CDataValue& value) const
  224. {
  225.     const CNamedDataValue* choice =
  226.         dynamic_cast<const CNamedDataValue*>(&value);
  227.     if ( !choice ) {
  228.         value.Warning("CHOICE value expected");
  229.         return false;
  230.     }
  231.     for ( TMembers::const_iterator i = GetMembers().begin();
  232.           i != GetMembers().end(); ++i ) {
  233.         if ( (*i)->GetName() == choice->GetName() )
  234.             return (*i)->GetType()->CheckValue(choice->GetValue());
  235.     }
  236.     return false;
  237. }
  238. CTypeInfo* CChoiceDataType::CreateTypeInfo(void)
  239. {
  240.     auto_ptr<CChoiceTypeInfo>
  241.         typeInfo(new CChoiceTypeInfo(sizeof(CAnyTypeChoice), GlobalName(),
  242.                                      TObjectPtr(0), &CreateAnyTypeChoice,
  243.                                      typeid(CAnyTypeChoice),
  244.                                      &GetIndexAnyTypeChoice,
  245.                                      &SetIndexAnyTypeChoice,
  246.                                      &ResetIndexAnyTypeChoice));
  247.     for ( TMembers::const_iterator i = GetMembers().begin();
  248.           i != GetMembers().end(); ++i ) {
  249.         CDataMember* member = i->get();
  250.         if (member->Attlist()) {
  251.             CMemberInfo* memInfo =
  252.                 typeInfo->AddMember(member->GetName(),0,
  253.                                     member->GetType()->GetTypeInfo());
  254.             if (member->NoPrefix()) {
  255.                 memInfo->SetNoPrefix();
  256.             }
  257.             if (member->Attlist()) {
  258.                 memInfo->SetAttlist();
  259.             }
  260.             if (member->Notag()) {
  261.                 memInfo->SetNotag();
  262.             }
  263.         } else {
  264.             CVariantInfo* varInfo = 
  265.                 typeInfo->AddVariant(member->GetName(), 0,
  266.                                      member->GetType()->GetTypeInfo());
  267.             if (member->NoPrefix()) {
  268.                 varInfo->SetNoPrefix();
  269.             }
  270.             if (member->Notag()) {
  271.                 varInfo->SetNotag();
  272.             }
  273.         }
  274.     }
  275.     return UpdateModuleName(typeInfo.release());
  276. }
  277. AutoPtr<CTypeStrings> CChoiceDataType::GenerateCode(void) const
  278. {
  279.     return GetFullCType();
  280. }
  281. AutoPtr<CTypeStrings> CChoiceDataType::GetRefCType(void) const
  282. {
  283.     if ( !GetVar("_virtual_choice").empty() ) {
  284.         AutoPtr<CTypeStrings> cls(new CClassRefTypeStrings(ClassName(),
  285.                                                            Namespace(),
  286.                                                            FileName()));
  287.         return AutoPtr<CTypeStrings>(new CChoicePtrRefTypeStrings(cls));
  288.     }
  289.     else {
  290.         return AutoPtr<CTypeStrings>(new CChoiceRefTypeStrings(ClassName(),
  291.                                                                Namespace(),
  292.                                                                FileName()));
  293.     }
  294. }
  295. AutoPtr<CTypeStrings> CChoiceDataType::GetFullCType(void) const
  296. {
  297.     if ( !GetVar("_virtual_choice").empty() ) {
  298.         AutoPtr<CChoicePtrTypeStrings>
  299.             code(new CChoicePtrTypeStrings(GlobalName(),
  300.                                            ClassName()));
  301.         ITERATE ( TMembers, i, GetMembers() ) {
  302.             AutoPtr<CTypeStrings> varType = (*i)->GetType()->GetFullCType();
  303.             code->AddVariant((*i)->GetName(), varType);
  304.         }
  305.         SetParentClassTo(*code);
  306.         return AutoPtr<CTypeStrings>(code.release());
  307.     }
  308.     else {
  309.         bool rootClass = GetParentType() == 0;
  310.         AutoPtr<CChoiceTypeStrings> code(new CChoiceTypeStrings(GlobalName(),
  311.                                                                 ClassName()));
  312.         bool haveUserClass = rootClass;
  313.         code->SetHaveUserClass(haveUserClass);
  314.         code->SetObject(true);
  315.         ITERATE ( TMembers, i, GetMembers() ) {
  316.             AutoPtr<CTypeStrings> varType = (*i)->GetType()->GetFullCType();
  317.             bool delayed = !GetVar((*i)->GetName()+"._delay").empty();
  318.             code->AddVariant((*i)->GetName(), varType, delayed,
  319.                              (*i)->GetType()->GetTag(),
  320.                              (*i)->NoPrefix(), (*i)->Attlist(), (*i)->Notag(),
  321.                              (*i)->SimpleType(),(*i)->GetType());
  322.             (*i)->GetType()->SetTypeStr(&(*code));
  323.         }
  324.         SetTypeStr(&(*code));
  325.         SetParentClassTo(*code);
  326.         return AutoPtr<CTypeStrings>(code.release());
  327.     }
  328. }
  329. END_NCBI_SCOPE