Page MenuHomePhorge

MessagingSystem.hpp
No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None

MessagingSystem.hpp

/*******************************************************************************
*
* File: MessagingSystem.hpp
*
* Contents: Declaration of the class MessagingSystem.
*
* Copyright 2017
*
* Author: David Juhasz (david.juhasz@tuwien.ac.at)
*
******************************************************************************/
#ifndef ROSA_CORE_MESSAGINGSYSTEM_HPP
#define ROSA_CORE_MESSAGINGSYSTEM_HPP
#include "rosa/core/AgentHandle.hpp"
#include "rosa/core/System.hpp"
#include "rosa/support/atom.hpp"
namespace rosa {
// Extends the System interface with features to create Agents and register
// Messages for them.
class MessagingSystem : public System {
friend class AgentHandle; // AgentHandle is our friend.
public:
// Returns an object implementing the interface defined by the class.
static std::unique_ptr<MessagingSystem>
createSystem(const std::string &Name) noexcept;
private:
// Kind used for Unit categorization of Agents.
static constexpr AtomValue AgentKind = atom("agent");
protected:
// Ctor.
MessagingSystem(void) noexcept = default;
protected:
// Creates an Agent owned by the MessagingSystem and returns a handle for it.
// STATIC PRE: std::is_base_of<Agent, T>::value
template <typename T, typename Fun, typename... Funs>
AgentHandle createAgent(const std::string &Name, Fun &&F, Funs &&... Fs);
public:
// Sends the given Message to the Agent referred by the given AgentHandle.
// NOTE: If the given Message cannot be handled by the referred Agent, the
// Message is simply ignored.
// PRE: isUnitRegistered(H.agent())
virtual void send(const AgentHandle &H, message_t &&M) noexcept = 0;
// Convenience template, which creates the Message from the given constant
// lvalue references and sends to the Agent.
// PRE: isUnitRegistered(H.agent())
template <typename Type, typename... Types>
void send(const AgentHandle &H, const Type &T, const Types &... Ts) noexcept;
// Convenience template, which creates the Message from the given rvalue
// references and sends to the Agent.
// PRE: isUnitRegistered(H.agent())
template <typename Type, typename... Types>
void send(const AgentHandle &H, Type &&T, Types &&... Ts) noexcept;
};
template <typename T, typename Fun, typename... Funs>
AgentHandle MessagingSystem::createAgent(const std::string &Name, Fun &&F,
Funs &&... Fs) {
STATIC_ASSERT((std::is_base_of<Agent, T>::value), "not an Agent");
Agent &A = createUnit<T, MessagingSystem>([&](const id_t Id,
MessagingSystem &S) noexcept {
return new T(AgentKind, Id, Name, S, std::move(F), std::move(Fs)...);
});
return {A};
}
template <typename Type, typename... Types>
void MessagingSystem::send(const AgentHandle &H, const Type &T,
const Types &... Ts) noexcept {
send(H, Message::create<Type, Types...>(T, Ts...));
}
template <typename Type, typename... Types>
void MessagingSystem::send(const AgentHandle &H, Type &&T,
Types &&... Ts) noexcept {
send(H, Message::create<Type, Types...>(std::move(T), std::move(Ts)...));
}
} // End namespace rosa
#endif // ROSA_CORE_MESSAGINGSYSTEM_HPP

File Metadata

Mime Type
text/x-c++
Expires
Sun, Jun 21, 8:54 PM (1 d, 21 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
335168
Default Alt Text
MessagingSystem.hpp (3 KB)

Event Timeline