NCBI C++ ToolKit
asn_converter.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CTOOLS___ASN_CONVERTER__HPP
2 #define CTOOLS___ASN_CONVERTER__HPP
3 
4 /* $Id: asn_converter.hpp 91926 2020-12-16 16:11:48Z grichenk $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Denis Vakatov, Aaron Ucko
30  *
31  * File Description:
32  * Templates for converting ASN.1-based objects between NCBI's C and C++
33  * in-memory layouts.
34  *
35  */
36 
38 #include <serial/objistrasnb.hpp>
39 #include <serial/objostrasnb.hpp>
40 #include <serial/serial.hpp>
41 #include <ctools/asn_connection.h>
42 
43 
44 /** @addtogroup CToolsASNConv
45  *
46  * @{
47  */
48 
49 
51 
52 
53 #define DECLARE_ASN_CONVERTER(TCpp, TC, name) \
54 CAsnConverter<TCpp, TC> name((AsnWriteFunc) TC##AsnWrite, \
55  (AsnReadFunc) TC##AsnRead)
56 
57 
58 template <typename TCpp, typename TC>
60 {
61 public:
62  typedef AsnWriteFunc FWrite;
63  typedef AsnReadFunc FRead;
64  CAsnConverter(FWrite writer, FRead reader)
65  : m_Write(writer), m_Read(reader) { }
66 
67  /// Creates and returns a new object if cpp_obj is null.
68  TCpp* FromC(const TC* c_obj, TCpp* cpp_obj = 0,
70  /// Always returns a new object, as that's how C readers work.
71  TC* ToC (const TCpp& cpp_obj, EAsnConn_Format format = eAsnConn_Binary);
72 
73 private:
76 };
77 
78 
79 /* @} */
80 
81 
82 ///////////////////////////////////////////////////////////////////////////
83 // inline functions
84 
85 
86 inline
88 {
89  switch (format) {
91  case eAsnConn_Text: return eSerial_AsnText;
92  default: _TROUBLE;
93  }
94  return eSerial_None;
95 }
96 
97 
98 template <typename TCpp, typename TC>
99 inline
100 TCpp* CAsnConverter<TCpp, TC>::FromC(const TC* c_obj, TCpp* cpp_obj,
102 {
103  if ( !c_obj ) {
104  return 0;
105  }
106 
107  CConn_MemoryStream conn_stream;
108 
109  AsnIoPtr aip = CreateAsnConn(conn_stream.GetCONN(), eAsnConn_Output,
110  format);
111  m_Write(const_cast<TC*>(c_obj), aip, 0);
112  AsnIoFlush(aip);
113 
114  CRef<TCpp> cpp_ref(cpp_obj ? cpp_obj : new TCpp);
115  unique_ptr<CObjectIStream> ois(CObjectIStream::Open
116  (MapAcfToSdf(format), conn_stream));
117  *ois >> *cpp_ref;
118  return cpp_ref.Release();
119 }
120 
121 
122 template <typename TCpp, typename TC>
123 inline
125 {
126  CConn_MemoryStream conn_stream;
127 
128  unique_ptr<CObjectOStream> oos(CObjectOStream::Open
129  (MapAcfToSdf(format), conn_stream));
130  CObjectOStreamAsnBinary* bin_stream
131  = dynamic_cast<CObjectOStreamAsnBinary*>(oos.get());
132  if (bin_stream) {
133  bin_stream->SetCStyleBigInt(true);
134  }
135  *oos << cpp_obj;
136  oos->Flush();
137 
138  AsnIoPtr aip = CreateAsnConn(conn_stream.GetCONN(), eAsnConn_Input,
139  format);
140  return (TC*) m_Read(aip, 0);
141 }
142 
143 
145 
146 
147 #endif /* CTOOLS___ASN_CONVERTER__HPP */
In-memory stream (a la strstream or stringstream)
CObjectOStreamAsnBinary –.
Definition: objostrasnb.hpp:58
CRef –.
Definition: ncbiobj.hpp:618
AsnIoPtr CreateAsnConn(CONN conn, EAsnConn_Direction direction, EAsnConn_Format fmt)
EAsnConn_Format
@ eAsnConn_Text
@ eAsnConn_Binary
@ eAsnConn_Input
@ eAsnConn_Output
TC * ToC(const TCpp &cpp_obj, EAsnConn_Format format=eAsnConn_Binary)
Always returns a new object, as that's how C readers work.
AsnReadFunc FRead
AsnWriteFunc FWrite
ESerialDataFormat MapAcfToSdf(EAsnConn_Format format)
TCpp * FromC(const TC *c_obj, TCpp *cpp_obj=0, EAsnConn_Format format=eAsnConn_Binary)
Creates and returns a new object if cpp_obj is null.
CAsnConverter(FWrite writer, FRead reader)
CONN GetCONN(void) const
ESerialDataFormat
Data file format.
Definition: serialdef.hpp:71
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
@ eSerial_None
Definition: serialdef.hpp:72
@ eSerial_AsnBinary
ASN.1 binary.
Definition: serialdef.hpp:74
static CObjectOStream * Open(ESerialDataFormat format, CNcbiOstream &outStream, bool deleteOutStream)
Create serial object writer and attach it to an output stream.
Definition: objostr.cpp:126
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 SetCStyleBigInt(bool set=true)
TObjectType * Release(void)
Release a reference to the object and return a pointer to the object.
Definition: ncbiobj.hpp:846
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static Format format
Definition: njn_ioutil.cpp:53
#define _TROUBLE
Modified on Sat Apr 13 11:43:55 2024 by modify_doxy.py rev. 669887