Nebula
Loading...
Searching...
No Matches
messagecallbackhandler.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
25//------------------------------------------------------------------------------
26#include "messaging/message.h"
27#include "util/dictionary.h"
28#include "util/delegate.h"
29#include "core/ptr.h"
30namespace Messaging
31{
33{
34public:
36 template<class CLASS, void (CLASS::*METHOD)(const Ptr<Messaging::Message>&)> static void AddCallback(const Ptr<Messaging::Message>& msg, CLASS* obj);
38 static void AbortCallback(const Ptr<Messaging::Message>& msg);
40 template<class CLASS> static void AbortCallbacks(CLASS* obj);
42 static void Update();
43private:
46};
47
48//------------------------------------------------------------------------------
51template<class CLASS>
52void
54{
55 n_assert(0 != obj);
56 IndexT i;
57 for (i = 0; i < Callbacks.Size(); i++)
58 {
59 //const Util::Delegate<const Ptr<Messaging::Message>& >& callback = Callbacks[i];
60 const Util::Delegate<void(const Ptr<Messaging::Message>&)>& callback = Callbacks[i];
61 if (callback.GetObject<CLASS>() == obj)
62 {
65 i--;
66 }
67 }
68}
69
70//------------------------------------------------------------------------------
73template<class CLASS, void (CLASS::*METHOD)(const Ptr<Messaging::Message>&)>
74void
76{
77 Util::Delegate<void(const Ptr<Messaging::Message>&)> del = Util::Delegate<void(const Ptr<Messaging::Message>&)>::FromMethod<CLASS,METHOD>(obj);
78 Messages.Append(msg);
79 Callbacks.Append(del);
80}
81
82//------------------------------------------------------------------------------
86#define __SingleFireCallback(CLASS, METHOD, OBJ, MSG) Messaging::MessageCallbackHandler::AddCallback<CLASS, &CLASS::METHOD>(MSG, OBJ);
87#define __AbortSingleFireCallback(MSG) Messaging::MessageCallbackHandler::AbortCallback(MSG);
88#define __AbortSingleFireCallbacks(CLASS, OBJ) Messaging::MessageCallbackHandler::AbortCallbacks<CLASS>(OBJ);
89} // namespace Messaging
90//------------------------------------------------------------------------------
Handles asynchronous message callbacks.
Definition messagecallbackhandler.h:33
static void AbortCallbacks(CLASS *obj)
remove all message callbacks related to a class
Definition messagecallbackhandler.h:53
static void AbortCallback(const Ptr< Messaging::Message > &msg)
remove a single message callback
Definition messagecallbackhandler.cc:47
static Util::Array< Ptr< Messaging::Message > > Messages
Definition messagecallbackhandler.h:44
static Util::Array< Util::Delegate< void(const Ptr< Messaging::Message > &)> > Callbacks
Definition messagecallbackhandler.h:45
static void Update()
update messages
Definition messagecallbackhandler.cc:18
static void AddCallback(const Ptr< Messaging::Message > &msg, CLASS *obj)
setup a message callback
Definition messagecallbackhandler.h:75
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
void Append(const TYPE &first, const ELEM_TYPE &... elements)
Append multiple elements to the end of the array.
Definition array.h:1334
void EraseIndex(IndexT index)
erase element at index, keep sorting intact
Definition array.h:1036
const SizeT Size() const
get number of elements in array
Definition array.h:880
Nebula delegate class, allows to store a function, method or lambda call into a C++ object for later ...
Definition delegate.h:39
#define n_assert(exp)
Definition debug.h:50
Definition asyncport.cc:10
int IndexT
Definition types.h:48