📄 serialobject.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: serialobject.cpp,v $ * PRODUCTION Revision 1000.4 2004/06/01 19:41:49 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.24 * PRODUCTION * =========================================================================== *//* $Id: serialobject.cpp,v 1000.4 2004/06/01 19:41:49 gouriano Exp $* ===========================================================================** PUBLIC DOMAIN NOTICE* National Center for Biotechnology Information** This software/database is a "United States Government Work" under the* terms of the United States Copyright Act. It was written as part of* the author's official duties as a United States Government employee and* thus cannot be copyrighted. This software/database is freely available* to the public for use. The National Library of Medicine and the U.S.* Government have not placed any restriction on its use or reproduction.** Although all reasonable efforts have been taken to ensure the accuracy* and reliability of the software and data, the NLM and the U.S.* Government do not and cannot warrant the performance or results that* may be obtained by using this software or data. The NLM and the U.S.* Government disclaim all warranties, express or implied, including* warranties of performance, merchantability or fitness for any particular* purpose.** Please cite the author in any work or product based on this material.** ===========================================================================** Author: Aleksey Grichenko** File Description:* Base class for serializable objects** ---------------------------------------------------------------------------* $Log: serialobject.cpp,v $* Revision 1000.4 2004/06/01 19:41:49 gouriano* PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.24** Revision 1.24 2004/05/17 21:03:03 gorelenk* Added include of PCH ncbi_pch.hpp** Revision 1.23 2004/03/25 15:57:08 gouriano* Added possibility to copy and compare serial object non-recursively** Revision 1.22 2004/02/09 18:22:35 gouriano* enforced checking environment vars when setting initialization* verification parameters** Revision 1.21 2004/01/16 19:56:09 gouriano* Added I/O stream manipulators for serializable objects** Revision 1.20 2003/11/21 16:17:44 vasilche* Correct closing of ASN.1 stream in DebugDump.** Revision 1.19 2003/11/17 22:13:23 gouriano* in CSerialObject::Assign: on self-assignment issue a warning only** Revision 1.18 2003/11/17 14:39:15 gouriano* Prohibit assignment of a serial object to itself** Revision 1.17 2003/11/13 14:07:38 gouriano* Elaborated data verification on read/write/get to enable skipping mandatory class data members** Revision 1.16 2003/09/22 20:56:31 gouriano* Changed base type of AnyContent object to CSerialObject** Revision 1.15 2003/09/16 14:48:36 gouriano* Enhanced AnyContent objects to support XML namespaces and attribute info items.** Revision 1.14 2003/08/25 15:59:09 gouriano* added possibility to use namespaces in XML i/o streams** Revision 1.13 2003/08/13 15:47:45 gouriano* implemented serialization of AnyContent objects** Revision 1.12 2003/07/17 18:48:01 gouriano* re-enabled initialization verification** Revision 1.11 2003/06/25 17:49:05 gouriano* fixed verification flag initialization, disabled verification** Revision 1.10 2003/06/04 21:23:04 gouriano* changed the value of ms_UnassignedStr** Revision 1.9 2003/05/21 16:12:04 vasilche* Correct index argument to ThrowUnassigned() to match index of GetItemInfo().** Revision 1.8 2003/05/21 16:01:52 vasilche* Avoid failed assert while generating exception message.** Revision 1.7 2003/04/29 18:30:37 gouriano* object data member initialization verification** Revision 1.6 2003/01/22 18:54:09 gouriano* added unfreezing of the string stream** Revision 1.5 2003/01/06 17:14:15 gouriano* corrected CSerialObject::DebugDump: disabled autoseparator in output stream** Revision 1.4 2002/07/30 20:24:58 grichenk* Fixed error messages in Assign() and Equals()** Revision 1.3 2002/05/30 14:16:44 gouriano* fix in debugdump: memory leak** Revision 1.2 2002/05/29 21:19:17 gouriano* added debug dump** Revision 1.1 2002/05/15 20:20:38 grichenk* Initial revision*** ===========================================================================*/#include <ncbi_pch.hpp>#include <corelib/ncbi_safe_static.hpp>#include <serial/serialbase.hpp>#include <serial/typeinfo.hpp>#include <serial/objostr.hpp>#include <serial/objistr.hpp>#include <serial/classinfob.hpp>BEGIN_NCBI_SCOPECSerialObject::CSerialObject(void){}CSerialObject::~CSerialObject(){}void CSerialObject::Assign(const CSerialObject& source, ESerialRecursionMode how){ if (this == &source) { ERR_POST(Warning << "CSerialObject::Assign(): an attempt to assign a serial object to itself"); return; } if ( typeid(source) != typeid(*this) ) { ERR_POST(Fatal << "CSerialObject::Assign() -- Assignment of incompatible types: " << typeid(*this).name() << " = " << typeid(source).name()); } GetThisTypeInfo()->Assign(this, &source, how);}bool CSerialObject::Equals(const CSerialObject& object, ESerialRecursionMode how) const{ if ( typeid(object) != typeid(*this) ) { ERR_POST(Fatal << "CSerialObject::Equals() -- Can not compare types: " << typeid(*this).name() << " == " << typeid(object).name()); } return GetThisTypeInfo()->Equals(this, &object, how);}void CSerialObject::DebugDump(CDebugDumpContext ddc, unsigned int depth) const{ ddc.SetFrame("CSerialObject"); CObject::DebugDump( ddc, depth);// this is not good, but better than nothing CNcbiOstrstream ostr; ostr << "\n****** begin ASN dump ******\n"; {{ auto_ptr<CObjectOStream> oos(CObjectOStream::Open(eSerial_AsnText, ostr)); oos->SetAutoSeparator(false); oos->Write(this, GetThisTypeInfo()); }} ostr << "\n****** end ASN dump ******\n" << '\0'; const char* str = ostr.str(); ostr.freeze(); ddc.Log( "Serial_AsnText", str);}/////////////////////////////////////////////////////////////////////////////// data verification setupconst char* CSerialObject::ms_UnassignedStr = "<*unassigned*>";const char CSerialObject::ms_UnassignedByte = char(0xcd);ESerialVerifyData CSerialObject::ms_VerifyDataDefault = eSerialVerifyData_Default;static CSafeStaticRef< CTls<int> > s_VerifyTLS;void CSerialObject::SetVerifyDataThread(ESerialVerifyData verify){ x_GetVerifyData(); ESerialVerifyData tls_verify = ESerialVerifyData(long(s_VerifyTLS->GetValue())); if (tls_verify != eSerialVerifyData_Never && tls_verify != eSerialVerifyData_Always && tls_verify != eSerialVerifyData_DefValueAlways) { s_VerifyTLS->SetValue(reinterpret_cast<int*>(verify)); }}void CSerialObject::SetVerifyDataGlobal(ESerialVerifyData verify){ x_GetVerifyData(); if (ms_VerifyDataDefault != eSerialVerifyData_Never && ms_VerifyDataDefault != eSerialVerifyData_Always && ms_VerifyDataDefault != eSerialVerifyData_DefValueAlways) { ms_VerifyDataDefault = verify; }}ESerialVerifyData CSerialObject::x_GetVerifyData(void){ ESerialVerifyData verify; if (ms_VerifyDataDefault == eSerialVerifyData_Never || ms_VerifyDataDefault == eSerialVerifyData_Always || ms_VerifyDataDefault == eSerialVerifyData_DefValueAlways) { verify = ms_VerifyDataDefault; } else { verify = ESerialVerifyData(long(s_VerifyTLS->GetValue())); if (verify == eSerialVerifyData_Default) { if (ms_VerifyDataDefault == eSerialVerifyData_Default) { // change the default here, if you wish ms_VerifyDataDefault = eSerialVerifyData_Yes; //ms_VerifyDataDefault = eSerialVerifyData_No; const char* str = getenv(SERIAL_VERIFY_DATA_GET); if (str) { if (NStr::CompareNocase(str,"YES") == 0) { ms_VerifyDataDefault = eSerialVerifyData_Yes; } else if (NStr::CompareNocase(str,"NO") == 0) { ms_VerifyDataDefault = eSerialVerifyData_No; } else if (NStr::CompareNocase(str,"NEVER") == 0) { ms_VerifyDataDefault = eSerialVerifyData_Never; } else if (NStr::CompareNocase(str,"ALWAYS") == 0) { ms_VerifyDataDefault = eSerialVerifyData_Always; } else if (NStr::CompareNocase(str,"DEFVALUE") == 0) { ms_VerifyDataDefault = eSerialVerifyData_DefValue; } else if (NStr::CompareNocase(str,"DEFVALUE_ALWAYS") == 0) { ms_VerifyDataDefault = eSerialVerifyData_DefValueAlways; } } } verify = ms_VerifyDataDefault; } } switch (verify) { default: case eSerialVerifyData_Default: return ms_VerifyDataDefault; case eSerialVerifyData_No: case eSerialVerifyData_Never: return eSerialVerifyData_No; break; case eSerialVerifyData_Yes: case eSerialVerifyData_Always: return eSerialVerifyData_Yes; break; case eSerialVerifyData_DefValue: case eSerialVerifyData_DefValueAlways: return eSerialVerifyData_No; }}void CSerialObject::ThrowUnassigned(TMemberIndex index) const{ if (x_GetVerifyData() == eSerialVerifyData_Yes) { const CTypeInfo* type = GetThisTypeInfo(); CNcbiOstrstream s; s << type->GetModuleName() << "::" << type->GetName() << "."; const CClassTypeInfoBase* classtype = dynamic_cast<const CClassTypeInfoBase*>(type); // offset index as the argument is zero based but items are 1 based
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -