| 1 | 
tim | 
12 | 
#include "applications/simpleBuilder/LatticeFactory.hpp" | 
| 2 | 
  | 
  | 
#include "applications/simpleBuilder/BaseLattice.hpp" | 
| 3 | 
  | 
  | 
#include "applications/simpleBuilder/LatticeCreator.hpp" | 
| 4 | 
  | 
  | 
 | 
| 5 | 
  | 
  | 
LatticeFactory* LatticeFactory::instance = NULL; | 
| 6 | 
  | 
  | 
LatticeFactory::~LatticeFactory(){ | 
| 7 | 
  | 
  | 
  map<string, BaseLatticeCreator*>::iterator mapIter; | 
| 8 | 
  | 
  | 
        for (mapIter = creatorMap.begin(); mapIter == creatorMap.end(); ++mapIter) { | 
| 9 | 
  | 
  | 
                delete mapIter->second; | 
| 10 | 
  | 
  | 
        }   | 
| 11 | 
  | 
  | 
} | 
| 12 | 
  | 
  | 
 | 
| 13 | 
  | 
  | 
 LatticeFactory* LatticeFactory::getInstance(){ | 
| 14 | 
  | 
  | 
        if (instance == NULL) | 
| 15 | 
  | 
  | 
      instance = new LatticeFactory(); | 
| 16 | 
  | 
  | 
         | 
| 17 | 
  | 
  | 
        return instance; | 
| 18 | 
  | 
  | 
} | 
| 19 | 
  | 
  | 
 | 
| 20 | 
  | 
  | 
bool LatticeFactory::registerCreator( BaseLatticeCreator*  latCreator ){   | 
| 21 | 
  | 
  | 
  string latticeType = latCreator->getType(); | 
| 22 | 
  | 
  | 
  map<string, BaseLatticeCreator*>::iterator mapIter; | 
| 23 | 
  | 
  | 
 | 
| 24 | 
  | 
  | 
  mapIter = creatorMap.find(latticeType); | 
| 25 | 
  | 
  | 
 | 
| 26 | 
  | 
  | 
  if (mapIter == creatorMap.end()) { | 
| 27 | 
  | 
  | 
    creatorMap[ latticeType ] = latCreator; | 
| 28 | 
  | 
  | 
    return true; | 
| 29 | 
  | 
  | 
  } | 
| 30 | 
  | 
  | 
  else{ | 
| 31 | 
  | 
  | 
    delete mapIter->second; | 
| 32 | 
  | 
  | 
    mapIter->second = latCreator; | 
| 33 | 
  | 
  | 
    return false; | 
| 34 | 
  | 
  | 
  } | 
| 35 | 
  | 
  | 
} | 
| 36 | 
  | 
  | 
 | 
| 37 | 
  | 
  | 
BaseLattice* LatticeFactory::createLattice( const string& latticeType ){  | 
| 38 | 
  | 
  | 
  map<string, BaseLatticeCreator*>::iterator mapIter;  | 
| 39 | 
  | 
  | 
   | 
| 40 | 
  | 
  | 
  mapIter = creatorMap.find(latticeType); | 
| 41 | 
  | 
  | 
 | 
| 42 | 
  | 
  | 
  if (mapIter != creatorMap.end()) { | 
| 43 | 
  | 
  | 
     return (mapIter->second)->createLattice(); | 
| 44 | 
  | 
  | 
  } | 
| 45 | 
  | 
  | 
  else | 
| 46 | 
  | 
  | 
    return NULL; | 
| 47 | 
  | 
  | 
} | 
| 48 | 
  | 
  | 
 | 
| 49 | 
  | 
  | 
bool LatticeFactory::hasLatticeCreator( const string& latticeType ){ | 
| 50 | 
  | 
  | 
  map<string, BaseLatticeCreator*>::iterator mapIter; | 
| 51 | 
  | 
  | 
 | 
| 52 | 
  | 
  | 
  mapIter = creatorMap.find(latticeType); | 
| 53 | 
  | 
  | 
 | 
| 54 | 
  | 
  | 
  if (mapIter != creatorMap.end()) | 
| 55 | 
  | 
  | 
    return true; | 
| 56 | 
  | 
  | 
  else | 
| 57 | 
  | 
  | 
    return false; | 
| 58 | 
  | 
  | 
} | 
| 59 | 
  | 
  | 
 | 
| 60 | 
  | 
  | 
const string LatticeFactory::toString(){ | 
| 61 | 
  | 
  | 
  string result; | 
| 62 | 
  | 
  | 
  map<string, BaseLatticeCreator*>::iterator mapIter; | 
| 63 | 
  | 
  | 
 | 
| 64 | 
  | 
  | 
  result = "Avaliable lattice creators in LatticeFactory are:\n"; | 
| 65 | 
  | 
  | 
   | 
| 66 | 
  | 
  | 
  for(mapIter = creatorMap.begin(); mapIter != creatorMap.end(); ++mapIter){ | 
| 67 | 
  | 
  | 
    result += mapIter->first + " "; | 
| 68 | 
  | 
  | 
  } | 
| 69 | 
  | 
  | 
   | 
| 70 | 
  | 
  | 
  result += "\n"; | 
| 71 | 
  | 
  | 
 | 
| 72 | 
  | 
  | 
  return result; | 
| 73 | 
  | 
  | 
} | 
| 74 | 
  | 
  | 
 |