Skip to content

File NPApplication.cxx

File List > core > NPApplication.cxx

Go to the documentation of this file

#include "NPApplication.h"
#include "NPException.h"

using namespace nptool;

// Static pointer to the current application
std::shared_ptr<Application> nptool::Application::m_instance = nullptr;
// Variable that prevent initialisation of many application
bool local_correct_init = false; //
std::shared_ptr<Application> nptool::Application::InitApplication(int argc, char** argv,
                                                                  unsigned int number_of_threads) {

  if (!m_instance) {
    local_correct_init = true;
    m_instance = std::make_shared<Application>(argc, argv, number_of_threads);
    if (m_instance->HasFlag("--cores")) {
      auto cores = m_instance->GetIntArg("--cores");
      m_instance->SetMaxThreads(cores);
      message("green", "core", "nptool::Application", "Using " + itoa(cores) + "cores");
    }
    if (m_instance->HasFlag("--disable-mt") && !m_instance->HasFlag("--enable-mt")) {
      m_instance->DisableMT();
      message("green", "core", "nptool::Application", "Multithreading disable in framework");
    }
    else
      message("green", "core", "nptool::Application", "Multithreading enable");
    local_correct_init = false;
  }
  else {
    std::string msg = "An Application instance already exist, cannot recreate";
    throw(nptool::Error("Application", msg));
  }
  return m_instance;
}
std::shared_ptr<Application> nptool::Application::InitApplication(std::string s, unsigned int number_of_threads) {

  if (!m_instance) {
    local_correct_init = true;
    m_instance = std::make_shared<Application>(s, number_of_threads);
    if (m_instance->HasFlag("--cores")) {
      auto cores = m_instance->GetIntArg("--cores");
      m_instance->SetMaxThreads(cores);
      message("green", "core", "nptool::Application", "Using " + itoa(cores) + "cores");
    }
    if (m_instance->HasFlag("--disable-mt") && !m_instance->HasFlag("--enable-mt")) {
      m_instance->DisableMT();
      message("green", "core", "nptool::Application", "Multithreading disable in framework");
    }
    else
      message("green", "core", "nptool::Application", "Multithreading enable");
    local_correct_init = false;
  }
  else {
    std::string msg = "An Application instance already exist, cannot recreate";
    throw(nptool::Error("Application", msg));
  }
  return m_instance;
}
std::shared_ptr<Application> nptool::Application::GetApplication() {
  if (m_instance) {
    return m_instance;
  }
  else {
    std::string msg = "Cannot initialise Application without argument";
    throw(nptool::Error("Application", msg));
  }

  return m_instance;
}

nptool::Application::Application(int argc, char** argv, unsigned int number_of_threads)
    : thread_pool(number_of_threads) {
  if (local_correct_init) {
    ReadProjectConfigFile(argv[0]);
    ReadArgument(argc, argv);
  }
  else {
    std::string msg = "Using constructor of Application is not allowed. Use InitApplication or GetApplication";
    throw(nptool::Error("Application", msg));
  }
}
nptool::Application::Application(std::string s, unsigned int number_of_threads) : thread_pool(number_of_threads) {
  if (local_correct_init) {
    ReadArgument(s);
  }
  else {
    std::string msg = "Using constructor of Application is not allowed. Use InitApplication or GetApplication";
    throw(nptool::Error("Application", msg));
  }
}
nptool::Application::~Application() {
  local_correct_init = false;
  m_instance = nullptr;
}
void nptool::Application::Start(bool load) {
  if (load) {
    std::string Home = getenv("NPTOOL_HOME");
    if (Home.back() != '/')
      Home += "/";
    std::string Env = Home + getenv("NPTOOL_ENV");
    std::string command = "ls " + Env + "/nptool.log" + " &>/dev/null";
    if (system(command.c_str()) == 0) {
      command = "echo \"nptool log: \" > " + Env + "/nptool.log";
      system(command.c_str());
    }
    std::string path_list = Env + "/plugin_lib.list";
    // load the general install list
    LoadPluginList(path_list);
    // local project list and allow to fail
    LoadPluginList("./local_plugin.list", true);
    m_instance->InitCalibration();
    m_instance->InitDetectorManager();
  }
  return;
}
void nptool::Application::Stop() {
  PrintIgnoredFlag();
  return;
}