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

Go to the SVN repository for this file.

1 /* $Id: async_obj_convert.cpp 46706 2021-09-09 19:54:51Z asztalos $
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  * Authors: Roman Katargin
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 
32 #include <serial/typeinfo.hpp>
36 
38 
41 
43 {
45  set<CBioseq_Handle> seqSet;
46 
47  for (auto o : objects) {
48  if (canceled.IsCanceled()) {
49  objects.clear();
50  return;
51  }
52 
53  if (!o.scope) continue;
54  const CSeq_id* seqId = dynamic_cast<const CSeq_id*>(o.object.GetPointerOrNull());
55  if (!seqId) {
56  const CSeq_loc* seqLoc = dynamic_cast<const CSeq_loc*>(o.object.GetPointerOrNull());
57  if (seqLoc && seqLoc->IsWhole()) {
58  seqId = seqLoc->GetId();
59  }
60  }
61  if (seqId) {
62  CBioseq_Handle h = o.scope->GetBioseqHandle(*seqId);
63  if (h && seqSet.find(h) == seqSet.end()) {
64  result.push_back(o);
65  seqSet.insert(h);
66  }
67  continue;
68  }
69  result.push_back(o);
70  }
71 
72  objects = result;
73 }
74 
75 static void s_ConvertObjectsCat(
76  const CTypeInfo* typeInfo,
77  const TConstScopedObjects& inputObjects,
79  ICanceled& canceled)
80 {
81  results.clear();
82 
83  ITERATE(TConstScopedObjects, it, inputObjects) {
84  CScope* scope = const_cast<CScope*>(it->scope.GetPointerOrNull());
85 
87  CObjectConverter::FindRelations(*scope, *it->object, typeInfo->GetName(), relations);
88 
90  if (relations.size() > 1 && itr == relations.begin()) {
91  continue;
92  }
93 
94  if (canceled.IsCanceled()) {
95  results.clear();
96  return;
97  }
98 
99  const CRelation& rel = **itr;
100  string relName = rel.GetProperty(typeInfo->GetName());
101  TConstScopedObjects& vec = results[relName];
102 
103  CRelation::TObjects related;
104  rel.GetRelated(*scope, *it->object, related, CRelation::fConvert_All, &canceled);
105 
106  ITERATE(CRelation::TObjects, it2, related) {
107  const CSerialObject* serial = dynamic_cast<const CSerialObject*>(it2->GetObjectPtr());
108  if (serial && serial->GetThisTypeInfo() == typeInfo)
109  vec.push_back(SConstScopedObject(it2->GetObjectPtr(), scope));
110  else
111  _ASSERT(false);
112  }
113  }
114  }
115 
116  for (auto& rel : results)
117  s_EliminateDuplicates(rel.second, canceled);
118 
119  if (canceled.IsCanceled()) {
120  results.clear();
121  }
122 }
123 
124 static void s_ConvertObjects(
125  const CTypeInfo* typeInfo,
126  const TConstScopedObjects& inputObjects,
127  TConstScopedObjects& results,
128  ICanceled& canceled)
129 {
130  ITERATE(TConstScopedObjects, it, inputObjects) {
131  CScope* scope = const_cast<CScope*>(it->scope.GetPointerOrNull());
132 
134  CObjectConverter::FindRelations(*scope, *it->object,
135  typeInfo->GetName(), relations);
136 
137  if (canceled.IsCanceled())
138  return;
139 
140  if (relations.empty())
141  continue;
142 
143  CRelation::TObjects related;
144  relations.front()->GetRelated(*scope, *it->object, related, CRelation::fConvert_NoExpensive, &canceled);
145 
146  ITERATE(CRelation::TObjects, it2, related) {
147  const CSerialObject* serial = dynamic_cast<const CSerialObject*>(it2->GetObjectPtr());
148  if (serial && serial->GetThisTypeInfo() == typeInfo)
149  results.push_back(SConstScopedObject(it2->GetObjectPtr(), scope));
150  else
151  _ASSERT(false);
152  }
153  }
154  s_EliminateDuplicates(results, canceled);
155 
156  if (canceled.IsCanceled()) {
157  results.clear();
158  }
159 }
160 
162  const CTypeInfo* typeInfo,
163  const TConstScopedObjects& inputObjects,
165 {
167  [typeInfo, &inputObjects, &results](ICanceled& canceled)
168  { s_ConvertObjectsCat(typeInfo, inputObjects, results, canceled); },
169  wxT("Doing object conversions..."));
170 
171 }
172 
174  const CTypeInfo* typeInfo,
175  const TConstScopedObjects& inputObjects,
176  TConstScopedObjects& results)
177 {
179  [typeInfo, &inputObjects, &results](ICanceled& canceled)
180  { s_ConvertObjects(typeInfo, inputObjects, results, canceled); },
181  wxT("Doing object conversions..."));
182 }
183 
std::invoke_result< _Fty, ICanceled & >::type GUI_AsyncExec(_Fty &&_Fnarg, const wxString &msg=wxT("Accessing network..."))
Definition: async_call.hpp:130
USING_SCOPE(objects)
void AsyncConvertObjects(const CTypeInfo *typeInfo, const TConstScopedObjects &inputObjects, map< string, TConstScopedObjects > &results)
static void s_ConvertObjectsCat(const CTypeInfo *typeInfo, const TConstScopedObjects &inputObjects, map< string, TConstScopedObjects > &results, ICanceled &canceled)
static void s_EliminateDuplicates(TConstScopedObjects &objects, ICanceled &canceled)
static void s_ConvertObjects(const CTypeInfo *typeInfo, const TConstScopedObjects &inputObjects, TConstScopedObjects &results, ICanceled &canceled)
CBioseq_Handle –.
vector< TRelation > TRelationVector
static void FindRelations(objects::CScope &scope, const CObject &obj, const string &to_type_in, TRelationVector &relations)
vector< SObject > TObjects
Definition: relation.hpp:130
virtual string GetProperty(const string &) const
Definition: relation.hpp:145
virtual void GetRelated(objects::CScope &scope, const CObject &obj, TObjects &related, TFlags flags=eDefault, ICanceled *cancel=NULL) const =0
@ fConvert_All
retrieve all possible identities if multiple can be shown
Definition: relation.hpp:53
@ fConvert_NoExpensive
do not perform any expensive tests (such as fetching from the network)
Definition: relation.hpp:60
CScope –.
Definition: scope.hpp:92
Base class for all serializable objects.
Definition: serialbase.hpp:150
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Interface for testing cancellation request in a long lasting operation.
Definition: icanceled.hpp:51
void clear()
Definition: map.hpp:169
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
const_iterator find(const key_type &key) const
Definition: set.hpp:137
const_iterator end() const
Definition: set.hpp:136
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
vector< SConstScopedObject > TConstScopedObjects
Definition: objects.hpp:65
virtual const CTypeInfo * GetThisTypeInfo(void) const =0
const CSeq_id * GetId(void) const
Get the id of the location return NULL if has multiple ids or no id at all.
Definition: Seq_loc.hpp:941
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
virtual bool IsCanceled(void) const =0
const string & GetName(void) const
Get name of this type.
Definition: typeinfo.cpp:249
bool IsWhole(void) const
Check if variant Whole is selected.
Definition: Seq_loc_.hpp:522
#define wxT(x)
Definition: muParser.cpp:41
#define _ASSERT
else result
Definition: token2.c:20
Modified on Fri May 24 14:54:35 2024 by modify_doxy.py rev. 669887