Fawkes API  Fawkes Development Version
qa_interface_msgq.cpp
1 
2 /***************************************************************************
3  * qa_interface_msgq.cpp - QA for interface message queue
4  *
5  * Created: Tue Oct 24 14:34:40 2006
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 /// @cond QA
24 
25 #include <interface/message_queue.h>
26 #include <iostream>
27 
28 #include <interfaces/test.h>
29 #include <typeinfo>
30 
31 using namespace std;
32 using namespace fawkes;
33 
34 void
35 printMessages(MessageQueue *mq)
36 {
37  cout << "Iterating through messages:" << endl;
39  mq->lock();
40  for ( i = mq->begin(); i != mq->end(); ++i) {
42  cout << "Message " << i.id() << " int: "
43  << i.get<TestInterface::SetTestIntMessage>()->test_int()
44  << " type: " << typeid((*i)).name()
45  << endl;
46  } else if ( i.is<Message>() ) {
47  cout << "It's just a message" << endl;
48  } else {
49  cout << "Message " << i.id() << " is not of correct type, it is " << typeid((*i)).name() << endl;
50  }
51  }
52  mq->unlock();
53 }
54 
55 int
56 main(int argc, char **argv)
57 {
58 
59  unsigned int id;
60  MessageQueue *mq = new MessageQueue();
61 
62  cout << "Message queue initialized, now appending three test messages" << endl;
63 
68  id = mq->append(m1);
69  cout << "m1 added with id " << id << endl;
70  id = mq->append(m1);
71  cout << "m1 added with id " << id << endl;
72  id = mq->append(m2);
73  cout << "m2 added with id " << id << endl;
74  id = mq->append(m3);
75  cout << "m3 added with id " << id << endl;
76  id = mq->append(m4);
77  cout << "m4 (of different type!) added with id " << id << endl;
78 
79  cout << "Size is now " << mq->size() << endl;
80 
81  cout << "Unreferencing messages" << endl;
82  m1->unref();
83  m2->unref();
84  m3->unref();
85  m4->unref();
86 
87  cout << "Appending m1 again" << endl;
88  id = mq->append(m1);
89  cout << "m1 added with id " << id << endl;
90  cout << "Size is now " << mq->size() << endl;
91  cout << "m1 refcount is now " << m1->refcount() << endl;
92 
93  printMessages(mq);
94 
95  cout << "Now removing message m1 once" << endl;
96  mq->remove(m1);
97  printMessages(mq);
98 
99 
100  cout << "m1 has refcount " << m1->refcount() << endl;
101 
102  cout << "Now removing message m2" << endl;
103  mq->remove(m2);
104  printMessages(mq);
105 
106  cout << "Now removing message m4" << endl;
107  mq->remove(m4);
108  printMessages(mq);
109 
110  cout << "Size is now " << mq->size() << endl;
111 
112  printMessages(mq);
113 
114  delete mq;
115  // messages will be erased when enqueued in mq!
116 
117 }
118 
119 /// @endcond
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
void unref()
Decrement reference count and conditionally delete this instance.
Definition: refcount.cpp:99
unsigned int id() const
Get ID of current element or 0 if element is end.
unsigned int refcount()
Get reference count for this instance.
Definition: refcount.cpp:125
MessageIterator end()
Get iterator to element beyond end of message queue list.
Fawkes library namespace.
STL namespace.
MessageType * get() const
Get current message of given type.
Message queue used in interfaces.
Definition: message_queue.h:42
bool is() const
Check if message is of given type.
unsigned int size() const
Get number of messages in queue.
SetTestIntMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:68
MessageIterator begin()
Get iterator to first element in message queue.
void append(Message *msg)
Append message to queue.
void remove(const Message *msg)
Remove message from queue.
void unlock()
Unlock message queue.
SetTestStringMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:94
void lock()
Lock message queue.