#include "Layer.hh" #include #include LayerStackBuilder * LayerStackBuilder::_instance = NULL; // static functions LayerStackBuilder * LayerStackBuilder::GetInstance() { return _instance ? _instance : _instance=new LayerStackBuilder(); } void LayerStackBuilder::RegisterLayerFactory(const std::string & type, LayerFactory * f) { LayerStackBuilder::GetInstance()->registerLayerFactory(type, f); } // member functions LayerStackBuilder::LayerStackBuilder() { } void LayerStackBuilder::registerLayerFactory(const std::string & type, LayerFactory * f) { _fs[type] = f; } Layer* LayerStackBuilder::createLayerStack(const char* s) { Layer * up = NULL; //parse str std::regex rgx ("(\\w+)(\\((.*?)\\))?(\\/|$)"); std::smatch m; std::string str = s; try { while(std::regex_search(str, m, rgx)) { std::string mname = m[1]; LayerFactoryMap::iterator it =_fs.find(mname); if(it == _fs.end()){ throw (std::logic_error(mname + ": Unknown layer type")); } // TODO: parse parameters Layer * l = it->second->createLayer(mname, m[3]); if(NULL == l){ throw (std::logic_error(mname + ": Layer creation error")); } l->addUpperLayer(up); up = l; } } catch(const std::logic_error& e){ if(up){ up->deleteLayer(); up = NULL; } } return up; } void Layer::deleteLayer() { }