SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GenericIEPlugin.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 #ifndef SCI_project_GenericIEPlugin_h
30 #define SCI_project_GenericIEPlugin_h 1
31 
32 #include <Core/Logging/LoggerFwd.h>
33 #include <Core/Thread/Mutex.h>
34 #include <map>
35 #include <boost/lexical_cast.hpp>
36 #include <boost/foreach.hpp>
38 
39 namespace SCIRun {
40 
41 //----------------------------------------------------------------------
42 template <class Data>
44 {
45 public:
47  virtual std::string pluginname() const = 0;
48  virtual std::string fileExtension() const = 0;
49  virtual std::string fileMagic() const = 0;
50 
51  virtual boost::shared_ptr<Data> readFile(const std::string& filename, Core::Logging::LoggerHandle log) const = 0;
52  virtual bool writeFile(boost::shared_ptr<Data> f, const std::string& filename, Core::Logging::LoggerHandle log) const = 0;
53  virtual bool equals(const GenericIEPluginInterface<Data>& other) const = 0;
54  virtual bool hasReader() const = 0;
55  virtual bool hasWriter() const = 0;
56 };
57 
58 template <class Data>
60 {
61  return lhs.equals(rhs);
62 }
63 
64 template <class Data>
66 {
67  return !(lhs == rhs);
68 }
69 
70 
71 template <class Data>
73 {
74 public:
75  virtual std::string pluginname() const override { return pluginname_; }
76  virtual std::string fileExtension() const override { return fileextension_; }
77  virtual std::string fileMagic() const override { return filemagic_; }
78  virtual bool hasReader() const { return filereader_ != nullptr; }
79  virtual bool hasWriter() const { return filewriter_ != nullptr; }
80 
81  virtual boost::shared_ptr<Data> readFile(const std::string& filename, Core::Logging::LoggerHandle log) const override;
82  virtual bool writeFile(boost::shared_ptr<Data> f, const std::string& filename, Core::Logging::LoggerHandle log) const override;
83  virtual bool equals(const GenericIEPluginInterface<Data>& other) const override;
84 
85  IEPluginLegacyAdapter(const std::string &name,
86  const std::string &fileextension,
87  const std::string &filemagic,
88  boost::shared_ptr<Data> (*freader)(Core::Logging::LoggerHandle pr, const char *filename) = 0,
89  bool (*fwriter)(Core::Logging::LoggerHandle pr, boost::shared_ptr<Data> f, const char *filename) = 0);
90 
92 
93  bool operator==(const IEPluginLegacyAdapter& other) const;
94 
95 private:
96  const std::string pluginname_;
97  const std::string fileextension_;
98  const std::string filemagic_;
99 
100  boost::shared_ptr<Data> (*filereader_)(Core::Logging::LoggerHandle pr, const char *filename);
101  bool (*filewriter_)(Core::Logging::LoggerHandle pr,
102  boost::shared_ptr<Data> f, const char *filename);
103 };
104 
105 template <class Data>
107 {
108 public:
109  static Core::Thread::Mutex& getLock();
110  typedef std::map<std::string, GenericIEPluginInterface<Data>*> PluginMap;
111  static PluginMap& getMap();
112  static void createMap();
113  static void destroyMap();
114 
115  size_t numPlugins() const;
116  void get_importer_list(std::vector<std::string>& results) const;
117  void get_exporter_list(std::vector<std::string>& results) const;
118  GenericIEPluginInterface<Data>* get_plugin(const std::string& name) const;
119 private:
120  static Core::Thread::Mutex lock_;
121  static PluginMap* pluginTable_;
122 };
123 
124 
125 template <class Data>
127 
128 template <class Data>
130 
131 template <class Data>
133 {
134  return lock_;
135 }
136 
137 template <class Data>
139 {
140  if (!pluginTable_)
141  createMap();
142  return *pluginTable_;
143 }
144 
145 template <class Data>
147 {
148  if (!pluginTable_)
149  {
150  pluginTable_ = new typename GenericIEPluginManager<Data>::PluginMap();
151  }
152 }
153 
154 template <class Data>
156 {
157  return pluginTable_ ? pluginTable_->size() : 0;
158 }
159 
160 template <class Data>
161 void GenericIEPluginManager<Data>::get_importer_list(std::vector<std::string>& results) const
162 {
163  if (!pluginTable_)
164  {
165  return;
166  }
167 
168  Core::Thread::Guard s(lock_.get());
169  BOOST_FOREACH(const typename PluginMap::value_type& plugin, *pluginTable_)
170  {
171  if (plugin.second->hasReader())
172  results.push_back(plugin.first);
173  }
174 }
175 
176 template <class Data>
177 void GenericIEPluginManager<Data>::get_exporter_list(std::vector<std::string>& results) const
178 {
179  if (!pluginTable_)
180  {
181  return;
182  }
183 
184  Core::Thread::Guard s(lock_.get());
185  BOOST_FOREACH(const typename PluginMap::value_type& plugin, *pluginTable_)
186  {
187  if (plugin.second->hasWriter())
188  results.push_back(plugin.first);
189  }
190 }
191 
192 template <class Data>
194 {
195  if (!pluginTable_)
196  return 0;
197 
198  Core::Thread::Guard s(lock_.get());
199  // Should check for invalid name.
200  auto loc = pluginTable_->find(name);
201  if (loc == pluginTable_->end())
202  {
203  return 0;
204  }
205  else
206  {
207  return loc->second;
208  }
209 }
210 
211 template <class Data>
213 {
214  delete pluginTable_;
215  pluginTable_ = 0;
216 }
217 
218 
219 template <class Data>
221  const std::string& fextension,
222  const std::string& fmagic,
223  boost::shared_ptr<Data> (*freader)(Core::Logging::LoggerHandle pr, const char *filename),
224  bool (*fwriter)(Core::Logging::LoggerHandle pr, boost::shared_ptr<Data> f, const char *filename))
225  : pluginname_(pname),
226  fileextension_(fextension),
227  filemagic_(fmagic),
228  filereader_(freader),
229  filewriter_(fwriter)
230 {
232 
234 
235  std::string tmppname = pluginname_;
236  int counter = 2;
237  for (;;)
238  {
239  auto loc = GenericIEPluginManager<Data>::getMap().find(tmppname);
240  if (loc == GenericIEPluginManager<Data>::getMap().end())
241  {
242  if (tmppname != pluginname_) { const_cast<std::string&>(pluginname_) = tmppname; }
243  GenericIEPluginManager<Data>::getMap()[pluginname_] = this;
244  break;
245  }
246  if (*(*loc).second == *this)
247  {
248  std::cerr << "WARNING: FieldIEPlugin '" << tmppname << "' duplicated.\n";
249  break;
250  }
251 
252  std::cout << "WARNING: Multiple FieldIEPlugins with '" << pluginname_ << "' name.\n";
253  tmppname = pluginname_ + "(" + boost::lexical_cast<std::string>(counter) + ")";
254  counter++;
255  }
256 }
257 
258 template <class Data>
260 {
262 
263  auto iter = GenericIEPluginManager<Data>::getMap().find(pluginname_);
264  if (iter == GenericIEPluginManager<Data>::getMap().end())
265  {
266  std::cerr << "WARNING: FieldIEPlugin " << pluginname_ << " not found in database for removal.\n";
267  }
268  else
269  {
271  }
272 
274  {
276  }
277 }
278 
279 template <class Data>
280 boost::shared_ptr<Data> IEPluginLegacyAdapter<Data>::readFile(const std::string& filename, Core::Logging::LoggerHandle log) const
281 {
282  return filereader_(log, filename.c_str());
283 }
284 
285 template <class Data>
286 bool IEPluginLegacyAdapter<Data>::writeFile(boost::shared_ptr<Data> f, const std::string& filename, Core::Logging::LoggerHandle log) const
287 {
288  return filewriter_(log, f, filename.c_str());
289 }
290 
291 template <class Data>
293 {
294  auto impl = dynamic_cast<const IEPluginLegacyAdapter<Data>*>(&other);
295  if (!impl)
296  return false;
297  return *this == *impl;
298 }
299 
300 template <class Data>
302 {
303  return (pluginname_ == other.pluginname_ &&
304  fileextension_ == other.fileextension_ &&
305  filemagic_ == other.filemagic_ &&
306  filereader_ == other.filereader_ &&
307  filewriter_ == other.filewriter_);
308 }
309 
310 template <class Data>
312 {
313  return "";
314 }
315 
316 template <>
317 SCISHARE std::string defaultImportTypeForFile(const GenericIEPluginManager<Field>* mgr);
318 
319 SCISHARE std::string fileTypeDescriptionFromDialogBoxFilter(const std::string& fileFilter);
320 
321 template <class Data>
322 std::string printPluginDescriptionsForFilter(const GenericIEPluginManager<Data>& mgr, const std::string& defaultType, const std::vector<std::string>& pluginNames)
323 {
324  std::ostringstream types;
325  types << defaultType;
326 
327  BOOST_FOREACH(const std::string& name, pluginNames)
328  {
329  auto pl = mgr.get_plugin(name);
330  types << ";;" << name;
331  if (!pl->fileExtension().empty())
332  {
333  types << " (*." << pl->fileExtension() << ")";
334  }
335  else
336  {
337  types << " (.*)";
338  }
339  }
340 
341  return types.str();
342 }
343 
344 template <class Data>
346 {
347  std::vector<std::string> importers;
348  mgr.get_importer_list(importers);
349 
350  return printPluginDescriptionsForFilter(mgr, defaultImportTypeForFile(&mgr), importers);
351 }
352 
353 template <class Data>
355 {
356  return "";
357 }
358 
359 template <>
360 SCISHARE std::string defaultExportTypeForFile(const GenericIEPluginManager<Field>* mgr);
361 
362 template <class Data>
364 {
365  std::vector<std::string> exporters;
366  mgr.get_exporter_list(exporters);
367  return printPluginDescriptionsForFilter(mgr, defaultExportTypeForFile(&mgr), exporters);
368 }
369 
370 }
371 
372 #endif
static void createMap()
Definition: GenericIEPlugin.h:146
boost::shared_ptr< LegacyLoggerInterface > LoggerHandle
Definition: LoggerFwd.h:42
bool operator==(const IEPluginLegacyAdapter &other) const
Definition: GenericIEPlugin.h:301
virtual bool hasReader() const
Definition: GenericIEPlugin.h:78
std::string makeGuiTypesListForImport(const GenericIEPluginManager< Data > &mgr)
Definition: GenericIEPlugin.h:345
virtual ~GenericIEPluginInterface()
Definition: GenericIEPlugin.h:46
Definition: GenericIEPlugin.h:106
Definition: GenericIEPlugin.h:43
virtual bool writeFile(boost::shared_ptr< Data > f, const std::string &filename, Core::Logging::LoggerHandle log) const override
Definition: GenericIEPlugin.h:286
IEPluginLegacyAdapter(const std::string &name, const std::string &fileextension, const std::string &filemagic, boost::shared_ptr< Data >(*freader)(Core::Logging::LoggerHandle pr, const char *filename)=0, bool(*fwriter)(Core::Logging::LoggerHandle pr, boost::shared_ptr< Data > f, const char *filename)=0)
Definition: GenericIEPlugin.h:220
#define SCISHARE
Definition: share.h:39
virtual std::string pluginname() const =0
void get_importer_list(std::vector< std::string > &results) const
Definition: GenericIEPlugin.h:161
Definition: Mutex.h:43
virtual std::string fileExtension() const override
Definition: GenericIEPlugin.h:76
std::map< std::string, GenericIEPluginInterface< Data > * > PluginMap
Definition: GenericIEPlugin.h:110
virtual boost::shared_ptr< Data > readFile(const std::string &filename, Core::Logging::LoggerHandle log) const =0
virtual std::string fileMagic() const =0
size_t numPlugins() const
Definition: GenericIEPlugin.h:155
GenericIEPluginInterface< Data > * get_plugin(const std::string &name) const
Definition: GenericIEPlugin.h:193
virtual bool equals(const GenericIEPluginInterface< Data > &other) const =0
std::string makeGuiTypesListForExport(const GenericIEPluginManager< Data > &mgr)
Definition: GenericIEPlugin.h:363
bool operator==(const SparseSparseElement &s1, const SparseSparseElement &s2)
Definition: SparseMatrixFunctions.cc:104
std::string defaultImportTypeForFile(const GenericIEPluginManager< Data > *mgr=0)
Definition: GenericIEPlugin.h:311
const char * name[]
Definition: BoostGraphExampleTests.cc:87
~IEPluginLegacyAdapter()
Definition: GenericIEPlugin.h:259
bool operator!=(const GenericIEPluginInterface< Data > &lhs, const GenericIEPluginInterface< Data > &rhs)
Definition: GenericIEPlugin.h:65
virtual std::string fileMagic() const override
Definition: GenericIEPlugin.h:77
std::string printPluginDescriptionsForFilter(const GenericIEPluginManager< Data > &mgr, const std::string &defaultType, const std::vector< std::string > &pluginNames)
Definition: GenericIEPlugin.h:322
virtual bool writeFile(boost::shared_ptr< Data > f, const std::string &filename, Core::Logging::LoggerHandle log) const =0
virtual bool equals(const GenericIEPluginInterface< Data > &other) const override
Definition: GenericIEPlugin.h:292
void get_exporter_list(std::vector< std::string > &results) const
Definition: GenericIEPlugin.h:177
SCISHARE std::string fileTypeDescriptionFromDialogBoxFilter(const std::string &fileFilter)
Definition: NrrdIEPlugin.cc:37
static Core::Thread::Mutex & getLock()
Definition: GenericIEPlugin.h:132
static PluginMap & getMap()
Definition: GenericIEPlugin.h:138
virtual std::string fileExtension() const =0
virtual std::string pluginname() const override
Definition: GenericIEPlugin.h:75
virtual bool hasWriter() const
Definition: GenericIEPlugin.h:79
virtual bool hasReader() const =0
virtual bool hasWriter() const =0
virtual boost::shared_ptr< Data > readFile(const std::string &filename, Core::Logging::LoggerHandle log) const override
Definition: GenericIEPlugin.h:280
boost::lock_guard< boost::mutex > Guard
Definition: Mutex.h:55
std::string defaultExportTypeForFile(const GenericIEPluginManager< Data > *mgr=0)
Definition: GenericIEPlugin.h:354
Definition: GenericIEPlugin.h:72
static void destroyMap()
Definition: GenericIEPlugin.h:212