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 <interfaces/test.h>
27 
28 #include <iostream>
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()
43  << " int: " << i.get<TestInterface::SetTestIntMessage>()->test_int()
44  << " type: " << typeid((*i)).name() << endl;
45  } else if (i.is<Message>()) {
46  cout << "It's just a message" << endl;
47  } else {
48  cout << "Message " << i.id() << " is not of correct type, it is " << typeid((*i)).name()
49  << endl;
50  }
51  }
52  mq->unlock();
53 }
54 
55 int
56 main(int argc, char **argv)
57 {
58  unsigned int id;
59  MessageQueue *mq = new MessageQueue();
60 
61  cout << "Message queue initialized, now appending three test messages" << endl;
62 
67  id = mq->append(m1);
68  cout << "m1 added with id " << id << endl;
69  id = mq->append(m1);
70  cout << "m1 added with id " << id << endl;
71  id = mq->append(m2);
72  cout << "m2 added with id " << id << endl;
73  id = mq->append(m3);
74  cout << "m3 added with id " << id << endl;
75  id = mq->append(m4);
76  cout << "m4 (of different type!) added with id " << id << endl;
77 
78  cout << "Size is now " << mq->size() << endl;
79 
80  cout << "Unreferencing messages" << endl;
81  m1->unref();
82  m2->unref();
83  m3->unref();
84  m4->unref();
85 
86  cout << "Appending m1 again" << endl;
87  id = mq->append(m1);
88  cout << "m1 added with id " << id << endl;
89  cout << "Size is now " << mq->size() << endl;
90  cout << "m1 refcount is now " << m1->refcount() << endl;
91 
92  printMessages(mq);
93 
94  cout << "Now removing message m1 once" << endl;
95  mq->remove(m1);
96  printMessages(mq);
97 
98  cout << "m1 has refcount " << m1->refcount() << endl;
99 
100  cout << "Now removing message m2" << endl;
101  mq->remove(m2);
102  printMessages(mq);
103 
104  cout << "Now removing message m4" << endl;
105  mq->remove(m4);
106  printMessages(mq);
107 
108  cout << "Size is now " << mq->size() << endl;
109 
110  printMessages(mq);
111 
112  delete mq;
113  // messages will be erased when enqueued in mq!
114 }
115 
116 /// @endcond
bool is() const
Check if message is of given type.
MessageType * get() const
Get current message of given type.
unsigned int id() const
Get ID of current element or 0 if element is end.
Message queue used in interfaces.
Definition: message_queue.h:42
MessageIterator end()
Get iterator to element beyond end of message queue list.
void append(Message *msg)
Append message to queue.
void unlock()
Unlock message queue.
void lock()
Lock message queue.
void remove(const Message *msg)
Remove message from queue.
MessageIterator begin()
Get iterator to first element in message queue.
unsigned int size() const
Get number of messages in queue.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:45
void unref()
Decrement reference count and conditionally delete this instance.
Definition: refcount.cpp:95
unsigned int refcount()
Get reference count for this instance.
Definition: refcount.cpp:120
SetTestIntMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:69
SetTestStringMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:95
Fawkes library namespace.