application.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Kutumov Alexey                                  *
00003  *   ru.pixel@gmail.com                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00033 #include "gpucalc/application.h"
00034 #include "gpucalc/gpucalc.h"
00035 #include "gpucalc/simple_task_manager.h"
00036 
00037 
00038 namespace gpucalc
00039 {
00040     typedef void (*DLL_START_PLUGIN)();
00041     typedef void (*DLL_STOP_PLUGIN)();
00042 
00043 
00044     template<> Application * Singleton<Application>::mSingleton = 0;
00045 
00046     Application * Application::getSingletonPtr()
00047     {
00048         return mSingleton;
00049     }
00050 
00051 
00052     Application & Application::getSingleton()
00053     {
00054         assert(mSingleton);
00055         return(* mSingleton);
00056     }
00057 
00058 
00059     Application::Application(const InputArguments & InputArgs):
00060         Singleton<Application>(_ApplicationClassName), mLogManager(new LogManager()), mTimer(new Timer), mProfiler(new Profiler),
00061         mDefaultGraphicCore(0), mGraphicCoreVector(), mDefaultShaderSystem(0), mShaderSystemVector(),
00062         mDefaultTaskManager(0), mTaskManagerMap(), mPluginMap(), mLibraryMap(), mSupressWarnings(false), mInputArguments(InputArgs)
00063     {
00064   mLogManager->createLog(InputArgs.getLogFile(), true, 15, 20, true);
00065   mLogManager->setLogDetail(mInputArguments.getLoggingLevel());
00066         applyInputArguments();
00067 
00068         LogManager::getSingleton().logMessage(this, "---===========================================---");
00069         LogManager::getSingleton().logMessage(this, "---===         Application created         ===---");
00070         LogManager::getSingleton().logMessage(this, std::string("---===Using \"gpucalc\" library version ") + gpucalc_version + "===---");
00071         LogManager::getSingleton().logMessage(this, "---===========================================---");
00072 
00073         LogManager::getSingleton().logMessage(this, "Application name is \"" + InputArgs.getApplicationName() + "\"");
00074         LogManager::getSingleton().logMessage(this, "Configuration file name is \"" + InputArgs.getConfigFile() + "\"");
00075 
00076         addTaskManager(new SimpleTaskManager());
00077 
00078         mDebugger = new debugger::Debugger();
00079     }
00080 
00081 
00082     Application::~Application()
00083     {
00084         automaticUnloadLibraries();
00085         automaticUninstallPlugins();
00086 
00087         clearContainer(mGraphicCoreVector);
00088         clearContainer(mShaderSystemVector);
00089         clearContainer(mTaskManagerMap);
00090 
00091         delete mDebugger;
00092 
00093         LogManager::getSingleton().logMessage(this, "---===========================================---");
00094         LogManager::getSingleton().logMessage(this, "---===         Application deleted         ===---");
00095         LogManager::getSingleton().logMessage(this, "---===========================================---");
00096         delete mLogManager;
00097     }
00098 
00099 
00100     void Application::addGraphicCore(GraphicCore * Core)
00101     {
00102         assert(Core && "adding non initialized GraphicCore pointer is not allowed");
00103         addToContainer(Core, mGraphicCoreVector);
00104         mDefaultGraphicCore = Core;
00105     }
00106 
00107 
00108     GraphicCore * Application::getCompatibleGraphicCore(const ShaderSystem * System)
00109     {
00110         for (GraphicCoreVector::iterator i = mGraphicCoreVector.begin(); i != mGraphicCoreVector.end(); ++i)
00111         {
00112             if ((*i)->isAcceptSystem(System))
00113             {
00115             mDefaultGraphicCore = *i;
00116             //mDefaultShaderSystem = System;
00117             return mDefaultGraphicCore;
00118             }
00119         }
00120         Except<ERR_ITEM_NOT_FOUND>(this, "Cannot find valid acceptable GraphicCore.", "Application::getGraphicCore", __FILE__, __LINE__);
00121         return 0;
00122     }
00123 
00124 
00125  void Application::removeGraphicCore(GraphicCore * Core)
00126  {
00127   removeFromContainer(Core, mGraphicCoreVector);
00128   if (Core == mDefaultGraphicCore)
00129   {
00130    if (mGraphicCoreVector.empty())
00131    {
00132     mDefaultGraphicCore = 0;
00133    }
00134    else
00135    {
00136     mDefaultGraphicCore = (*mGraphicCoreVector.begin());
00137    }
00138   }
00139  }
00140 
00141 
00142  void Application::addShaderSystem(ShaderSystem * System)
00143  {
00144   assert(System && "adding non initialized ShaderSystem pointer is not allowed.");
00145   addToContainer(System, mShaderSystemVector);
00146   mDefaultShaderSystem = System;
00147  }
00148 
00149 
00150  ShaderSystem * Application::getCompatibleShaderSystem(const GraphicCore * Core)
00151  {
00152   for (ShaderSystemVector::iterator i = mShaderSystemVector.begin(); i != mShaderSystemVector.end(); ++i)
00153   {
00154    if ((*i)->isAcceptCore(Core))
00155    {
00156     mDefaultShaderSystem = *i;
00157     return mDefaultShaderSystem;
00158    }
00159   }
00160   Except<ERR_ITEM_NOT_FOUND>(this, "Cannot find valid acceptable ShaderSystem.", "Application::getShaderSystem()", __FILE__, __LINE__);
00161   return 0;
00162  }
00163 
00164 
00165  void Application::removeShaderSystem(ShaderSystem * System)
00166  {
00167   removeFromContainer(System, mShaderSystemVector);
00168   if (System == mDefaultShaderSystem)
00169   {
00170    if (mShaderSystemVector.empty())
00171    {
00172     mDefaultShaderSystem = 0;
00173    }
00174    else
00175    {
00176     mDefaultShaderSystem = (*mShaderSystemVector.begin());
00177    }
00178   }
00179  }
00180 
00181 
00182  void Application::addTaskManager(TaskManager * TaskMgr)
00183  {
00184   if (!addToContainer(TaskMgr, TaskMgr->getObjectName(), mTaskManagerMap))
00185   {
00186    Warning<ERR_DUPLICATE_ITEM>(this, "TaskManager \"" + TaskMgr->getObjectName() + "\" already exists!!! Nothing added.",
00187     "Application::addTaskManager()", __FILE__, __LINE__);
00188   }
00189   else
00190   {
00191    mDefaultTaskManager = TaskMgr;
00192   }
00193  }
00194 
00195 
00196  TaskManager * Application::getDefaultTaskManager() const
00197  {
00198   return mDefaultTaskManager;
00199  }
00200 
00201 
00202  TaskManager * Application::getTaskManager(const std::string & TaskMgrName) const
00203  {
00204   TaskManagerMap::const_iterator i = mTaskManagerMap.find(TaskMgrName);
00205   if (i != mTaskManagerMap.end())
00206   {
00207    return i->second;
00208   }
00209   else
00210   {
00211    Except<ERR_ITEM_NOT_FOUND>(this, "TaskManager \"" + TaskMgrName + "\" cannot be found.", "Application::getTaskManager()", __FILE__, __LINE__);
00212    return 0;
00213   }
00214  }
00215 
00216 
00217  void Application::removeTaskManager(TaskManager * TaskMgr)
00218  {
00219   if (!removeFromContainer(TaskMgr->getObjectName(), mTaskManagerMap))
00220   {
00221    Warning<ERR_ITEM_NOT_FOUND>(this, "TaskManager \"" + TaskMgr->getObjectName() + "\" cannot be found!!! Deleting anyway.",
00222     "Application::removeTaskManager()", __FILE__, __LINE__);
00223   }
00224   delete TaskMgr;
00225  }
00226 
00227 
00228  void Application::installPlugin(Plugin * plugin)
00229  {
00230   if (addToContainer(plugin, plugin->getObjectName(), mPluginMap))
00231   {
00232    plugin->install();
00233   }
00234   else
00235   {
00236    Warning<ERR_DUPLICATE_ITEM>(this, "Plugin \"" + plugin->getObjectName() + "\" already exists!!! Nothing added.",
00237     "Application::installPlugin()", __FILE__, __LINE__);
00238   }
00239  }
00240 
00241 
00242  void Application::uninstallPlugin(Plugin * plugin)
00243  {
00244   if (!removeFromContainer(plugin->getObjectName(), mPluginMap))
00245   {
00246    Warning<ERR_ITEM_NOT_FOUND>(this, "Plugin \"" + plugin->getObjectName() + "\" cannot be found!!! Uninstalling anyway.",
00247     "Application::uninstallPLugin()", __FILE__, __LINE__);
00248   }
00249   plugin->uninstall();
00250  }
00251 
00252 
00253  void Application::loadLibrary(const std::string & LibraryFileName)
00254  {
00255         LibraryMap::iterator i = mLibraryMap.find(LibraryFileName);
00256         if (i == mLibraryMap.end())
00257         {
00258             LogManager::getSingleton().logMessage(this, "Loading \"" + LibraryFileName + "\"...");
00259             Library * lib = new Library(LibraryFileName);
00260    lib->load();
00261    addToContainer(lib, LibraryFileName, mLibraryMap);
00262 
00263    DLL_START_PLUGIN pFunc = (DLL_START_PLUGIN)lib->getSymbol("dllStartPlugin");
00264    if (!pFunc)
00265    {
00266     Except<ERR_FILE_NOT_FOUND>(this, "Entry point in plugin \"" +  LibraryFileName + "\" cannot be found",
00267      "Application::loadPlugin", __FILE__, __LINE__);
00268    }
00269    pFunc();
00270    LogManager::getSingleton().logMessage(this, "Library \"" + LibraryFileName + "\" successfully loaded");
00271   }
00272  }
00273 
00274 
00275  void Application::unloadLibrary(const std::string & LibraryFileName)
00276  {
00277   LibraryMap::iterator i = mLibraryMap.find(LibraryFileName);
00278   if (i != mLibraryMap.end())
00279   {
00280    DLL_STOP_PLUGIN pFunc = (DLL_STOP_PLUGIN)i->second->getSymbol("dllStopPlugin");
00281    pFunc();
00282    delete i->second;
00283    mLibraryMap.erase(i);
00284    LogManager::getSingleton().logMessage(this, "Library \"" + LibraryFileName + "\" successfully unloaded");
00285   }
00286  }
00287 
00288 
00289  void Application::automaticUnloadLibraries()
00290  {
00291   if (!mLibraryMap.empty())
00292   {
00293    Warning<ERR_INVALID_STATE>(this, "Called automatic unloading libraries.\
00294     It is preferred to unload its manualy by calling method Application::unloadLibrary()",
00295     "Application::AutomaticUnloadLibraries()", __FILE__, __LINE__);
00296    for (LibraryMap::iterator i = mLibraryMap.begin(); i != mLibraryMap.end(); ++i)
00297    {
00298     DLL_STOP_PLUGIN pFunc = (DLL_STOP_PLUGIN)i->second->getSymbol("dllStopPlugin");
00299     pFunc();
00300     delete i->second;
00301     LogManager::getSingleton().logMessage(this, "Library \"" + i->first + "\" successfully unloaded");
00302    }
00303    mLibraryMap.clear();
00304   }
00305  }
00306 
00307 
00308  void Application::automaticUninstallPlugins()
00309  {
00310   if (!mPluginMap.empty())
00311   {
00312    Warning<ERR_INVALID_STATE>(this, "Called automatic plugins uninstalling.\
00313     It is preferred to uninstall its manualy by calling method Application::uninstallPLugin",
00314     "Application::AutomaticuninstallPlugins", __FILE__, __LINE__);
00315    for (PluginMap::iterator i = mPluginMap.begin(); i != mPluginMap.end(); ++i)
00316    {
00317     i->second->uninstall();
00318     delete i->second;
00319     LogManager::getSingleton().logMessage(this, "Plugin \"" + i->first + "\" successfully uninstalled");
00320    }
00321    mPluginMap.clear();
00322   }
00323  }
00324 
00325 
00326     void Application::applyInputArguments()
00327     {
00328         const StringList & pluginList = mInputArguments.getPluginList();
00329         for (StringList::const_iterator i = pluginList.begin(); i != pluginList.end(); ++i)
00330         {
00331             try
00332             {
00333                 loadLibrary(*i);
00334             }
00335             catch(const Exception & e)
00336             {
00337                 // does nothing
00338             }
00339         }
00340 
00341         // assume that our graphic core and shader system are compatible
00342         mDefaultGraphicCore->start(mInputArguments.getArgc(), mInputArguments.getArgv());
00343         mDefaultShaderSystem->start();
00344 
00345 
00346         const StringMap & shaders = mInputArguments.getShaders();
00347         for (StringMap::const_iterator i = shaders.begin(); i != shaders.end(); ++i)
00348         {
00349             mDefaultShaderSystem->preCompile(i->first, auxillary::TextFileLoader::load(i->second));
00350         }
00351     }
00352 }

Generated on Thu Mar 5 22:36:42 2009 for gpucalc by  doxygen 1.5.6