| 1 |
// -*- mode: C++ -*- |
| 2 |
// |
| 3 |
// mqueue.h - a simple-minded message queue with priority |
| 4 |
// |
| 5 |
// The mqueue-cpp extension module to show how to embed external C++ |
| 6 |
// library in Gauche. |
| 7 |
// This file is a header of a supposed external C++ library, knowing |
| 8 |
// nothing about Gauche. In typical case, such external library is |
| 9 |
// provided by the third party. |
| 10 |
// |
| 11 |
|
| 12 |
#ifndef MQUEUE_H |
| 13 |
#define MQUEUE_H |
| 14 |
|
| 15 |
#include <queue> |
| 16 |
#include <string> |
| 17 |
#include <set> |
| 18 |
|
| 19 |
using namespace std; |
| 20 |
|
| 21 |
class Message { |
| 22 |
public: |
| 23 |
Message(string body_, int urgency_ = 0) |
| 24 |
: body(body_), urgency(urgency_) |
| 25 |
{} |
| 26 |
|
| 27 |
bool operator< (const Message& m) const { |
| 28 |
return urgency < m.urgency; |
| 29 |
} |
| 30 |
|
| 31 |
int getUrgency() const { return urgency;} |
| 32 |
string getBody() const { return body; } |
| 33 |
|
| 34 |
private: |
| 35 |
string body; |
| 36 |
int urgency; |
| 37 |
}; |
| 38 |
|
| 39 |
class MQueueException { |
| 40 |
public: |
| 41 |
string reason; |
| 42 |
MQueueException(string reason_) : reason(reason_) {} |
| 43 |
}; |
| 44 |
|
| 45 |
class MQueue { |
| 46 |
public: |
| 47 |
MQueue(string name_) : name(name_) { registerSelf(); } |
| 48 |
~MQueue() { unregisterSelf(); } |
| 49 |
|
| 50 |
string getName() const { return name; } |
| 51 |
|
| 52 |
// Basic queue operations. The client doesn't need to know |
| 53 |
// about Message. |
| 54 |
bool empty() const { return q.empty(); } |
| 55 |
string popMessage(); |
| 56 |
size_t pushMessage(string body, int urgency = 0); |
| 57 |
|
| 58 |
// One can find a previously created MQueue by its name. |
| 59 |
static MQueue *findByName(string name); |
| 60 |
|
| 61 |
bool operator< (const MQueue& m) const { return name < m.name; } |
| 62 |
|
| 63 |
private: |
| 64 |
string name; |
| 65 |
priority_queue<Message> q; |
| 66 |
|
| 67 |
static set<MQueue*> knownQueues; |
| 68 |
void registerSelf(); |
| 69 |
void unregisterSelf(); |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
#endif // MQUEUE_H |