KeosMediaManager.h

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 #ifndef KEOS_MEDIAMANAGER_H
00022 #define KEOS_MEDIAMANAGER_H
00023 
00024 #include "KeosPrerequisites.h"
00025 #include "KeosSingleton.h"
00026 #include "KeosGenHierarchies.h"
00027 #include "KeosFile.h"
00028 #include "KeosUtil.h"
00029 #include "KeosLoader.h"
00030 #include "KeosException.h"
00031 #include "KeosString.h"
00032 
00033 namespace Keos
00034 {
00036   typedef TYPELIST_4(CMesh, CImage, IShaderBase, CAnimatedMeshKF) MediaList;
00037 
00039   template <class T>
00040   struct CMediaHolder
00041   {
00042     typedef std::map<String, CSmartPtr<ILoader<T> > > TLoadersMap;
00043     TLoadersMap m_Loaders;
00044   };
00045 
00046   //-----------------------------------------------------------------------
00050   class KEOS_EXPORT CMediaManager : public CSingleton<CMediaManager>, public CScatteredHierarchy<MediaList, CMediaHolder>
00051   {
00052     friend CSingleton<CMediaManager>;
00053 
00054   public :
00055 
00058     void AddSearchPath(const String& strPath);
00059 
00064     template <class T> void RegisterLoader(ILoader<T>* pLoader, const String& strExtensions);
00065 
00070     template <class T> T* LoadMediaFromFile(const CFile& Filename) const;
00071 
00076     template <class T> void SaveMediaToFile(const T* pObject, const CFile& Filename) const;
00077 
00080     CFile FindMedia(const CFile& Filename) const;
00081 
00082   private :
00083 
00086     CMediaManager();
00087 
00090     ~CMediaManager();
00091 
00096     template <class T> ILoader<T>& FindLoader(const CFile& Filename) const;
00097 
00099     std::set<String> m_Paths;
00100   };
00101 
00102   //=======================================================================
00103   // CMediaManager inline functions
00104   //=======================================================================
00105 
00106   //-----------------------------------------------------------------------
00107   template <class T>
00108   inline void CMediaManager::RegisterLoader(ILoader<T>* pLoader, const String& strExtensions)
00109   {
00110     // Recovery of the extensions
00111     std::vector<String> Ext;
00112     CStringUtil::Split(strExtensions, Ext, " /\\*.,;|-_\t\n'\"");
00113 
00114     // Add the extensions
00115     CSmartPtr<ILoader<T> > Ptr = pLoader;
00116     for (std::vector<std::string>::iterator i = Ext.begin(); i != Ext.end(); ++i)
00117       CMediaHolder<T>::m_Loaders[CStringUtil::ToLower(*i)] = Ptr;
00118   }
00119 
00120   //-----------------------------------------------------------------------
00121   template <class T>
00122   inline T* CMediaManager::LoadMediaFromFile(const CFile& Filename) const
00123   {
00124     // Search for the file in the registered directories
00125     CFile MediaPath = FindMedia(Filename);
00126 
00127     // Search the appropriate loader
00128     return FindLoader<T>(MediaPath).LoadFromFile(MediaPath.Fullname());
00129   }
00130 
00131   //-----------------------------------------------------------------------
00132   template <class T>
00133   inline void CMediaManager::SaveMediaToFile(const T* pObject, const CFile& Filename) const
00134   {
00135     // Search the appropriate loader
00136     FindLoader<T>(Filename).SaveToFile(pObject, Filename.Fullname());
00137   }
00138 
00139   //-----------------------------------------------------------------------
00140   template <class T>
00141   inline ILoader<T>& CMediaManager::FindLoader(const CFile& Filename) const
00142   {
00143     // Search the extension in the map
00144     typename CMediaHolder<T>::TLoadersMap::const_iterator It = CMediaHolder<T>::m_Loaders.find(CStringUtil::ToLower(Filename.Extension()));
00145 
00146     // If we find the extension, we return the loader
00147     if ((It != CMediaHolder<T>::m_Loaders.end()) && It->second)
00148       return *It->second;
00149 
00150     LOADINGFAILED_EXCEPT(Filename.Fullname(), "No loader takes care of this file format", "CMediaManager::FindLoader");
00151   }
00152 
00153 } // namespace Keos
00154 
00155 
00156 #endif // KEOS_MEDIAMANAGER_H

Generated on Fri Mar 9 14:29:02 2007 for Keos by  doxygen 1.5.1-p1