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

Go to the SVN repository for this file.

1 /* $Id: asnwalk_write.cpp 90001 2020-05-04 12:53:02Z ivanov $
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: Andrei Gourianov
27 *
28 * File Description:
29 * Simple program demonstrating how to walk a serial object.
30 *
31 * The program recursively visits all members of CSeq_entry class object
32 * and modifies all primitive type values with random data
33 *
34 * ===========================================================================
35 */
36 
37 #include <ncbi_pch.hpp>
38 #include <corelib/ncbiapp.hpp>
40 #include <serial/objistr.hpp>
41 
44 
45 
46 class CDemoApp : public CNcbiApplication
47 {
48  virtual int Run(void);
49 
50  void SetDispatch(int depth, size_t offset, CObjectInfo& info);
51  void SetPrimitive(int depth, size_t offset, CObjectInfo& info);
52  void SetClass(int depth, size_t offset, CObjectInfo& info);
53  void SetChoice(int depth, size_t offset, CObjectInfo& info);
54  void SetContainer(int depth, size_t offset, CObjectInfo& info);
55  void SetPointer(int depth, size_t offset, CObjectInfo& info);
56 
57  int m_MaxDepth;
58 };
59 
60 
61 int CDemoApp::Run(void)
62 {
63  // Set this arbitrarily for demo purposes.
64  m_MaxDepth = 5;
65 
66  // Get a Seq-entry object.
67  CSeq_entry obj;
68 
69  // Get the object info and walk the object hierarchy.
71  SetDispatch(0,0,info);
72  cout << MSerial_AsnText << obj;
73 
74  return 0;
75 }
76 
77 
79 {
80  ++depth;
81  offset += 2;
82  switch (info.GetTypeFamily()) {
83  default:
84  cout << "ERROR: unknown type family" << endl;
85  break;
88  break;
89  case eTypeFamilyClass:
91  break;
92  case eTypeFamilyChoice:
94  break;
97  break;
98  case eTypeFamilyPointer:
100  break;
101  }
102  --depth;
103 }
104 
105 
107 {
108  EPrimitiveValueType type = info.GetPrimitiveValueType();
109  switch (type) {
111  cout << "special" << endl;
112  break;
113  case ePrimitiveValueBool:
114  info.SetPrimitiveValueBool(true);
115  break;
116  case ePrimitiveValueChar:
117  info.SetPrimitiveValueChar('X');
118  break;
120  info.SetPrimitiveValueInt(123 + depth);
121  break;
122  case ePrimitiveValueReal:
123  info.SetPrimitiveValueDouble(123.456 + depth);
124  break;
126  info.SetPrimitiveValueString("xxx");
127  break;
128  case ePrimitiveValueEnum:
129  {
130  const CEnumeratedTypeValues& values = info.GetEnumeratedTypeValues();
132  CEnumeratedTypeValues::TValues::const_iterator v = all.begin();
133  if (all.size() > 1) {
134  ++v;
135  }
136  if (all.size() > 2) {
137  ++v;
138  }
139  info.SetPrimitiveValueInt(v->second);
140  }
141  break;
143  cout << "octet string" << endl;
144  break;
146  cout << "bitstring" << endl;
147  break;
148  case ePrimitiveValueAny:
149  cout << "any" << endl;
150  break;
152  cout << "other" << endl;
153  break;
154  }
155 }
156 
157 
159 {
160  string off(offset,' ');
161  cout << off << "class: " << info.GetName() << " {" << endl;
162  CObjectInfoMI mem = info.BeginMembers();
163  for ( ; mem.Valid(); ++mem) {
164  if (depth > m_MaxDepth && mem.GetMemberInfo()->Optional()) {
165  // set some optional ones, but not too many of them
166  continue;
167  }
168  cout << off << " [" << mem.GetMemberIndex() << "]: "
169  << mem.GetMemberInfo()->GetId().GetName()
170  << endl;
171  CObjectInfo tmp_info(info.SetClassMember(mem.GetMemberIndex()));
172  SetDispatch(depth, offset, tmp_info);
173  }
174  cout << off << "} end of " << info.GetName() << endl;
175 }
176 
177 
179 {
180  // select choice #2
181  TMemberIndex ind = 2;
182  if (depth > m_MaxDepth) {
183  // or choice #1
184  ind = 1;
185  }
186  string off(offset,' ');
187  cout << off << "choice: " << info.GetName() << " {" << endl;
188  cout << off << " [" << ind << "]: "
189  << info.GetVariantIterator(ind).GetVariantInfo()->GetId().GetName()
190  << " {" << endl;
191  CObjectInfo tmp_info(info.SetChoiceVariant(ind));
192  SetDispatch(depth,offset,tmp_info);
193  cout << off << "} end of " << info.GetName() << endl;
194 }
195 
196 
198 {
199  string off(offset,' ');
200  cout << off << "container of: " << info.GetName() << " {" << endl;
201  for (int i=0; i<2; ++i) {
202  if (info.GetElementType().GetTypeFamily() == eTypeFamilyPointer) {
203  CObjectInfo tmp_info(info.AddNewPointedElement());
204  SetDispatch(depth,offset,tmp_info);
205  } else {
206  CObjectInfo tmp_info(info.AddNewElement());
207  SetDispatch(depth,offset,tmp_info);
208  }
209  }
210  cout << off << "} end of " << info.GetName() << endl;
211 }
212 
213 
215 {
216  CObjectInfo tmp_info(info.SetPointedObject());
217  SetDispatch(depth,offset,tmp_info);
218 }
219 
220 
221 int main(int argc, const char* argv[])
222 {
223  return CDemoApp().AppMain(argc, argv);
224 }
USING_SCOPE(ncbi)
int main(int argc, const char *argv[])
Derive our application class from CwxNCBIApp and use it together with standard CNCBIwxApplication.
void SetClass(int depth, size_t offset, CObjectInfo &info)
void SetChoice(int depth, size_t offset, CObjectInfo &info)
virtual int Run(void)
Run the application.
void SetPrimitive(int depth, size_t offset, CObjectInfo &info)
void SetDispatch(int depth, size_t offset, CObjectInfo &info)
void SetPointer(int depth, size_t offset, CObjectInfo &info)
int m_MaxDepth
void SetContainer(int depth, size_t offset, CObjectInfo &info)
virtual int Run(void)
Run the application.
CObjectInfoMI –.
Definition: objectiter.hpp:432
CObjectInfo –.
Definition: objectinfo.hpp:597
Definition: Seq_entry.hpp:56
static unsigned char depth[2 *(256+1+29)+1]
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
Definition: ncbiapp.cpp:799
list< pair< string, TEnumValueType > > TValues
Definition: enumvalues.hpp:54
bool Optional(void) const
const CMemberId & GetId(void) const
const string & GetName(void) const
const TValues & GetValues(void) const
Get the list of name-value pairs.
Definition: enumvalues.hpp:98
size_t TMemberIndex
Type used for indexing class members and choice variants.
Definition: serialdef.hpp:230
EPrimitiveValueType
Primitive value type.
Definition: serialdef.hpp:147
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
@ ePrimitiveValueSpecial
null, void
Definition: serialdef.hpp:148
@ ePrimitiveValueOctetString
vector<(signed|unsigned)? char>
Definition: serialdef.hpp:155
@ ePrimitiveValueString
string|char*|const char*
Definition: serialdef.hpp:153
@ ePrimitiveValueOther
Definition: serialdef.hpp:161
@ ePrimitiveValueInteger
(signed|unsigned) (char|short|int|long)
Definition: serialdef.hpp:151
@ ePrimitiveValueAny
Definition: serialdef.hpp:160
@ ePrimitiveValueChar
char
Definition: serialdef.hpp:150
@ ePrimitiveValueBool
bool
Definition: serialdef.hpp:149
@ ePrimitiveValueEnum
enum
Definition: serialdef.hpp:154
@ ePrimitiveValueReal
float|double
Definition: serialdef.hpp:152
@ ePrimitiveValueBitString
Definition: serialdef.hpp:156
@ eTypeFamilyClass
Definition: serialdef.hpp:140
@ eTypeFamilyContainer
Definition: serialdef.hpp:142
@ eTypeFamilyChoice
Definition: serialdef.hpp:141
@ eTypeFamilyPointer
Definition: serialdef.hpp:143
@ eTypeFamilyPrimitive
Definition: serialdef.hpp:139
bool Valid(void) const
Is iterator valid.
TMemberIndex GetMemberIndex(void) const
Get index of the member in the class.
pair< TObjectPtr, TTypeInfo > ObjectInfo(C &obj)
Definition: objectinfo.hpp:762
const CMemberInfo * GetMemberInfo(void) const
int i
static MDB_envinfo info
Definition: mdb_load.c:37
Magic spell ;-) needed for some weird compilers... very empiric.
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
int offset
Definition: replacements.h:160
Definition: type.c:6
Modified on Wed Dec 06 07:12:35 2023 by modify_doxy.py rev. 669887