/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- MQueue_eq
- popMessage
- pushMessage
- registerSelf
- unregisterSelf
- findByName
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
14 class MQueue_eq : public unary_function<MQueue*, bool> {
15 string name;
16 public:
17 explicit MQueue_eq(const string& name_) : name(name_) {}
18 bool operator() (const MQueue* q) const { return q->getName() == name; }
19 };
20
21 set<MQueue*> MQueue::knownQueues;
22
23 string MQueue::popMessage() throw (MQueueException)
24 {
25 if (q.empty()) {
26 throw MQueueException("attempt to pop from an empty queue");
27 }
28 Message m = q.top();
29 q.pop();
30 return m.getBody();
31 }
32
33 size_t MQueue::pushMessage(string body, int urgency)
34 {
35 Message *m = new Message(body, urgency);
36 q.push(*m);
37 return q.size();
38 }
39
40 void MQueue::registerSelf()
41 {
42 knownQueues.insert(this);
43 }
44
45 void MQueue::unregisterSelf()
46 {
47 knownQueues.erase(this);
48 }
49
50 MQueue *MQueue::findByName(string name)
51 {
52 set<MQueue*>::iterator z = find_if(knownQueues.begin(),
53 knownQueues.end(),
54 MQueue_eq(name));
55 if (z == knownQueues.end()) return NULL;
56 return *z;
57 }