Nebula
Loading...
Searching...
No Matches
port.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
23#include "core/refcounted.h"
24#include "core/ptr.h"
25#include "messaging/message.h"
26#include "messaging/handler.h"
27
28//------------------------------------------------------------------------------
29namespace Messaging
30{
31class Port : public Core::RefCounted
32{
34public:
36 virtual void SetupAcceptedMessages();
38 void AttachHandler(const Ptr<Handler>& h);
40 void RemoveHandler(const Ptr<Handler>& h);
42 void RemoveAllHandlers();
44 SizeT GetNumHandlers() const;
48 virtual void Send(const Ptr<Message>& msg);
52 bool AcceptsMessage(const Id& msgId) const;
54 virtual void HandleMessage(const Ptr<Messaging::Message>& msg);
55
56protected:
58 void RegisterMessage(const Id& msgId);
59
60private:
63};
64
65//------------------------------------------------------------------------------
68inline
71{
72 return this->handlers.Size();
73}
74
75//------------------------------------------------------------------------------
78inline
79void
81{
82 // ignore duplicate message ids (may happen when derived classes
83 // process the same messages)
84 if (InvalidIndex == this->acceptedMessageIds.BinarySearchIndex(&msgId))
85 {
86 this->acceptedMessageIds.InsertSorted(&msgId);
87 }
88}
89
90//------------------------------------------------------------------------------
93inline
96{
97 return this->acceptedMessageIds;
98}
99
100//------------------------------------------------------------------------------
103inline
104const Ptr<Handler>&
106{
107 return this->handlers[i];
108}
109
110//------------------------------------------------------------------------------
113inline
114bool
115Port::AcceptsMessage(const Id& msgId) const
116{
117 return (InvalidIndex != this->acceptedMessageIds.FindIndex(&msgId));
118}
119
120} // namespace Messaging
121//------------------------------------------------------------------------------
122
The common base class of Nebula.
Definition refcounted.h:38
A message identifier.
Definition id.h:19
A message port is a receiving point for messages.
Definition port.h:32
virtual void SetupAcceptedMessages()
override to register accepted messages
Definition port.cc:19
void RemoveHandler(const Ptr< Handler > &h)
remove a message handler from the port
Definition port.cc:42
void RemoveAllHandlers()
remove all message handler from the port
Definition port.cc:87
SizeT GetNumHandlers() const
return number of handlers attached to the port
Definition port.h:70
virtual void HandleMessage(const Ptr< Messaging::Message > &msg)
handle a single accepted message
Definition port.cc:78
const Ptr< Handler > & GetHandlerAtIndex(IndexT i) const
get a message handler by index
Definition port.h:105
virtual void Send(const Ptr< Message > &msg)
send a message to the port
Definition port.cc:57
void AttachHandler(const Ptr< Handler > &h)
attach a message handler to the port
Definition port.cc:29
Util::Array< Ptr< Handler > > handlers
Definition port.h:61
bool AcceptsMessage(const Id &msgId) const
return true if port accepts this msg
Definition port.h:115
void RegisterMessage(const Id &msgId)
register a single accepted message
Definition port.h:80
const Util::Array< const Id * > & GetAcceptedMessages() const
get the array of accepted messages (sorted)
Definition port.h:95
Util::Array< const Id * > acceptedMessageIds
Definition port.h:62
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
Nebula's dynamic array class.
Definition array.h:60
Definition asyncport.cc:10
static const int InvalidIndex
Definition types.h:54
int SizeT
Definition types.h:49
int IndexT
Definition types.h:48