Chemical Data Processing Library C++ API - Version 1.1.1
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 
293  static InputHandlerPointer getInputHandlerByMimeType(const std::string& mime_type);
294 
302 
310  static OutputHandlerPointer getOutputHandlerByName(const std::string& name);
311 
320  static OutputHandlerPointer getOutputHandlerByFileExtension(const std::string& file_ext);
321 
330  static OutputHandlerPointer getOutputHandlerByMimeType(const std::string& mime_type);
331 
332  private:
333  DataIOManager() {}
334  ~DataIOManager() {}
335 
336  static DataIOManager& getInstance();
337 
338  InputHandlerList inputHandlers;
339  OutputHandlerList outputHandlers;
340  };
341 
342  // \cond DOC_IMPL_DETAILS
343 
344 #ifdef _MSC_VER
345 # define _CDPL_BASE_API
346 #else
347 # define _CDPL_BASE_API CDPL_BASE_API
348 #endif // _MSC_VER
349 
350  extern template class _CDPL_BASE_API DataIOManager<Chem::Molecule>;
351 
352  extern template class _CDPL_BASE_API DataIOManager<Chem::MolecularGraph>;
353 
354  extern template class _CDPL_BASE_API DataIOManager<Chem::Reaction>;
355 
356  extern template class _CDPL_BASE_API DataIOManager<Pharm::Pharmacophore>;
357 
358  extern template class _CDPL_BASE_API DataIOManager<Pharm::FeatureContainer>;
359 
360  extern template class _CDPL_BASE_API DataIOManager<Grid::RegularGrid<double, double> >;
361 
362  extern template class _CDPL_BASE_API DataIOManager<Grid::RegularGridSet<double, double> >;
363 
364 #undef _CDPL_BASE_API
365 
366  // \endcond
367  } // namespace Base
368 } // namespace CDPL
369 
370 
371 // Implementation
372 
373 template <typename T>
375 {
376  static DataIOManager<T> instance;
377 
378  return instance;
379 }
380 
381 template <typename T>
383 {
384  getInstance().inputHandlers.push_back(handler);
385 }
386 
387 template <typename T>
389 {
390  getInstance().outputHandlers.push_back(handler);
391 }
392 
393 template <typename T>
395 {
396  InputHandlerList& handlers = getInstance().inputHandlers;
397 
398  typename InputHandlerList::iterator it = std::find_if(handlers.begin(), handlers.end(),
399  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
400  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1)));
401  if (it != handlers.end()) {
402  handlers.erase(it);
403  return true;
404  }
405 
406  return false;
407 }
408 
409 template <typename T>
411 {
412  OutputHandlerList& handlers = getInstance().outputHandlers;
413 
414  typename OutputHandlerList::iterator it = std::find_if(handlers.begin(), handlers.end(),
415  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
416  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1)));
417  if (it != handlers.end()) {
418  handlers.erase(it);
419  return true;
420  }
421 
422  return false;
423 }
424 
425 template <typename T>
427 {
428  InputHandlerList& handlers = getInstance().inputHandlers;
429 
430  typename InputHandlerList::iterator it = std::find(handlers.begin(), handlers.end(), handler);
431 
432  if (it != handlers.end()) {
433  handlers.erase(it);
434  return true;
435  }
436 
437  return false;
438 }
439 
440 template <typename T>
442 {
443  OutputHandlerList& handlers = getInstance().outputHandlers;
444 
445  typename OutputHandlerList::iterator it = std::find(handlers.begin(), handlers.end(), handler);
446 
447  if (it != handlers.end()) {
448  handlers.erase(it);
449  return true;
450  }
451 
452  return false;
453 }
454 
455 template <typename T>
457 {
458  InputHandlerList& handlers = getInstance().inputHandlers;
459 
460  if (idx >= handlers.size())
461  throw IndexError("DataIOManager: handler index out of bounds");
462 
463  handlers.erase(handlers.begin() + idx);
464 }
465 
466 template <typename T>
468 {
469  using namespace std::placeholders;
470 
471  OutputHandlerList& handlers = getInstance().outputHandlers;
472 
473  if (idx >= handlers.size())
474  throw IndexError("DataIOManager: handler index out of bounds");
475 
476  handlers.erase(handlers.begin() + idx);
477 }
478 
479 template <typename T>
482 {
483  InputHandlerList& handlers = getInstance().inputHandlers;
484 
485  if (it < handlers.begin() || it >= handlers.end())
486  throw RangeError("DataIOManager: input-handler iterator out of valid range");
487 
488  return handlers.erase(it);
489 }
490 
491 template <typename T>
494 {
495  OutputHandlerList& handlers = getInstance().outputHandlers;
496 
497  if (it < handlers.begin() || it >= handlers.end())
498  throw RangeError("DataIOManager: output-handler iterator out of valid range");
499 
500  return handlers.erase(it);
501 }
502 
503 template <typename T>
505 {
506  return getInstance().inputHandlers.size();
507 }
508 
509 template <typename T>
511 {
512  return getInstance().outputHandlers.size();
513 }
514 
515 template <typename T>
517 {
518  const InputHandlerList& handlers = getInstance().inputHandlers;
519 
520  if (idx >= handlers.size())
521  throw IndexError("DataIOManager: handler index out of bounds");
522 
523  return handlers[idx];
524 }
525 
526 template <typename T>
528 {
529  const OutputHandlerList& handlers = getInstance().outputHandlers;
530 
531  if (idx >= handlers.size())
532  throw IndexError("DataIOManager: handler index out of bounds");
533 
534  return handlers[idx];
535 }
536 
537 template <typename T>
539 {
540  return getInstance().inputHandlers.begin();
541 }
542 
543 template <typename T>
545 {
546  return getInstance().inputHandlers.end();
547 }
548 
549 template <typename T>
551 {
552  return getInstance().outputHandlers.begin();
553 }
554 
555 template <typename T>
557 {
558  return getInstance().outputHandlers.end();
559 }
560 
561 template <typename T>
563 {
564  const InputHandlerList& handlers = getInstance().inputHandlers;
565 
566  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
567  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
568  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1)));
569  return (it == handlers.end() ? InputHandlerPointer() : *it);
570 }
571 
572 template <typename T>
574 {
575  const InputHandlerList& handlers = getInstance().inputHandlers;
576 
577  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
578  std::bind(&DataFormat::matchesFileExtension,
579  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
580  std::ref(file_ext)));
581  return (it == handlers.end() ? InputHandlerPointer() : *it);
582 }
583 
584 template <typename T>
586 {
587  const InputHandlerList& handlers = getInstance().inputHandlers;
588 
589  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
590  std::bind(&DataFormat::matchesName,
591  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
592  std::ref(name)));
593  return (it == handlers.end() ? InputHandlerPointer() : *it);
594 }
595 
596 template <typename T>
598 {
599  const InputHandlerList& handlers = getInstance().inputHandlers;
600 
601  typename InputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
602  std::bind(&DataFormat::matchesMimeType,
603  std::bind(&DataInputHandler<T>::getDataFormat, std::placeholders::_1),
604  std::ref(mime_type)));
605  return (it == handlers.end() ? InputHandlerPointer() : *it);
606 }
607 
608 template <typename T>
610 {
611  const OutputHandlerList& handlers = getInstance().outputHandlers;
612 
613  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
614  std::bind(std::equal_to<DataFormat>(), std::ref(fmt),
615  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1)));
616  return (it == handlers.end() ? OutputHandlerPointer() : *it);
617 }
618 
619 template <typename T>
621 {
622  const OutputHandlerList& handlers = getInstance().outputHandlers;
623 
624  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
625  std::bind(&DataFormat::matchesName,
626  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
627  std::ref(name)));
628  return (it == handlers.end() ? OutputHandlerPointer() : *it);
629 }
630 
631 template <typename T>
633 {
634  const OutputHandlerList& handlers = getInstance().outputHandlers;
635 
636  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
637  std::bind(&DataFormat::matchesFileExtension,
638  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
639  std::ref(file_ext)));
640  return (it == handlers.end() ? OutputHandlerPointer() : *it);
641 }
642 
643 template <typename T>
645 {
646  const OutputHandlerList& handlers = getInstance().outputHandlers;
647 
648  typename OutputHandlerList::const_iterator it = std::find_if(handlers.begin(), handlers.end(),
649  std::bind(&DataFormat::matchesMimeType,
650  std::bind(&DataOutputHandler<T>::getDataFormat, std::placeholders::_1),
651  std::ref(mime_type)));
652  return (it == handlers.end() ? OutputHandlerPointer() : *it);
653 }
654 
655 #endif // CDPL_BASE_DATAIOMANAGER_HPP
CDPL::Base::DataIOManager::InputHandlerPointer
InputHandlerType::SharedPointer InputHandlerPointer
Definition: DataIOManager.hpp:110
DataOutputHandler.hpp
Definition of the class CDPL::Base::DataOutputHandler.
CDPL::Base::DataFormat
Provides meta-information about a particular data storage format.
Definition: Base/DataFormat.hpp:49
CDPL::Base::DataIOManager::registerOutputHandler
static void registerOutputHandler(const OutputHandlerPointer &handler)
Registers the specified Base::DataOutputHandler implementation instance.
Definition: DataIOManager.hpp:388
CDPL::Base::DataOutputHandler::SharedPointer
std::shared_ptr< DataOutputHandler > SharedPointer
Definition: DataOutputHandler.hpp:58
CDPL::Base::DataIOManager::getInputHandlerByFileExtension
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:573
CDPL::Base::DataIOManager::getInputHandlerByFormat
static InputHandlerPointer getInputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the specified da...
Definition: DataIOManager.hpp:562
CDPL::Base::DataOutputHandler
A factory interface providing methods for the creation of Base::DataWriter instances handling a parti...
Definition: DataOutputHandler.hpp:53
CDPL::Base::DataIOManager::unregisterOutputHandler
static bool unregisterOutputHandler(const DataFormat &fmt)
Unregisters the Base::DataOutputHandler implementation instance for the specified data format.
Definition: DataIOManager.hpp:410
CDPL::Base::RangeError
Thrown to indicate that a value is out of range.
Definition: Base/Exceptions.hpp:114
CDPL::Base::DataIOManager::OutputHandlerPointer
OutputHandlerType::SharedPointer OutputHandlerPointer
Definition: DataIOManager.hpp:111
CDPL::Base::DataIOManager::getNumOutputHandlers
static std::size_t getNumOutputHandlers()
Returns the number of registered Base::DataOutputHandler implementation instances.
Definition: DataIOManager.hpp:510
CDPL::Base::DataInputHandler::SharedPointer
std::shared_ptr< DataInputHandler > SharedPointer
Definition: DataInputHandler.hpp:59
CDPL::Base::DataIOManager::getInputHandlersEnd
static InputHandlerIterator getInputHandlersEnd()
Returns an iterator pointing to the end of the list of registered Base::DataInputHandler implementati...
Definition: DataIOManager.hpp:544
CDPL::Base::DataIOManager::getOutputHandlerByName
static OutputHandlerPointer getOutputHandlerByName(const std::string &name)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the data format...
Definition: DataIOManager.hpp:620
APIPrefix.hpp
Definition of the preprocessor macro CDPL_BASE_API.
CDPL::Base::DataIOManager::getOutputHandlerByMimeType
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:644
CDPL::Base::IndexError
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
CDPL::Base::DataIOManager::unregisterInputHandler
static bool unregisterInputHandler(const DataFormat &fmt)
Unregisters the Base::DataInputHandler implementation instance for the specified data format.
Definition: DataIOManager.hpp:394
CDPL::Base::DataIOManager::OutputHandlerType
DataOutputHandler< T > OutputHandlerType
Definition: DataIOManager.hpp:108
CDPL::Base::DataIOManager
A singleton class that serves as a global registry for Base::DataInputHandler and Base::DataOutputHan...
Definition: DataIOManager.hpp:104
CDPL::Base::DataIOManager::getOutputHandlerByFileExtension
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:632
CDPL::Base::DataIOManager::OutputHandlerIterator
OutputHandlerList::iterator OutputHandlerIterator
An iterator used to iterate over the list of registered output handlers.
Definition: DataIOManager.hpp:126
DataInputHandler.hpp
Definition of the class CDPL::Base::DataInputHandler.
Exceptions.hpp
Definition of exception classes.
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Base::DataInputHandler
A factory interface providing methods for the creation of Base::DataReader instances handling a parti...
Definition: DataInputHandler.hpp:54
DataFormat.hpp
Definition of the class CDPL::Base::DataFormat.
CDPL::Base::DataIOManager::getOutputHandlersEnd
static OutputHandlerIterator getOutputHandlersEnd()
Returns an iterator pointing to the end of the list of registered Base::DataOutputHandler implementat...
Definition: DataIOManager.hpp:556
CDPL::Base::DataIOManager::getInputHandlersBegin
static InputHandlerIterator getInputHandlersBegin()
Returns an iterator pointing to the beginning of the list of registered Base::DataInputHandler implem...
Definition: DataIOManager.hpp:538
CDPL::Base::DataIOManager::getInputHandlerByMimeType
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:597
CDPL::Base::DataIOManager::registerInputHandler
static void registerInputHandler(const InputHandlerPointer &handler)
Registers the specified Base::DataInputHandler implementation instance.
Definition: DataIOManager.hpp:382
CDPL::Base::DataIOManager::getInputHandlerByName
static InputHandlerPointer getInputHandlerByName(const std::string &name)
Returns a pointer to a Base::DataInputHandler implementation instance registered for the data format ...
Definition: DataIOManager.hpp:585
CDPL::Base::DataIOManager::InputHandlerIterator
InputHandlerList::iterator InputHandlerIterator
An iterator used to iterate over the list of registered input handlers.
Definition: DataIOManager.hpp:121
CDPL::Base::DataIOManager::InputHandlerType
DataInputHandler< T > InputHandlerType
Definition: DataIOManager.hpp:107
CDPL::Base::DataIOManager::getOutputHandlerByFormat
static OutputHandlerPointer getOutputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the specified d...
Definition: DataIOManager.hpp:609
CDPL::Base::DataIOManager::getOutputHandler
static const OutputHandlerPointer & getOutputHandler(std::size_t idx)
Returns a reference to the registered Base::DataOutputHandler implementation instance with the specif...
Definition: DataIOManager.hpp:527
CDPL::Base::DataIOManager::getNumInputHandlers
static std::size_t getNumInputHandlers()
Returns the number of registered Base::DataInputHandler implementation instances.
Definition: DataIOManager.hpp:504
CDPL::Base::DataIOManager::getInputHandler
static const InputHandlerPointer & getInputHandler(std::size_t idx)
Returns a reference to the registered Base::DataInputHandler implementation instance with the specifi...
Definition: DataIOManager.hpp:516
CDPL::Base::DataIOManager::getOutputHandlersBegin
static OutputHandlerIterator getOutputHandlersBegin()
Returns an iterator pointing to the beginning of the list of registered Base::DataOutputHandler imple...
Definition: DataIOManager.hpp:550