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

Go to the SVN repository for this file.

1 /* $Id: objcopy.cpp 84832 2018-12-17 14:45:09Z 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 */
33 
34 #include <ncbi_pch.hpp>
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbimtx.hpp>
37 
38 #include <serial/objcopy.hpp>
39 #include <serial/typeinfo.hpp>
40 #include <serial/objectinfo.hpp>
42 #include <serial/impl/continfo.hpp>
43 #include <serial/impl/choice.hpp>
44 #include <serial/objistr.hpp>
45 #include <serial/objostr.hpp>
47 #include <serial/impl/objlist.hpp>
48 #include <serial/serialimpl.hpp>
49 
50 #undef _TRACE
51 #define _TRACE(arg) ((void)0)
52 
54 
57  : m_In(in), m_Out(out)
58 {
59 }
60 
62 {
64 }
65 
67 {
72 }
73 
75 {
76  TTypeInfo type = objectType.GetTypeInfo();
77  // root object
78  BEGIN_OBJECT_2FRAMES2(eFrameNamed, type);
81 
83 
84  Separator(Out());
85 
86  Out().EndOfWrite();
87  In().EndOfRead();
89 }
90 
92 {
93  // root object
94  BEGIN_OBJECT_2FRAMES2(eFrameNamed, type);
96 
98 
99  Separator(Out());
100 
101  Out().EndOfWrite();
102  In().EndOfRead();
104 }
105 
107 {
108  _TRACE("CObjectIStream::CopyPointer("<<declaredType->GetName()<<")");
110  if ( ptype != CObjectIStream::eNullPointer && !In().DetectLoops() ) {
111  CopyObject(declaredType);
112  return;
113  }
114  TTypeInfo typeInfo;
115  switch ( ptype ) {
117  _TRACE("CObjectIStream::CopyPointer: null");
118  Out().WriteNullPointer();
119  return;
121  {
122  _TRACE("CObjectIStream::CopyPointer: @...");
124  _TRACE("CObjectIStream::CopyPointer: @" << index);
125  typeInfo = In().GetRegisteredObject(index).GetTypeInfo();
126  Out().WriteObjectReference(index);
127  break;
128  }
130  {
131  _TRACE("CObjectIStream::CopyPointer: new");
132  In().RegisterObject(declaredType);
133  Out().RegisterObject(declaredType);
134  CopyObject(declaredType);
135  return;
136  }
138  {
139  _TRACE("CObjectIStream::CopyPointer: new...");
140  string className = In().ReadOtherPointer();
141  _TRACE("CObjectIStream::CopyPointer: new " << className);
142  typeInfo = CClassTypeInfoBase::GetClassInfoByName(className);
143 
144  BEGIN_OBJECT_2FRAMES2(eFrameNamed, typeInfo);
145 
146  In().RegisterObject(typeInfo);
147  Out().RegisterObject(typeInfo);
148 
149  Out().WriteOtherBegin(typeInfo);
150 
151  CopyObject(typeInfo);
152 
153  Out().WriteOtherEnd(typeInfo);
154 
156 
158  break;
159  }
160  default:
161  ThrowError(CObjectIStream::fFormatError,"illegal pointer type");
162  return;
163  }
164  while ( typeInfo != declaredType ) {
165  // try to check parent class pointer
166  if ( typeInfo->GetTypeFamily() != eTypeFamilyClass ) {
167  ThrowError(CObjectIStream::fFormatError,"incompatible member type");
168  }
169  const CClassTypeInfo* parentClass =
170  CTypeConverter<CClassTypeInfo>::SafeCast(typeInfo)->GetParentClassInfo();
171  if ( parentClass ) {
172  typeInfo = parentClass;
173  }
174  else {
175  ThrowError(CObjectIStream::fFormatError,"incompatible member type");
176  }
177  }
178  return;
179 }
180 
182 {
184  if ( ptype == CObjectIStream::eNullPointer ) {
185  Out().WriteNullPointer();
186  return true;
187  }
188  return false;
189 }
190 
192 {
194  if ( ib.KnownLength() ) {
195  size_t length = ib.GetExpectedLength();
196  CObjectOStream::ByteBlock ob(Out(), length);
197  char buffer[4096];
198  size_t count;
199  while ( (count = ib.Read(buffer, sizeof(buffer))) != 0 ) {
200  ob.Write(buffer, count);
201  }
202  ob.End();
203  }
204  else {
205  // length is unknown -> copy via buffer
206  vector<char> o;
207  {
208  // load data
209  char buffer[4096];
210  size_t count;
211  while ( (count = ib.Read(buffer, sizeof(buffer))) != 0 ) {
212  o.insert(o.end(), buffer, buffer + count);
213  }
214  }
215  {
216  // store data
217  size_t length = o.size();
218  CObjectOStream::ByteBlock ob(Out(), length);
219  if ( length > 0 )
220  ob.Write(&o.front(), length);
221  ob.End();
222  }
223  }
224  ib.End();
225 }
226 
228  CCopyObjectHook* hook)
229 {
230  m_PathCopyObjectHooks.SetHook(path,hook);
231  In().WatchPathHooks();
232  Out().WatchPathHooks();
233 }
235  CCopyClassMemberHook* hook)
236 {
237  m_PathCopyMemberHooks.SetHook(path,hook);
238  In().WatchPathHooks();
239  Out().WatchPathHooks();
240 }
243 {
244  m_PathCopyVariantHooks.SetHook(path,hook);
245  In().WatchPathHooks();
246  Out().WatchPathHooks();
247 }
248 
250 {
253  if (hook) {
255  if (item) {
256  item->SetPathCopyHook(this,stk.GetStackPath(),set ? hook : NULL);
257  }
258  }
259  }
262  if (hook) {
264  if (item) {
265  item->SetPathCopyHook(this,stk.GetStackPath(),set ? hook : NULL);
266  }
267  }
268  }
271  if (hook) {
273  if (item) {
274  item->SetPathCopyHook(this,stk.GetStackPath(),set ? hook : NULL);
275  }
276  }
277  }
278 }
279 
Copy hook for a choice variant (CHOICE)
Definition: objhook.hpp:280
Copy hook for data member of a containing object (eg, SEQUENCE)
Definition: objhook.hpp:266
Copy hook for a standalone object.
Definition: objhook.hpp:254
CObjectIStream –.
Definition: objistr.hpp:93
CObjectOStream –.
Definition: objostr.hpp:83
CObjectTypeInfo –.
Definition: objectinfo.hpp:94
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Definition: set.hpp:45
Include a standard set of the NCBI C++ Toolkit most basic headers.
std::ofstream out("events_result.xml")
main entry point for tests
#define NULL
Definition: ncbistd.hpp:225
void SetPathCopyHook(CObjectStreamCopier *copier, const string &path, CCopyClassMemberHook *hook)
Definition: member.cpp:647
void SetPathCopyHook(CObjectStreamCopier *copier, const string &path, CCopyChoiceVariantHook *hook)
Definition: variant.cpp:869
#define XSERIAL_TYPEINFO_WRITELOCK
Definition: serialimpl.hpp:277
static const TObjectType * SafeCast(TTypeInfo type)
Definition: serialutil.hpp:76
@ eTypeFamilyClass
Definition: serialdef.hpp:140
static CTypeInfo * FindType(const CObjectStack &stk)
Definition: pathhook.cpp:272
virtual EPointerType ReadPointerType(void)=0
CStreamPathHook< CMemberInfo *, CCopyClassMemberHook * > m_PathCopyMemberHooks
Definition: objcopy.hpp:152
#define BEGIN_OBJECT_2FRAMES2(Type, Arg)
Definition: objstack.hpp:252
void SkipFileHeader(TTypeInfo typeInfo)
Read file header and compare the type name with the expected one.
Definition: objistr.cpp:883
virtual void WriteFileHeader(TTypeInfo type)
Definition: objostr.cpp:680
void SetPathCopyVariantHook(const string &path, CCopyChoiceVariantHook *hook)
Definition: objcopy.cpp:241
void SetHook(const string &path, THook hook)
Definition: pathhook.hpp:102
#define ThrowError(flag, mess)
Definition: objstack.hpp:113
void RegisterObject(TTypeInfo typeInfo)
Definition: objistr.cpp:861
CObjectStreamCopier(CObjectIStream &in, CObjectOStream &out)
Constructor.
Definition: objcopy.cpp:55
TTypeInfo GetTypeInfo(void) const
CLocalHookSet< CCopyChoiceVariantHook > m_ChoiceVariantHookKey
Definition: objcopy.hpp:160
#define END_OBJECT_2FRAMES()
Definition: objstack.hpp:253
virtual void WriteNullPointer(void)=0
void CopyPointer(TTypeInfo declaredType)
Definition: objcopy.cpp:106
virtual string ReadOtherPointer(void)=0
const CReadObjectInfo & GetRegisteredObject(TObjectIndex index)
Definition: objistr.cpp:874
virtual void ReadOtherPointerEnd(void)
Definition: objistr.cpp:1223
const string & GetStackPath(void) const
Definition: objstack.cpp:263
void SetPathCopyObjectHook(const string &path, CCopyObjectHook *hook)
Definition: objcopy.cpp:227
void SetPathHooks(CObjectStack &stk, bool set)
Definition: objcopy.cpp:249
void Write(const void *bytes, size_t length)
void SetPathCopyMemberHook(const string &path, CCopyClassMemberHook *hook)
Definition: objcopy.cpp:234
virtual void WriteOtherEnd(TTypeInfo typeInfo)
Definition: objostr.cpp:794
bool KnownLength(void) const
void CopyByteBlock(void)
Definition: objcopy.cpp:191
size_t GetExpectedLength(void) const
TTypeInfo GetTypeInfo(void) const
CLocalHookSet< CCopyObjectHook > m_ObjectHookKey
Definition: objcopy.hpp:158
CObjectIStream & In(void) const
virtual void WriteObjectReference(TObjectIndex index)=0
bool CopyNullPointer(void)
Definition: objcopy.cpp:181
CStreamPathHook< CVariantInfo *, CCopyChoiceVariantHook * > m_PathCopyVariantHooks
Definition: objcopy.hpp:153
CLocalHookSet< CCopyClassMemberHook > m_ClassMemberHookKey
Definition: objcopy.hpp:159
virtual void EndOfWrite(void)
Definition: objostr.cpp:559
CObjectOStream & Out(void) const
THook GetHook(CObjectStack &stk) const
Definition: pathhook.hpp:106
void SetHook(const string &path, THook hook)
Definition: pathhook.hpp:121
~CObjectStreamCopier(void)
Destructor.
Definition: objcopy.cpp:61
void ResetLocalHooks(void)
Definition: objcopy.cpp:66
void RegisterObject(TTypeInfo typeInfo)
Definition: objostr.cpp:632
size_t TObjectIndex
Definition: objistr.hpp:1029
virtual TObjectIndex ReadObjectPointer(void)=0
void WatchPathHooks(bool set=true)
static TInfo FindItem(const CObjectStack &stk)
Definition: pathhook.hpp:110
THook GetHook(CObjectStack &stk) const
Definition: pathhook.hpp:125
virtual void WriteOtherBegin(TTypeInfo typeInfo)=0
void Copy(const CObjectTypeInfo &type)
Copy data.
Definition: objcopy.cpp:74
CStreamObjectPathHook< CCopyObjectHook * > m_PathCopyObjectHooks
Definition: objcopy.hpp:154
virtual void EndOfRead(void)
Definition: objistr.cpp:903
void CopyObject(TTypeInfo type)
Copy object, omitting file header both in input and output streams.
size_t Read(void *dst, size_t length, bool forceLength=false)
Definition: objistr.cpp:1576
bool IsEmpty(void) const
Definition: pathhook.hpp:82
@ fFormatError
Input file formatting does not conform with specification.
Definition: objistr.hpp:377
#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 string & GetName(void) const
Get name of this type.
Definition: typeinfo.cpp:249
void SetPathCopyHook(CObjectStreamCopier *copier, const string &path, CCopyObjectHook *hook)
Set local context-specific copy hook.
Definition: typeinfo.cpp:470
ETypeFamily GetTypeFamily(void) const
static TTypeInfo GetClassInfoByName(const string &name)
Definition: classinfob.cpp:244
Multi-threading – mutexes; rw-locks; semaphore.
std::istream & in(std::istream &in_, double &x_)
#define _TRACE(arg)
Definition: objcopy.cpp:51
#define count
static uint8_t * buffer
Definition: pcre2test.c:1016
Definition: type.c:6
Modified on Wed Sep 04 15:01:22 2024 by modify_doxy.py rev. 669887