Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
Messenger.cpp
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2011-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "Messenger.h"
22 
23 #include <algorithm>
24 #include <assert.h>
25 #include <stdio.h>
26 #include "MessageListener.h"
27 #include "HFDataTypes.h"
28 
29 using namespace HFCore;
30 
31 Messenger *Messenger::gInstance = NULL;
32 
33 #include "debug/CrtDbgNew.h"
34 
35 Messenger::Messenger()
36 {
37 }
38 
39 Messenger::~Messenger()
40 {
41  mGlobalListeners.clear();
42 }
43 
44 void Messenger::sendMessage(std::string messageId, MsgParamMap *params)
45 {
46  // notify global listeners:
47  for (int32_hf i = (int32_hf)mGlobalListeners.size() - 1; i >= 0; i--)
48  {
49  mGlobalListeners[i]->handleMessage(messageId, params);
50  }
51 }
52 
54 {
55  mGlobalListeners.push_back(listener);
56 }
57 
59 {
60  MsgListenerVector::iterator i = std::find(mGlobalListeners.begin(),
61  mGlobalListeners.end(),
62  listener);
63  if (i != mGlobalListeners.end())
64  {
65  mGlobalListeners.erase(i);
66  }
67 }
68 
70 {
71  assert(gInstance != NULL);
72  return gInstance;
73 }
74 
76 {
77  assert(gInstance == NULL);
78  gInstance = new Messenger();
79 }
80 
82 {
83  HF_SAFE_DELETE(gInstance);
84 }
Interface for MessageListener callback methods.
#define HF_SAFE_DELETE(p)
Check if not NULL, delete and set to NULL.
Definition: HFDataTypes.h:49
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
static Messenger * instance()
Messenger singleton instance.
Definition: Messenger.cpp:69
Singleton interface for sending basic Message&#39;s to listeners.
Definition: Messenger.h:53
Framework data types, defines, typedef&#39;s, enum and structs.
void addListener(MessageListener *listener)
Add a new listener class to receive MessageListener callbacks.
Definition: Messenger.cpp:53
void removeListener(MessageListener *listener)
Remove a listener class from receiving further MessageListener callbacks.
Definition: Messenger.cpp:58
Interface for MessageListener callback methods.
#define NULL
Convenient define for 0.
Definition: HFDataTypes.h:42
std::map< std::string, std::string > MsgParamMap
Map for holding std::string pair.
Definition: Messenger.h:35
Singleton interface for sending basic Message&#39;s to listeners.
Definition: Actor.h:34
static void destroy()
Destroy singleton Messenger instance.
Definition: Messenger.cpp:81
static void init()
Create singleton Messenger instance.
Definition: Messenger.cpp:75
void sendMessage(std::string messageId, MsgParamMap *params=NULL)
Send a message to registered listeners.
Definition: Messenger.cpp:44