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 
35 
37  public:
39 };
40 
41 
43 {
44  private:
45  // define our own list type since std::list is way too fat
46  /** Message list, internal only
47  */
48  struct msg_list_t {
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 class MessageQueue;
61  private:
62  MessageIterator(msg_list_t *cur);
63  public:
66  MessageIterator & operator++ (); // prefix
67  MessageIterator operator++ (int inc); // postfix
68  MessageIterator & operator+ (unsigned int i);
69  MessageIterator & operator+= (unsigned int i);
70  bool operator== (const MessageIterator & c) const;
71  bool operator!= (const MessageIterator & c) const;
72  Message * operator* () const;
73  Message * operator-> () const;
75 
76  unsigned int id() const;
77 
78  template <class MessageType>
79  bool is() const;
80 
81  template <class MessageType>
82  MessageType * get() const;
83 
84  private:
85  msg_list_t *cur;
86  };
87 
88 
89  void append(Message *msg);
90  void remove(const Message *msg);
91  void remove(const unsigned int msg_id);
92  void insert_after(const MessageIterator &it, Message *msg);
93 
94  unsigned int size() const;
95 
96  void flush();
97  bool empty() const;
98 
99  void lock();
100  bool try_lock();
101  void unlock();
102 
103  Message * first();
104  void pop();
105 
108 
109  private:
110  void remove(msg_list_t *l, msg_list_t *p);
111 
112  msg_list_t *__list;
113  msg_list_t *__end_el;
114  Mutex *__mutex;
115 };
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 
132 /** Get current message of given type.
133  * This will return the current message of the given template type. An TypeMismatchException
134  * is thrown if the current message is not of the requested type.
135  * @exception TypeMismatchException thrown, if current message is not of requested type.
136  * @return current message of requested type
137  */
138 template <class MessageType>
139 MessageType *
141 {
142  MessageType *msg = dynamic_cast<MessageType *>(cur->msg);
143  if ( msg == 0 ) {
144  throw TypeMismatchException("Message types do not match (get)");
145  }
146  return msg;
147 }
148 
149 } // end namespace fawkes
150 
151 #endif
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
Message already enqueued exception.
Definition: message_queue.h:36
Fawkes library namespace.
iterator end()
Get end iterator for messages.
Definition: exception.cpp:717
MessageType * get() const
Get current message of given type.
Base class for exceptions in Fawkes.
Definition: exception.h:36
Message queue used in interfaces.
Definition: message_queue.h:42
bool is() const
Check if message is of given type.
iterator begin()
Get iterator for messages.
Definition: exception.cpp:700
Exception & operator=(const Exception &exc)
Assign an Exception.
Definition: exception.cpp:519
Mutex mutual exclusion lock.
Definition: mutex.h:32
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:341