2013-11-28 16:15:28 +01:00
|
|
|
#ifndef NETWORK_DEPENDENCIES_POST_BOX_H
|
|
|
|
#define NETWORK_DEPENDENCIES_POST_BOX_H
|
|
|
|
|
|
|
|
/////////////////////////////////////
|
|
|
|
// Created by Pontus Fransson 2013 //
|
|
|
|
/////////////////////////////////////
|
|
|
|
|
|
|
|
#include "IPostBox.h"
|
|
|
|
#include "../../Misc/Thread/OysterMutex.h"
|
2013-11-28 16:20:50 +01:00
|
|
|
#include "../../Misc/ThreadSafeQueue.h"
|
2013-11-28 16:15:28 +01:00
|
|
|
|
|
|
|
namespace Oyster
|
|
|
|
{
|
|
|
|
namespace Network
|
|
|
|
{
|
2013-11-29 09:11:30 +01:00
|
|
|
//With this class you can post items to it and then fetch them somewhere else.
|
|
|
|
//It is thread safe beacause of the ThreadSafeQueue.
|
2013-11-28 16:15:28 +01:00
|
|
|
template <class T>
|
|
|
|
class PostBox : public IPostBox<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PostBox();
|
|
|
|
virtual ~PostBox();
|
|
|
|
|
|
|
|
virtual void PostMessage(T& message);
|
|
|
|
virtual void FetchMessage(T& message);
|
|
|
|
virtual bool IsFull();
|
|
|
|
|
|
|
|
private:
|
2013-11-28 16:20:50 +01:00
|
|
|
Oyster::Queue::ThreadSafeQueue<T> messages;
|
2013-11-28 16:15:28 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
//Implementation of PostBox
|
|
|
|
template <class T>
|
|
|
|
PostBox<T>::PostBox()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
PostBox<T>::~PostBox()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void PostBox<T>::PostMessage(T& message)
|
|
|
|
{
|
2013-11-28 16:20:50 +01:00
|
|
|
messages.Push(message);
|
2013-11-28 16:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void PostBox<T>::FetchMessage(T& message)
|
|
|
|
{
|
|
|
|
if(IsFull())
|
|
|
|
{
|
2013-11-28 16:20:50 +01:00
|
|
|
message = messages.Front();
|
|
|
|
messages.Pop();
|
2013-11-28 16:15:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
bool PostBox<T>::IsFull()
|
|
|
|
{
|
2013-11-28 16:20:50 +01:00
|
|
|
return !messages.IsEmpty();
|
2013-11-28 16:15:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|