Neo  0.5.0
Developer Documentation
Messenger.h
Go to the documentation of this file.
1 //========================================================================
2 // Copyright (c) 2014-2015 Yannick Pflanzer <neo-engine.de>
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgment in the product documentation would
15 // be appreciated but is not required.
16 //
17 // 2. Altered source versions must be plainly marked as such, and must not
18 // be misrepresented as being the original software.
19 //
20 // 3. This notice may not be removed or altered from any source
21 // distribution.
22 //
23 //========================================================================
24 
25 #ifndef __MESSENGER_H__
26 #define __MESSENGER_H__
27 
28 #include <string>
29 #include <map>
30 #include <deque>
31 
32 namespace Neo
33 {
34 
41 struct Message
42 {
43  std::string message;
44  std::string sender;
45  unsigned int messageId;
46  void* data;
47 };
48 
55 class NEO_ENGINE_EXPORT Messenger
56 {
57 private:
58  struct Inbox
59  {
60  std::string name;
61  unsigned int id;
62 
63  std::deque<Message> messages;
64  };
65 
66  std::map<std::string, Inbox> m_boxes;
67  Semaphore* m_semaphore;
68 
69  void waitForAccess();
70  void finishAccess();
71 
72  bool m_locked;
73 
74 public:
76  {
77  static Messenger m_instance;
78  return &m_instance;
79  }
80 
81  Messenger() : m_semaphore(NULL), m_locked(false) {}
82 
88  void addInbox(const char* name, unsigned int id);
89 
95  unsigned int getMessagesCount(const char* name);
96 
108  void sendMessage(const char* message, unsigned int messageId, void* data, const char* dest, const char* from);
109 
116  Message getNextMessage(const char* threadName);
117 
125  Message getNextMessage(const char* threadName, const char* sender);
126 };
127 }
128 
129 #endif
The MSemaphore class implements a semaphore mechanism based on SDL for use with MThread.
Definition: Thread.h:107
The Messenger class is the host of all inboxes for all threads.
Definition: Messenger.h:55
Messenger()
Definition: Messenger.h:81
std::string message
Definition: Messenger.h:43
The Message struct contains methods and data necessary to transmit an useful message.
Definition: Messenger.h:41
unsigned int messageId
Definition: Messenger.h:45
static Messenger * getInstance()
Definition: Messenger.h:75
std::string sender
Definition: Messenger.h:44
Definition: Color.h:29
void * data
Definition: Messenger.h:46