OpenMesh
OpenMesh/Core/IO/IOManager.hh
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh is free software: you can redistribute it and/or modify         * 
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenMesh is distributed in the hope that it will be useful,              *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/ 
00034 
00035 /*===========================================================================*\
00036  *                                                                           *             
00037  *   $Revision: 362 $                                                         *
00038  *   $Date: 2011-01-26 10:21:12 +0100 (Mi, 26 Jan 2011) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00042 //=============================================================================
00043 //
00044 //  Implements the OpenMesh IOManager singleton
00045 //
00046 //=============================================================================
00047 
00048 #ifndef __IOMANAGER_HH__
00049 #define __IOMANAGER_HH__
00050 
00051 
00052 //=== INCLUDES ================================================================
00053 
00054 
00055 // STL
00056 #include <iostream>
00057 #include <sstream>
00058 #include <string>
00059 #include <set>
00060 
00061 // OpenMesh
00062 #include <OpenMesh/Core/System/config.h>
00063 #include <OpenMesh/Core/IO/Options.hh>
00064 #include <OpenMesh/Core/IO/reader/BaseReader.hh>
00065 #include <OpenMesh/Core/IO/writer/BaseWriter.hh>
00066 #include <OpenMesh/Core/IO/importer/BaseImporter.hh>
00067 #include <OpenMesh/Core/IO/exporter/BaseExporter.hh>
00068 #include <OpenMesh/Core/Utils/SingletonT.hh>
00069 
00070 
00071 //== NAMESPACES ===============================================================
00072 
00073 
00074 namespace OpenMesh {
00075 namespace IO {
00076 
00077 
00078 //=== IMPLEMENTATION ==========================================================
00079 
00080 
00100 class _IOManager_
00101 {
00102 private:
00103   
00104   _IOManager_() {}
00105   friend _IOManager_& IOManager();
00106 
00107 
00108 public:
00109 
00110 
00117   bool read(const std::string& _filename, 
00118             BaseImporter& _bi, 
00119             Options& _opt);
00120 
00127   bool read(std::istream& _filename,
00128             const std::string& _ext,
00129             BaseImporter& _bi, 
00130             Options& _opt);
00131 
00132 
00139   bool write(const std::string& _filename, 
00140              BaseExporter& _be,
00141              Options _opt=Options::Default);
00142              
00149   bool write(std::ostream& _filename, 
00150              const std::string& _ext,
00151              BaseExporter& _be,
00152              Options _opt=Options::Default);
00153    
00154 
00156   bool can_read( const std::string& _format ) const;
00157 
00159   bool can_write( const std::string& _format ) const;
00160 
00161    
00162   size_t binary_size(const std::string& _format, 
00163                      BaseExporter& _be,
00164                      Options _opt = Options::Default)
00165   {
00166     const BaseWriter *bw = find_writer(_format);
00167     return bw ? bw->binary_size(_be,_opt) : 0;
00168   }    
00169 
00170 
00171 
00172 public: //-- QT convenience function ------------------------------------------
00173    
00174 
00179   const std::string& qt_read_filters()  const { return read_filters_;  }
00180 
00181 
00186   const std::string& qt_write_filters() const { return write_filters_; }
00187 
00188 
00189 
00190 private:
00191 
00192   // collect all readable file extensions
00193   void update_read_filters();
00194 
00195 
00196   // collect all writeable file extensions
00197   void update_write_filters();
00198    
00199 
00200 
00201 public:  //-- SYSTEM PART------------------------------------------------------
00202 
00203 
00207   bool register_module(BaseReader* _bl)
00208   {
00209     reader_modules_.insert(_bl);
00210     update_read_filters();
00211     return true;
00212   }
00213 
00214 
00215   
00219   bool register_module(BaseWriter* _bw)
00220   {
00221     writer_modules_.insert(_bw);
00222     update_write_filters();
00223     return true;
00224   }
00225 
00226   
00227 private:
00228   
00229   const BaseWriter *find_writer(const std::string& _format);
00230   
00231   // stores registered reader modules
00232   std::set<BaseReader*> reader_modules_;
00233   
00234   // stores registered writer modules
00235   std::set<BaseWriter*> writer_modules_;
00236   
00237   // input filters (e.g. for Qt file dialog)
00238   std::string read_filters_;
00239   
00240   // output filters (e.g. for Qt file dialog)
00241   std::string write_filters_;
00242 };
00243 
00244 
00245 //=============================================================================
00246 
00247 
00248 extern _IOManager_*  __IOManager_instance;
00249 
00250 _IOManager_& IOManager();
00251 
00252 
00253 
00254 //=============================================================================
00255 } // namespace IO
00256 } // namespace OpenMesh
00257 //=============================================================================
00258 #endif
00259 //=============================================================================