Skip to content
LayerFactory.cc 2.13 KiB
Newer Older
#include <stdexcept>
#include <regex>
filatov's avatar
filatov committed

garciay's avatar
garciay committed
#include "loggers.hh"

LayerStackBuilder * LayerStackBuilder::_instance = NULL;

// static functions
LayerStackBuilder * LayerStackBuilder::GetInstance()
{
  return _instance ? _instance : _instance = new LayerStackBuilder();
void LayerStackBuilder::RegisterLayerFactory(const std::string & type, LayerFactory * f)
{
garciay's avatar
garciay committed
  LayerStackBuilder::GetInstance()->registerLayerFactory(type, f);
}

// member functions
LayerStackBuilder::LayerStackBuilder()
{
void LayerStackBuilder::registerLayerFactory(const std::string & type, LayerFactory * f)
filatov's avatar
filatov committed
{
garciay's avatar
garciay committed
  _fs[type] = f;
Layer* LayerStackBuilder::createLayerStack(const char* s)
filatov's avatar
filatov committed
{
garciay's avatar
garciay committed
  loggers::loggers::log(">>> LayerStackBuilder::createLayerStack: %s", s);
garciay's avatar
garciay committed
  Layer * up = NULL;
  // Parse str
  try {
    std::regex rgx ("(\\w+)(\\((.*?)\\))?(\\/|$)");
    std::string str = s;
    std::sregex_iterator begin(str.cbegin(), str.cend(), rgx);
    std::sregex_iterator end = std::sregex_iterator();
    for (std::sregex_iterator it = begin; it != end; ++it) {
      std::smatch m = *it;
      //loggers::loggers::log("LayerStackBuilder::createLayerStack: %d - %s - %s - %s - %s", m.size(), m[0].str().c_str(), m[1].str().c_str(), m[2].str().c_str(), m[3].str().c_str());
      LayerFactoryMap::iterator i =_fs.find(m[1].str());
      if (i == _fs.end()) {
	loggers::loggers::error("LayerStackBuilder::createLayerStack: %s: Unknown layer type", m[1].str().c_str());
      }
      loggers::loggers::log("LayerStackBuilder::createLayerStack: Create layer %s, %s", m[1].str().c_str(), m[3].str().c_str());
      Layer * l = i->second->createLayer(m[1].str(), m[3].str());
      if (NULL == l) {
	loggers::loggers::error("LayerStackBuilder::createLayerStack: %s: Layer creation error", m[1].str().c_str());
      }
garciay's avatar
garciay committed
      loggers::loggers::log("LayerStackBuilder::createLayerStack: Setup layers for %s", l->to_string().c_str());
      l->addUpperLayer(up);
      up = l;
    } // End of 'for' statement
  }
  catch(const std::logic_error& e){
    if(up){
      up->deleteLayer();
      up = NULL;
garciay's avatar
garciay committed
  }
  return up;
filatov's avatar
filatov committed
}