Chemical Data Processing Library C++ API - Version 1.2.0
DataIOManager.hpp
Go to the documentation of this file.
1 /*
2  * DataIOManager.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_BASE_DATAIOMANAGER_HPP
30 #define CDPL_BASE_DATAIOMANAGER_HPP
31 
32 #include <algorithm>
33 #include <functional>
34 #include <vector>
35 #include <string>
36 #include <cstddef>
37 
38 #include "CDPL/Base/APIPrefix.hpp"
41 #include "CDPL/Base/DataFormat.hpp"
42 #include "CDPL/Base/Exceptions.hpp"
43 
44 
45 namespace CDPL
46 {
47 
48  namespace Chem
49  {
50 
51  class Molecule;
52  class MolecularGraph;
53  class Reaction;
54  } // namespace Chem
55 
56  namespace Pharm
57  {
58 
59  class Pharmacophore;
60  class FeatureContainer;
61  } // namespace Pharm
62 
63  namespace Grid
64  {
65 
66  template <typename T, typename CVT>
67  class RegularGrid;
68  template <typename T, typename CVT>
69  class RegularGridSet;
70  } // namespace Grid
71 
72  namespace Base
73  {
74 
102  template <typename T>
104  {
105 
106  public:
109 
112 
113  private:
114  typedef std::vector<InputHandlerPointer> InputHandlerList;
115  typedef std::vector<OutputHandlerPointer> OutputHandlerList;
116 
117  public:
121  typedef typename InputHandlerList::iterator InputHandlerIterator;
122 
126  typedef typename OutputHandlerList::iterator OutputHandlerIterator;
127 
132  static void registerInputHandler(const InputHandlerPointer& handler);
133 
138  static void registerOutputHandler(const OutputHandlerPointer& handler);
139 
150  static bool unregisterInputHandler(const DataFormat& fmt);
151 
162  static bool unregisterOutputHandler(const DataFormat& fmt);
163 
169  static bool unregisterInputHandler(const InputHandlerPointer& handler);
170 
176  static bool unregisterOutputHandler(const OutputHandlerPointer& handler);
177 
183  static void unregisterInputHandler(std::size_t idx);
184 
190  static void unregisterOutputHandler(std::size_t idx);
191 
198 
205 
210  static std::size_t getNumInputHandlers();
211 
216  static std::size_t getNumOutputHandlers();
217 
224  static const InputHandlerPointer& getInputHandler(std::size_t idx);
225 
232  static const OutputHandlerPointer& getOutputHandler(std::size_t idx);
233 
239 
245 
251 
257 
265 
273  static InputHandlerPointer getInputHandlerByName(const std::string& name);
274 
283  static InputHandlerPointer getInputHandlerByFileExtension(const std::string& file_ext);
284 
292  static InputHandlerPointer getInputHandlerByFileName(const std::string& file_name);
293 
302  static InputHandlerPointer getInputHandlerByMimeType(const std::string& mime_type);
303 
311 
319  static OutputHandlerPointer getOutputHandlerByName(const std::string& name);
320 
329  static OutputHandlerPointer getOutputHandlerByFileExtension(const std::string& file_ext);
330 
338  static OutputHandlerPointer getOutputHandlerByFileName(const std::string& file_name);
339 
348  static OutputHandlerPointer getOutputHandlerByMimeType(const std::string& mime_type);
349 
350  private:
351  DataIOManager() {}
352  ~DataIOManager() {}
353 
354  static DataIOManager& getInstance();
355 
356  InputHandlerList inputHandlers;
357  OutputHandlerList outputHandlers;
358  };
359 
360  // \cond DOC_IMPL_DETAILS
361 
362 #ifdef _MSC_VER
363 # define _CDPL_BASE_API
364 #else
365 # define _CDPL_BASE_API CDPL_BASE_API
366 #endif // _MSC_VER
367 
368  extern template class _CDPL_BASE_API DataIOManager<Chem::Molecule>;
369 
370  extern template class _CDPL_BASE_API DataIOManager<Chem::MolecularGraph>;
371 
372  extern template class _CDPL_BASE_API DataIOManager<Chem::Reaction>;
373 
374  extern template class _CDPL_BASE_API DataIOManager<Pharm::Pharmacophore>;
375 
376  extern template class _CDPL_BASE_API DataIOManager<Pharm::FeatureContainer>;
377 
378  extern template class _CDPL_BASE_API DataIOManager<Grid::RegularGrid<double, double> >;
379 
380  extern template class _CDPL_BASE_API DataIOManager<Grid::RegularGridSet<double, double> >;
381 
382 #undef _CDPL_BASE_API
383 
384  // \endcond
385  } // namespace Base
386 } // namespace CDPL
387 
388 
389 // Implementation
390 
391 template <typename T>
393 {
394  static DataIOManager<T> instance;
395 
396  return instance;
397 }
398 
399 template <typename T>
401 {
402  getInstance().inputHandlers.push_back(handler);
403 }
404 
405 template <typename T>
407 {
408  getInstance().outputHandlers.push_back(handler);
409 }
410 
411 template <typename T>
413 {
414  InputHandlerList& handlers = getInstance().inputHandlers;
415 
416  typename InputHandlerList::iterator it = std::find_if(handlers.begin(), handlers.end(),
417  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
418  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1)));
419  if (it != handlers.end()) {
420  handlers.erase(it);
421  return true;
422  }
423 
424  return false;
425 }
426 
427 template <typename T>
429 {
430  OutputHandlerList& handlers = getInstance().outputHandlers;
431 
432  typename OutputHandlerList::iterator it = std::find_if(handlers.begin(), handlers.end(),
433  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
434  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1)));
435  if (it != handlers.end()) {
436  handlers.erase(it);
437  return true;
438  }
439 
440  return false;
441 }
442 
443 template <typename T>
445 {
446  InputHandlerList& handlers = getInstance().inputHandlers;
447 
448  typename InputHandlerList::iterator it = std::find(handlers.begin(), handlers.end(), handler);
449 
450  if (it != handlers.end()) {
451  handlers.erase(it);
452  return true;
453  }
454 
455  return false;
456 }
457 
458 template <typename T>
460 {
461  OutputHandlerList& handlers = getInstance().outputHandlers;
462 
463  typename OutputHandlerList::iterator it = std::find(handlers.begin(), handlers.end(), handler);
464 
465  if (it != handlers.end()) {
466  handlers.erase(it);
467  return true;
468  }
469 
470  return false;
471 }
472 
473 template <typename T>
475 {
476  InputHandlerList& handlers = getInstance().inputHandlers;
477 
478  if (idx >= handlers.size())
479  throw IndexError("DataIOManager: handler index out of bounds");
480 
481  handlers.erase(handlers.begin() + idx);
482 }
483 
484 template <typename T>
486 {
487  using namespace std::placeholders;
488 
489  OutputHandlerList& handlers = getInstance().outputHandlers;
490 
491  if (idx >= handlers.size())
492  throw IndexError("DataIOManager: handler index out of bounds");
493 
494  handlers.erase(handlers.begin() + idx);
495 }
496 
497 template <typename T>
500 {
501  InputHandlerList& handlers = getInstance().inputHandlers;
502 
503  if (it < handlers.begin() || it >= handlers.end())
504  throw RangeError("DataIOManager: input-handler iterator out of valid range");
505 
506  return handlers.erase(it);
507 }
508 
509 template <typename T>
512 {
513  OutputHandlerList& handlers = getInstance().outputHandlers;
514 
515  if (it < handlers.begin() || it >= handlers.end())
516  throw RangeError("DataIOManager: output-handler iterator out of valid range");
517 
518  return handlers.erase(it);
519 }
520 
521 template <typename T>
523 {
524  return getInstance().inputHandlers.size();
525 }
526 
527 template <typename T>
529 {
530  return getInstance().outputHandlers.size();
531 }
532 
533 template <typename T>
535 {
536  const InputHandlerList& handlers = getInstance().inputHandlers;
537 
538  if (idx >= handlers.size())
539  throw IndexError("DataIOManager: handler index out of bounds");
540 
541  return handlers[idx];
542 }
543 
544 template <typename T>
546 {
547  const OutputHandlerList& handlers = getInstance().outputHandlers;
548 
549  if (idx >= handlers.size())
550  throw IndexError("DataIOManager: handler index out of bounds");
551 
552  return handlers[idx];
553 }
554 
555 template <typename T>
557 {
558  return getInstance().inputHandlers.begin();
559 }
560 
561 template <typename T>
563 {
564  return getInstance().inputHandlers.end();
565 }
566 
567 template <typename T>
569 {
570  return getInstance().outputHandlers.begin();
571 }
572 
573 template <typename T>
575 {
576  return getInstance().outputHandlers.end();
577 }
578 
579 template <typename T>
581 {
582  const InputHandlerList& handlers = getInstance().inputHandlers;
583 
584  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
585  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
586  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1)));
587  return (it == handlers.end() ? InputHandlerPointer() : *it);
588 }
589 
590 template <typename T>
592 {
593  const InputHandlerList& handlers = getInstance().inputHandlers;
594 
595  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
596  std::bind(&DataFormat::matchesFileExtension,
597  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
598  std::ref(file_ext)));
599  return (it == handlers.end() ? InputHandlerPointer() : *it);
600 }
601 
602 template <typename T>
604 {
605  auto& handlers = getInstance().inputHandlers;
606  std::string file_ext;
607 
608  for (std::string::size_type i = file_name.find_first_of('.', 0); i != std::string::npos; i = file_name.find_first_of('.', i)) {
609  file_ext = file_name.substr(++i);
610 
611  auto it = std::find_if(handlers.begin(), handlers.end(), [&](const InputHandlerPointer& handler) {
612  return handler->getDataFormat().matchesFileExtension(file_ext);
613  });
614 
615  if (it != handlers.end())
616  return *it;
617  }
618 
619  return InputHandlerPointer();
620 }
621 
622 template <typename T>
624 {
625  const InputHandlerList& handlers = getInstance().inputHandlers;
626 
627  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
628  std::bind(&DataFormat::matchesName,
629  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
630  std::ref(name)));
631  return (it == handlers.end() ? InputHandlerPointer() : *it);
632 }
633 
634 template <typename T>
636 {
637  const InputHandlerList& handlers = getInstance().inputHandlers;
638 
639  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
640  std::bind(&DataFormat::matchesMimeType,
641  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
642  std::ref(mime_type)));
643  return (it == handlers.end() ? InputHandlerPointer() : *it);
644 }
645 
646 template <typename T>
648 {
649  const OutputHandlerList& handlers = getInstance().outputHandlers;
650 
651  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
652  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
653  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1)));
654  return (it == handlers.end() ? OutputHandlerPointer() : *it);
655 }
656 
657 template <typename T>
659 {
660  const OutputHandlerList& handlers = getInstance().outputHandlers;
661 
662  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
663  std::bind(&DataFormat::matchesName,
664  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
665  std::ref(name)));
666  return (it == handlers.end() ? OutputHandlerPointer() : *it);
667 }
668 
669 template <typename T>
671 {
672  const OutputHandlerList& handlers = getInstance().outputHandlers;
673 
674  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
675  std::bind(&DataFormat::matchesFileExtension,
676  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
677  std::ref(file_ext)));
678  return (it == handlers.end() ? OutputHandlerPointer() : *it);
679 }
680 
681 template <typename T>
683 {
684  auto& handlers = getInstance().outputHandlers;
685  std::string file_ext;
686 
687  for (std::string::size_type i = file_name.find_first_of('.', 0); i != std::string::npos; i = file_name.find_first_of('.', i)) {
688  file_ext = file_name.substr(++i);
689 
690  auto it = std::find_if(handlers.begin(), handlers.end(), [&](const OutputHandlerPointer& handler) {
691  return handler->getDataFormat().matchesFileExtension(file_ext);
692  });
693 
694  if (it != handlers.end())
695  return *it;
696  }
697 
698  return OutputHandlerPointer();
699 }
700 
701 template <typename T>
703 {
704  const OutputHandlerList& handlers = getInstance().outputHandlers;
705 
706  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
707  std::bind(&DataFormat::matchesMimeType,
708  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
709  std::ref(mime_type)));
710  return (it == handlers.end() ? OutputHandlerPointer() : *it);
711 }
712 
713 #endif // CDPL_BASE_DATAIOMANAGER_HPP
Definition of the preprocessor macro CDPL_BASE_API.
Definition of the class CDPL::Base::DataFormat.
Definition of exception classes.
Definition of the class CDPL::Base::DataInputHandler.
Definition of the class CDPL::Base::DataOutputHandler.
Provides meta-information about a particular data storage format.
Definition: Base/DataFormat.hpp:49
A singleton class that serves as a global registry for Base::DataInputHandler and Base::DataOutputHan...
Definition: DataIOManager.hpp:104
static OutputHandlerPointer getOutputHandlerByMimeType(const std::string &mime_type)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the data format...
Definition: DataIOManager.hpp:702
static InputHandlerPointer getInputHandlerByName(const std::string &name)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the data format ...
Definition: DataIOManager.hpp:623
static void registerInputHandler(const InputHandlerPointer &handler)
Registers the specified Base::DataInputHandler implementation instance.
Definition: DataIOManager.hpp:400
OutputHandlerList::iterator OutputHandlerIterator
An iterator used to iterate over the list of registered output handlers.
Definition: DataIOManager.hpp:126
static std::size_t getNumOutputHandlers()
Returns the number of registered Base::DataOutputHandler implementation instances.
Definition: DataIOManager.hpp:528
DataInputHandler< T > InputHandlerType
Definition: DataIOManager.hpp:107
static InputHandlerPointer getInputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the specified da...
Definition: DataIOManager.hpp:580
static OutputHandlerIterator getOutputHandlersEnd()
Returns an iterator pointing to the end of the list of registered Base::DataOutputHandler implementat...
Definition: DataIOManager.hpp:574
static OutputHandlerIterator getOutputHandlersBegin()
Returns an iterator pointing to the beginning of the list of registered Base::DataOutputHandler imple...
Definition: DataIOManager.hpp:568
static bool unregisterInputHandler(const DataFormat &fmt)
Unregisters the Base::DataInputHandler implementation instance for the specified data format.
Definition: DataIOManager.hpp:412
static void registerOutputHandler(const OutputHandlerPointer &handler)
Registers the specified Base::DataOutputHandler implementation instance.
Definition: DataIOManager.hpp:406
static const InputHandlerPointer & getInputHandler(std::size_t idx)
Returns a reference to the registered Base::DataInputHandler implementation instance with the specifi...
Definition: DataIOManager.hpp:534
static OutputHandlerPointer getOutputHandlerByFileName(const std::string &file_name)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the data format...
Definition: DataIOManager.hpp:682
OutputHandlerType::SharedPointer OutputHandlerPointer
Definition: DataIOManager.hpp:111
InputHandlerList::iterator InputHandlerIterator
An iterator used to iterate over the list of registered input handlers.
Definition: DataIOManager.hpp:121
static std::size_t getNumInputHandlers()
Returns the number of registered Base::DataInputHandler implementation instances.
Definition: DataIOManager.hpp:522
static OutputHandlerPointer getOutputHandlerByFileExtension(const std::string &file_ext)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the data format...
Definition: DataIOManager.hpp:670
static InputHandlerPointer getInputHandlerByMimeType(const std::string &mime_type)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the data format ...
Definition: DataIOManager.hpp:635
DataOutputHandler< T > OutputHandlerType
Definition: DataIOManager.hpp:108
static bool unregisterOutputHandler(const DataFormat &fmt)
Unregisters the Base::DataOutputHandler implementation instance for the specified data format.
Definition: DataIOManager.hpp:428
static InputHandlerPointer getInputHandlerByFileExtension(const std::string &file_ext)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the data format ...
Definition: DataIOManager.hpp:591
static const OutputHandlerPointer & getOutputHandler(std::size_t idx)
Returns a reference to the registered Base::DataOutputHandler implementation instance with the specif...
Definition: DataIOManager.hpp:545
static OutputHandlerPointer getOutputHandlerByName(const std::string &name)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the data format...
Definition: DataIOManager.hpp:658
static InputHandlerIterator getInputHandlersBegin()
Returns an iterator pointing to the beginning of the list of registered Base::DataInputHandler implem...
Definition: DataIOManager.hpp:556
static OutputHandlerPointer getOutputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the specified d...
Definition: DataIOManager.hpp:647
InputHandlerType::SharedPointer InputHandlerPointer
Definition: DataIOManager.hpp:110
static InputHandlerPointer getInputHandlerByFileName(const std::string &file_name)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the data format ...
Definition: DataIOManager.hpp:603
static InputHandlerIterator getInputHandlersEnd()
Returns an iterator pointing to the end of the list of registered Base::DataInputHandler implementati...
Definition: DataIOManager.hpp:562
A factory interface providing methods for the creation of Base::DataReader instances handling a parti...
Definition: DataInputHandler.hpp:54
std::shared_ptr< DataInputHandler > SharedPointer
Definition: DataInputHandler.hpp:59
A factory interface providing methods for the creation of Base::DataWriter instances handling a parti...
Definition: DataOutputHandler.hpp:53
std::shared_ptr< DataOutputHandler > SharedPointer
Definition: DataOutputHandler.hpp:58
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
Thrown to indicate that a value is out of range.
Definition: Base/Exceptions.hpp:114
The namespace of the Chemical Data Processing Library.