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

#include "rosa/core/AgentHandle.hpp"

#include "rosa/core/Agent.hpp"

namespace rosa {

AgentHandle::AgentHandle(Agent &A, bool) : A(A), S(A.system()) {}

AgentHandle::AgentHandle(Agent &A) : A(A), S(A.system()) {
  ASSERT(S.isUnitRegistered(A));
}

AgentHandle::operator bool(void) const noexcept {
  // NOTE: The referred MessageSystem is supposed to be still alive.
  return S.isUnitRegistered(A);
}

bool AgentHandle::operator==(const AgentHandle &H) const noexcept {
  // Return if the referred Agent is the same object in both AgentHandlers.
  return &A == &H.A;
}

AgentHandle AgentHandle::self(void) noexcept {
  // Return a copy of this AgentHandle.
  return *this;
}

void AgentHandle::sendMessage(message_t &&M) noexcept {
  ASSERT(bool(*this));
  S.send(*this, std::move(M));
}

} // End namespace rosa

