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

Go to the SVN repository for this file.

1 #include <ncbi_pch.hpp>
4 #include <serial/objistr.hpp>
5 #include <serial/objostr.hpp>
6 #include <serial/objcopy.hpp>
7 #include <serial/serial.hpp>
8 
11 
12 
13 class CDemoHook : public CReadObjectHook
14 {
15 public:
16  virtual void ReadObject(CObjectIStream& strm, const CObjectInfo& passed_info)
17  {
18  cout << strm.GetStackPath() << endl;
19 #if 1
20  DefaultRead(strm, passed_info);
21 
22 #else
23  // typeinfo of the object (Seq-annot)
24  TTypeInfo ti = passed_info.GetTypeInfo();
25 
26 #if 1
27 // or skip the object
28  DefaultSkip(strm, passed_info);
29 #endif
30 #if 0
31 // or copy it into stdout
32  unique_ptr<CObjectOStream> out(CObjectOStream::Open(eSerial_AsnText, "stdout", eSerial_StdWhenStd));
33  CObjectStreamCopier copier(strm, *out);
34  copier.CopyObject(passed_info.GetTypeInfo());
35 #endif
36 #if 0
37 // or read it into delay buffer
38  strm.StartDelayBuffer();
39  DefaultSkip(strm, passed_info);
41 
42  // now all data is in CByteSource and unparsed, so the following will fail:
43  //cout << MSerial_AsnText << passed_info;
44  // FYI: insertion operator calls
45  // WriteObject(cout,passed_info.GetObjectPtr(),passed_info.GetTypeInfo());
46 
47  // we can parse the buffer:
48  unique_ptr<CObjectIStream> in(CObjectIStream::Create(eSerial_AsnText, *data));
49 #if 0
50 // like this
51  CSeq_annot annot;
52  in->ReadObject(&annot, CSeq_annot::GetTypeInfo());
53  // now the object is valid
54  cout << MSerial_AsnText << annot;
55 #else
56 // or like this
57  in->ReadObject(passed_info);
58  // now the object is valid
59  cout << MSerial_AsnText << passed_info;
60 #endif
61 #endif
62 #endif
63  }
64 };
65 
66 
67 int main(int argc, char** argv)
68 {
69  unique_ptr<CObjectIStream> in(CObjectIStream::Open(eSerial_AsnText, "seq-entry-sample.asn"));
70 
72 
73  CSeq_entry entry;
74  *in >> entry;
75 
76  return 0;
77 }
virtual void ReadObject(CObjectIStream &strm, const CObjectInfo &passed_info)
This method will be called at approriate time when the object of requested type is to be read.
CObjectIStream –.
Definition: objistr.hpp:93
CObjectInfo –.
Definition: objectinfo.hpp:597
CObjectStreamCopier –.
Definition: objcopy.hpp:71
CObjectTypeInfo –.
Definition: objectinfo.hpp:94
Read hook for a standalone object.
Definition: objhook.hpp:59
Definition: Seq_entry.hpp:56
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
std::ofstream out("events_result.xml")
main entry point for tests
char data[12]
Definition: iconv.c:80
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
@ eSerial_StdWhenStd
use std when filename is "stdin"/"stdout"
Definition: serialdef.hpp:130
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
void DefaultRead(CObjectIStream &in, const CObjectInfo &object)
Default read.
Definition: objhook.cpp:171
void SetLocalReadHook(CObjectIStream &stream, CReadObjectHook *hook) const
Set local (for the specified stream) read hook.
Definition: objectinfo.cpp:366
static CObjectIStream * Create(ESerialDataFormat format)
Create serial object reader.
Definition: objistr.cpp:144
const string & GetStackPath(void) const
Definition: objstack.cpp:263
static CObjectOStream * Open(ESerialDataFormat format, CNcbiOstream &outStream, bool deleteOutStream)
Create serial object writer and attach it to an output stream.
Definition: objostr.cpp:126
TTypeInfo GetTypeInfo(void) const
static CObjectIStream * Open(ESerialDataFormat format, CNcbiIstream &inStream, bool deleteInStream)
Create serial object reader and attach it to an input stream.
Definition: objistr.cpp:195
void DefaultSkip(CObjectIStream &in, const CObjectTypeInfo &object)
Default skip.
Definition: objhook.cpp:177
virtual void StartDelayBuffer(void)
Definition: objistr.cpp:1014
virtual CRef< CByteSource > EndDelayBuffer(void)
Definition: objistr.cpp:1019
void CopyObject(TTypeInfo type)
Copy object, omitting file header both in input and output streams.
int main(int argc, char **argv)
USING_SCOPE(ncbi::objects)
USING_NCBI_SCOPE
std::istream & in(std::istream &in_, double &x_)
Modified on Fri Sep 20 14:57:33 2024 by modify_doxy.py rev. 669887