|
From: <axl...@us...> - 2009-01-01 03:38:45
|
Revision: 130
http://hgengine.svn.sourceforge.net/hgengine/?rev=130&view=rev
Author: axlecrusher
Date: 2009-01-01 03:38:38 +0000 (Thu, 01 Jan 2009)
Log Message:
-----------
add message manager
Added Paths:
-----------
Mercury2/src/MercuryMessageManager.cpp
Mercury2/src/MercuryMessageManager.h
Mercury2/src/MessageHandler.h
Mercury2/src/PriorityQueue.h
Added: Mercury2/src/MercuryMessageManager.cpp
===================================================================
--- Mercury2/src/MercuryMessageManager.cpp (rev 0)
+++ Mercury2/src/MercuryMessageManager.cpp 2009-01-01 03:38:38 UTC (rev 130)
@@ -0,0 +1,76 @@
+#include <MercuryMessageManager.h>
+
+void MercuryMessageManager::PostMessage(const MString& message, float delay)
+{
+ uint64_t fireTime = m_currTime + uint64_t(delay*1000000);
+ m_messageQueue.Insert(fireTime, message);
+}
+
+void MercuryMessageManager::PumpMessages(const uint64_t& currTime)
+{
+ m_currTime = currTime;
+ while ( !m_messageQueue.empty() )
+ {
+ if ( m_messageQueue.PeekNextPriority() > m_currTime ) return;
+
+ MString& message = m_messageQueue.GetNext();
+ FireOffMessage( message );
+ m_messageQueue.PopNext();
+ }
+}
+
+void MercuryMessageManager::RegisterForMessage(const MString& message, MessageHandler* ptr)
+{
+ m_messageRecipients[message].push_back(ptr);
+}
+
+void MercuryMessageManager::FireOffMessage(const MString& message)
+{
+ std::map< MString, std::list< MessageHandler* > >::iterator i = m_messageRecipients.find(message);
+
+
+ std::list< MessageHandler* >::iterator recipients = i->second.begin();
+
+ for (; recipients != i->second.end(); ++recipients)
+ (*recipients)->HandleMessage(message);
+}
+
+MercuryMessageManager& MercuryMessageManager::GetInstance()
+{
+ static MercuryMessageManager *instance = NULL;
+ if (!instance)
+ instance = new MercuryMessageManager();
+ return *instance;
+}
+
+/****************************************************************************
+ * Copyright (C) 2008 by Joshua Allen *
+ * *
+ * *
+ * All rights reserved. *
+ * *
+ * Redistribution and use in source and binary forms, with or without *
+ * modification, are permitted provided that the following conditions *
+ * are met: *
+ * * Redistributions of source code must retain the above copyright *
+ * notice, this list of conditions and the following disclaimer. *
+ * * Redistributions in binary form must reproduce the above *
+ * copyright notice, this list of conditions and the following *
+ * disclaimer in the documentation and/or other materials provided *
+ * with the distribution. *
+ * * Neither the name of the Mercury Engine nor the names of its *
+ * contributors may be used to endorse or promote products derived *
+ * from this software without specific prior written permission. *
+ * *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
+ ***************************************************************************/
Added: Mercury2/src/MercuryMessageManager.h
===================================================================
--- Mercury2/src/MercuryMessageManager.h (rev 0)
+++ Mercury2/src/MercuryMessageManager.h 2009-01-01 03:38:38 UTC (rev 130)
@@ -0,0 +1,71 @@
+#ifndef MERCURYMESSAGEMANAGER_H
+#define MERCURYMESSAGEMANAGER_H
+
+#include <Callback.h>
+#include <MessageHandler.h>
+#include <map>
+#include <PriorityQueue.h>
+#include <MercuryString.h>
+#include <MercuryUtil.h>
+
+/* This message system uses absolute integer time values to fire off events.
+This ensures accuarate firing times while eliminating floating point error.
+Because we use absolute times in the queue we do not need to "count down" the
+firing times of all the messages. We only need to pop off the nearest events
+from the beginning of the queue." */
+class MercuryMessageManager
+{
+ public:
+ void PostMessage(const MString& message, float delay);
+ void PumpMessages(const uint64_t& currTime);
+ void RegisterForMessage(const MString& message, MessageHandler* ptr);
+
+ static MercuryMessageManager& GetInstance();
+ private:
+ void FireOffMessage(const MString& message);
+
+ PriorityQueue<uint64_t, MString> m_messageQueue;
+ uint64_t m_currTime; //microseconds
+
+ std::map< MString, std::list< MessageHandler* > > m_messageRecipients;
+};
+
+static InstanceCounter<MercuryMessageManager> MMcounter("MessageManager");
+
+#define MESSAGEMAN MercuryMessageManager
+#define REGISTER_FOR_MESSAGE(x) MESSAGEMAN::GetInstance().RegisterForMessage(#x, this)
+#define POST_MESSAGE(x,delay) MESSAGEMAN::GetInstance().PostMessage(#x, delay)
+
+#endif
+
+/****************************************************************************
+ * Copyright (C) 2008 by Joshua Allen *
+ * *
+ * *
+ * All rights reserved. *
+ * *
+ * Redistribution and use in source and binary forms, with or without *
+ * modification, are permitted provided that the following conditions *
+ * are met: *
+ * * Redistributions of source code must retain the above copyright *
+ * notice, this list of conditions and the following disclaimer. *
+ * * Redistributions in binary form must reproduce the above *
+ * copyright notice, this list of conditions and the following *
+ * disclaimer in the documentation and/or other materials provided *
+ * with the distribution. *
+ * * Neither the name of the Mercury Engine nor the names of its *
+ * contributors may be used to endorse or promote products derived *
+ * from this software without specific prior written permission. *
+ * *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
+ ***************************************************************************/
Added: Mercury2/src/MessageHandler.h
===================================================================
--- Mercury2/src/MessageHandler.h (rev 0)
+++ Mercury2/src/MessageHandler.h 2009-01-01 03:38:38 UTC (rev 130)
@@ -0,0 +1,44 @@
+#ifndef MESSAGEHANDLER_H
+#define MESSAGEHANDLER_H
+
+#include <MercuryString.h>
+
+class MessageHandler
+{
+ public:
+ virtual ~MessageHandler() {};
+ virtual void HandleMessage(const MString& message) {};
+};
+
+#endif
+/****************************************************************************
+ * Copyright (C) 2008 by Joshua Allen *
+ * *
+ * *
+ * All rights reserved. *
+ * *
+ * Redistribution and use in source and binary forms, with or without *
+ * modification, are permitted provided that the following conditions *
+ * are met: *
+ * * Redistributions of source code must retain the above copyright *
+ * notice, this list of conditions and the following disclaimer. *
+ * * Redistributions in binary form must reproduce the above *
+ * copyright notice, this list of conditions and the following *
+ * disclaimer in the documentation and/or other materials provided *
+ * with the distribution. *
+ * * Neither the name of the Mercury Engine nor the names of its *
+ * contributors may be used to endorse or promote products derived *
+ * from this software without specific prior written permission. *
+ * *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
+ ***************************************************************************/
Added: Mercury2/src/PriorityQueue.h
===================================================================
--- Mercury2/src/PriorityQueue.h (rev 0)
+++ Mercury2/src/PriorityQueue.h 2009-01-01 03:38:38 UTC (rev 130)
@@ -0,0 +1,64 @@
+#ifndef PRIORITYQUEUE_H
+#define PRIORITYQUEUE_H
+
+#include <list>
+#include <map>
+
+template<typename T1, typename T2>
+class PriorityQueue
+{
+ public:
+ void Insert(const T1& priority, const T2& x)
+ {
+ m_queue[priority].push_back(x);
+ }
+
+ bool empty() { return m_queue.empty(); }
+
+ const T1& PeekNextPriority() { return m_queue.begin()->first; }
+ T2& GetNext() { return m_queue.begin()->second.front(); }
+ void PopNext()
+ {
+ m_queue.begin()->second.pop_front();
+
+ if ( m_queue.begin()->second.empty() )
+ m_queue.erase( m_queue.begin() );
+ }
+
+ private:
+ std::map< T1, std::list<T2> > m_queue;
+};
+
+#endif
+
+/****************************************************************************
+ * Copyright (C) 2008 by Joshua Allen *
+ * *
+ * *
+ * All rights reserved. *
+ * *
+ * Redistribution and use in source and binary forms, with or without *
+ * modification, are permitted provided that the following conditions *
+ * are met: *
+ * * Redistributions of source code must retain the above copyright *
+ * notice, this list of conditions and the following disclaimer. *
+ * * Redistributions in binary form must reproduce the above *
+ * copyright notice, this list of conditions and the following *
+ * disclaimer in the documentation and/or other materials provided *
+ * with the distribution. *
+ * * Neither the name of the Mercury Engine nor the names of its *
+ * contributors may be used to endorse or promote products derived *
+ * from this software without specific prior written permission. *
+ * *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
+ ***************************************************************************/
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|