NCBI C++ ToolKit
objectio.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: objectio.cpp 76732 2017-03-01 17:43:15Z gouriano $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Eugene Vasilchenko
27 *
28 * File Description:
29 * !!! PUT YOUR DESCRIPTION HERE !!!
30 */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbistd.hpp>
34 #include <serial/objectio.hpp>
35 #include <serial/objistr.hpp>
36 #include <serial/objostr.hpp>
37 #include <serial/objcopy.hpp>
38 #include <serial/objhook.hpp>
39 
41 
42 
43 // readers
46 {
47  const CContainerTypeInfo* containerType = GetContainerTypeInfo();
48  BEGIN_OBJECT_FRAME_OF2(in, eFrameArray, containerType);
49  in.BeginContainer(containerType);
50 
51  TTypeInfo elementType = containerType->GetElementType();
52  BEGIN_OBJECT_FRAME_OF2(in, eFrameArrayElement, elementType);
53 
54  while ( in.BeginContainerElement(elementType) ) {
55  hook.ReadContainerElement(in, *this);
56  in.EndContainerElement();
57  }
58 
60 
61  in.EndContainer();
63 }
64 
65 inline
67  : m_Stream(stream), m_Depth(stream.GetStackDepth())
68 {
69 }
70 
72 {
73  if ( GetStream().GetStackDepth() != m_Depth ) {
74  try {
76  }
77  catch (...) {
79  "object stack frame error");
80  }
81  }
82 }
83 
84 inline
85 bool CIStreamFrame::Good(void) const
86 {
87  return GetStream().InGoodState();
88 }
89 
90 inline
92  : m_Stream(stream), m_Depth(stream.GetStackDepth())
93 {
94 }
95 
96 inline
97 bool COStreamFrame::Good(void) const
98 {
99  return GetStream().InGoodState();
100 }
101 
103 {
104  if ( GetStream().GetStackDepth() != m_Depth ) {
105  try {
107  }
108  catch (...) {
110  "object stack frame error");
111  }
112  }
113 }
114 
115 #ifdef NCBI_COMPILER_ICC
116 void* COStreamFrame::operator new(size_t size)
117 {
118  return ::operator new(size);
119 }
120 
121 void* COStreamFrame::operator new[](size_t size)
122 {
123  return ::operator new[](size);
124 }
125 
126 void* CIStreamFrame::operator new(size_t size)
127 {
128  return ::operator new(size);
129 }
130 
131 void* CIStreamFrame::operator new[](size_t size)
132 {
133  return ::operator new[](size);
134 }
135 #endif
136 
137 
138 /////////////////////////////////////////////////////////////////////////////
139 // read/write classMember
140 
141 inline
143 {
145 }
146 
147 inline
149 {
151  m_MemberIndex =
153  } else {
154  m_MemberIndex =
156  m_MemberIndex + 1);
157  }
158  if ( *this )
160 }
161 
162 inline
163 void CIStreamClassMemberIterator::IllegalCall(const char* message) const
164 {
165  GetStream().ThrowError(CObjectIStream::fIllegalCall, message);
166 }
167 
168 inline
170 {
171  IllegalCall("bad CIStreamClassMemberIterator state");
172 }
173 
175  const CObjectTypeInfo& classType)
176  : CParent(in), m_ClassType(classType)
177 {
178  const CClassTypeInfo* classTypeInfo = classType.GetClassTypeInfo();
179  in.PushFrame(CObjectStackFrame::eFrameClass, classTypeInfo);
180  in.BeginClass(classTypeInfo);
181 
185 }
186 
188 {
189  if ( Good() ) {
190  try {
191  if ( *this )
193  GetStream().PopFrame();
194  GetStream().EndClass();
195  GetStream().PopFrame();
196  }
197  catch (...) {
199  "class member iterator error");
200  }
201  }
202 }
203 
204 inline
206 {
207  if ( m_MemberIndex == kInvalidMember )
208  BadState();
209 }
210 
212 {
213  CheckState();
216 }
217 
219 {
220  CheckState();
222 }
223 
225 {
226  CheckState();
227  GetStream().SkipObject(member.GetTypeInfo());
228 }
229 
231 {
232  CheckState();
233  GetStream().SkipObject(GetMemberInfo()->GetTypeInfo());
234 }
235 
236 
237 /////////////////////////////////////////////////////////////////////////////
238 // read/write class member
239 
241  const CObjectTypeInfoMI& member)
242  : CParent(out)
243 {
244  const CMemberInfo* memberInfo = member.GetMemberInfo();
245  out.PushFrame(CObjectStackFrame::eFrameClassMember, memberInfo->GetId());
246  out.BeginClassMember(memberInfo->GetId());
247 }
248 
250 {
251  if ( Good() ) {
252  try {
254  GetStream().PopFrame();
255  }
256  catch (...) {
258  "class member write error");
259  }
260  }
261 }
262 
263 // read/write container
264 inline
266 {
268  if ( GetStream().BeginContainerElement(m_ElementTypeInfo) )
270  else
272 }
273 
274 inline
275 void CIStreamContainerIterator::IllegalCall(const char* message) const
276 {
277  GetStream().ThrowError(CObjectIStream::fIllegalCall, message);
278  // GetStream().SetFailFlags(CObjectIStream::fIllegalCall, message);
279 }
280 
281 inline
283 {
284  IllegalCall("bad CIStreamContainerIterator state");
285 }
286 
288  const CObjectTypeInfo& containerType)
289  : CParent(in), m_ContainerType(containerType), m_State(eElementEnd)
290 {
291  const CContainerTypeInfo* containerTypeInfo;
293  const CClassTypeInfo* classType =
295  const CItemInfo* itemInfo =
296  classType->GetItems().GetItemInfo(classType->GetItems().FirstIndex());
298  m_Container = itemInfo;
299  containerTypeInfo =
302  in.BeginNamedType(m_ContainerType.GetTypeInfo());
303  } else {
304  m_Container = nullptr;
305  containerTypeInfo = GetContainerType().GetContainerTypeInfo();
306  }
307  m_ContainerTypeInfo = containerTypeInfo;
308 
309  in.PushFrame(CObjectStackFrame::eFrameArray, containerTypeInfo);
310  in.BeginContainer(containerTypeInfo);
311 
312  TTypeInfo elementTypeInfo = m_ElementTypeInfo =
313  containerTypeInfo->GetElementType();
314  in.PushFrame(CObjectStackFrame::eFrameArrayElement, elementTypeInfo);
315  BeginElement();
316  if ( m_State == eNoMoreElements ) {
317  in.PopFrame();
318  in.EndContainer();
319  in.PopFrame();
321  in.EndNamedType();
322  in.PopFrame();
323  }
324  }
325 }
326 
328 {
329  if ( Good() ) {
330  switch ( m_State ) {
331  case eNoMoreElements:
332  // normal state
333  return;
334  case eElementBegin:
335  case eElementEnd:
336  // not read element(s)
337  m_State = eError;
339  "not all elements read");
340  break;
341  default:
342  // error -> do nothing
343  return;
344  }
345  }
346 }
347 
348 inline
350 {
351  bool ok = (m_State == state);
352  if ( !ok ) {
353  m_State = eError;
354  BadState();
355  }
356 }
357 
358 
360 {
364  BeginElement();
365  if ( m_State == eNoMoreElements ) {
366  GetStream().PopFrame();
368  GetStream().PopFrame();
371  GetStream().PopFrame();
372  }
373  }
374  if (m_State != eNoMoreElements) {
376  }
377 }
378 
379 inline
381 {
383 }
384 
385 inline
387 {
388  //if ( elementType.GetTypeInfo() != GetElementTypeInfo() )
389  // IllegalCall("wrong element type");
391 }
392 
394 {
396  TObjectPtr cont = m_Container ? m_Container->GetItemPtr(container) : container;
398  NextElement();
399  return CObjectInfo(elem, m_ElementTypeInfo);
400 }
401 
403 {
404  BeginElementData(element);
405  GetStream().ReadSeparateObject(element);
406  NextElement();
407 }
408 
410 {
411  BeginElementData(elementType);
412  GetStream().SkipObject(elementType.GetTypeInfo());
413  NextElement();
414 }
415 
417 {
420  NextElement();
421 }
422 
425 {
427  out.GetStream().BeginContainerElement(m_ElementTypeInfo);
429  out.GetStream().EndContainerElement();
430  NextElement();
431 }
432 
434 {
435  if (m_State == eElementBegin) {
436  SkipElement();
437  }
438  if (m_State != eNoMoreElements) {
441  }
442  else {
443  m_State = eFinished;
444  }
445  return *this;
446 }
447 
448 
450  const CObjectTypeInfo& containerType)
451  : CParent(out), m_ContainerType(containerType)
452 {
453  const CContainerTypeInfo* containerTypeInfo;
455  const CClassTypeInfo* classType =
457  const CItemInfo* itemInfo =
458  classType->GetItems().GetItemInfo(classType->GetItems().FirstIndex());
460  containerTypeInfo =
463  out.BeginNamedType(m_ContainerType.GetTypeInfo());
464  } else {
465  containerTypeInfo = GetContainerType().GetContainerTypeInfo();
466  }
467  out.PushFrame(CObjectStackFrame::eFrameArray, containerTypeInfo);
468  out.BeginContainer(containerTypeInfo);
469 
470  TTypeInfo elementTypeInfo = m_ElementTypeInfo =
471  containerTypeInfo->GetElementType();
472  out.PushFrame(CObjectStackFrame::eFrameArrayElement, elementTypeInfo);
473 }
474 
476 {
477  if ( Good() ) {
478  try {
479  GetStream().PopFrame();
481  GetStream().PopFrame();
484  GetStream().PopFrame();
485  }
486  }
487  catch (...) {
489  "container write error");
490  }
491  }
492 }
493 
495 {
497  GetStream().WriteSeparateObject(element);
499 }
500 
502  CObjectIStream& /*in*/)
503 {
507 }
508 
509 
CConstObjectInfo –.
Definition: objectinfo.hpp:421
Reading (iterating through) elements of containers (SET OF, SEQUENCE OF).
Definition: objectio.hpp:164
Writing containers (SET OF, SEQUENCE OF).
Definition: objectio.hpp:237
CObjectIStream –.
Definition: objistr.hpp:93
CObjectInfo –.
Definition: objectinfo.hpp:597
CObjectOStream –.
Definition: objostr.hpp:83
CObjectStreamCopier –.
Definition: objcopy.hpp:71
CObjectTypeInfoMI –.
Definition: objectiter.hpp:246
CObjectTypeInfo –.
Definition: objectinfo.hpp:94
Read hook for a container element (SEQUENCE OF)
Definition: objhook.hpp:151
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Include a standard set of the NCBI C++ Toolkit most basic headers.
std::ofstream out("events_result.xml")
main entry point for tests
const CMemberId & GetId(void) const
TObjectPtr GetItemPtr(TObjectPtr object) const
const CItemInfo * GetItemInfo(TMemberIndex index) const
static TMemberIndex FirstIndex(void)
Definition: memberlist.hpp:78
TTypeInfo GetTypeInfo(void) const
void ReadMember(CObjectIStream &in, TObjectPtr classPtr) const
void * TObjectPtr
Definition: serialdef.hpp:55
const TMemberIndex kFirstMemberIndex
Start if member indexing.
Definition: serialdef.hpp:235
const TMemberIndex kInvalidMember
Special value returned from FindMember.
Definition: serialdef.hpp:237
static const TObjectType * SafeCast(TTypeInfo type)
Definition: serialutil.hpp:76
@ eTypeFamilyClass
Definition: serialdef.hpp:140
@ eTypeFamilyContainer
Definition: serialdef.hpp:142
CObjectInfo ReadElement(TObjectPtr container)
Definition: objectio.cpp:393
CObjectTypeInfo m_ContainerType
Definition: objectio.hpp:255
virtual void EndClassMember(void)
Definition: objostr.cpp:888
virtual void EndClassMember(void)
Definition: objistr.cpp:1372
CIStreamContainerIterator(CObjectIStream &in, const CObjectTypeInfo &containerType)
Definition: objectio.cpp:287
const CObjectTypeInfo & GetContainerType(void) const
virtual void EndContainerElement(void)
Definition: objistr.cpp:1314
~COStreamClassMember(void)
Definition: objectio.cpp:249
virtual void EndContainer(void)=0
COStreamContainer(CObjectOStream &out, const CObjectTypeInfo &containerType)
Definition: objectio.cpp:449
size_t m_Depth
Definition: objectio.hpp:62
~COStreamFrame(void)
Definition: objectio.cpp:102
const CContainerTypeInfo * m_ContainerTypeInfo
Definition: objectio.hpp:211
virtual void EndContainerElement(void)
Definition: objostr.cpp:806
virtual TMemberIndex BeginClassMember(const CClassTypeInfo *classType)=0
CObjectTypeInfo m_ClassType
Definition: objectio.hpp:149
void BadState(void) const
Definition: objectio.cpp:282
const CMemberInfo * GetMemberInfo(void) const
const CMemberInfo * GetMemberInfo(void) const
Definition: objectio.cpp:142
const CObjectTypeInfo & GetContainerType(void) const
void SkipObject(const CObjectTypeInfo &objectType)
Skip child object.
Definition: objistr.cpp:1102
COStreamFrame(CObjectOStream &stream)
Definition: objectio.cpp:91
TObjectPtr GetObjectPtr(void) const
Get pointer to object.
void ReadClassMember(const CObjectInfo &classMember)
Definition: objectio.cpp:218
void BadState(void) const
Definition: objectio.cpp:169
~CIStreamFrame(void)
Definition: objectio.cpp:71
virtual void EndNamedType(void)
Definition: objistr.cpp:1280
virtual void ReadContainerElement(CObjectIStream &in, const CObjectInfo &container)=0
TTypeInfo GetTypeInfo(void) const
#define BEGIN_OBJECT_FRAME_OF2(Stream, Type, Arg)
Definition: objstack.hpp:219
const CItemInfo * m_Container
Definition: objectio.hpp:212
bool InGoodState(void)
Check fail flags and also the state of input data source.
Definition: objistr.cpp:570
void ReadContainer(CObjectIStream &in, CReadContainerElementHook &hook)
Read element data from stream.
Definition: objectio.cpp:44
CIStreamClassMemberIterator(CObjectIStream &in, const CObjectTypeInfo &classMemberType)
Definition: objectio.cpp:174
bool InGoodState(void)
Check fail flags and also the state of output stream.
Definition: objostr.cpp:386
bool Good(void) const
Definition: objectio.cpp:97
CObjectTypeInfo m_ContainerType
Definition: objectio.hpp:208
const CClassTypeInfo * GetClassTypeInfo(void) const
Definition: objectinfo.cpp:61
~COStreamContainer(void)
Definition: objectio.cpp:475
size_t m_Depth
Definition: objectio.hpp:82
TFailFlags SetFailFlags(TFailFlags flags, const char *message=0)
Set fail flags.
Definition: objistr.cpp:552
void SetTopMemberId(const CMemberId &memberId)
TTypeInfo m_ElementTypeInfo
Definition: objectio.hpp:256
ETypeFamily GetTypeFamily(void) const
Get data type family.
const CObjectTypeInfo GetElementType(void) const
CIStreamContainerIterator & operator++(void)
Definition: objectio.cpp:433
void CheckState(EState state)
Definition: objectio.cpp:349
void PopFrame(void)
CObjectOStream & GetStream(void) const
CObjectIStream & GetStream(void) const
CIStreamFrame(CObjectIStream &stream)
Definition: objectio.cpp:66
void ReadSeparateObject(const CObjectInfo &object)
Temporary reader.
Definition: objistr.cpp:1053
void WriteElement(const CConstObjectInfo &element)
Definition: objectio.cpp:494
void IllegalCall(const char *message) const
Definition: objectio.cpp:163
virtual void EndClass(void)
Definition: objistr.cpp:1368
virtual void EndNamedType(void)
Definition: objostr.cpp:738
void PopErrorFrame(void)
Definition: objstack.cpp:296
void WriteSeparateObject(const CConstObjectInfo &object)
Definition: objostr.cpp:645
TFailFlags SetFailFlags(TFailFlags flags, const char *message)
Set fail flags.
Definition: objostr.cpp:373
const CContainerTypeInfo * GetContainerTypeInfo(void) const
Definition: objectinfo.cpp:73
void IllegalCall(const char *message) const
Definition: objectio.cpp:275
#define END_OBJECT_FRAME_OF(Stream)
Definition: objstack.hpp:201
virtual void BeginContainerElement(TTypeInfo elementType)
Definition: objostr.cpp:802
bool Good(void) const
Definition: objectio.cpp:85
COStreamClassMember(CObjectOStream &out, const CObjectTypeInfo::CMemberIterator &member)
Definition: objectio.cpp:240
virtual void EndContainer(void)
Definition: objostr.cpp:798
void CopyObject(TTypeInfo type)
Copy object, omitting file header both in input and output streams.
void CopyElement(CObjectStreamCopier &copier, COStreamContainer &out)
Definition: objectio.cpp:423
@ fIllegalCall
Illegal in a given context function call.
Definition: objistr.hpp:383
@ fIllegalCall
Illegal in a given context function call.
Definition: objostr.hpp:324
const CSeq_id & GetId(const CSeq_loc &loc, CScope *scope)
If all CSeq_ids embedded in CSeq_loc refer to the same CBioseq, returns the first CSeq_id found,...
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
const CItemsInfo & GetItems(void) const
ETypeFamily GetTypeFamily(void) const
TObjectPtr AddElement(TObjectPtr containerPtr, TConstObjectPtr elementPtr, ESerialRecursionMode how=eRecursive) const
bool RandomOrder(void) const
const CMemberInfo * GetMemberInfo(TMemberIndex index) const
TTypeInfo GetElementType(void) const
const struct ncbi::grid::netcache::search::fields::SIZE size
std::istream & in(std::istream &in_, double &x_)
#define _ASSERT
Modified on Fri Sep 20 14:58:30 2024 by modify_doxy.py rev. 669887