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

Go to the SVN repository for this file.

1 #ifndef GUI_UTILS___OBJ_CONVERT__HPP
2 #define GUI_UTILS___OBJ_CONVERT__HPP
3 
4 /* $Id: obj_convert.hpp 23138 2011-02-10 21:46:45Z katargir $
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: Mike DiCuccio
30  *
31  * File Description:
32  *
33  */
34 
35 
36 #include <corelib/ncbiobj.hpp>
37 #include <objmgr/scope.hpp>
38 #include <gui/gui_export.h>
40 
41 
43 
44 
45 //
46 // base interface for a conversion object
47 //
48 
49 class ITypeConverter : public CObject
50 {
51 public:
52  enum {
53  /// retrieve all possible identities if multiple can be shown
54  fConvert_All = 0x01,
55 
56  /// retrieve the best single entity if multiple can be shown
57  fConvert_Best = 0x02,
58 
59  /// do not perform any expensive tests tests (such as fetching from
60  /// the network)
62 
63  /// combined sets of flags
65  };
66  typedef int TFlags;
67 
68  /// struct SObject provides an interface for defining what is returned from
69  /// object conversion. Rather than simply provide the object, we can
70  /// provide some contextual information as well.
71  struct SObject
72  {
73  public:
75  {
76  }
77 
78  SObject(const CObject& obj)
79  : object(&obj)
80  {
81  }
82 
83  SObject(const CObject& obj, const string& comm)
84  : object(&obj)
85  , comment(comm)
86  {
87  }
88 
89  /// retrieve the source for this object
90  /// this is the original object that was used to generate this object
91  const CObject& GetSource() const
92  {
93  return *source;
94  }
95 
96  /// retrieve a comment concerning what this object means
97  /// this may be, for example, a description of the relationship of
98  /// the object to its source
99  const string& GetComment() const
100  {
101  return comment;
102  }
103 
104  /// return the object through a couple of equivalent functions:
105 
106  /// dereference operator
107  const CObject& operator*() const
108  {
109  return *object;
110  }
111 
112  /// cast operator
113  operator const CObject&(void) const
114  {
115  return *object;
116  }
117 
118  /// named function
119  const CObject& GetObject(void) const
120  {
121  return *object;
122  }
123 
124  /// named function
125  const CObject* GetObjectPtr(void) const
126  {
127  return object;
128  }
129 
130  private:
131  friend class CObjectConverter;
132 
133  /// the converted object
135 
136  /// the source object
138 
139  /// a description of this object
140  string comment;
141  };
142  typedef vector<SObject> TObjList;
143 
144  virtual void Convert(objects::CScope& scope, const CObject& obj,
145  TObjList& objs,
146  TFlags flags = eDefault) const = 0;
147 };
148 
149 
150 
151 ///
152 /// single conversion interface
153 ///
154 
155 class CConvGraph;
156 
158 {
159 public:
160  enum {
161  /// default here has a different value than above
162  /// this is intentional
163  eDefault = 0
164  };
165  typedef int TFlags;
166 
167 // New Interface
169  typedef vector<TRelation> TRelationVector;
170 
171  static void Register(CRelation* rel);
172  static const CRelation* FindRelationByName(const string& name);
173 
174  static void FindRelations(objects::CScope& scope,
175  const CObject& obj,
176  const string& to_type_in,
177  TRelationVector& relations);
178 
179  static void DumpDotGraph(ostream& ostream, bool dumpIDs = false);
180 
181 // Old Interface
183  /// Convert an object of potentially unknown type to a set of objects of
184  /// known type
185  static void Convert(objects::CScope& scope, const CObject& obj,
186  const CTypeInfo* info, TObjList& objs,
187  TFlags flags = eDefault);
188 
189  /// Convert an object of potentially unknown type to a set of objects of
190  /// known type
191  static void Convert(objects::CScope& scope, const CObject& obj,
192  const string& type_name, TObjList& objs,
193  TFlags flags = eDefault);
194 
195  /// Determine whether an indicated conversion can be performed
196  static bool CanConvert(objects::CScope& scope, const CObject& obj,
197  const CTypeInfo* type_info);
198 
199  static bool CanConvert(objects::CScope& scope, const CObject& obj,
200  const string& type_name);
201 
202  /// Register a conversion function object from a given type to
203  /// another given type
204  static void Register(const CTypeInfo* from_type,
205  const CTypeInfo* to_type,
206  ITypeConverter* cvt);
207 
208  static void Register(const CTypeInfo* from_type,
209  const string& to_type,
210  ITypeConverter* cvt);
211 
212  static void Register(const string& from_type,
213  const CTypeInfo* to_type,
214  ITypeConverter* cvt);
215 
216  static void Register(const string& from_type,
217  const string& to_type,
218  ITypeConverter* cvt);
219 
220  static void RegisterTypeAlias(const string& real_name,
221  const string& alias);
222 
223  static void SetDefaultFlags(TFlags flags);
224  static TFlags GetDefaultFlags(void);
225 
226 private:
227  /// the default conversion flags
229 
230  /// a list of type aliases
231  /// aliases are applied to interpret the to type only!
234 
235  static const string& x_NormalizeTypeName(const string& str);
236 
238  static void x_BuildGraph(CConvGraph& graph, map<string, size_t>& vertices);
239  static size_t x_FindRelationByName(const string& name);
240 };
241 
242 
243 
244 //
245 // CConvertCache holds a cache of converted objects
246 // This is useful in situation in which a given object may be requested
247 // as a given type multiple times in a row.
248 //
249 
251 {
252 public:
253 
254  // re-typedef from CObjectConvert
256 
257  // convert an object using a CTypeInfo object
258  virtual const TObjList& Convert(objects::CScope& scope, const CObject& obj,
259  const CTypeInfo* info,
261 
262  // convert an object using a string-based type name
263  virtual const TObjList& Convert(objects::CScope& scope, const CObject& obj,
264  const string& type_name,
266 
267 
268 
269  // SCacheKey holds the information relevant for a single converted
270  // object
271  struct SCacheKey
272  {
273  SCacheKey(objects::CScope& scope, const CObject& obj,
274  const string& type)
275  : m_Scope(&scope),
276  m_Obj(&obj),
277  m_Type(type)
278  {}
279 
281  : m_Scope(key.m_Scope),
282  m_Obj(key.m_Obj),
283  m_Type(key.m_Type)
284  {}
285 
288  string m_Type;
289  };
290 
291  // functor for sorting SCacheKey objects
293  {
294  bool operator() (const SCacheKey& key1, const SCacheKey& key2) const;
295  };
296 
297 private:
298 
299  // the cache itself
302 
303  // empty list of objects to be returned if no hits found
305 };
306 
307 
308 
310 
311 #endif // GUI_UTILS___OBJ_CONVERT__HPP
static CRef< CScope > m_Scope
TObjList m_EmptyObjList
virtual const TObjList & Convert(objects::CScope &scope, const CObject &obj, const string &type_name, CObjectConverter::TFlags flags=CObjectConverter::eDefault)
virtual const TObjList & Convert(objects::CScope &scope, const CObject &obj, const CTypeInfo *info, CObjectConverter::TFlags flags=CObjectConverter::eDefault)
map< SCacheKey, TObjList, SCacheKeySort > TCache
CObjectConverter::TObjList TObjList
static TTypeAliases sm_TypeAliases
static void Convert(objects::CScope &scope, const CObject &obj, const CTypeInfo *info, TObjList &objs, TFlags flags=eDefault)
Convert an object of potentially unknown type to a set of objects of known type.
CRef< CRelation > TRelation
vector< TRelation > TRelationVector
static TFlags sm_DefaultFlags
the default conversion flags
map< string, string > TTypeAliases
a list of type aliases aliases are applied to interpret the to type only!
static bool CanConvert(objects::CScope &scope, const CObject &obj, const string &type_name)
ITypeConverter::TObjList TObjList
static bool CanConvert(objects::CScope &scope, const CObject &obj, const CTypeInfo *type_info)
Determine whether an indicated conversion can be performed.
@ eDefault
default here has a different value than above this is intentional
static void Convert(objects::CScope &scope, const CObject &obj, const string &type_name, TObjList &objs, TFlags flags=eDefault)
Convert an object of potentially unknown type to a set of objects of known type.
static TRelationVector sm_Relations
CObject –.
Definition: ncbiobj.hpp:180
CRef –.
Definition: ncbiobj.hpp:618
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
@ fConvert_NoExpensive
do not perform any expensive tests tests (such as fetching from the network)
Definition: obj_convert.hpp:61
@ fConvert_All
retrieve all possible identities if multiple can be shown
Definition: obj_convert.hpp:54
@ eDefault
combined sets of flags
Definition: obj_convert.hpp:64
@ fConvert_Best
retrieve the best single entity if multiple can be shown
Definition: obj_convert.hpp:57
vector< SObject > TObjList
virtual void Convert(objects::CScope &scope, const CObject &obj, TObjList &objs, TFlags flags=eDefault) const =0
static uch flags
static const char * str(char *buf, int n)
Definition: stats.c:84
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NCBI_GUIOBJUTILS_EXPORT
Definition: gui_export.h:512
Defines to provide correct exporting from DLLs in Windows.
static MDB_envinfo info
Definition: mdb_load.c:37
const struct ncbi::grid::netcache::search::fields::KEY key
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
SCacheKey(const SCacheKey &key)
SCacheKey(objects::CScope &scope, const CObject &obj, const string &type)
CConstRef< objects::CScope > m_Scope
CConstRef< CObject > m_Obj
struct SObject provides an interface for defining what is returned from object conversion.
Definition: obj_convert.hpp:72
CConstRef< CObject > object
the converted object
const CObject * GetObjectPtr(void) const
named function
const CObject & GetSource() const
retrieve the source for this object this is the original object that was used to generate this object
Definition: obj_convert.hpp:91
const CObject & operator*() const
return the object through a couple of equivalent functions:
SObject(const CObject &obj, const string &comm)
Definition: obj_convert.hpp:83
const string & GetComment() const
retrieve a comment concerning what this object means this may be, for example, a description of the r...
Definition: obj_convert.hpp:99
const CObject & GetObject(void) const
named function
CConstRef< CObject > source
the source object
SObject(const CObject &obj)
Definition: obj_convert.hpp:78
string comment
a description of this object
Definition: type.c:6
static const char * type_name(CS_INT value)
Definition: will_convert.c:122
Modified on Fri Sep 20 14:57:56 2024 by modify_doxy.py rev. 669887