| 1 |
// |
| 2 |
// mqueue.c |
| 3 |
// |
| 4 |
// The mqueue-cpp extension module to show how to embed external C++ |
| 5 |
// library in Gauche. |
| 6 |
// This file is a source of a supposed external C++ library, knowing |
| 7 |
// nothing about Gauche. In typical case, such external library is |
| 8 |
// provided by the third party. |
| 9 |
// |
| 10 |
|
| 11 |
#include "mqueue.h" |
| 12 |
#include <functional> |
| 13 |
#include <algorithm> |
| 14 |
|
| 15 |
class MQueue_eq { |
| 16 |
string name; |
| 17 |
public: |
| 18 |
explicit MQueue_eq(const string& name_) : name(name_) {} |
| 19 |
bool operator() (const MQueue* q) const { return q->getName() == name; } |
| 20 |
}; |
| 21 |
|
| 22 |
set<MQueue*> MQueue::knownQueues; |
| 23 |
|
| 24 |
string MQueue::popMessage() |
| 25 |
{ |
| 26 |
if (q.empty()) { |
| 27 |
throw MQueueException("attempt to pop from an empty queue"); |
| 28 |
} |
| 29 |
Message m = q.top(); |
| 30 |
q.pop(); |
| 31 |
return m.getBody(); |
| 32 |
} |
| 33 |
|
| 34 |
size_t MQueue::pushMessage(string body, int urgency) |
| 35 |
{ |
| 36 |
Message *m = new Message(body, urgency); |
| 37 |
q.push(*m); |
| 38 |
return q.size(); |
| 39 |
} |
| 40 |
|
| 41 |
void MQueue::registerSelf() |
| 42 |
{ |
| 43 |
knownQueues.insert(this); |
| 44 |
} |
| 45 |
|
| 46 |
void MQueue::unregisterSelf() |
| 47 |
{ |
| 48 |
knownQueues.erase(this); |
| 49 |
} |
| 50 |
|
| 51 |
MQueue *MQueue::findByName(string name) |
| 52 |
{ |
| 53 |
set<MQueue*>::iterator z = find_if(knownQueues.begin(), |
| 54 |
knownQueues.end(), |
| 55 |
MQueue_eq(name)); |
| 56 |
if (z == knownQueues.end()) return NULL; |
| 57 |
return *z; |
| 58 |
} |