Nebula
Toggle main menu visibility
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
//------------------------------------------------------------------------------
29
namespace
Messaging
30
{
31
class
Port
:
public
Core::RefCounted
32
{
33
__DeclareClass
(
Port
);
34
public
:
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
;
46
const
Ptr<Handler>
&
GetHandlerAtIndex
(
IndexT
i)
const
;
48
virtual
void
Send
(
const
Ptr<Message>
& msg);
50
const
Util::Array<const Id*>
&
GetAcceptedMessages
()
const
;
52
bool
AcceptsMessage
(
const
Id
& msgId)
const
;
54
virtual
void
HandleMessage
(
const
Ptr<Messaging::Message>
& msg);
55
56
protected
:
58
void
RegisterMessage
(
const
Id
& msgId);
59
60
private
:
61
Util::Array<Ptr<Handler>
>
handlers
;
62
Util::Array<const Id*>
acceptedMessageIds
;
63
};
64
65
//------------------------------------------------------------------------------
68
inline
69
SizeT
70
Port::GetNumHandlers
()
const
71
{
72
return
this->
handlers
.Size();
73
}
74
75
//------------------------------------------------------------------------------
78
inline
79
void
80
Port::RegisterMessage
(
const
Id
& msgId)
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
//------------------------------------------------------------------------------
93
inline
94
const
Util::Array<const Id*>
&
95
Port::GetAcceptedMessages
()
const
96
{
97
return
this->
acceptedMessageIds
;
98
}
99
100
//------------------------------------------------------------------------------
103
inline
104
const
Ptr<Handler>
&
105
Port::GetHandlerAtIndex
(
IndexT
i)
const
106
{
107
return
this->
handlers
[i];
108
}
109
110
//------------------------------------------------------------------------------
113
inline
114
bool
115
Port::AcceptsMessage
(
const
Id
& msgId)
const
116
{
117
return
(
InvalidIndex
!= this->
acceptedMessageIds
.FindIndex(&msgId));
118
}
119
120
}
// namespace Messaging
121
//------------------------------------------------------------------------------
122
Core::RefCounted
The common base class of Nebula.
Definition
refcounted.h:38
Messaging::Id
A message identifier.
Definition
id.h:19
Messaging::Port
A message port is a receiving point for messages.
Definition
port.h:32
Messaging::Port::SetupAcceptedMessages
virtual void SetupAcceptedMessages()
override to register accepted messages
Definition
port.cc:19
Messaging::Port::RemoveHandler
void RemoveHandler(const Ptr< Handler > &h)
remove a message handler from the port
Definition
port.cc:42
Messaging::Port::RemoveAllHandlers
void RemoveAllHandlers()
remove all message handler from the port
Definition
port.cc:87
Messaging::Port::GetNumHandlers
SizeT GetNumHandlers() const
return number of handlers attached to the port
Definition
port.h:70
Messaging::Port::HandleMessage
virtual void HandleMessage(const Ptr< Messaging::Message > &msg)
handle a single accepted message
Definition
port.cc:78
Messaging::Port::GetHandlerAtIndex
const Ptr< Handler > & GetHandlerAtIndex(IndexT i) const
get a message handler by index
Definition
port.h:105
Messaging::Port::__DeclareClass
__DeclareClass(Port)
Messaging::Port::Send
virtual void Send(const Ptr< Message > &msg)
send a message to the port
Definition
port.cc:57
Messaging::Port::AttachHandler
void AttachHandler(const Ptr< Handler > &h)
attach a message handler to the port
Definition
port.cc:29
Messaging::Port::handlers
Util::Array< Ptr< Handler > > handlers
Definition
port.h:61
Messaging::Port::AcceptsMessage
bool AcceptsMessage(const Id &msgId) const
return true if port accepts this msg
Definition
port.h:115
Messaging::Port::RegisterMessage
void RegisterMessage(const Id &msgId)
register a single accepted message
Definition
port.h:80
Messaging::Port::GetAcceptedMessages
const Util::Array< const Id * > & GetAcceptedMessages() const
get the array of accepted messages (sorted)
Definition
port.h:95
Messaging::Port::acceptedMessageIds
Util::Array< const Id * > acceptedMessageIds
Definition
port.h:62
Ptr
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition
ptr.h:38
Util::Array
Nebula's dynamic array class.
Definition
array.h:61
handler.h
Messaging
Definition
asyncport.cc:10
ptr.h
refcounted.h
InvalidIndex
static const int InvalidIndex
Definition
types.h:47
SizeT
int SizeT
Definition
types.h:42
IndexT
int IndexT
Definition
types.h:41
code
foundation
messaging
port.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.