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

Go to the SVN repository for this file.

1 /* $Id: asn_assign.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 a assign value to a serial object.
30 *
31 * ===========================================================================
32 */
33 
34 #include <ncbi_pch.hpp>
38 #include <serial/objistr.hpp>
39 
42 
43 
44 int main(int argc, const char* argv[])
45 {
46  {{
47  // set primitive value
48  CSeq_id obj;
50  CObjectInfo local(info.SetChoiceVariant(info.FindVariantIndex("local")));
51  local.SetChoiceVariant(local.FindVariantIndex("id")).SetPrimitiveValueInt(234);
52  cout << MSerial_AsnText << obj;
53  }}
54 
55  {{
56  CSeq_loc obj;
58 
59  // set choice variant
60  info.SetChoiceVariant(info.FindVariantIndex("null"));
61  cout << MSerial_AsnText << obj;
62 
63  // change choice variant
64  CObjectInfo seqid(info.SetChoiceVariant(info.FindVariantIndex("empty")));
65  CObjectInfo local(seqid.SetChoiceVariant(seqid.FindVariantIndex("local")));
66  local.SetChoiceVariant(local.FindVariantIndex("id")).SetPrimitiveValueInt(987);
67  cout << MSerial_AsnText << obj;
68 
69  // there is no way to reset choice variant
70  // this does not work:
71  //info.SetChoiceVariant(0);
72  }}
73 
74  {{
75  CSeq_id_set obj;
77  // 'implicit' class has one member with no name
78  CObjectInfo mem(info.SetClassMember(1));
79 
80  // add container element
83  local.SetChoiceVariant(local.FindVariantIndex("id")).SetPrimitiveValueInt(345);
84 
85  // add container element
86  elem = mem.AddNewPointedElement();
87  local = elem.SetChoiceVariant(elem.FindVariantIndex("local"));
88  local.SetChoiceVariant(local.FindVariantIndex("str")).SetPrimitiveValueString("345string");
89 
90  // add container element
91  elem = mem.AddNewPointedElement();
92  CObjectInfo prf = elem.SetChoiceVariant(elem.FindVariantIndex("prf"));
93  prf.SetClassMember(prf.FindMemberIndex("name")).SetPrimitiveValueString("prfname");
94  prf.SetClassMember(prf.FindMemberIndex("release")).SetPrimitiveValueString("prfrelease");
95  prf.SetClassMember(prf.FindMemberIndex("version")).SetPrimitiveValueInt(567);
96 
97  cout << MSerial_AsnText << obj;
98 
99  // unset class member
100  // only optional members can be unset
101  if (prf.FindClassMember("version").IsSet()) {
102  prf.FindClassMember("version").Reset();
103  }
104  cout << MSerial_AsnText << obj;
105  }}
106 
107  return 0;
108 }
User-defined methods of the data storage class.
USING_SCOPE(ncbi)
int main(int argc, const char *argv[])
Definition: asn_assign.cpp:44
CObjectInfo –.
Definition: objectinfo.hpp:597
CSeq_id_set –.
Definition: Seq_id_set.hpp:66
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
TMemberIndex FindMemberIndex(const string &name) const
Find class member index by its name.
Definition: objectinfo.cpp:124
TMemberIndex FindVariantIndex(const string &name) const
Find choice variant index by its name.
Definition: objectinfo.cpp:134
CObjectInfo AddNewPointedElement(void) const
add new pointer element, create new pointed object and return it
Definition: objectinfo.cpp:329
pair< TObjectPtr, TTypeInfo > ObjectInfo(C &obj)
Definition: objectinfo.hpp:762
void SetPrimitiveValueString(const string &value)
Definition: objectinfo.cpp:282
bool IsSet(const CConstObjectInfo &object) const
Definition: objectiter.cpp:69
CMemberIterator FindClassMember(const string &memberName) const
Find class member by its name.
void SetPrimitiveValueInt(int value)
Definition: objectinfo.cpp:257
CObjectInfo SetChoiceVariant(TMemberIndex index) const
Select choice variant, if necessary, and return variant object.
Definition: objectinfo.cpp:355
CObjectInfo SetClassMember(TMemberIndex index) const
Create member if necessary and return member object.
Definition: objectinfo.cpp:345
static MDB_envinfo info
Definition: mdb_load.c:37
Magic spell ;-) needed for some weird compilers... very empiric.
#define local
Definition: zutil.h:33
Modified on Wed Apr 17 13:10:22 2024 by modify_doxy.py rev. 669887