/*******************************************************************************
 *
 * File:     System.cpp
 *
 * Contents: Implementation of System base-class.
 *
 * Copyright 2017
 *
 * Author: David Juhasz (david.juhasz@tuwien.ac.at)
 *
 ******************************************************************************/

#include "SystemImpl.h"
#include "rosa/config/config.h"
#include "rosa/core/Unit.h"
#include "rosa/core/System.h"
#include "rosa/support/debug.hpp"
#include "rosa/support/log.h"

namespace rosa {

std::unique_ptr<System> System::createSystem(const std::string &Name) {
  return std::unique_ptr<System>(new SystemImpl(Name));
}

System::System(const std::string &Name) noexcept : Name(Name),
                                                   CountUnits(0),
                                                   SystemCleaned(false) {
  LOG_TRACE("Creating System (" + Name + ")");
}

System::~System(void) {
  LOG_TRACE("Destroying System (" + Name + ")");
  if (!SystemCleaned) {
    ROSA_CRITICAL("Trying to destroy a uncleaned System (" + Name + ")");
  }
}

void System::markCleaned(void) noexcept {
  if (SystemCleaned) {
    ROSA_CRITICAL("System (" + Name + ") has been already mark cleaned");
  } else if (!empty()) {
    ROSA_CRITICAL("Trying to mark a non-empty System (" + Name + ")");
  } else {
    SystemCleaned = true;
    LOG_TRACE("System (" + Name + ") is marked clean");
  }
}

Unit &System::createUnit(UnitCreator C, const std::string &Name) noexcept {
  if (SystemCleaned) {
    ROSA_CRITICAL("Trying to create a Unit in an already cleaned System (" +
                  Name + ")");
  }
  const uint64_t Id = ++CountUnits;
  const std::string N = Name.empty() ? "Unit_" + std::to_string(Id) : Name;
  Unit *U = C(Id, N, *this);
  registerUnit(*U);
  LOG_TRACE("Unit created and registered (" + U->FullName + ")");
  return *U;
}

size_t System::numberOfConstructedUnits(void) const noexcept {
  return CountUnits;
}

} // End namespace rosa

