Ilwis-Objects  1.0
GIS and Remote Sensing framework for data access and processing
 All Classes Functions Enumerations Pages
connectorfactory.h
1 #ifndef CONNECTORFACTORY_H
2 #define CONNECTORFACTORY_H
3 
4 #include "Kernel_global.h"
5 #include <QScopedPointer>
6 #include <QHash>
7 
8 namespace Ilwis {
9 
10 class ConnectorInterface;
11 class CatalogExplorer;
12 
13 typedef std::function<Ilwis::CatalogExplorer*(const Ilwis::Resource &resource,const PrepareOptions& options)> createCatalogExplorer;
14 
15 //typedef ConnectorInterface* (*ConnectorCreate)(const IlwisResource &resource);
16 //-----------------------------------------------------------------------------------------
18  ConnectorFilter(quint64 types, const QString& provider) : _objectTypes(types), _provider(provider) {}
19  quint64 _objectTypes;
20  QString _provider;
21 };
22 
24  ConnectorFormatSelector(const QString& format, const QString& provider=sUNDEF) : _format(format), _provider(provider) {}
25 
26  QString _format;
27  QString _provider;
28 
29 };
30 
31 KERNELSHARED_EXPORT uint qHash(const ConnectorFilter& resource );
32 KERNELSHARED_EXPORT bool operator==(const ConnectorFilter& filter1, const ConnectorFilter& filter2 );
33 KERNELSHARED_EXPORT bool operator<(const ConnectorFilter& filter1, const ConnectorFilter& filter2 );
34 
35 KERNELSHARED_EXPORT uint qHash(const ConnectorFormatSelector& resource );
36 KERNELSHARED_EXPORT bool operator==(const ConnectorFormatSelector& filter1, const ConnectorFormatSelector& filter2 );
37 KERNELSHARED_EXPORT bool operator<(const ConnectorFormatSelector& filter1, const ConnectorFormatSelector& filter2 );
38 
39 
46 class KERNELSHARED_EXPORT ConnectorFactory : public AbstractFactory
47 {
48  friend class ConnectorInterface;
49 
50 public:
55 
62  void addCreator(quint64 objecttypes, const QString &provider, ConnectorCreate);
68  void addCreator(const QString& format, const QString &provider, ConnectorCreate);
69 
79  template<class T=ConnectorInterface> T *createFromResource(const Resource& resource, const QString &provider,const PrepareOptions& options=PrepareOptions()) const{
80  ConnectorFilter filter(resource.ilwisType(), provider);
81  auto iter = _creatorsPerObject.find(filter);
82  if ( iter == _creatorsPerObject.end())
83  return 0;
84  ConnectorCreate createConnector = iter.value();
85  if ( createConnector ) {
86  ConnectorInterface *cif = createConnector(resource, true, options);
87  if ( cif && cif->canUse(resource))
88  return dynamic_cast<T *>(cif);
89  delete cif;
90  }
91  kernel()->issues()->log(TR(ERR_COULDNT_CREATE_OBJECT_FOR_2).arg("Connector",resource.name()));
92  return 0;
93 
94  }
95 
96  template<class T=ConnectorInterface> QList<T*> connectorsForResource(const Resource& resource, const QString &provider="*",const PrepareOptions& options=PrepareOptions()) const{
97  ConnectorFilter filter(resource.ilwisType(), provider);
98  QList<T*> results;
99  for(auto key : _creatorsPerObject.keys()){
100  if ( filter == key) {
101  ConnectorCreate createConnector = _creatorsPerObject[key];
102  if ( createConnector ) {
103  ConnectorInterface *cif = createConnector(resource, true, options);
104  if ( cif && cif->canUse(resource)){
105  T* conn = dynamic_cast<T *>(cif);
106  if ( conn)
107  results.push_back(conn);
108  }else
109  delete cif;
110  }
111  }
112  }
113  return results;
114 
115  }
116 
123  template<class T=ConnectorInterface> T *createFromFormat(const Resource& resource, const QString &format, const QString& provider=sUNDEF,const PrepareOptions& options=PrepareOptions()) const{
124  ConnectorFormatSelector filter(format, provider);
125  auto iter = _creatorsPerFormat.find(filter);
126  if ( iter == _creatorsPerFormat.end())
127  return 0;
128  ConnectorCreate createConnector = iter.value();
129  if ( createConnector ) {
130  ConnectorInterface *cif = createConnector(resource, false, options);
131  if ( cif){
132  cif->format(format);
133  return dynamic_cast<T *>(cif);
134  }
135  }
136 
137 
138  kernel()->issues()->log(TR(ERR_COULDNT_CREATE_OBJECT_FOR_2).arg("Connector",resource.name()));
139  return 0;
140  }
141 
142  template<class T=ConnectorInterface> T *createContainerConnector(const Resource& resource, const QString &provider=sUNDEF,const PrepareOptions& options=PrepareOptions()) const{
143  for(auto iter=_creatorsPerObject.begin(); iter != _creatorsPerObject.end(); ++iter){
144  if ( !hasType(iter.key()._objectTypes, resource.ilwisType()))
145  continue;
146  ConnectorCreate createConnector = iter.value();
147  if ( createConnector && ( iter.key()._provider == provider || provider == sUNDEF)) {
148  ConnectorInterface *cif = createConnector(resource, true, options);
149  if ( cif && cif->canUse(resource))
150  return dynamic_cast<T *>(cif);
151  delete cif;
152  }
153  }
154  kernel()->issues()->log(TR(ERR_COULDNT_CREATE_OBJECT_FOR_2).arg("Connector",resource.name()));
155  return 0;
156 
157  }
158 
159  std::vector<CatalogExplorer*> explorersForResource(const Resource& resource, const QString &provider=sUNDEF,const PrepareOptions& options=PrepareOptions()) const;
160 
161  static std::nullptr_t registerCatalogExplorer(createCatalogExplorer func);
162 
163 protected:
164  QHash<ConnectorFilter, ConnectorCreate > _creatorsPerObject;
165  QHash<ConnectorFormatSelector, ConnectorCreate > _creatorsPerFormat;
166  static std::vector<createCatalogExplorer> _explorers;
167 };
168 }
169 
170 
171 #endif // CONNECTORFACTORY_H