Chemical Data Processing Library C++ API - Version 1.3.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:
115 
118 
119  private:
120  typedef std::vector<InputHandlerPointer> InputHandlerList;
121  typedef std::vector<OutputHandlerPointer> OutputHandlerList;
122 
123  public:
127  typedef typename InputHandlerList::iterator InputHandlerIterator;
128 
132  typedef typename OutputHandlerList::iterator OutputHandlerIterator;
133 
138  static void registerInputHandler(const InputHandlerPointer& handler);
139 
144  static void registerOutputHandler(const OutputHandlerPointer& handler);
145 
156  static bool unregisterInputHandler(const DataFormat& fmt);
157 
168  static bool unregisterOutputHandler(const DataFormat& fmt);
169 
175  static bool unregisterInputHandler(const InputHandlerPointer& handler);
176 
182  static bool unregisterOutputHandler(const OutputHandlerPointer& handler);
183 
189  static void unregisterInputHandler(std::size_t idx);
190 
196  static void unregisterOutputHandler(std::size_t idx);
197 
204 
211 
216  static std::size_t getNumInputHandlers();
217 
222  static std::size_t getNumOutputHandlers();
223 
230  static const InputHandlerPointer& getInputHandler(std::size_t idx);
231 
238  static const OutputHandlerPointer& getOutputHandler(std::size_t idx);
239 
245 
251 
257 
263 
271 
279  static InputHandlerPointer getInputHandlerByName(const std::string& name);
280 
289  static InputHandlerPointer getInputHandlerByFileExtension(const std::string& file_ext);
290 
298  static InputHandlerPointer getInputHandlerByFileName(const std::string& file_name);
299 
308  static InputHandlerPointer getInputHandlerByMimeType(const std::string& mime_type);
309 
317 
325  static OutputHandlerPointer getOutputHandlerByName(const std::string& name);
326 
335  static OutputHandlerPointer getOutputHandlerByFileExtension(const std::string& file_ext);
336 
344  static OutputHandlerPointer getOutputHandlerByFileName(const std::string& file_name);
345 
354  static OutputHandlerPointer getOutputHandlerByMimeType(const std::string& mime_type);
355 
356  private:
357  DataIOManager() {}
358  ~DataIOManager() {}
359 
360  static DataIOManager& getInstance();
361 
362  InputHandlerList inputHandlers;
363  OutputHandlerList outputHandlers;
364  };
365 
366  // \cond DOC_IMPL_DETAILS
367 
368 #ifdef _MSC_VER
369 # define _CDPL_BASE_API
370 #else
371 # define _CDPL_BASE_API CDPL_BASE_API
372 #endif // _MSC_VER
373 
374  extern template class _CDPL_BASE_API DataIOManager<Chem::Molecule>;
375 
376  extern template class _CDPL_BASE_API DataIOManager<Chem::MolecularGraph>;
377 
378  extern template class _CDPL_BASE_API DataIOManager<Chem::Reaction>;
379 
380  extern template class _CDPL_BASE_API DataIOManager<Pharm::Pharmacophore>;
381 
382  extern template class _CDPL_BASE_API DataIOManager<Pharm::FeatureContainer>;
383 
384  extern template class _CDPL_BASE_API DataIOManager<Grid::RegularGrid<double, double> >;
385 
386  extern template class _CDPL_BASE_API DataIOManager<Grid::RegularGridSet<double, double> >;
387 
388  extern template class _CDPL_BASE_API DataIOManager<Vis::Object3D>;
389 
390 #undef _CDPL_BASE_API
391 
392  // \endcond
393  } // namespace Base
394 } // namespace CDPL
395 
396 
397 // Implementation
398 
399 template <typename T>
401 {
402  static DataIOManager<T> instance;
403 
404  return instance;
405 }
406 
407 template <typename T>
409 {
410  getInstance().inputHandlers.push_back(handler);
411 }
412 
413 template <typename T>
415 {
416  getInstance().outputHandlers.push_back(handler);
417 }
418 
419 template <typename T>
421 {
422  InputHandlerList& handlers = getInstance().inputHandlers;
423 
424  typename InputHandlerList::iterator it = std::find_if(handlers.begin(), handlers.end(),
425  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
426  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1)));
427  if (it != handlers.end()) {
428  handlers.erase(it);
429  return true;
430  }
431 
432  return false;
433 }
434 
435 template <typename T>
437 {
438  OutputHandlerList& handlers = getInstance().outputHandlers;
439 
440  typename OutputHandlerList::iterator it = std::find_if(handlers.begin(), handlers.end(),
441  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
442  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1)));
443  if (it != handlers.end()) {
444  handlers.erase(it);
445  return true;
446  }
447 
448  return false;
449 }
450 
451 template <typename T>
453 {
454  InputHandlerList& handlers = getInstance().inputHandlers;
455 
456  typename InputHandlerList::iterator it = std::find(handlers.begin(), handlers.end(), handler);
457 
458  if (it != handlers.end()) {
459  handlers.erase(it);
460  return true;
461  }
462 
463  return false;
464 }
465 
466 template <typename T>
468 {
469  OutputHandlerList& handlers = getInstance().outputHandlers;
470 
471  typename OutputHandlerList::iterator it = std::find(handlers.begin(), handlers.end(), handler);
472 
473  if (it != handlers.end()) {
474  handlers.erase(it);
475  return true;
476  }
477 
478  return false;
479 }
480 
481 template <typename T>
483 {
484  InputHandlerList& handlers = getInstance().inputHandlers;
485 
486  if (idx >= handlers.size())
487  throw IndexError("DataIOManager: handler index out of bounds");
488 
489  handlers.erase(handlers.begin() + idx);
490 }
491 
492 template <typename T>
494 {
495  using namespace std::placeholders;
496 
497  OutputHandlerList& handlers = getInstance().outputHandlers;
498 
499  if (idx >= handlers.size())
500  throw IndexError("DataIOManager: handler index out of bounds");
501 
502  handlers.erase(handlers.begin() + idx);
503 }
504 
505 template <typename T>
508 {
509  InputHandlerList& handlers = getInstance().inputHandlers;
510 
511  if (it < handlers.begin() || it >= handlers.end())
512  throw RangeError("DataIOManager: input-handler iterator out of valid range");
513 
514  return handlers.erase(it);
515 }
516 
517 template <typename T>
520 {
521  OutputHandlerList& handlers = getInstance().outputHandlers;
522 
523  if (it < handlers.begin() || it >= handlers.end())
524  throw RangeError("DataIOManager: output-handler iterator out of valid range");
525 
526  return handlers.erase(it);
527 }
528 
529 template <typename T>
531 {
532  return getInstance().inputHandlers.size();
533 }
534 
535 template <typename T>
537 {
538  return getInstance().outputHandlers.size();
539 }
540 
541 template <typename T>
543 {
544  const InputHandlerList& handlers = getInstance().inputHandlers;
545 
546  if (idx >= handlers.size())
547  throw IndexError("DataIOManager: handler index out of bounds");
548 
549  return handlers[idx];
550 }
551 
552 template <typename T>
554 {
555  const OutputHandlerList& handlers = getInstance().outputHandlers;
556 
557  if (idx >= handlers.size())
558  throw IndexError("DataIOManager: handler index out of bounds");
559 
560  return handlers[idx];
561 }
562 
563 template <typename T>
565 {
566  return getInstance().inputHandlers.begin();
567 }
568 
569 template <typename T>
571 {
572  return getInstance().inputHandlers.end();
573 }
574 
575 template <typename T>
577 {
578  return getInstance().outputHandlers.begin();
579 }
580 
581 template <typename T>
583 {
584  return getInstance().outputHandlers.end();
585 }
586 
587 template <typename T>
589 {
590  const InputHandlerList& handlers = getInstance().inputHandlers;
591 
592  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
593  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
594  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1)));
595  return (it == handlers.end() ? InputHandlerPointer() : *it);
596 }
597 
598 template <typename T>
600 {
601  const InputHandlerList& handlers = getInstance().inputHandlers;
602 
603  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
604  std::bind(&DataFormat::matchesFileExtension,
605  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
606  std::ref(file_ext)));
607  return (it == handlers.end() ? InputHandlerPointer() : *it);
608 }
609 
610 template <typename T>
612 {
613  auto& handlers = getInstance().inputHandlers;
614  std::string file_ext;
615 
616  for (std::string::size_type i = file_name.find_first_of('.', 0); i != std::string::npos; i = file_name.find_first_of('.', i)) {
617  file_ext = file_name.substr(++i);
618 
619  auto it = std::find_if(handlers.begin(), handlers.end(), [&](const InputHandlerPointer& handler) {
620  return handler->getDataFormat().matchesFileExtension(file_ext);
621  });
622 
623  if (it != handlers.end())
624  return *it;
625  }
626 
627  return InputHandlerPointer();
628 }
629 
630 template <typename T>
632 {
633  const InputHandlerList& handlers = getInstance().inputHandlers;
634 
635  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
636  std::bind(&DataFormat::matchesName,
637  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
638  std::ref(name)));
639  return (it == handlers.end() ? InputHandlerPointer() : *it);
640 }
641 
642 template <typename T>
644 {
645  const InputHandlerList& handlers = getInstance().inputHandlers;
646 
647  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
648  std::bind(&DataFormat::matchesMimeType,
649  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
650  std::ref(mime_type)));
651  return (it == handlers.end() ? InputHandlerPointer() : *it);
652 }
653 
654 template <typename T>
656 {
657  const OutputHandlerList& handlers = getInstance().outputHandlers;
658 
659  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
660  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
661  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1)));
662  return (it == handlers.end() ? OutputHandlerPointer() : *it);
663 }
664 
665 template <typename T>
667 {
668  const OutputHandlerList& handlers = getInstance().outputHandlers;
669 
670  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
671  std::bind(&DataFormat::matchesName,
672  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
673  std::ref(name)));
674  return (it == handlers.end() ? OutputHandlerPointer() : *it);
675 }
676 
677 template <typename T>
679 {
680  const OutputHandlerList& handlers = getInstance().outputHandlers;
681 
682  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
683  std::bind(&DataFormat::matchesFileExtension,
684  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
685  std::ref(file_ext)));
686  return (it == handlers.end() ? OutputHandlerPointer() : *it);
687 }
688 
689 template <typename T>
691 {
692  auto& handlers = getInstance().outputHandlers;
693  std::string file_ext;
694 
695  for (std::string::size_type i = file_name.find_first_of('.', 0); i != std::string::npos; i = file_name.find_first_of('.', i)) {
696  file_ext = file_name.substr(++i);
697 
698  auto it = std::find_if(handlers.begin(), handlers.end(), [&](const OutputHandlerPointer& handler) {
699  return handler->getDataFormat().matchesFileExtension(file_ext);
700  });
701 
702  if (it != handlers.end())
703  return *it;
704  }
705 
706  return OutputHandlerPointer();
707 }
708 
709 template <typename T>
711 {
712  const OutputHandlerList& handlers = getInstance().outputHandlers;
713 
714  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
715  std::bind(&DataFormat::matchesMimeType,
716  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
717  std::ref(mime_type)));
718  return (it == handlers.end() ? OutputHandlerPointer() : *it);
719 }
720 
721 #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: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:710
static InputHandlerPointer getInputHandlerByName(const std::string &name)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the data format ...
Definition: DataIOManager.hpp:631
static void registerInputHandler(const InputHandlerPointer &handler)
Registers the specified Base::DataInputHandler implementation instance.
Definition: DataIOManager.hpp:408
OutputHandlerList::iterator OutputHandlerIterator
An iterator used to iterate over the list of registered output handlers.
Definition: DataIOManager.hpp:132
static std::size_t getNumOutputHandlers()
Returns the number of registered Base::DataOutputHandler implementation instances.
Definition: DataIOManager.hpp:536
DataInputHandler< T > InputHandlerType
Definition: DataIOManager.hpp:113
static InputHandlerPointer getInputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the specified da...
Definition: DataIOManager.hpp:588
static OutputHandlerIterator getOutputHandlersEnd()
Returns an iterator pointing to the end of the list of registered Base::DataOutputHandler implementat...
Definition: DataIOManager.hpp:582
static OutputHandlerIterator getOutputHandlersBegin()
Returns an iterator pointing to the beginning of the list of registered Base::DataOutputHandler imple...
Definition: DataIOManager.hpp:576
static bool unregisterInputHandler(const DataFormat &fmt)
Unregisters the Base::DataInputHandler implementation instance for the specified data format.
Definition: DataIOManager.hpp:420
static void registerOutputHandler(const OutputHandlerPointer &handler)
Registers the specified Base::DataOutputHandler implementation instance.
Definition: DataIOManager.hpp:414
static const InputHandlerPointer & getInputHandler(std::size_t idx)
Returns a reference to the registered Base::DataInputHandler implementation instance with the specifi...
Definition: DataIOManager.hpp:542
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:690
OutputHandlerType::SharedPointer OutputHandlerPointer
Definition: DataIOManager.hpp:117
InputHandlerList::iterator InputHandlerIterator
An iterator used to iterate over the list of registered input handlers.
Definition: DataIOManager.hpp:127
static std::size_t getNumInputHandlers()
Returns the number of registered Base::DataInputHandler implementation instances.
Definition: DataIOManager.hpp:530
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:678
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:643
DataOutputHandler< T > OutputHandlerType
Definition: DataIOManager.hpp:114
static bool unregisterOutputHandler(const DataFormat &fmt)
Unregisters the Base::DataOutputHandler implementation instance for the specified data format.
Definition: DataIOManager.hpp:436
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:599
static const OutputHandlerPointer & getOutputHandler(std::size_t idx)
Returns a reference to the registered Base::DataOutputHandler implementation instance with the specif...
Definition: DataIOManager.hpp:553
static OutputHandlerPointer getOutputHandlerByName(const std::string &name)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the data format...
Definition: DataIOManager.hpp:666
static InputHandlerIterator getInputHandlersBegin()
Returns an iterator pointing to the beginning of the list of registered Base::DataInputHandler implem...
Definition: DataIOManager.hpp:564
static OutputHandlerPointer getOutputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the specified d...
Definition: DataIOManager.hpp:655
InputHandlerType::SharedPointer InputHandlerPointer
Definition: DataIOManager.hpp:116
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:611
static InputHandlerIterator getInputHandlersEnd()
Returns an iterator pointing to the end of the list of registered Base::DataInputHandler implementati...
Definition: DataIOManager.hpp:570
A factory interface providing methods for the creation of Base::DataReader instances handling a parti...
Definition: DataInputHandler.hpp:55
std::shared_ptr< DataInputHandler > SharedPointer
Definition: DataInputHandler.hpp:60
A factory interface providing methods for the creation of Base::DataWriter instances handling a parti...
Definition: DataOutputHandler.hpp:54
std::shared_ptr< DataOutputHandler > SharedPointer
Definition: DataOutputHandler.hpp:59
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.