Chemical Data Processing Library C++ API - Version 1.4.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 Vis
73  {
74 
75  class Object3D;
76  } // namespace Vis
77 
78  namespace Base
79  {
80 
108  template <typename T>
110  {
111 
112  public:
117 
122 
127 
132 
133  private:
134  typedef std::vector<InputHandlerPointer> InputHandlerList;
135  typedef std::vector<OutputHandlerPointer> OutputHandlerList;
136 
137  public:
141  typedef typename InputHandlerList::iterator InputHandlerIterator;
142 
146  typedef typename OutputHandlerList::iterator OutputHandlerIterator;
147 
152  static void registerInputHandler(const InputHandlerPointer& handler);
153 
158  static void registerOutputHandler(const OutputHandlerPointer& handler);
159 
170  static bool unregisterInputHandler(const DataFormat& fmt);
171 
182  static bool unregisterOutputHandler(const DataFormat& fmt);
183 
189  static bool unregisterInputHandler(const InputHandlerPointer& handler);
190 
196  static bool unregisterOutputHandler(const OutputHandlerPointer& handler);
197 
203  static void unregisterInputHandler(std::size_t idx);
204 
210  static void unregisterOutputHandler(std::size_t idx);
211 
218 
225 
230  static std::size_t getNumInputHandlers();
231 
236  static std::size_t getNumOutputHandlers();
237 
244  static const InputHandlerPointer& getInputHandler(std::size_t idx);
245 
252  static const OutputHandlerPointer& getOutputHandler(std::size_t idx);
253 
259 
265 
271 
277 
285 
293  static InputHandlerPointer getInputHandlerByName(const std::string& name);
294 
303  static InputHandlerPointer getInputHandlerByFileExtension(const std::string& file_ext);
304 
312  static InputHandlerPointer getInputHandlerByFileName(const std::string& file_name);
313 
322  static InputHandlerPointer getInputHandlerByMimeType(const std::string& mime_type);
323 
331 
339  static OutputHandlerPointer getOutputHandlerByName(const std::string& name);
340 
349  static OutputHandlerPointer getOutputHandlerByFileExtension(const std::string& file_ext);
350 
358  static OutputHandlerPointer getOutputHandlerByFileName(const std::string& file_name);
359 
368  static OutputHandlerPointer getOutputHandlerByMimeType(const std::string& mime_type);
369 
370  private:
371  DataIOManager() {}
372  ~DataIOManager() {}
373 
374  static DataIOManager& getInstance();
375 
376  InputHandlerList inputHandlers;
377  OutputHandlerList outputHandlers;
378  };
379 
380  // \cond DOC_IMPL_DETAILS
381 
382 #ifdef _MSC_VER
383 # define _CDPL_BASE_API
384 #else
385 # define _CDPL_BASE_API CDPL_BASE_API
386 #endif // _MSC_VER
387 
388  extern template class _CDPL_BASE_API DataIOManager<Chem::Molecule>;
389 
390  extern template class _CDPL_BASE_API DataIOManager<Chem::MolecularGraph>;
391 
392  extern template class _CDPL_BASE_API DataIOManager<Chem::Reaction>;
393 
394  extern template class _CDPL_BASE_API DataIOManager<Pharm::Pharmacophore>;
395 
396  extern template class _CDPL_BASE_API DataIOManager<Pharm::FeatureContainer>;
397 
398  extern template class _CDPL_BASE_API DataIOManager<Grid::RegularGrid<double, double> >;
399 
400  extern template class _CDPL_BASE_API DataIOManager<Grid::RegularGridSet<double, double> >;
401 
402  extern template class _CDPL_BASE_API DataIOManager<Vis::Object3D>;
403 
404 #undef _CDPL_BASE_API
405 
406  // \endcond
407  } // namespace Base
408 } // namespace CDPL
409 
410 
411 // Implementation
412 
413 template <typename T>
415 {
416  static DataIOManager<T> instance;
417 
418  return instance;
419 }
420 
421 template <typename T>
423 {
424  getInstance().inputHandlers.push_back(handler);
425 }
426 
427 template <typename T>
429 {
430  getInstance().outputHandlers.push_back(handler);
431 }
432 
433 template <typename T>
435 {
436  InputHandlerList& handlers = getInstance().inputHandlers;
437 
438  typename InputHandlerList::iterator it = std::find_if(handlers.begin(), handlers.end(),
439  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
440  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1)));
441  if (it != handlers.end()) {
442  handlers.erase(it);
443  return true;
444  }
445 
446  return false;
447 }
448 
449 template <typename T>
451 {
452  OutputHandlerList& handlers = getInstance().outputHandlers;
453 
454  typename OutputHandlerList::iterator it = std::find_if(handlers.begin(), handlers.end(),
455  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
456  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1)));
457  if (it != handlers.end()) {
458  handlers.erase(it);
459  return true;
460  }
461 
462  return false;
463 }
464 
465 template <typename T>
467 {
468  InputHandlerList& handlers = getInstance().inputHandlers;
469 
470  typename InputHandlerList::iterator it = std::find(handlers.begin(), handlers.end(), handler);
471 
472  if (it != handlers.end()) {
473  handlers.erase(it);
474  return true;
475  }
476 
477  return false;
478 }
479 
480 template <typename T>
482 {
483  OutputHandlerList& handlers = getInstance().outputHandlers;
484 
485  typename OutputHandlerList::iterator it = std::find(handlers.begin(), handlers.end(), handler);
486 
487  if (it != handlers.end()) {
488  handlers.erase(it);
489  return true;
490  }
491 
492  return false;
493 }
494 
495 template <typename T>
497 {
498  InputHandlerList& handlers = getInstance().inputHandlers;
499 
500  if (idx >= handlers.size())
501  throw IndexError("DataIOManager: handler index out of bounds");
502 
503  handlers.erase(handlers.begin() + idx);
504 }
505 
506 template <typename T>
508 {
509  using namespace std::placeholders;
510 
511  OutputHandlerList& handlers = getInstance().outputHandlers;
512 
513  if (idx >= handlers.size())
514  throw IndexError("DataIOManager: handler index out of bounds");
515 
516  handlers.erase(handlers.begin() + idx);
517 }
518 
519 template <typename T>
522 {
523  InputHandlerList& handlers = getInstance().inputHandlers;
524 
525  if (it < handlers.begin() || it >= handlers.end())
526  throw RangeError("DataIOManager: input-handler iterator out of valid range");
527 
528  return handlers.erase(it);
529 }
530 
531 template <typename T>
534 {
535  OutputHandlerList& handlers = getInstance().outputHandlers;
536 
537  if (it < handlers.begin() || it >= handlers.end())
538  throw RangeError("DataIOManager: output-handler iterator out of valid range");
539 
540  return handlers.erase(it);
541 }
542 
543 template <typename T>
545 {
546  return getInstance().inputHandlers.size();
547 }
548 
549 template <typename T>
551 {
552  return getInstance().outputHandlers.size();
553 }
554 
555 template <typename T>
557 {
558  const InputHandlerList& handlers = getInstance().inputHandlers;
559 
560  if (idx >= handlers.size())
561  throw IndexError("DataIOManager: handler index out of bounds");
562 
563  return handlers[idx];
564 }
565 
566 template <typename T>
568 {
569  const OutputHandlerList& handlers = getInstance().outputHandlers;
570 
571  if (idx >= handlers.size())
572  throw IndexError("DataIOManager: handler index out of bounds");
573 
574  return handlers[idx];
575 }
576 
577 template <typename T>
579 {
580  return getInstance().inputHandlers.begin();
581 }
582 
583 template <typename T>
585 {
586  return getInstance().inputHandlers.end();
587 }
588 
589 template <typename T>
591 {
592  return getInstance().outputHandlers.begin();
593 }
594 
595 template <typename T>
597 {
598  return getInstance().outputHandlers.end();
599 }
600 
601 template <typename T>
603 {
604  const InputHandlerList& handlers = getInstance().inputHandlers;
605 
606  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
607  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
608  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1)));
609  return (it == handlers.end() ? InputHandlerPointer() : *it);
610 }
611 
612 template <typename T>
614 {
615  const InputHandlerList& handlers = getInstance().inputHandlers;
616 
617  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
618  std::bind(&DataFormat::matchesFileExtension,
619  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
620  std::ref(file_ext)));
621  return (it == handlers.end() ? InputHandlerPointer() : *it);
622 }
623 
624 template <typename T>
626 {
627  auto& handlers = getInstance().inputHandlers;
628  std::string file_ext;
629 
630  for (std::string::size_type i = file_name.find_first_of('.', 0); i != std::string::npos; i = file_name.find_first_of('.', i)) {
631  file_ext = file_name.substr(++i);
632 
633  auto it = std::find_if(handlers.begin(), handlers.end(), [&](const InputHandlerPointer& handler) {
634  return handler->getDataFormat().matchesFileExtension(file_ext);
635  });
636 
637  if (it != handlers.end())
638  return *it;
639  }
640 
641  return InputHandlerPointer();
642 }
643 
644 template <typename T>
646 {
647  const InputHandlerList& handlers = getInstance().inputHandlers;
648 
649  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
650  std::bind(&DataFormat::matchesName,
651  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
652  std::ref(name)));
653  return (it == handlers.end() ? InputHandlerPointer() : *it);
654 }
655 
656 template <typename T>
658 {
659  const InputHandlerList& handlers = getInstance().inputHandlers;
660 
661  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
662  std::bind(&DataFormat::matchesMimeType,
663  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
664  std::ref(mime_type)));
665  return (it == handlers.end() ? InputHandlerPointer() : *it);
666 }
667 
668 template <typename T>
670 {
671  const OutputHandlerList& handlers = getInstance().outputHandlers;
672 
673  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
674  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
675  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1)));
676  return (it == handlers.end() ? OutputHandlerPointer() : *it);
677 }
678 
679 template <typename T>
681 {
682  const OutputHandlerList& handlers = getInstance().outputHandlers;
683 
684  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
685  std::bind(&DataFormat::matchesName,
686  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
687  std::ref(name)));
688  return (it == handlers.end() ? OutputHandlerPointer() : *it);
689 }
690 
691 template <typename T>
693 {
694  const OutputHandlerList& handlers = getInstance().outputHandlers;
695 
696  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
697  std::bind(&DataFormat::matchesFileExtension,
698  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
699  std::ref(file_ext)));
700  return (it == handlers.end() ? OutputHandlerPointer() : *it);
701 }
702 
703 template <typename T>
705 {
706  auto& handlers = getInstance().outputHandlers;
707  std::string file_ext;
708 
709  for (std::string::size_type i = file_name.find_first_of('.', 0); i != std::string::npos; i = file_name.find_first_of('.', i)) {
710  file_ext = file_name.substr(++i);
711 
712  auto it = std::find_if(handlers.begin(), handlers.end(), [&](const OutputHandlerPointer& handler) {
713  return handler->getDataFormat().matchesFileExtension(file_ext);
714  });
715 
716  if (it != handlers.end())
717  return *it;
718  }
719 
720  return OutputHandlerPointer();
721 }
722 
723 template <typename T>
725 {
726  const OutputHandlerList& handlers = getInstance().outputHandlers;
727 
728  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
729  std::bind(&DataFormat::matchesMimeType,
730  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
731  std::ref(mime_type)));
732  return (it == handlers.end() ? OutputHandlerPointer() : *it);
733 }
734 
735 #endif // CDPL_BASE_DATAIOMANAGER_HPP
Definition of the preprocessor macro CDPL_BASE_API.
Definition of class CDPL::Base::DataFormat.
Definition of exception classes.
Definition of class CDPL::Base::DataInputHandler.
Definition of class CDPL::Base::DataOutputHandler.
Provides meta-information about a particular data storage format.
Definition: Base/DataFormat.hpp:49
Singleton class that serves as a global registry for Base::DataInputHandler and Base::DataOutputHandl...
Definition: DataIOManager.hpp:110
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:724
static InputHandlerPointer getInputHandlerByName(const std::string &name)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the data format ...
Definition: DataIOManager.hpp:645
static void registerInputHandler(const InputHandlerPointer &handler)
Registers the specified Base::DataInputHandler implementation instance.
Definition: DataIOManager.hpp:422
OutputHandlerList::iterator OutputHandlerIterator
An iterator used to iterate over the list of registered output handlers.
Definition: DataIOManager.hpp:146
static std::size_t getNumOutputHandlers()
Returns the number of registered Base::DataOutputHandler implementation instances.
Definition: DataIOManager.hpp:550
DataInputHandler< T > InputHandlerType
The type of the registered input handler instances.
Definition: DataIOManager.hpp:116
static InputHandlerPointer getInputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the specified da...
Definition: DataIOManager.hpp:602
static OutputHandlerIterator getOutputHandlersEnd()
Returns an iterator pointing to the end of the list of registered Base::DataOutputHandler implementat...
Definition: DataIOManager.hpp:596
static OutputHandlerIterator getOutputHandlersBegin()
Returns an iterator pointing to the beginning of the list of registered Base::DataOutputHandler imple...
Definition: DataIOManager.hpp:590
static bool unregisterInputHandler(const DataFormat &fmt)
Unregisters the Base::DataInputHandler implementation instance for the specified data format.
Definition: DataIOManager.hpp:434
static void registerOutputHandler(const OutputHandlerPointer &handler)
Registers the specified Base::DataOutputHandler implementation instance.
Definition: DataIOManager.hpp:428
static const InputHandlerPointer & getInputHandler(std::size_t idx)
Returns a reference to the registered Base::DataInputHandler implementation instance with the specifi...
Definition: DataIOManager.hpp:556
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:704
OutputHandlerType::SharedPointer OutputHandlerPointer
A reference-counted smart pointer to a registered output handler instance.
Definition: DataIOManager.hpp:131
InputHandlerList::iterator InputHandlerIterator
An iterator used to iterate over the list of registered input handlers.
Definition: DataIOManager.hpp:141
static std::size_t getNumInputHandlers()
Returns the number of registered Base::DataInputHandler implementation instances.
Definition: DataIOManager.hpp:544
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:692
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:657
DataOutputHandler< T > OutputHandlerType
The type of the registered output handler instances.
Definition: DataIOManager.hpp:121
static bool unregisterOutputHandler(const DataFormat &fmt)
Unregisters the Base::DataOutputHandler implementation instance for the specified data format.
Definition: DataIOManager.hpp:450
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:613
static const OutputHandlerPointer & getOutputHandler(std::size_t idx)
Returns a reference to the registered Base::DataOutputHandler implementation instance with the specif...
Definition: DataIOManager.hpp:567
static OutputHandlerPointer getOutputHandlerByName(const std::string &name)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the data format...
Definition: DataIOManager.hpp:680
static InputHandlerIterator getInputHandlersBegin()
Returns an iterator pointing to the beginning of the list of registered Base::DataInputHandler implem...
Definition: DataIOManager.hpp:578
static OutputHandlerPointer getOutputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the specified d...
Definition: DataIOManager.hpp:669
InputHandlerType::SharedPointer InputHandlerPointer
A reference-counted smart pointer to a registered input handler instance.
Definition: DataIOManager.hpp:126
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:625
static InputHandlerIterator getInputHandlersEnd()
Returns an iterator pointing to the end of the list of registered Base::DataInputHandler implementati...
Definition: DataIOManager.hpp:584
Factory interface providing methods for the creation of Base::DataReader instances handling a particu...
Definition: DataInputHandler.hpp:55
std::shared_ptr< DataInputHandler > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated DataInputHandler instances.
Definition: DataInputHandler.hpp:66
Factory interface providing methods for the creation of Base::DataWriter instances handling a particu...
Definition: DataOutputHandler.hpp:54
std::shared_ptr< DataOutputHandler > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated DataOutputHandler instances.
Definition: DataOutputHandler.hpp:65
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.