MOAB: Mesh Oriented datABase  (version 5.4.1)
ReaderWriterSet.hpp
Go to the documentation of this file.
00001 /**
00002  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
00003  * storing and accessing finite element mesh data.
00004  *
00005  * Copyright 2004 Sandia Corporation.  Under the terms of Contract
00006  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00007  * retains certain rights in this software.
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  */
00015 
00016 #ifndef MOAB_READER_WRITER_SET_HPP
00017 #define MOAB_READER_WRITER_SET_HPP
00018 
00019 #include <list>
00020 #include <string>
00021 #include "moab/Types.hpp"
00022 
00023 namespace moab
00024 {
00025 
00026 class ReaderIface;
00027 class WriterIface;
00028 class Core;
00029 
00030 /**
00031  *\brief Maintain list of readers and writers.
00032  *\version 1.00
00033  *\date 2004-4-23
00034  *\author Jason Kraftcheck
00035  */
00036 class ReaderWriterSet
00037 {
00038 
00039   public:
00040     typedef ReaderIface* ( *reader_factory_t )( Interface* );
00041     typedef WriterIface* ( *writer_factory_t )( Interface* );
00042 
00043     ReaderWriterSet( Core* mdb );
00044 
00045     ~ReaderWriterSet();
00046 
00047     /**
00048      * Regiseter a reader and/or writer
00049      * Either factory function may be NULL, but not both.
00050      *
00051      *\param reader_fact  A factory method to create an instance of the reader
00052      *\param writer_fact  A factory method to create an instance of the reader
00053      *\param description  A short description of the file format.
00054      *\param extensions   A null-terminated list of file extensions
00055      *\param name         File format identifier string.
00056      */
00057     ErrorCode register_factory( reader_factory_t reader_fact,
00058                                 writer_factory_t writer_fact,
00059                                 const char* description,
00060                                 const char* const* extensions,
00061                                 const char* name );
00062     ErrorCode register_factory( reader_factory_t reader_fact,
00063                                 writer_factory_t writer_fact,
00064                                 const char* description,
00065                                 const char* extension,
00066                                 const char* name );
00067 
00068     /**
00069      * Create a reader object for the passed file name
00070      * according to the dot-extension of the file name.
00071      * Caller must delete the object when finished.
00072      * Returns null if no matching file extension.
00073      */
00074     ReaderIface* get_file_extension_reader( const std::string& filename ) const;
00075 
00076     /**
00077      * Create a writer object for the passed file name
00078      * according to the dot-extension of the file name.
00079      * Caller must delete the object when finished.
00080      * Returns null if no matching file extension.
00081      */
00082     WriterIface* get_file_extension_writer( const std::string& filename ) const;
00083 
00084     /**
00085      * Create a reader object for the passed file format type.
00086      * Caller is responsible for deletion of returned object.
00087      * Returns NULL if no match.
00088      */
00089     ReaderIface* get_file_reader( const char* format_name ) const;
00090 
00091     /**
00092      * Create a writer object for the passed file format type.
00093      * Caller is responsible for deletion of returned object.
00094      * Returns NULL if no match.
00095      */
00096     WriterIface* get_file_writer( const char* format_name ) const;
00097 
00098     /**
00099      * Get the file extension from a file name
00100      */
00101     static std::string extension_from_filename( const std::string& filename );
00102 
00103     class Handler
00104     {
00105 
00106         friend class ReaderWriterSet;
00107 
00108       public:
00109         Handler( reader_factory_t read_f,
00110                  writer_factory_t write_f,
00111                  const char* name,
00112                  const char* desc,
00113                  const char* const* ext,
00114                  int num_ext );
00115 
00116         inline const std::string& name() const
00117         {
00118             return mName;
00119         }
00120         inline const std::string& description() const
00121         {
00122             return mDescription;
00123         }
00124         inline void get_extensions( std::vector< std::string >& list_out ) const
00125         {
00126             list_out = mExtensions;
00127         }
00128 
00129         inline bool have_reader() const
00130         {
00131             return NULL != mReader;
00132         }
00133         inline bool have_writer() const
00134         {
00135             return NULL != mWriter;
00136         }
00137 
00138         inline ReaderIface* make_reader( Interface* iface ) const
00139         {
00140             return have_reader() ? mReader( iface ) : NULL;
00141         }
00142 
00143         inline WriterIface* make_writer( Interface* iface ) const
00144         {
00145             return have_writer() ? mWriter( iface ) : NULL;
00146         }
00147 
00148         bool reads_extension( const char* ext ) const;
00149         bool writes_extension( const char* ext ) const;
00150 
00151         bool operator==( const char* name ) const;
00152 
00153       private:
00154         reader_factory_t mReader;
00155         writer_factory_t mWriter;
00156 
00157         std::string mName, mDescription;
00158         std::vector< std::string > mExtensions;
00159     };
00160 
00161     typedef std::list< Handler >::const_iterator iterator;
00162 
00163     inline iterator begin() const
00164     {
00165         return handlerList.begin();
00166     }
00167 
00168     inline iterator end() const
00169     {
00170         return handlerList.end();
00171     }
00172 
00173     iterator handler_from_extension( const std::string& extension,
00174                                      bool with_reader = false,
00175                                      bool with_writer = false ) const;
00176 
00177     iterator handler_by_name( const char* name ) const;
00178 
00179   private:
00180     Core* mbCore;
00181 
00182     std::list< Handler > handlerList;
00183 };
00184 
00185 }  // namespace moab
00186 
00187 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines