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

Go to the SVN repository for this file.

1 /* $Id: asnwalk_type.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 TypeInfo.
30 *
31 * The program recursively visits all members of CSeq_entry class TypeInfo
32 * and prints out their types and names
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 Dispatch(int depth, size_t offset, const CObjectTypeInfo& info);
51  void WalkPrimitive(int depth, size_t offset, const CObjectTypeInfo& info);
52  void WalkClass(int depth, size_t offset, const CObjectTypeInfo& info);
53  void WalkChoice(int depth, size_t offset, const CObjectTypeInfo& info);
54  void WalkContainer(int depth, size_t offset, const CObjectTypeInfo& info);
55  void WalkPointer(int depth, size_t offset, const CObjectTypeInfo& info);
56 
58 };
59 
60 
61 int CDemoApp::Run(void)
62 {
63  // Set this arbitrarily for demo purposes.
64  m_MaxDepth = 5;
65 
66  // Get the type info and walk the object hierarchy.
67  CObjectTypeInfo info(CSeq_entry::GetTypeInfo());
68  Dispatch(0, 0, info);
69 
70  return 0;
71 }
72 
73 
75 {
76  if (depth > m_MaxDepth) {
77  cout << "..." << endl;
78  return;
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  string off(offset,' ');
109  cout << off << "primitive: ";
110  EPrimitiveValueType type = info.GetPrimitiveValueType();
111 
112  switch (type) {
113  case ePrimitiveValueSpecial: cout << "special"; break;
114  case ePrimitiveValueBool:
115  cout << "boolean";
116  break;
117  case ePrimitiveValueChar:
118  cout << "char";
119  break;
121  cout << (info.IsPrimitiveValueSigned() ? "signed" : "unsigned")
122  << " integer";
123  break;
124  case ePrimitiveValueReal:
125  cout << "float";
126  break;
128  cout << "string";
129  break;
130  case ePrimitiveValueEnum:
131  cout << "enum: ";
132  {
133  const CEnumeratedTypeValues& values = info.GetEnumeratedTypeValues();
134  if (values.IsInteger()) {
135  cout << " integer: ";
136  }
137  cout << "{" << endl;
138  for (auto& v : values.GetValues()) {
139  cout << off << " " << v.first << " (" << v.second << ")" << endl;
140  }
141  cout << off << "}";
142  }
143  break;
145  cout << "octetstring";
146  break;
148  cout << "bitstring";
149  break;
150  case ePrimitiveValueAny:
151  cout << "any";
152  break;
154  cout << "other";
155  break;
156  }
157  cout << endl;
158 }
159 
160 
162 {
163  string off(offset,' ');
164  cout << off;
165 
166  const CClassTypeInfo* info_class = info.GetClassTypeInfo();
167  if (info_class->RandomOrder()) {
168  cout << "random ";
169  } else if (info_class->Implicit()) {
170  cout << "implicit ";
171  } else {
172  cout << "sequential ";
173  }
174  cout << "class: " << info.GetName() << " {" << endl;
175  CObjectTypeInfoMI mem = info.BeginMembers();
176  for ( ; mem.Valid(); ++mem) {
177  cout << off << " [" << mem.GetMemberIndex() << "]: "
178  << mem.GetMemberInfo()->GetId().GetName()
179  << " {" << endl;
181  cout << off << " }";
182  if (mem.GetMemberInfo()->Optional()) {
183  cout << " optional";
184  }
185  if (mem.GetMemberInfo()->GetDefault()) {
186  cout << " with default: {" << endl;
188  mem.GetMemberInfo()->GetTypeInfo());
189  Dispatch(depth, offset, def);
190  cout << off << " }" << endl;
191  }
192  cout << endl;
193  }
194  cout << off << "} end of " << info.GetName() << endl;
195 }
196 
197 
199 {
200  string off(offset,' ');
201  cout << off << "choice: " << info.GetName() << " {" << endl;
202  CObjectTypeInfoVI var = info.BeginVariants();
203  for (; var.Valid(); ++var) {
204  cout << off << " [" << var.GetVariantIndex() << "]: "
205  << var.GetVariantInfo()->GetId().GetName()
206  << " {" << endl;
208  cout << off << " }" << endl;
209  }
210  cout << off << "} end of " << info.GetName() << endl;
211 }
212 
213 
215 {
216  string off(offset,' ');
217  cout << off << "container of: " << info.GetName() << " {" << endl;
218  Dispatch(depth, offset, info.GetElementType());
219  cout << off << "} end of " << info.GetName() << endl;
220 }
221 
222 
224 {
225  string off(offset,' ');
226  Dispatch(depth, offset,info.GetPointedType());
227 }
228 
229 
230 int main(int argc, const char* argv[])
231 {
232  return CDemoApp().AppMain(argc, argv);
233 }
234 
USING_SCOPE(ncbi)
int main(int argc, const char *argv[])
CConstObjectInfo –.
Definition: objectinfo.hpp:421
Derive our application class from CwxNCBIApp and use it together with standard CNCBIwxApplication.
void WalkContainer(size_t offset, const CConstObjectInfo &info)
void WalkPrimitive(size_t offset, const CConstObjectInfo &info)
virtual int Run(void)
Run the application.
void WalkClass(size_t offset, const CConstObjectInfo &info)
void WalkPointer(size_t offset, const CConstObjectInfo &info)
void WalkChoice(size_t offset, const CConstObjectInfo &info)
int m_MaxDepth
virtual int Run(void)
Run the application.
void Dispatch(size_t offset, const CConstObjectInfo &info)
CObjectTypeInfoMI –.
Definition: objectiter.hpp:246
CObjectTypeInfoVI –.
Definition: objectiter.hpp:324
CObjectTypeInfo –.
Definition: objectinfo.hpp:94
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
bool Optional(void) const
const CMemberId & GetId(void) const
const string & GetName(void) const
TConstObjectPtr GetDefault(void) const
const TValues & GetValues(void) const
Get the list of name-value pairs.
Definition: enumvalues.hpp:98
TTypeInfo GetTypeInfo(void) const
bool IsInteger(void) const
Check whether the type is defined as INTEGER in ASN.1 spec.
Definition: enumvalues.hpp:75
EPrimitiveValueType
Primitive value type.
Definition: serialdef.hpp:147
@ 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 GetVariantIndex(void) const
Get index of the variant in the choice.
TMemberIndex GetMemberIndex(void) const
Get index of the member in the class.
const CMemberInfo * GetMemberInfo(void) const
CObjectTypeInfo GetMemberType(void) const
Get data type information.
CObjectTypeInfo GetVariantType(void) const
Get data type information.
const CVariantInfo * GetVariantInfo(void) const
bool RandomOrder(void) const
bool Implicit(void) const
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 Sat Dec 02 09:20:01 2023 by modify_doxy.py rev. 669887