Fawkes API  Fawkes Development Version
message_queue.h
1 
2 /***************************************************************************
3  * message_queue.h - BlackBoard Interface message queue
4  *
5  * Created: Tue Oct 17 19:05:33 2006
6  * Copyright 2006-2009 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. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef _INTERFACE_MESSAGE_QUEUE_H_
25 #define _INTERFACE_MESSAGE_QUEUE_H_
26 
27 #include <core/exception.h>
28 #include <core/exceptions/software.h>
29 
30 namespace fawkes {
31 
32 class Message;
33 class Mutex;
34 
36 {
37 public:
39 };
40 
42 {
43 private:
44  // define our own list type since std::list is way too fat
45  /** Message list, internal only
46  */
47  struct msg_list_t
48  {
49  msg_list_t * next; /**< pointer to next element in list */
50  unsigned int msg_id; /**< message id */
51  Message * msg; /**< pointer to message */
52  };
53 
54 public:
55  MessageQueue();
56  virtual ~MessageQueue();
57 
59  {
60  friend MessageQueue;
61 
62  private:
63  MessageIterator(msg_list_t *cur);
64 
65  public:
68  MessageIterator &operator++(); // prefix
69  MessageIterator operator++(int inc); // postfix
70  MessageIterator &operator+(unsigned int i);
71  MessageIterator &operator+=(unsigned int i);
72  bool operator==(const MessageIterator &c) const;
73  bool operator!=(const MessageIterator &c) const;
74  Message * operator*() const;
75  Message * operator->() const;
77 
78  unsigned int id() const;
79 
80  template <class MessageType>
81  bool is() const;
82 
83  template <class MessageType>
84  MessageType *get() const;
85 
86  private:
87  msg_list_t *cur;
88  };
89 
90  void append(Message *msg);
91  void remove(const Message *msg);
92  void remove(const unsigned int msg_id);
93  void insert_after(const MessageIterator &it, Message *msg);
94 
95  unsigned int size() const;
96 
97  void flush();
98  bool empty() const;
99 
100  void lock();
101  bool try_lock();
102  void unlock();
103 
104  Message *first();
105  void pop();
106 
109 
110 private:
111  void remove(msg_list_t *l, msg_list_t *p);
112 
113  msg_list_t *list_;
114  msg_list_t *end_el_;
115  Mutex * mutex_;
116 };
117 
118 /** Check if message is of given type.
119  * The current message is checked if it is of the type that the
120  * template parameter determines. Use non-pointer template arguments!
121  * @return true, if the current message is of the given type, false otherwise
122  */
123 template <class MessageType>
124 bool
126 {
127  MessageType *msg = dynamic_cast<MessageType *>(cur->msg);
128  return (msg != 0);
129 }
130 
131 /** Get current message of given type.
132  * This will return the current message of the given template type. An TypeMismatchException
133  * is thrown if the current message is not of the requested type.
134  * @exception TypeMismatchException thrown, if current message is not of requested type.
135  * @return current message of requested type
136  */
137 template <class MessageType>
138 MessageType *
140 {
141  MessageType *msg = dynamic_cast<MessageType *>(cur->msg);
142  if (msg == 0) {
143  throw TypeMismatchException("Message types do not match (get)");
144  }
145  return msg;
146 }
147 
148 } // end namespace fawkes
149 
150 #endif
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:41
void insert_after(const MessageIterator &it, Message *msg)
Enqueue message after given iterator.
unsigned int id() const
Get ID of current element or 0 if element is end.
Message already enqueued exception.
Definition: message_queue.h:35
MessageIterator & operator+=(unsigned int i)
Advance by a certain amount.
MessageIterator end()
Get iterator to element beyond end of message queue list.
Fawkes library namespace.
MessageQueue()
Constructor.
Message * operator*() const
Get memory pointer of chunk.
MessageIterator & operator+(unsigned int i)
Advance by a certain amount.
MessageIterator & operator++()
Increment iterator.
MessageType * get() const
Get current message of given type.
Base class for exceptions in Fawkes.
Definition: exception.h:35
virtual ~MessageQueue()
Destructor.
void flush()
Delete all messages from queue.
Message queue used in interfaces.
Definition: message_queue.h:41
bool operator!=(const MessageIterator &c) const
Check inequality of two iterators.
bool is() const
Check if message is of given type.
unsigned int size() const
Get number of messages in queue.
bool try_lock()
Try to lock message queue.
bool operator==(const MessageIterator &c) const
Check equality of two iterators.
Message * operator->() const
Act on current message.
MessageIterator begin()
Get iterator to first element in message queue.
void append(Message *msg)
Append message to queue.
bool empty() const
Check if message queue is empty.
void remove(const Message *msg)
Remove message from queue.
void pop()
Erase first message from queue.
void unlock()
Unlock message queue.
Message * first()
Get first message from queue.
Mutex mutual exclusion lock.
Definition: mutex.h:32
void lock()
Lock message queue.
MessageIterator & operator=(const MessageIterator &c)
Assign iterator.