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

Go to the SVN repository for this file.

1 #ifndef GUI_UTILS___RELATION__HPP
2 #define GUI_UTILS___RELATION__HPP
3 
4 /* $Id: relation.hpp 30262 2014-04-21 17:56:26Z kuznets $
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: Roman Katargin
30  *
31  * File Description:
32  *
33  */
34 
35 #include <corelib/ncbiobj.hpp>
36 #include <gui/gui_export.h>
37 #include <gui/objutils/objects.hpp>
38 #include <util/icanceled.hpp>
39 
41 
43  class CScope;
45 
46 class CObjectList;
47 
49 {
50 public:
51  enum {
52  /// retrieve all possible identities if multiple can be shown
53  fConvert_All = 0x01,
54 
55  /// retrieve the best single entity if multiple can be shown
56  fConvert_Best = 0x02,
57 
58  /// do not perform any expensive tests (such as fetching from
59  /// the network)
60  fConvert_NoExpensive = 0x03,
61 
62  /// combined sets of flags
63  eDefault = fConvert_Best
64  };
65  typedef int TFlags;
66 
67  /// struct SObject provides an interface for defining what is returned from
68  /// object conversion. Rather than simply provide the object, we can
69  /// provide some contextual information as well.
71  {
72  public:
73  SObject();
74  SObject(const CObject& obj);
75  SObject(const CObject& obj, objects::CScope& scope);
76  SObject(const CObject& obj, const string& desc);
77  SObject(const CObject& obj, const string& desc, const string& comm);
78 
79  /// retrieve the source for this object
80  /// this is the original object that was used to generate this object
81  const CObject& GetSource() const
82  {
83  return *source;
84  }
85 
86  /// retrieve a descriptive string concerning the object
87  const string& GetDescription() const
88  {
89  return description;
90  }
91 
92  /// retrieve a comment concerning what this object means
93  /// this may be, for example, a description of the relationship of
94  /// the object to its source
95  const string& GetComment() const
96  {
97  return comment;
98  }
99 
100  /// return the object through a couple of equivalent functions:
101 
102  /// dereference operator
103  const CObject& operator*() const
104  {
105  return *object;
106  }
107 
108  /// cast operator
109  operator const CObject&(void) const
110  {
111  return *object;
112  }
113 
114  /// named function
115  const CObject& GetObject(void) const
116  {
117  return *object;
118  }
119 
120  /// named function
121  const CObject* GetObjectPtr(void) const
122  {
123  return object;
124  }
125 
126  private:
127  friend class CObjectConverter;
128 
129  /// the converted object
131 
132  /// the source object
134 
135  /// a description of this object
136  string description;
137 
138  /// a comment concerning this object's origin
139  string comment;
140  };
141  typedef vector<SObject> TObjects;
142 
143  CRelation() : m_NeedDescr(true) {}
144 
145  virtual string GetName() const = 0;
146  virtual string GetDescription() const = 0;
147 
148  virtual string GetTypeName() const = 0;
149  virtual string GetRelatedTypeName() const = 0;
150 
151  virtual void GetRelated(objects::CScope& scope, const CObject& obj,
152  TObjects& related,
153  TFlags flags = eDefault,
154  ICanceled* cancel = NULL) const = 0;
155 
156  virtual void GetRelatedObjectList(
157  objects::CScope& scope, const CObject& obj,
158  CObjectList& related_list,
159  TFlags flags = eDefault,
160  ICanceled* cancel = NULL
161  ) const;
162 
163  virtual string GetProperty(const string& /*key*/) const { return ""; }
164 
165  virtual void Dump(ostream& /*ostream*/) const {}
166 
167  static const CRelation* ReadObject(CObjectIStream& in);
168  void WriteObject(CObjectOStream& out) const;
169 
170  /// Set flag if we need detailed relation descriptions
171  /// Descr calculation is associated with some extra costs
172  ///
173  void SetRelatedDescr(bool need_descr) { m_NeedDescr = need_descr; }
174 
175 protected:
176  static objects::CUser_object* UserObjectFromRelation(const CRelation& relation);
177  static const CRelation* RelationFromUserObject(const objects::CUser_object& user_obj);
178  virtual void ToUserObject(objects::CUser_object& /*user_obj*/) const {}
179  virtual void FromUserObject(const objects::CUser_object& /*user_obj*/) {}
180 
181 private:
182  static const char* m_szUserObjectType;
183  static const char* m_szRelationName;
184 protected:
185  bool m_NeedDescr; ///< flag if we need to calculate description
186 };
187 
188 //
189 // Identity Relation
190 //
191 
193 {
194 public:
195  virtual string GetName() const { return m_Name; }
196  virtual string GetDescription() const { return "Identity Relation"; }
197 
198  virtual string GetTypeName() const { return "Object"; }
199  virtual string GetRelatedTypeName() const { return "Object"; }
200 
201  virtual void GetRelated(objects::CScope& scope, const CObject& obj,
202  TObjects& related,
203  TFlags flags = eDefault,
204  ICanceled* cancel = NULL) const;
205  virtual string GetProperty(const string& /*key*/) const { return "Not Converted"; }
206  virtual void Dump(ostream& ostream) const;
207 
208  static const char* m_Name;
209 };
210 
212 {
213 public:
214  CComplexRelation(bool parallel = false) : m_Parallel(parallel) {}
215  virtual string GetName() const { return m_Name; }
216  virtual string GetDescription() const { return "Complex Relation"; }
217 
218  virtual string GetTypeName() const;
219  virtual string GetRelatedTypeName() const;
220 
221  virtual void GetRelated(objects::CScope& scope, const CObject& obj,
222  TObjects& related,
223  TFlags flags = eDefault,
224  ICanceled* cancel = NULL) const;
225 
226  virtual string GetProperty(const string& key) const;
227 
228  virtual void Dump(ostream& ostream) const;
229 
230  void AddRelation(const CRelation* relation);
231 
232  static const char* m_Name;
233 
234 protected:
235  virtual void ToUserObject(objects::CUser_object& user_obj) const;
236  virtual void FromUserObject(const objects::CUser_object& user_obj);
237 
238 private:
240  typedef vector<CConstRef<CRelation> > RelVector;
241  RelVector m_relations;
242  static const char* m_szRelationsFieldName;
243  static const char* m_szParallelFieldName;
244 };
245 
247 {
248 public:
249  typedef void (*TFNConvert)(objects::CScope& scope,
250  const CObject& obj,
251  TObjects& related,
252  TFlags flags,
253  ICanceled* cancel,
254  bool need_descr);
255 
256  struct SPropPair { const string typeName; const string value; };
257 
258  CBasicRelation() : m_pFnCvt(0) {}
259 
260  CBasicRelation(const string& name,
261  const string& from_type,
262  const string& to_type,
263  TFNConvert pfncvt,
264  const SPropPair* properties = 0) :
265  m_Name(name),
266  m_FromType(from_type),
267  m_ToType(to_type),
268  m_pFnCvt(pfncvt),
269  m_Properties(properties) {}
270 
271  virtual string GetName() const { return m_Name; }
272  virtual string GetDescription() const { return ""; }
273 
274  virtual string GetTypeName() const { return m_FromType; }
275  virtual string GetRelatedTypeName() const { return m_ToType; }
276 
277  virtual void GetRelated(objects::CScope& scope, const CObject& obj,
278  TObjects& related,
279  TFlags flags = eDefault,
280  ICanceled* cancel = NULL) const
281  {
282  m_pFnCvt(scope, obj, related, flags, cancel, m_NeedDescr);
283  }
284 
285  virtual string GetProperty(const string& key) const;
286 
287  virtual void Dump(ostream& ostream) const
288  {
289  ostream << m_Name;
290  }
291 
292 private:
293  string m_Name;
294  string m_FromType;
295  string m_ToType;
296  TFNConvert m_pFnCvt;
298 };
299 
300 /////////////////////////////////////////////////////////////////////////////
301 ///
302 /// Macros to assist in building property lists for relation converters
303 ///
304 
305 #define BEGIN_RELATION_PROPERTY_LIST(name)\
306 static const CBasicRelation::SPropPair name[]={\
307 
308 #define RELATION_PROPERTY_ENTRY(typeName,value)\
309 {typeName,value},
310 
311 #define END_RELATION_PROPERTY_LIST()\
312 {"",""}};
313 
314 
315 
316 
318 {
319  pOb->WriteObject(out);
320  return out;
321 }
323 {
324  pOb->WriteObject(out);
325  return out;
326 }
327 inline CObjectOStream& operator<<(CObjectOStream& out, CRef<CRelation>& pOb)
328 {
329  pOb->WriteObject(out);
330  return out;
331 }
332 inline CObjectOStream& operator<<(CObjectOStream& out, CConstRef<CRelation>& pOb)
333 {
334  pOb->WriteObject(out);
335  return out;
336 }
338 {
339  pOb = CRelation::ReadObject(in);
340  return in;
341 }
343 {
344  ptr.Reset(CRelation::ReadObject(in));
345  return in;
346 }
347 
349 
350 #endif // GUI_UTILS___RELATION__HPP
Interface for testing cancellation request in a long lasting operation.
Definition: icanceled.hpp:50
virtual string GetRelatedTypeName() const
Definition: relation.hpp:275
CObjectIStream –.
Definition: objistr.hpp:92
CComplexRelation(bool parallel=false)
Definition: relation.hpp:214
virtual string GetRelatedTypeName() const
Definition: relation.hpp:199
int TFlags
Definition: relation.hpp:65
Definition: dbpivot.c:60
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:73
std::ofstream out("events_result.xml")
main entry point for tests
CConstRef< CObject > object
the converted object
Definition: relation.hpp:130
static const char * m_szRelationName
Definition: relation.hpp:183
CObjectOStream –.
Definition: objostr.hpp:82
static const char * m_szUserObjectType
Definition: relation.hpp:182
virtual string GetTypeName() const
Definition: relation.hpp:274
string comment
a comment concerning this object's origin
Definition: relation.hpp:139
const CObject & operator*() const
return the object through a couple of equivalent functions:
Definition: relation.hpp:103
virtual string GetDescription() const
Definition: relation.hpp:216
static const CRelation * ReadObject(CObjectIStream &in)
Definition: relation.cpp:57
#define NULL
Definition: ncbistd.hpp:225
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:1343
TFNConvert m_pFnCvt
Definition: relation.hpp:296
static const char * m_szParallelFieldName
Definition: relation.hpp:243
CObjectList Data structure representing a list of CObjects with associated Scopes and other optional ...
Definition: object_list.hpp:61
void WriteObject(CObjectOStream &out) const
Definition: relation.cpp:64
virtual string GetRelatedTypeName() const =0
virtual string GetProperty(const string &) const
Definition: relation.hpp:205
virtual void FromUserObject(const objects::CUser_object &)
Definition: relation.hpp:179
string description
a description of this object
Definition: relation.hpp:136
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
string m_FromType
Definition: relation.hpp:294
Defines to provide correct exporting from DLLs in Windows.
CObjectIStream & operator>>(CObjectIStream &in, const CRelation *&pOb)
Definition: relation.hpp:337
vector< CConstRef< CRelation > > RelVector
Definition: relation.hpp:240
const string & GetComment() const
retrieve a comment concerning what this object means this may be, for example, a description of the r...
Definition: relation.hpp:95
vector< SObject > TObjects
Definition: relation.hpp:141
CBasicRelation(const string &name, const string &from_type, const string &to_type, TFNConvert pfncvt, const SPropPair *properties=0)
Definition: relation.hpp:260
CObjectOStream & operator<<(CObjectOStream &out, CRelation *pOb)
Definition: relation.hpp:317
const CObject & GetSource() const
retrieve the source for this object this is the original object that was used to generate this object...
Definition: relation.hpp:81
CConstRef< CObject > source
the source object
Definition: relation.hpp:133
const CharType(& source)[N]
Definition: pointer.h:1107
virtual string GetName() const
Definition: relation.hpp:215
const CObject & GetObject(void) const
named function
Definition: relation.hpp:115
virtual string GetProperty(const string &) const
Definition: relation.hpp:163
virtual void Dump(ostream &) const
Definition: relation.hpp:165
struct SObject provides an interface for defining what is returned from object conversion.
Definition: relation.hpp:70
CNcbiOstream & WriteObject(CNcbiOstream &str, TConstObjectPtr ptr, TTypeInfo info)
virtual string GetDescription() const
Definition: relation.hpp:196
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx...
const CObject * GetObjectPtr(void) const
named function
Definition: relation.hpp:121
static const char * m_szRelationsFieldName
Definition: relation.hpp:242
CScope –.
Definition: scope.hpp:90
void SetRelatedDescr(bool need_descr)
Set flag if we need detailed relation descriptions Descr calculation is associated with some extra co...
Definition: relation.hpp:173
void WriteObject(const CConstObjectInfo &object)
Definition: objostr.cpp:551
RelVector m_relations
Definition: relation.hpp:241
const string & GetDescription() const
retrieve a descriptive string concerning the object
Definition: relation.hpp:87
string m_ToType
Definition: relation.hpp:295
CObject –.
Definition: ncbiobj.hpp:180
static const char * m_Name
Definition: relation.hpp:208
virtual void ToUserObject(objects::CUser_object &) const
Definition: relation.hpp:178
virtual void GetRelated(objects::CScope &scope, const CObject &obj, TObjects &related, TFlags flags=eDefault, ICanceled *cancel=NULL) const
Definition: relation.hpp:277
virtual string GetTypeName() const
Definition: relation.hpp:198
#define NCBI_GUIOBJUTILS_EXPORT
Definition: gui_export.h:501
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:70
CNcbiIstream & ReadObject(CNcbiIstream &str, TObjectPtr ptr, TTypeInfo info)
virtual string GetName() const
Definition: relation.hpp:271
virtual string GetName() const
Definition: relation.hpp:195
std::istream & in(std::istream &in_, double &x_)
const SPropPair * m_Properties
Definition: relation.hpp:297
virtual string GetDescription() const
Definition: relation.hpp:272
vector< CRef< CObject > > TObjects
Definition: objects.hpp:100
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
virtual string GetTypeName() const =0
virtual void Dump(ostream &ostream) const
Definition: relation.hpp:287
bool m_NeedDescr
flag if we need to calculate description
Definition: relation.hpp:185
virtual void GetRelated(objects::CScope &scope, const CObject &obj, TObjects &related, TFlags flags=eDefault, ICanceled *cancel=NULL) const =0
static const char * m_Name
Definition: relation.hpp:232
Modified on Thu Aug 25 18:56:34 2016 by modify_doxy.py rev. 506947