GeneratorBase.cc 3.14 KB
Newer Older
1 2
#include "GeneratorBase.h"

3 4
#include "GenericSingelton.h"

5 6
namespace routing {

7
const char *GeneratorBase::typeKey = "GeneratorType";
8
const char *GeneratorBase::nameKey = "Name";
9

10 11 12 13
GeneratorBase::GeneratorBase(QObject *parent)
    : GeneratorBase(nullptr, parent) {}

GeneratorBase::GeneratorBase(GeneratorBase::Data d, QObject *parent)
14
    : QObject(parent), _d(d) {}
15 16 17

GeneratorBase::~GeneratorBase() {}

18
bool GeneratorBase::save(QJsonObject &obj) const {
19 20
  obj[typeKey] = this->type();
  obj[nameKey] = this->name();
21 22 23 24
  return true;
}

bool GeneratorBase::load(const QJsonObject &obj, QString &errorString) {
25
  if (obj.contains(nameKey) && obj[nameKey].isString()) {
26 27 28 29 30 31 32 33 34 35
    setName(obj[nameKey].toString());
    return true;
  } else {
    errorString.append(
        tr("Not able to load generator name. Leaving name unchanged."));
    return false;
  }
}

QString GeneratorBase::name() const { return _name; }
36 37 38 39 40 41 42 43

void GeneratorBase::setName(const QString &name) {
  if (_name != name) {
    _name = name;
    emit nameChanged();
  }
}

44 45
GeneratorBase::Data GeneratorBase::data() const { return _d; }

46
void GeneratorBase::setData(Data d) {
47
  if (d != _d) {
48
    _d = d;
49
    emit dataChanged();
50
  }
51 52
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
GeneratorFactory::GeneratorFactory() {}

GeneratorFactory *GeneratorFactory::createInstance() {
  return new GeneratorFactory();
}

GeneratorFactory::~GeneratorFactory() {}

GeneratorFactory *GeneratorFactory::instance() {
  return GenericSingelton<GeneratorFactory>::instance(
      GeneratorFactory::createInstance);
}

bool GeneratorFactory::registerGenerator(const QString &type,
                                         GeneratorFactory::Creator creator) {
  const auto pair = _creatorMap.insert(std::make_pair(type, creator));
  auto success = pair.second;
  return success;
}

bool GeneratorFactory::registered(const QString &type) {
  return _creatorMap.end() != _creatorMap.find(type);
}

void GeneratorFactory::unregisterGenerator(const QString &type) {
  _creatorMap.erase(type);
}

GeneratorBase *GeneratorFactory::create(const QString &type, QObject *parent) {
  auto it = _creatorMap.find(type);
  if (it != _creatorMap.end()) {
    auto &creator = it->second;
    return creator(parent);
  } else {
    return nullptr;
  }
}

GeneratorBase *GeneratorFactory::create(const QJsonObject &jsonGenerator,
                                        QString &errorMessage,
                                        QObject *parent) {
  if (jsonGenerator.contains(GeneratorBase::typeKey) &&
      jsonGenerator[GeneratorBase::typeKey].isString()) {
    auto gen = create(jsonGenerator[GeneratorBase::typeKey].toString(), parent);
    if (gen != nullptr) {
      QString e;
      if (gen->load(jsonGenerator, e)) {
        return gen;
      } else {
        gen->deleteLater();
        errorMessage.append(e);
        return nullptr;
      }
    } else {
107 108 109
      errorMessage.append(
          QObject::tr("Not able to create generator of type.\n") + " " +
          jsonGenerator[GeneratorBase::typeKey].toString());
110 111 112 113
      return nullptr;
    }
  } else {
    errorMessage.append(QObject::tr(
114
        "Not able to load Generator. Impossible to determine type.\n"));
115 116 117 118
    return nullptr;
  }
}

119
} // namespace routing