Resource.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //
00003 // $Id$
00004 //
00005 // Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno    
00006 //
00007 // This file is part of MIP.
00008 //
00009 // MIP is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // MIP is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
00023 //
00024 // ----------------------------------------------------------------------------
00025 
00026 
00030 
00031 #ifndef __RESOURCE_H_
00032 #define __RESOURCE_H_
00033 
00034 #ifdef MIP_HOST_APPLE
00035 #include <applePatch.h>
00036 #endif
00037 
00038 #include <stdio.h>
00039 #include <vector>
00040 #include <iostream>
00041 #include <fstream>
00042 #include <sstream>
00043 #include <string.h>
00044 
00045 #include <baselib.h>
00046 #include <File.h>
00047 #include "ResourceDict.h"
00048 
00049 using namespace std;
00050 
00053 namespace MipResources{
00054 
00066 class Resource:public MIPObject{
00067  public:
00068  
00069 //   Resource(){
00070 //   }
00072   virtual ResourcePlate getPlate() const =0;
00074   string getName() const {
00075    assert(getPlate()<RESOURCE_NUM);
00076    return ResourceNames[getPlate()];
00077   }
00078 
00079   virtual string getObjectName() const {
00080    return "Resource";
00081   }
00082 
00084   string print(){
00085    stringstream s;
00086    s << "Resource Card: [plate="  << getPlate() << ", name=" << getName() << "]";
00087    return s.str();
00088   }
00089 
00090 };
00091 
00092 
00095 typedef vector<Resource*> ResourcePointers;
00096 
00099 typedef vector<Resource**> ResourcePPointers;
00100 
00101 
00104 class ResourceUtilities {
00105  private:
00106 
00107  public:
00112   static Resource* resFromPlate(ResourcePointers& resources,ResourcePlate plate){
00113    unsigned int resNum = resources.size();
00114    for(unsigned int i=0;i<resNum;i++){
00115     if(plate == resources[i]->getPlate()){
00116      return resources[i];
00117     }
00118    }
00119    return 0;
00120   }
00126   static Resource* macroResFromPlate(ResourcePointers resources,ResourcePlate initPlate,ResourcePlate endPlate){
00127    unsigned int resNum = resources.size();
00128    for(unsigned int i=0;i<resNum;i++){
00129     if((resources[i]->getPlate() > initPlate)&&(resources[i]->getPlate() < endPlate)){
00130      return resources[i];
00131     }
00132    }
00133    return 0;
00134   }
00138   static void getPlatesFromFile(string fileName,ResourcePlates& plates){
00139    File file(fileName);
00140    vector<string> words;
00141    file.allWords(words);
00142    getPlatesFromWords(words,plates);
00143   }
00147   static void getPlatesFromWords(vector<string>& words,ResourcePlates& plates){
00148    plates.clear();
00149    unsigned int wordsNum = words.size();
00150    for(unsigned int i=0; i<wordsNum;i++){
00151     for(unsigned int plateNum=0;plateNum<RESOURCE_NUM;plateNum++){
00152      string resName(ResourceNames[plateNum]);
00153      if(words[i] == resName){
00154       ResourcePlate plate = (ResourcePlate) plateNum;
00155       plates.push_back(plate);
00156       continue;
00157      }
00158     }
00159    }
00160   }
00161   
00162   
00163   
00168    static void getPluginsFromFile(string fileName, vector<string>& names, vector<string>& libraries){
00169     names.clear();
00170     libraries.clear();
00171     File file(fileName);
00172     vector<string> words;
00173     file.allWords(words);
00174     getPluginsFromWords(words,names,libraries);
00175    }
00176    
00180    static void getPluginsFromWords(vector<string>& words, vector<string>& names, vector<string>& libraries){
00181     
00182     string fileName = getenv("MIP_ROOT");
00183     if(fileName.empty())
00184      fileName = "../../PluginExample/PluginList.txt";
00185     else
00186      fileName+= "/PluginExample/PluginList.txt";
00187     
00188     File pluginListFile(fileName);
00189     vector<string> pluginList;
00190     pluginListFile.allWords(pluginList);
00191     
00192     string plResName("PluginResource:");
00193     unsigned int wordsNum = words.size();
00194     for(unsigned int i=0; i<wordsNum;i++){
00195      if (words[i].length()>11){
00196       string wordBeginning(words[i].substr(0, 15));
00197       if (wordBeginning == plResName){
00198        bool libraryFound = false;
00199        int ddotsPos = words[i].find_first_of(':', 15);
00200        string libraryName(words[i].substr(15,ddotsPos-15));
00201        string library;
00202        unsigned int pluginListSize = pluginList.size();
00203        for(unsigned int j=0; j<pluginListSize; ++j){
00204         if (pluginList[j].npos > pluginList[j].rfind(libraryName)){
00205          libraryFound = true;
00206          stringstream ssLib;
00207          library = pluginList[j].substr(0, pluginList[j].size()-21);
00208          ssLib << library << "lib/lib" << libraryName << ".so";
00209          library = ssLib.str();
00210          break;
00211         }
00212        }
00213        if (!libraryFound){
00214         stringstream ssFail;
00215         ssFail << "Impossible to find MIP-Plugin " << libraryName << " library in the PluginExample/PluginList.txt. Please execute MIPProfilePlugins. ";
00216         ssFail << "If the error persists, you may have changed the name of your plugin folder, withouth changing the project name, or viceversa. ";
00217         ssFail << "Also check that the build directory of your plugin contains a MIPPluginManifest.MIP empty file.";
00218         LogTrace::fatal(ssFail.str());
00219        }
00220        string name(words[i].substr(ddotsPos+1));
00221        names.push_back(name);
00222        libraries.push_back(library);
00223       }
00224      }
00225     }
00226    }
00227    
00228 };
00229 
00230 // the types of the class factories
00231  typedef Resource* create_resource(int argc, const char* argv[]);
00232  
00233  typedef void destroy_resource(Resource*);
00234  
00235 };// end namespace MipResources
00236 
00237 
00238 #endif

Generated on Mon Feb 20 07:01:07 2017 for MIP by  doxygen 1.5.6